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
.ui
.Activator
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.ControlView
;
28 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.Messages
;
29 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.TraceSessionState
;
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
;
36 * <b><u>ChangeSessionStateHandler</u></b>
38 * Abstract command handler implementation to start or stop one or more trace sessions.
41 abstract public class ChangeSessionStateHandler
extends BaseControlViewHandler
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 * The list of session components the command is to be executed on.
49 protected List
<TraceSessionComponent
> fSessions
= new ArrayList
<TraceSessionComponent
>();
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * @return new required state.
58 abstract TraceSessionState
getNewState();
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
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
69 abstract public void changeState(TraceSessionComponent session
, IProgressMonitor monitor
) throws ExecutionException
;
73 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
76 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
78 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
87 final List
<TraceSessionComponent
> sessions
= new ArrayList
<TraceSessionComponent
>();
88 sessions
.addAll(fSessions
);
90 Job job
= new Job(Messages
.TraceControl_StartSessionJob
) {
92 protected IStatus
run(IProgressMonitor monitor
) {
94 for (Iterator
<TraceSessionComponent
> iterator
= sessions
.iterator(); iterator
.hasNext();) {
96 // Start all selected sessions
97 TraceSessionComponent session
= (TraceSessionComponent
) iterator
.next();
98 changeState(session
, monitor
);
101 session
.setSessionState(getNewState());
102 session
.fireComponentChanged(session
);
104 } catch (ExecutionException e
) {
105 return new Status(Status
.ERROR
, Activator
.PLUGIN_ID
, e
.toString());
107 return Status
.OK_STATUS
;
120 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
123 public boolean isEnabled() {
124 // Get workbench page for the Control View
125 IWorkbenchPage page
= getWorkbenchPage();
130 List
<TraceSessionComponent
> sessions
= new ArrayList
<TraceSessionComponent
>(0);
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())) {
142 sessions
.add(session
);
147 boolean isEnabled
= !sessions
.isEmpty();
152 fSessions
= sessions
;