1 /**********************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.handlers
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Iterator
;
16 import java
.util
.List
;
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
.control
.core
.model
.TraceSessionState
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.Activator
;
28 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.ControlView
;
29 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
30 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceSessionComponent
;
31 import org
.eclipse
.ui
.IWorkbenchPage
;
32 import org
.eclipse
.ui
.IWorkbenchWindow
;
33 import org
.eclipse
.ui
.PlatformUI
;
37 * Abstract command handler implementation to start or stop one or more trace sessions.
40 * @author Bernd Hufmann
42 public abstract class ChangeSessionStateHandler
extends BaseControlViewHandler
{
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 * The list of session components the command is to be executed on.
50 protected List
<TraceSessionComponent
> fSessions
= new ArrayList
<>();
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * @return new required state.
59 public abstract TraceSessionState
getNewState();
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * Performs the state change on given session.
69 * - a session which state is to be changed
71 * - a progress monitor
72 * @throws ExecutionException
73 * If the command fails
75 public abstract void changeState(TraceSessionComponent session
, IProgressMonitor monitor
) throws ExecutionException
;
78 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
80 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
89 final List
<TraceSessionComponent
> sessions
= new ArrayList
<>();
90 sessions
.addAll(fSessions
);
92 Job job
= new Job(Messages
.TraceControl_ChangeSessionStateJob
) {
94 protected IStatus
run(IProgressMonitor monitor
) {
96 for (Iterator
<TraceSessionComponent
> iterator
= sessions
.iterator(); iterator
.hasNext();) {
98 // Start all selected sessions
99 TraceSessionComponent session
= iterator
.next();
100 changeState(session
, monitor
);
103 session
.setSessionState(getNewState());
104 session
.fireComponentChanged(session
);
106 } catch (ExecutionException e
) {
107 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ChangeSessionStateFailure
, e
);
109 return Status
.OK_STATUS
;
121 public boolean isEnabled() {
122 // Get workbench page for the Control View
123 IWorkbenchPage page
= getWorkbenchPage();
128 List
<TraceSessionComponent
> sessions
= new ArrayList
<>(0);
130 // Check if one or more session are selected
131 ISelection selection
= page
.getSelection(ControlView
.ID
);
132 if (selection
instanceof StructuredSelection
) {
133 StructuredSelection structered
= ((StructuredSelection
) selection
);
134 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
135 Object element
= iterator
.next();
136 if (element
instanceof TraceSessionComponent
) {
137 // Add only TraceSessionComponents that are inactive and not destroyed
138 TraceSessionComponent session
= (TraceSessionComponent
) element
;
139 if ((session
.getSessionState() != getNewState()) && (!session
.isDestroyed())) {
140 sessions
.add(session
);
145 boolean isEnabled
= !sessions
.isEmpty();
150 fSessions
= sessions
;