lttng: Fix Javadoc and formatting in lttng2.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / 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.lttng2.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.lttng2.core.control.model.TraceSessionState;
27 import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
28 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
29 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
30 import org.eclipse.linuxtools.internal.lttng2.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 * <p>
37 * Abstract command handler implementation to start or stop one or more trace sessions.
38 * </p>
39 *
40 * @author Bernd Hufmann
41 */
42 abstract public class ChangeSessionStateHandler extends BaseControlViewHandler {
43
44 // ------------------------------------------------------------------------
45 // Attributes
46 // ------------------------------------------------------------------------
47 /**
48 * The list of session components the command is to be executed on.
49 */
50 protected List<TraceSessionComponent> fSessions = new ArrayList<TraceSessionComponent>();
51
52 // ------------------------------------------------------------------------
53 // Accessors
54 // ------------------------------------------------------------------------
55
56 /**
57 * @return new required state.
58 */
59 abstract TraceSessionState getNewState();
60
61 // ------------------------------------------------------------------------
62 // Operations
63 // ------------------------------------------------------------------------
64
65 /**
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
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
90 fLock.lock();
91 try {
92
93 final List<TraceSessionComponent> sessions = new ArrayList<TraceSessionComponent>();
94 sessions.addAll(fSessions);
95
96 Job job = new Job(Messages.TraceControl_ChangeSessionStateJob) {
97 @Override
98 protected IStatus run(IProgressMonitor monitor) {
99 try {
100 for (Iterator<TraceSessionComponent> iterator = sessions.iterator(); iterator.hasNext();) {
101
102 // Start all selected sessions
103 TraceSessionComponent session = iterator.next();
104 changeState(session, monitor);
105
106 // Set Session state
107 session.setSessionState(getNewState());
108 session.fireComponentChanged(session);
109 }
110 } catch (ExecutionException e) {
111 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeSessionStateFailure, e);
112 }
113 return Status.OK_STATUS;
114 }
115 };
116 job.setUser(true);
117 job.schedule();
118 } finally {
119 fLock.unlock();
120 }
121 return null;
122 }
123
124 /*
125 * (non-Javadoc)
126 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
127 */
128 @Override
129 public boolean isEnabled() {
130 // Get workbench page for the Control View
131 IWorkbenchPage page = getWorkbenchPage();
132 if (page == null) {
133 return false;
134 }
135
136 List<TraceSessionComponent> sessions = new ArrayList<TraceSessionComponent>(0);
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();) {
143 Object element = iterator.next();
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())) {
148 sessions.add(session);
149 }
150 }
151 }
152 }
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;
164 }
165 }
This page took 0.034074 seconds and 5 git commands to generate.