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 | **********************************************************************/ | |
31a6a4e4 | 12 | package org.eclipse.linuxtools.internal.lttng.ui.views.control.handlers; |
bbb3538a BH |
13 | |
14 | import java.util.ArrayList; | |
15 | import java.util.Iterator; | |
16 | import java.util.List; | |
17 | ||
bbb3538a BH |
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; | |
31a6a4e4 BH |
27 | import org.eclipse.linuxtools.internal.lttng.ui.Activator; |
28 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.ControlView; | |
29 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.Messages; | |
30 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.dialogs.ICreateChannelDialog; | |
31 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.dialogs.TraceControlDialogFactory; | |
32 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.TraceSessionState; | |
33 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceDomainComponent; | |
34 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceSessionComponent; | |
bbb3538a | 35 | import org.eclipse.ui.IWorkbenchPage; |
bbb3538a BH |
36 | |
37 | /** | |
38 | * <b><u>CreateChannelOnDomainHandler</u></b> | |
39 | * <p> | |
40 | * Command handler implementation to create a trace channel for known domain. | |
41 | * </p> | |
42 | */ | |
498704b3 | 43 | public class CreateChannelOnDomainHandler extends BaseControlViewHandler { |
bbb3538a BH |
44 | |
45 | // ------------------------------------------------------------------------ | |
46 | // Attributes | |
47 | // ------------------------------------------------------------------------ | |
48 | /** | |
49 | * The the domain component the command is to be executed on. | |
50 | */ | |
51 | private TraceDomainComponent fDomain; | |
52 | ||
53 | // ------------------------------------------------------------------------ | |
54 | // Operations | |
55 | // ------------------------------------------------------------------------ | |
56 | /* | |
57 | * (non-Javadoc) | |
58 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
59 | */ | |
60 | @Override | |
61 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
62 | ||
bbb3538a | 63 | // Get channel information from user |
d132bcc7 BH |
64 | final ICreateChannelDialog dialog = TraceControlDialogFactory.getInstance().getCreateChannelDialog(); |
65 | dialog.setDomainComponent(fDomain); | |
bbb3538a | 66 | |
6503ae0f BH |
67 | if (dialog.open() != Window.OK) { |
68 | return null; | |
69 | } | |
bbb3538a | 70 | |
6503ae0f BH |
71 | Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) { |
72 | @Override | |
73 | protected IStatus run(IProgressMonitor monitor) { | |
74 | String errorString = null; | |
bbb3538a | 75 | |
6503ae0f BH |
76 | List<String> channelNames = new ArrayList<String>(); |
77 | channelNames.add(dialog.getChannelInfo().getName()); | |
78 | ||
79 | try { | |
80 | fDomain.enableChannels(channelNames, dialog.getChannelInfo(), monitor); | |
81 | } catch (ExecutionException e) { | |
82 | if (errorString == null) { | |
83 | errorString = new String(); | |
84 | } | |
85 | errorString += e.toString() + "\n"; //$NON-NLS-1$ | |
86 | } | |
87 | ||
88 | // get session configuration in all cases | |
89 | try { | |
90 | fDomain.getConfigurationFromNode(monitor); | |
91 | } catch (ExecutionException e) { | |
92 | if (errorString == null) { | |
93 | errorString = new String(); | |
bbb3538a | 94 | } |
6503ae0f BH |
95 | errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$ |
96 | } | |
97 | ||
98 | if (errorString != null) { | |
31a6a4e4 | 99 | return new Status(Status.ERROR, Activator.PLUGIN_ID, errorString); |
6503ae0f BH |
100 | } |
101 | return Status.OK_STATUS; | |
102 | } | |
103 | }; | |
104 | ||
105 | job.setUser(true); | |
106 | job.schedule(); | |
107 | ||
bbb3538a BH |
108 | return null; |
109 | } | |
110 | ||
111 | /* | |
112 | * (non-Javadoc) | |
113 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
114 | */ | |
115 | @Override | |
116 | public boolean isEnabled() { | |
498704b3 BH |
117 | |
118 | // Get workbench page for the Control View | |
119 | IWorkbenchPage page = getWorkbenchPage(); | |
bbb3538a BH |
120 | if (page == null) { |
121 | return false; | |
122 | } | |
498704b3 | 123 | fDomain = null; |
bbb3538a BH |
124 | |
125 | // Check if one domain is selected | |
126 | ISelection selection = page.getSelection(ControlView.ID); | |
127 | if (selection instanceof StructuredSelection) { | |
128 | StructuredSelection structered = ((StructuredSelection) selection); | |
129 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
130 | Object element = (Object) iterator.next(); | |
131 | if (element instanceof TraceDomainComponent) { | |
132 | TraceDomainComponent domain = (TraceDomainComponent) element; | |
133 | TraceSessionComponent session = (TraceSessionComponent) domain.getParent(); | |
134 | // Add only TraceDomainComponent whose TraceSessionComponent parent is inactive and not destroyed | |
135 | if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) { | |
136 | fDomain = domain; | |
137 | } | |
138 | } | |
139 | } | |
140 | } | |
141 | return fDomain != null; | |
142 | } | |
143 | } |