1 /**********************************************************************
2 * Copyright (c) 2012 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
.ui
.views
.control
.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
.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
;
37 * Abstract command handler implementation to start or stop one or more trace sessions.
40 * @author Bernd Hufmann
42 abstract public 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
<TraceSessionComponent
>();
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * @return new required state.
59 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 abstract public void changeState(TraceSessionComponent session
, IProgressMonitor monitor
) throws ExecutionException
;
79 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
82 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
84 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
93 final List
<TraceSessionComponent
> sessions
= new ArrayList
<TraceSessionComponent
>();
94 sessions
.addAll(fSessions
);
96 Job job
= new Job(Messages
.TraceControl_ChangeSessionStateJob
) {
98 protected IStatus
run(IProgressMonitor monitor
) {
100 for (Iterator
<TraceSessionComponent
> iterator
= sessions
.iterator(); iterator
.hasNext();) {
102 // Start all selected sessions
103 TraceSessionComponent session
= iterator
.next();
104 changeState(session
, monitor
);
107 session
.setSessionState(getNewState());
108 session
.fireComponentChanged(session
);
110 } catch (ExecutionException e
) {
111 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ChangeSessionStateFailure
, e
);
113 return Status
.OK_STATUS
;
126 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
129 public boolean isEnabled() {
130 // Get workbench page for the Control View
131 IWorkbenchPage page
= getWorkbenchPage();
136 List
<TraceSessionComponent
> sessions
= new ArrayList
<TraceSessionComponent
>(0);
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
);
153 boolean isEnabled
= !sessions
.isEmpty();
158 fSessions
= sessions
;