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();
84 Job job
= new Job(Messages
.TraceControl_StartSessionJob
) {
86 protected IStatus
run(IProgressMonitor monitor
) {
88 for (Iterator
<TraceSessionComponent
> iterator
= fSessions
.iterator(); iterator
.hasNext();) {
90 // Start all selected sessions
91 TraceSessionComponent session
= (TraceSessionComponent
) iterator
.next();
92 changeState(session
, monitor
);
95 session
.setSessionState(getNewState());
96 session
.fireComponentChanged(session
);
98 } catch (ExecutionException e
) {
99 return new Status(Status
.ERROR
, Activator
.PLUGIN_ID
, e
.toString());
101 return Status
.OK_STATUS
;
111 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
114 public boolean isEnabled() {
115 // Get workbench page for the Control View
116 IWorkbenchPage page
= getWorkbenchPage();
123 // Check if one or more session are selected
124 ISelection selection
= page
.getSelection(ControlView
.ID
);
125 if (selection
instanceof StructuredSelection
) {
126 StructuredSelection structered
= ((StructuredSelection
) selection
);
127 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
128 Object element
= (Object
) iterator
.next();
129 if (element
instanceof TraceSessionComponent
) {
130 // Add only TraceSessionComponents that are inactive and not destroyed
131 TraceSessionComponent session
= (TraceSessionComponent
) element
;
132 if ((session
.getSessionState() != getNewState()) && (!session
.isDestroyed())) {
133 fSessions
.add((TraceSessionComponent
)element
);
138 return fSessions
.size() > 0;