Add new LTTng commands (create/destroy/start/stop session,
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / CreateChannelOnDomainHandler.java
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 **********************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.views.control.handlers;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
29 import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
30 import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
31 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.CreateChannelDialog;
32 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.ICreateChannelDialog;
33 import org.eclipse.linuxtools.lttng.ui.views.control.model.TraceSessionState;
34 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceDomainComponent;
35 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent;
36 import org.eclipse.ui.IWorkbenchPage;
37 import org.eclipse.ui.IWorkbenchPart;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.PlatformUI;
40
41 /**
42 * <b><u>CreateChannelOnDomainHandler</u></b>
43 * <p>
44 * Command handler implementation to create a trace channel for known domain.
45 * </p>
46 */
47 public class CreateChannelOnDomainHandler extends AbstractHandler {
48
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52 /**
53 * The the domain component the command is to be executed on.
54 */
55 private TraceDomainComponent fDomain;
56
57 // ------------------------------------------------------------------------
58 // Operations
59 // ------------------------------------------------------------------------
60 /*
61 * (non-Javadoc)
62 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
63 */
64 @Override
65 public Object execute(ExecutionEvent event) throws ExecutionException {
66
67 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
68
69 if (window == null) {
70 return false;
71 }
72
73 // Get channel information from user
74 final ICreateChannelDialog dialog = new CreateChannelDialog(window.getShell(), fDomain);
75
76 if (dialog.open() == Window.OK) {
77
78 Job job = new Job(Messages.TraceControl_EnableChannelJob) {
79 @Override
80 protected IStatus run(IProgressMonitor monitor) {
81 String errorString = null;
82
83 List<String> channelNames = new ArrayList<String>();
84 channelNames.add(dialog.getChannelInfo().getName());
85
86 try {
87 fDomain.enableChannels(channelNames, dialog.getChannelInfo(), monitor);
88 } catch (ExecutionException e) {
89 if (errorString == null) {
90 errorString = new String();
91 }
92 errorString += e.toString() + "\n"; //$NON-NLS-1$
93 }
94
95 // get session configuration in all cases
96 try {
97 fDomain.getConfigurationFromNode(monitor);
98 } catch (ExecutionException e) {
99 if (errorString == null) {
100 errorString = new String();
101 }
102 errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$
103 }
104
105 if (errorString != null) {
106 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString);
107 }
108 return Status.OK_STATUS;
109 }};
110 job.setUser(true);
111 job.schedule();
112 }
113 return null;
114 }
115
116 /*
117 * (non-Javadoc)
118 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
119 */
120 @Override
121 public boolean isEnabled() {
122 fDomain = null;
123
124 // Check if we are closing down
125 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
126 if (window == null) {
127 return false;
128 }
129
130 // Check if we are in the Project View
131 IWorkbenchPage page = window.getActivePage();
132 if (page == null) {
133 return false;
134 }
135
136 IWorkbenchPart part = page.getActivePart();
137 if (!(part instanceof ControlView)) {
138 return false;
139 }
140
141 // Check if one domain is selected
142 ISelection selection = page.getSelection(ControlView.ID);
143 if (selection instanceof StructuredSelection) {
144 StructuredSelection structered = ((StructuredSelection) selection);
145 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
146 Object element = (Object) iterator.next();
147 if (element instanceof TraceDomainComponent) {
148 TraceDomainComponent domain = (TraceDomainComponent) element;
149 TraceSessionComponent session = (TraceSessionComponent) domain.getParent();
150 // Add only TraceDomainComponent whose TraceSessionComponent parent is inactive and not destroyed
151 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
152 fDomain = domain;
153 }
154 }
155 }
156 }
157 return fDomain != null;
158 }
159 }
This page took 0.035596 seconds and 6 git commands to generate.