Added some more JUnit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / CreateChannelOnDomainHandler.java
CommitLineData
bbb3538a
BH
1/**********************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12package org.eclipse.linuxtools.lttng.ui.views.control.handlers;
13
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.List;
17
bbb3538a
BH
18import org.eclipse.core.commands.ExecutionEvent;
19import org.eclipse.core.commands.ExecutionException;
20import org.eclipse.core.runtime.IProgressMonitor;
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.Status;
23import org.eclipse.core.runtime.jobs.Job;
24import org.eclipse.jface.viewers.ISelection;
25import org.eclipse.jface.viewers.StructuredSelection;
26import org.eclipse.jface.window.Window;
27import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
28import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
29import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
bbb3538a 30import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.ICreateChannelDialog;
d132bcc7 31import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.TraceControlDialogFactory;
bbb3538a
BH
32import org.eclipse.linuxtools.lttng.ui.views.control.model.TraceSessionState;
33import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceDomainComponent;
34import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent;
35import org.eclipse.ui.IWorkbenchPage;
bbb3538a
BH
36
37/**
38 * <b><u>CreateChannelOnDomainHandler</u></b>
39 * <p>
40 * Command handler implementation to create a trace channel for known domain.
41 * </p>
42 */
498704b3 43public class CreateChannelOnDomainHandler extends BaseControlViewHandler {
bbb3538a
BH
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48 /**
49 * The the domain component the command is to be executed on.
50 */
51 private TraceDomainComponent fDomain;
52
53 // ------------------------------------------------------------------------
54 // Operations
55 // ------------------------------------------------------------------------
56 /*
57 * (non-Javadoc)
58 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
59 */
60 @Override
61 public Object execute(ExecutionEvent event) throws ExecutionException {
62
bbb3538a 63 // Get channel information from user
d132bcc7
BH
64 final ICreateChannelDialog dialog = TraceControlDialogFactory.getInstance().getCreateChannelDialog();
65 dialog.setDomainComponent(fDomain);
bbb3538a 66
6503ae0f
BH
67 if (dialog.open() != Window.OK) {
68 return null;
69 }
bbb3538a 70
6503ae0f
BH
71 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
72 @Override
73 protected IStatus run(IProgressMonitor monitor) {
74 String errorString = null;
bbb3538a 75
6503ae0f
BH
76 List<String> channelNames = new ArrayList<String>();
77 channelNames.add(dialog.getChannelInfo().getName());
78
79 try {
80 fDomain.enableChannels(channelNames, dialog.getChannelInfo(), monitor);
81 } catch (ExecutionException e) {
82 if (errorString == null) {
83 errorString = new String();
84 }
85 errorString += e.toString() + "\n"; //$NON-NLS-1$
86 }
87
88 // get session configuration in all cases
89 try {
90 fDomain.getConfigurationFromNode(monitor);
91 } catch (ExecutionException e) {
92 if (errorString == null) {
93 errorString = new String();
bbb3538a 94 }
6503ae0f
BH
95 errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$
96 }
97
98 if (errorString != null) {
99 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString);
100 }
101 return Status.OK_STATUS;
102 }
103 };
104
105 job.setUser(true);
106 job.schedule();
107
bbb3538a
BH
108 return null;
109 }
110
111 /*
112 * (non-Javadoc)
113 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
114 */
115 @Override
116 public boolean isEnabled() {
498704b3
BH
117
118 // Get workbench page for the Control View
119 IWorkbenchPage page = getWorkbenchPage();
bbb3538a
BH
120 if (page == null) {
121 return false;
122 }
498704b3 123 fDomain = null;
bbb3538a
BH
124
125 // Check if one domain is selected
126 ISelection selection = page.getSelection(ControlView.ID);
127 if (selection instanceof StructuredSelection) {
128 StructuredSelection structered = ((StructuredSelection) selection);
129 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
130 Object element = (Object) iterator.next();
131 if (element instanceof TraceDomainComponent) {
132 TraceDomainComponent domain = (TraceDomainComponent) element;
133 TraceSessionComponent session = (TraceSessionComponent) domain.getParent();
134 // Add only TraceDomainComponent whose TraceSessionComponent parent is inactive and not destroyed
135 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
136 fDomain = domain;
137 }
138 }
139 }
140 }
141 return fDomain != null;
142 }
143}
This page took 0.050034 seconds and 5 git commands to generate.