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