Improve package tangle index for LTTng 2.0 control design
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / 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 **********************************************************************/
115b4a01 12package org.eclipse.linuxtools.internal.lttng2.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;
9315aeee 26import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceSessionState;
115b4a01
BH
27import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
28import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
9315aeee 29import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
115b4a01 30import org.eclipse.linuxtools.internal.lttng2.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
c56972bb
BH
84 fLock.lock();
85 try {
86
87 final List<TraceSessionComponent> sessions = new ArrayList<TraceSessionComponent>();
88 sessions.addAll(fSessions);
bbb3538a 89
f455db37 90 Job job = new Job(Messages.TraceControl_ChangeSessionStateJob) {
c56972bb
BH
91 @Override
92 protected IStatus run(IProgressMonitor monitor) {
93 try {
94 for (Iterator<TraceSessionComponent> iterator = sessions.iterator(); iterator.hasNext();) {
bbb3538a 95
c56972bb
BH
96 // Start all selected sessions
97 TraceSessionComponent session = (TraceSessionComponent) iterator.next();
98 changeState(session, monitor);
99
100 // Set Session state
101 session.setSessionState(getNewState());
102 session.fireComponentChanged(session);
103 }
104 } catch (ExecutionException e) {
f455db37 105 return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeSessionStateFailure, e);
c56972bb
BH
106 }
107 return Status.OK_STATUS;
108 }
109 };
110 job.setUser(true);
111 job.schedule();
112 } finally {
113 fLock.unlock();
114 }
bbb3538a
BH
115 return null;
116 }
117
118 /*
119 * (non-Javadoc)
120 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
121 */
122 @Override
123 public boolean isEnabled() {
498704b3
BH
124 // Get workbench page for the Control View
125 IWorkbenchPage page = getWorkbenchPage();
bbb3538a
BH
126 if (page == null) {
127 return false;
128 }
129
c56972bb 130 List<TraceSessionComponent> sessions = new ArrayList<TraceSessionComponent>(0);
bbb3538a
BH
131
132 // Check if one or more session are selected
133 ISelection selection = page.getSelection(ControlView.ID);
134 if (selection instanceof StructuredSelection) {
135 StructuredSelection structered = ((StructuredSelection) selection);
136 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
137 Object element = (Object) iterator.next();
138 if (element instanceof TraceSessionComponent) {
139 // Add only TraceSessionComponents that are inactive and not destroyed
140 TraceSessionComponent session = (TraceSessionComponent) element;
141 if ((session.getSessionState() != getNewState()) && (!session.isDestroyed())) {
c56972bb 142 sessions.add(session);
bbb3538a
BH
143 }
144 }
145 }
146 }
c56972bb
BH
147 boolean isEnabled = !sessions.isEmpty();
148 fLock.lock();
149 try {
150 fSessions = null;
151 if (isEnabled) {
152 fSessions = sessions;
153 }
154 } finally {
155 fLock.unlock();
156 }
157 return isEnabled;
bbb3538a
BH
158 }
159}
This page took 0.035608 seconds and 5 git commands to generate.