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
CommitLineData
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 12package org.eclipse.linuxtools.internal.lttng.ui.views.control.handlers;
bbb3538a
BH
13
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.List;
17
bbb3538a
BH
18import org.eclipse.core.commands.ExecutionEvent;
19import org.eclipse.core.commands.ExecutionException;
20import org.eclipse.core.runtime.IProgressMonitor;
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.Status;
23import org.eclipse.core.runtime.jobs.Job;
24import org.eclipse.jface.viewers.ISelection;
25import org.eclipse.jface.viewers.StructuredSelection;
31a6a4e4
BH
26import org.eclipse.linuxtools.internal.lttng.ui.Activator;
27import org.eclipse.linuxtools.internal.lttng.ui.views.control.ControlView;
28import org.eclipse.linuxtools.internal.lttng.ui.views.control.Messages;
29import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.TraceSessionState;
30import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceSessionComponent;
bbb3538a 31import org.eclipse.ui.IWorkbenchPage;
bbb3538a
BH
32import org.eclipse.ui.IWorkbenchWindow;
33import 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 */
498704b3 41abstract public class ChangeSessionStateHandler extends BaseControlViewHandler {
bbb3538a
BH
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) {
31a6a4e4 99 return new Status(Status.ERROR, Activator.PLUGIN_ID, e.toString());
bbb3538a
BH
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() {
498704b3
BH
115 // Get workbench page for the Control View
116 IWorkbenchPage page = getWorkbenchPage();
bbb3538a
BH
117 if (page == null) {
118 return false;
119 }
120
498704b3 121 fSessions.clear();
bbb3538a
BH
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.029904 seconds and 5 git commands to generate.