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
.lttng
.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
.dialogs
.MessageDialog
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
27 import org
.eclipse
.linuxtools
.lttng
.ui
.LTTngUiPlugin
;
28 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.control
.ControlView
;
29 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.control
.Messages
;
30 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.control
.model
.TraceSessionState
;
31 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.control
.model
.impl
.TraceSessionComponent
;
32 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.control
.model
.impl
.TraceSessionGroup
;
33 import org
.eclipse
.ui
.IWorkbenchPage
;
34 import org
.eclipse
.ui
.IWorkbenchWindow
;
35 import org
.eclipse
.ui
.PlatformUI
;
38 * <b><u>DestroySessionHandler</u></b>
40 * Command handler implementation to destroy one or more trace sessions.
43 public class DestroySessionHandler
extends BaseControlViewHandler
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 * The list of session components the command is to be executed on.
51 private List
<TraceSessionComponent
> fSessions
= new ArrayList
<TraceSessionComponent
>();
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
58 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
61 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
63 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
68 // Get user confirmation
69 if (!MessageDialog
.openConfirm(window
.getShell(),
70 Messages
.TraceControl_DestroyConfirmationTitle
,
71 Messages
.TraceControl_DestroyConfirmationMessage
)) {
77 Job job
= new Job(Messages
.TraceControl_DestroySessionJob
) {
79 protected IStatus
run(IProgressMonitor monitor
) {
81 // Make a copy of the list of sessions to avoid ConcurrentModificationException when iterating
82 // over fSessions, since fSessions is modified in another thread triggered by the tree viewer refresh
83 // after removing a session.
84 TraceSessionComponent
[] sessions
= (TraceSessionComponent
[])fSessions
.toArray(new TraceSessionComponent
[fSessions
.size()]);
86 for (int i
= 0; i
< sessions
.length
; i
++) {
87 // Destroy all selected sessions
88 TraceSessionComponent session
= sessions
[i
];
89 TraceSessionGroup sessionGroup
= (TraceSessionGroup
)session
.getParent();
90 sessionGroup
.destroySession(session
, monitor
);
92 } catch (ExecutionException e
) {
93 return new Status(Status
.ERROR
, LTTngUiPlugin
.PLUGIN_ID
, e
.toString());
95 return Status
.OK_STATUS
;
106 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
109 public boolean isEnabled() {
110 // Get workbench page for the Control View
111 IWorkbenchPage page
= getWorkbenchPage();
117 // Check if one or more session are selected
118 ISelection selection
= page
.getSelection(ControlView
.ID
);
119 if (selection
instanceof StructuredSelection
) {
120 StructuredSelection structered
= ((StructuredSelection
) selection
);
121 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
122 Object element
= (Object
) iterator
.next();
123 if (element
instanceof TraceSessionComponent
) {
124 // Add only TraceSessionComponents that are inactive and not destroyed
125 TraceSessionComponent session
= (TraceSessionComponent
) element
;
126 if ((session
.getSessionState() == TraceSessionState
.INACTIVE
) && (!session
.isDestroyed())) {
127 fSessions
.add((TraceSessionComponent
)element
);
132 return fSessions
.size() > 0;