Add capability to enable events with different parameters on session,
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / CreateChannelOnSessionHandler.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;
30import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.CreateChannelDialog;
31import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.ICreateChannelOnSessionDialog;
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
36import org.eclipse.ui.IWorkbenchWindow;
37import org.eclipse.ui.PlatformUI;
38
39/**
40 * <b><u>CreateChannelOnSessionHandler</u></b>
41 * <p>
42 * Command handler implementation to create a trace channel for unknown domain
43 * (on session level).
44 * </p>
45 */
498704b3 46public class CreateChannelOnSessionHandler extends BaseControlViewHandler {
bbb3538a
BH
47
48 // ------------------------------------------------------------------------
49 // Attributes
50 // ------------------------------------------------------------------------
51 /**
52 * The session component the command is to be executed on.
53 */
54 private TraceSessionComponent fSession = null;
55
56 // ------------------------------------------------------------------------
57 // Operations
58 // ------------------------------------------------------------------------
59 /*
60 * (non-Javadoc)
61 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
62 */
63 @Override
64 public Object execute(ExecutionEvent event) throws ExecutionException {
65
66 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
67
68 if (window == null) {
69 return false;
70 }
71
72 final ICreateChannelOnSessionDialog dialog = new CreateChannelDialog(window.getShell());
73
6503ae0f
BH
74 if (dialog.open() != Window.OK) {
75 return null;
76 }
bbb3538a 77
6503ae0f
BH
78 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
79 @Override
80 protected IStatus run(IProgressMonitor monitor) {
81 String errorString = null;
82
83 List<String> channelNames = new ArrayList<String>();
84 TraceDomainComponent newDomain = new TraceDomainComponent("dummy", fSession); //$NON-NLS-1$
85 channelNames.add(dialog.getChannelInfo().getName());
86 newDomain.setIsKernel(dialog.isKernel());
87
88 try {
89 newDomain.enableChannels(channelNames, dialog.getChannelInfo(), monitor);
90 } catch (ExecutionException e) {
91 if (errorString == null) {
92 errorString = new String();
bbb3538a 93 }
6503ae0f
BH
94 errorString += e.toString() + "\n"; //$NON-NLS-1$
95 }
96
97 // get session configuration in all cases
98 try {
99 fSession.getConfigurationFromNode(monitor);
100 } catch (ExecutionException e) {
101 if (errorString == null) {
102 errorString = new String();
bbb3538a 103 }
6503ae0f
BH
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 job.setUser(true);
114 job.schedule();
bbb3538a
BH
115
116 return null;
117 }
118
119 /*
120 * (non-Javadoc)
121 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
122 */
123 @Override
124 public boolean isEnabled() {
498704b3
BH
125 // Get workbench page for the Control View
126 IWorkbenchPage page = getWorkbenchPage();
bbb3538a
BH
127 if (page == null) {
128 return false;
129 }
130
498704b3 131 fSession = null;
bbb3538a
BH
132
133 // Check if one session is selected
134 ISelection selection = page.getSelection(ControlView.ID);
135 if (selection instanceof StructuredSelection) {
136 StructuredSelection structered = ((StructuredSelection) selection);
137 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
138 Object element = (Object) iterator.next();
139 if (element instanceof TraceSessionComponent) {
140 // Add only TraceSessionComponents that are inactive and not destroyed
141 TraceSessionComponent session = (TraceSessionComponent) element;
142 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
143 fSession = session;
144 }
145 }
146 }
147 }
148 return fSession != null;
149 }
150}
This page took 0.029056 seconds and 5 git commands to generate.