30a8d8d9b9629e7343e1dad60442623781f19816
[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 return null;
78 }
79
80 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
81 @Override
82 protected IStatus run(IProgressMonitor monitor) {
83 String errorString = null;
84
85 List<String> channelNames = new ArrayList<String>();
86 channelNames.add(dialog.getChannelInfo().getName());
87
88 try {
89 fDomain.enableChannels(channelNames, dialog.getChannelInfo(), monitor);
90 } catch (ExecutionException e) {
91 if (errorString == null) {
92 errorString = new String();
93 }
94 errorString += e.toString() + "\n"; //$NON-NLS-1$
95 }
96
97 // get session configuration in all cases
98 try {
99 fDomain.getConfigurationFromNode(monitor);
100 } catch (ExecutionException e) {
101 if (errorString == null) {
102 errorString = new String();
103 }
104 errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$
105 }
106
107 if (errorString != null) {
108 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString);
109 }
110 return Status.OK_STATUS;
111 }
112 };
113
114 job.setUser(true);
115 job.schedule();
116
117 return null;
118 }
119
120 /*
121 * (non-Javadoc)
122 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
123 */
124 @Override
125 public boolean isEnabled() {
126 fDomain = null;
127
128 // Check if we are closing down
129 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
130 if (window == null) {
131 return false;
132 }
133
134 // Check if we are in the Project View
135 IWorkbenchPage page = window.getActivePage();
136 if (page == null) {
137 return false;
138 }
139
140 IWorkbenchPart part = page.getActivePart();
141 if (!(part instanceof ControlView)) {
142 return false;
143 }
144
145 // Check if one domain is selected
146 ISelection selection = page.getSelection(ControlView.ID);
147 if (selection instanceof StructuredSelection) {
148 StructuredSelection structered = ((StructuredSelection) selection);
149 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
150 Object element = (Object) iterator.next();
151 if (element instanceof TraceDomainComponent) {
152 TraceDomainComponent domain = (TraceDomainComponent) element;
153 TraceSessionComponent session = (TraceSessionComponent) domain.getParent();
154 // Add only TraceDomainComponent whose TraceSessionComponent parent is inactive and not destroyed
155 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
156 fDomain = domain;
157 }
158 }
159 }
160 }
161 return fDomain != null;
162 }
163 }
This page took 0.034743 seconds and 5 git commands to generate.