Add further support for enabling kernel events
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / CreateChannelOnSessionHandler.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.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
28 import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
29 import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
30 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.CreateChannelDialog;
31 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.ICreateChannelOnSessionDialog;
32 import org.eclipse.linuxtools.lttng.ui.views.control.model.TraceSessionState;
33 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceDomainComponent;
34 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent;
35 import org.eclipse.ui.IWorkbenchPage;
36 import org.eclipse.ui.IWorkbenchPart;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.PlatformUI;
39
40 /**
41 * <b><u>CreateChannelOnSessionHandler</u></b>
42 * <p>
43 * Command handler implementation to create a trace channel for unknown domain
44 * (on session level).
45 * </p>
46 */
47 public class CreateChannelOnSessionHandler extends BaseControlViewHandler {
48
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52 /**
53 * The session component the command is to be executed on.
54 */
55 private TraceSessionComponent fSession = null;
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 final ICreateChannelOnSessionDialog dialog = new CreateChannelDialog(window.getShell());
74
75 if (dialog.open() != Window.OK) {
76 return null;
77 }
78
79 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
80 @Override
81 protected IStatus run(IProgressMonitor monitor) {
82 String errorString = null;
83
84 List<String> channelNames = new ArrayList<String>();
85 TraceDomainComponent newDomain = new TraceDomainComponent("dummy", fSession); //$NON-NLS-1$
86 channelNames.add(dialog.getChannelInfo().getName());
87 newDomain.setIsKernel(dialog.isKernel());
88
89 try {
90 newDomain.enableChannels(channelNames, dialog.getChannelInfo(), monitor);
91 } catch (ExecutionException e) {
92 if (errorString == null) {
93 errorString = new String();
94 }
95 errorString += e.toString() + "\n"; //$NON-NLS-1$
96 }
97
98 // get session configuration in all cases
99 try {
100 fSession.getConfigurationFromNode(monitor);
101 } catch (ExecutionException e) {
102 if (errorString == null) {
103 errorString = new String();
104 }
105 errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$
106 }
107
108 if (errorString != null) {
109 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString);
110 }
111 return Status.OK_STATUS;
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 // Get workbench page for the Control View
127 IWorkbenchPage page = getWorkbenchPage();
128 if (page == null) {
129 return false;
130 }
131
132 fSession = null;
133
134 // Check if one session is selected
135 ISelection selection = page.getSelection(ControlView.ID);
136 if (selection instanceof StructuredSelection) {
137 StructuredSelection structered = ((StructuredSelection) selection);
138 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
139 Object element = (Object) iterator.next();
140 if (element instanceof TraceSessionComponent) {
141 // Add only TraceSessionComponents that are inactive and not destroyed
142 TraceSessionComponent session = (TraceSessionComponent) element;
143 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
144 fSession = session;
145 }
146 }
147 }
148 }
149 return fSession != null;
150 }
151 }
This page took 0.033992 seconds and 5 git commands to generate.