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.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.model.TraceSessionState; | |
31 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent; | |
32 | import org.eclipse.ui.IWorkbenchPage; | |
33 | import org.eclipse.ui.IWorkbenchPart; | |
34 | import org.eclipse.ui.IWorkbenchWindow; | |
35 | import org.eclipse.ui.PlatformUI; | |
36 | ||
37 | /** | |
38 | * <b><u>ChangeSessionStateHandler</u></b> | |
39 | * <p> | |
40 | * Abstract command handler implementation to start or stop one or more trace sessions. | |
41 | * </p> | |
42 | */ | |
43 | abstract public class ChangeSessionStateHandler extends AbstractHandler { | |
44 | ||
45 | // ------------------------------------------------------------------------ | |
46 | // Attributes | |
47 | // ------------------------------------------------------------------------ | |
48 | /** | |
49 | * The list of session components the command is to be executed on. | |
50 | */ | |
51 | protected List<TraceSessionComponent> fSessions = new ArrayList<TraceSessionComponent>(); | |
52 | ||
53 | // ------------------------------------------------------------------------ | |
54 | // Accessors | |
55 | // ------------------------------------------------------------------------ | |
56 | ||
57 | /** | |
58 | * @return new required state. | |
59 | */ | |
60 | abstract TraceSessionState getNewState(); | |
61 | ||
62 | // ------------------------------------------------------------------------ | |
63 | // Operations | |
64 | // ------------------------------------------------------------------------ | |
65 | ||
66 | /** | |
67 | * Performs the state change on given session. | |
68 | * @param session - a session which state is to be changed | |
69 | * @param monitor - a progress monitor | |
70 | */ | |
71 | abstract public void changeState(TraceSessionComponent session, IProgressMonitor monitor) throws ExecutionException; | |
72 | ||
73 | /* | |
74 | * (non-Javadoc) | |
75 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
76 | */ | |
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 | ||
86 | Job job = new Job(Messages.TraceControl_StartSessionJob) { | |
87 | @Override | |
88 | protected IStatus run(IProgressMonitor monitor) { | |
89 | try { | |
90 | for (Iterator<TraceSessionComponent> iterator = fSessions.iterator(); iterator.hasNext();) { | |
91 | ||
92 | // Start all selected sessions | |
93 | TraceSessionComponent session = (TraceSessionComponent) iterator.next(); | |
94 | changeState(session, monitor); | |
95 | ||
96 | // Set Session state | |
97 | session.setSessionState(getNewState()); | |
98 | session.fireComponentChanged(session); | |
99 | } | |
100 | } catch (ExecutionException e) { | |
101 | return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, e.toString()); | |
102 | } | |
103 | return Status.OK_STATUS; | |
104 | } | |
105 | }; | |
106 | job.setUser(true); | |
107 | job.schedule(); | |
108 | return null; | |
109 | } | |
110 | ||
111 | /* | |
112 | * (non-Javadoc) | |
113 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
114 | */ | |
115 | @Override | |
116 | public boolean isEnabled() { | |
117 | fSessions.clear(); | |
118 | ||
119 | // Check if we are closing down | |
120 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
121 | if (window == null) { | |
122 | return false; | |
123 | } | |
124 | ||
125 | // Check if we are in the Project View | |
126 | IWorkbenchPage page = window.getActivePage(); | |
127 | if (page == null) { | |
128 | return false; | |
129 | } | |
130 | ||
131 | IWorkbenchPart part = page.getActivePart(); | |
132 | if (!(part instanceof ControlView)) { | |
133 | return false; | |
134 | } | |
135 | ||
136 | // Check if one or more session are selected | |
137 | ISelection selection = page.getSelection(ControlView.ID); | |
138 | if (selection instanceof StructuredSelection) { | |
139 | StructuredSelection structered = ((StructuredSelection) selection); | |
140 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
141 | Object element = (Object) iterator.next(); | |
142 | if (element instanceof TraceSessionComponent) { | |
143 | // Add only TraceSessionComponents that are inactive and not destroyed | |
144 | TraceSessionComponent session = (TraceSessionComponent) element; | |
145 | if ((session.getSessionState() != getNewState()) && (!session.isDestroyed())) { | |
146 | fSessions.add((TraceSessionComponent)element); | |
147 | } | |
148 | } | |
149 | } | |
150 | } | |
151 | return fSessions.size() > 0; | |
152 | } | |
153 | } |