Fix for bug 382684 (connection re-use)
[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
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 **********************************************************************/
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/**
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 */
498704b3 42abstract public 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
BH
49 */
50 protected List<TraceSessionComponent> fSessions = new ArrayList<TraceSessionComponent>();
cfdb727a 51
bbb3538a
BH
52 // ------------------------------------------------------------------------
53 // Accessors
54 // ------------------------------------------------------------------------
55
56 /**
57 * @return new required state.
58 */
59 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
BH
74 */
75 abstract public void changeState(TraceSessionComponent session, IProgressMonitor monitor) throws ExecutionException;
76
77 /*
78 * (non-Javadoc)
79 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
80 */
81 @Override
82 public Object execute(ExecutionEvent event) throws ExecutionException {
83
84 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
85
86 if (window == null) {
87 return false;
88 }
89
c56972bb
BH
90 fLock.lock();
91 try {
cfdb727a 92
c56972bb
BH
93 final List<TraceSessionComponent> sessions = new ArrayList<TraceSessionComponent>();
94 sessions.addAll(fSessions);
bbb3538a 95
f455db37 96 Job job = new Job(Messages.TraceControl_ChangeSessionStateJob) {
c56972bb
BH
97 @Override
98 protected IStatus run(IProgressMonitor monitor) {
99 try {
100 for (Iterator<TraceSessionComponent> iterator = sessions.iterator(); iterator.hasNext();) {
bbb3538a 101
c56972bb 102 // Start all selected sessions
cfdb727a 103 TraceSessionComponent session = iterator.next();
c56972bb
BH
104 changeState(session, monitor);
105
106 // Set Session state
107 session.setSessionState(getNewState());
108 session.fireComponentChanged(session);
109 }
110 } catch (ExecutionException e) {
cfdb727a
AM
111 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeSessionStateFailure, e);
112 }
c56972bb
BH
113 return Status.OK_STATUS;
114 }
115 };
116 job.setUser(true);
117 job.schedule();
118 } finally {
119 fLock.unlock();
120 }
bbb3538a
BH
121 return null;
122 }
123
124 /*
125 * (non-Javadoc)
126 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
127 */
128 @Override
129 public boolean isEnabled() {
498704b3
BH
130 // Get workbench page for the Control View
131 IWorkbenchPage page = getWorkbenchPage();
bbb3538a
BH
132 if (page == null) {
133 return false;
134 }
135
c56972bb 136 List<TraceSessionComponent> sessions = new ArrayList<TraceSessionComponent>(0);
bbb3538a
BH
137
138 // Check if one or more session are selected
139 ISelection selection = page.getSelection(ControlView.ID);
140 if (selection instanceof StructuredSelection) {
141 StructuredSelection structered = ((StructuredSelection) selection);
142 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
cfdb727a 143 Object element = iterator.next();
bbb3538a
BH
144 if (element instanceof TraceSessionComponent) {
145 // Add only TraceSessionComponents that are inactive and not destroyed
146 TraceSessionComponent session = (TraceSessionComponent) element;
147 if ((session.getSessionState() != getNewState()) && (!session.isDestroyed())) {
c56972bb 148 sessions.add(session);
bbb3538a
BH
149 }
150 }
151 }
152 }
c56972bb
BH
153 boolean isEnabled = !sessions.isEmpty();
154 fLock.lock();
155 try {
156 fSessions = null;
157 if (isEnabled) {
158 fSessions = sessions;
159 }
160 } finally {
161 fLock.unlock();
162 }
163 return isEnabled;
bbb3538a
BH
164 }
165}
This page took 0.036878 seconds and 5 git commands to generate.