Commit | Line | Data |
---|---|---|
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 | **********************************************************************/ | |
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.ICreateChannelOnSessionDialog; | |
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>CreateChannelOnSessionHandler</u></b> | |
43 | * <p> | |
44 | * Command handler implementation to create a trace channel for unknown domain | |
45 | * (on session level). | |
46 | * </p> | |
47 | */ | |
48 | public class CreateChannelOnSessionHandler extends AbstractHandler { | |
49 | ||
50 | // ------------------------------------------------------------------------ | |
51 | // Attributes | |
52 | // ------------------------------------------------------------------------ | |
53 | /** | |
54 | * The session component the command is to be executed on. | |
55 | */ | |
56 | private TraceSessionComponent fSession = null; | |
57 | ||
58 | // ------------------------------------------------------------------------ | |
59 | // Operations | |
60 | // ------------------------------------------------------------------------ | |
61 | /* | |
62 | * (non-Javadoc) | |
63 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
64 | */ | |
65 | @Override | |
66 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
67 | ||
68 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
69 | ||
70 | if (window == null) { | |
71 | return false; | |
72 | } | |
73 | ||
74 | final ICreateChannelOnSessionDialog dialog = new CreateChannelDialog(window.getShell()); | |
75 | ||
6503ae0f BH |
76 | if (dialog.open() != Window.OK) { |
77 | return null; | |
78 | } | |
bbb3538a | 79 | |
6503ae0f BH |
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 | TraceDomainComponent newDomain = new TraceDomainComponent("dummy", fSession); //$NON-NLS-1$ | |
87 | channelNames.add(dialog.getChannelInfo().getName()); | |
88 | newDomain.setIsKernel(dialog.isKernel()); | |
89 | ||
90 | try { | |
91 | newDomain.enableChannels(channelNames, dialog.getChannelInfo(), monitor); | |
92 | } catch (ExecutionException e) { | |
93 | if (errorString == null) { | |
94 | errorString = new String(); | |
bbb3538a | 95 | } |
6503ae0f BH |
96 | errorString += e.toString() + "\n"; //$NON-NLS-1$ |
97 | } | |
98 | ||
99 | // get session configuration in all cases | |
100 | try { | |
101 | fSession.getConfigurationFromNode(monitor); | |
102 | } catch (ExecutionException e) { | |
103 | if (errorString == null) { | |
104 | errorString = new String(); | |
bbb3538a | 105 | } |
6503ae0f BH |
106 | errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$ |
107 | } | |
108 | ||
109 | if (errorString != null) { | |
110 | return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString); | |
111 | } | |
112 | return Status.OK_STATUS; | |
113 | } | |
114 | }; | |
115 | job.setUser(true); | |
116 | job.schedule(); | |
bbb3538a BH |
117 | |
118 | return null; | |
119 | } | |
120 | ||
121 | /* | |
122 | * (non-Javadoc) | |
123 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
124 | */ | |
125 | @Override | |
126 | public boolean isEnabled() { | |
127 | fSession = null; | |
128 | ||
129 | // Check if we are closing down | |
130 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
131 | if (window == null) { | |
132 | return false; | |
133 | } | |
134 | ||
135 | // Check if we are in the Project View | |
136 | IWorkbenchPage page = window.getActivePage(); | |
137 | if (page == null) { | |
138 | return false; | |
139 | } | |
140 | ||
141 | IWorkbenchPart part = page.getActivePart(); | |
142 | if (!(part instanceof ControlView)) { | |
143 | return false; | |
144 | } | |
145 | ||
146 | // Check if one session is selected | |
147 | ISelection selection = page.getSelection(ControlView.ID); | |
148 | if (selection instanceof StructuredSelection) { | |
149 | StructuredSelection structered = ((StructuredSelection) selection); | |
150 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
151 | Object element = (Object) iterator.next(); | |
152 | if (element instanceof TraceSessionComponent) { | |
153 | // Add only TraceSessionComponents that are inactive and not destroyed | |
154 | TraceSessionComponent session = (TraceSessionComponent) element; | |
155 | if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) { | |
156 | fSession = session; | |
157 | } | |
158 | } | |
159 | } | |
160 | } | |
161 | return fSession != null; | |
162 | } | |
163 | } |