Commit | Line | Data |
---|---|---|
bbb3538a | 1 | /********************************************************************** |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 Ericsson |
cfdb727a | 3 | * |
bbb3538a BH |
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 | |
cfdb727a AM |
8 | * |
9 | * Contributors: | |
bbb3538a BH |
10 | * Bernd Hufmann - Initial API and implementation |
11 | **********************************************************************/ | |
8e8c0226 | 12 | package org.eclipse.linuxtools.internal.lttng2.control.ui.views.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; | |
8e8c0226 AM |
26 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceSessionState; |
27 | import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator; | |
28 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.ControlView; | |
29 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages; | |
30 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent; | |
bbb3538a | 31 | import org.eclipse.ui.IWorkbenchPage; |
bbb3538a BH |
32 | import org.eclipse.ui.IWorkbenchWindow; |
33 | import org.eclipse.ui.PlatformUI; | |
34 | ||
35 | /** | |
bbb3538a BH |
36 | * <p> |
37 | * Abstract command handler implementation to start or stop one or more trace sessions. | |
38 | * </p> | |
cfdb727a | 39 | * |
dbd4432d | 40 | * @author Bernd Hufmann |
bbb3538a | 41 | */ |
046b6849 | 42 | public abstract class ChangeSessionStateHandler extends BaseControlViewHandler { |
bbb3538a BH |
43 | |
44 | // ------------------------------------------------------------------------ | |
45 | // Attributes | |
46 | // ------------------------------------------------------------------------ | |
47 | /** | |
cfdb727a | 48 | * The list of session components the command is to be executed on. |
bbb3538a | 49 | */ |
e0838ca1 | 50 | protected List<TraceSessionComponent> fSessions = new ArrayList<>(); |
cfdb727a | 51 | |
bbb3538a BH |
52 | // ------------------------------------------------------------------------ |
53 | // Accessors | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
56 | /** | |
57 | * @return new required state. | |
58 | */ | |
046b6849 | 59 | public abstract TraceSessionState getNewState(); |
cfdb727a | 60 | |
bbb3538a BH |
61 | // ------------------------------------------------------------------------ |
62 | // Operations | |
63 | // ------------------------------------------------------------------------ | |
cfdb727a | 64 | |
bbb3538a | 65 | /** |
cfdb727a AM |
66 | * Performs the state change on given session. |
67 | * | |
68 | * @param session | |
69 | * - a session which state is to be changed | |
70 | * @param monitor | |
71 | * - a progress monitor | |
72 | * @throws ExecutionException | |
73 | * If the command fails | |
bbb3538a | 74 | */ |
77735e82 | 75 | public abstract void changeState(TraceSessionComponent session, IProgressMonitor monitor) throws ExecutionException; |
bbb3538a | 76 | |
bbb3538a BH |
77 | @Override |
78 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
79 | ||
80 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
81 | ||
82 | if (window == null) { | |
83 | return false; | |
84 | } | |
85 | ||
c56972bb BH |
86 | fLock.lock(); |
87 | try { | |
cfdb727a | 88 | |
e0838ca1 | 89 | final List<TraceSessionComponent> sessions = new ArrayList<>(); |
c56972bb | 90 | sessions.addAll(fSessions); |
bbb3538a | 91 | |
f455db37 | 92 | Job job = new Job(Messages.TraceControl_ChangeSessionStateJob) { |
c56972bb BH |
93 | @Override |
94 | protected IStatus run(IProgressMonitor monitor) { | |
95 | try { | |
96 | for (Iterator<TraceSessionComponent> iterator = sessions.iterator(); iterator.hasNext();) { | |
bbb3538a | 97 | |
c56972bb | 98 | // Start all selected sessions |
cfdb727a | 99 | TraceSessionComponent session = iterator.next(); |
c56972bb BH |
100 | changeState(session, monitor); |
101 | ||
102 | // Set Session state | |
103 | session.setSessionState(getNewState()); | |
104 | session.fireComponentChanged(session); | |
105 | } | |
106 | } catch (ExecutionException e) { | |
cfdb727a AM |
107 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeSessionStateFailure, e); |
108 | } | |
c56972bb BH |
109 | return Status.OK_STATUS; |
110 | } | |
111 | }; | |
112 | job.setUser(true); | |
113 | job.schedule(); | |
114 | } finally { | |
115 | fLock.unlock(); | |
116 | } | |
bbb3538a BH |
117 | return null; |
118 | } | |
119 | ||
bbb3538a BH |
120 | @Override |
121 | public boolean isEnabled() { | |
498704b3 BH |
122 | // Get workbench page for the Control View |
123 | IWorkbenchPage page = getWorkbenchPage(); | |
bbb3538a BH |
124 | if (page == null) { |
125 | return false; | |
126 | } | |
127 | ||
e0838ca1 | 128 | List<TraceSessionComponent> sessions = new ArrayList<>(0); |
bbb3538a BH |
129 | |
130 | // Check if one or more session are selected | |
131 | ISelection selection = page.getSelection(ControlView.ID); | |
132 | if (selection instanceof StructuredSelection) { | |
133 | StructuredSelection structered = ((StructuredSelection) selection); | |
134 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
cfdb727a | 135 | Object element = iterator.next(); |
bbb3538a BH |
136 | if (element instanceof TraceSessionComponent) { |
137 | // Add only TraceSessionComponents that are inactive and not destroyed | |
138 | TraceSessionComponent session = (TraceSessionComponent) element; | |
139 | if ((session.getSessionState() != getNewState()) && (!session.isDestroyed())) { | |
c56972bb | 140 | sessions.add(session); |
bbb3538a BH |
141 | } |
142 | } | |
143 | } | |
144 | } | |
c56972bb BH |
145 | boolean isEnabled = !sessions.isEmpty(); |
146 | fLock.lock(); | |
147 | try { | |
148 | fSessions = null; | |
149 | if (isEnabled) { | |
150 | fSessions = sessions; | |
151 | } | |
152 | } finally { | |
153 | fLock.unlock(); | |
154 | } | |
155 | return isEnabled; | |
bbb3538a BH |
156 | } |
157 | } |