1 /**********************************************************************
2 * Copyright (c) 2012, 2015 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
.tracecompass
.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
.jdt
.annotation
.NonNull
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
27 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.TraceSessionState
;
28 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.Activator
;
29 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.ControlView
;
30 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.dialogs
.IConfirmDialog
;
31 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.dialogs
.TraceControlDialogFactory
;
32 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
33 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceSessionComponent
;
34 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceSessionGroup
;
35 import org
.eclipse
.ui
.IWorkbenchPage
;
36 import org
.eclipse
.ui
.IWorkbenchWindow
;
37 import org
.eclipse
.ui
.PlatformUI
;
41 * Command handler implementation to destroy one or more trace sessions.
44 * @author Bernd Hufmann
46 public class DestroySessionHandler
extends BaseControlViewHandler
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 * The list of session components the command is to be executed on.
54 @NonNull private final List
<TraceSessionComponent
> fSessions
= new ArrayList
<>();
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
63 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
69 List
<TraceSessionComponent
> tmpSessions
= new ArrayList
<>();
71 // Make a copy of the session list to avoid concurrent modification
72 // of the list of sessions
75 tmpSessions
.addAll(fSessions
);
79 final List
<TraceSessionComponent
> sessions
= tmpSessions
;
81 // Get user confirmation
82 IConfirmDialog dialog
= TraceControlDialogFactory
.getInstance().getConfirmDialog();
83 if (!dialog
.openConfirm(window
.getShell(),
84 Messages
.TraceControl_DestroyConfirmationTitle
,
85 Messages
.TraceControl_DestroyConfirmationMessage
)) {
90 Job job
= new Job(Messages
.TraceControl_DestroySessionJob
) {
92 protected IStatus
run(IProgressMonitor monitor
) {
94 for (TraceSessionComponent session
: sessions
) {
95 // Destroy all selected sessions
96 TraceSessionGroup sessionGroup
= (TraceSessionGroup
)session
.getParent();
97 sessionGroup
.destroySession(session
, monitor
);
99 } catch (ExecutionException e
) {
100 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_DestroySessionFailure
, e
);
102 return Status
.OK_STATUS
;
112 public boolean isEnabled() {
113 // Get workbench page for the Control View
114 IWorkbenchPage page
= getWorkbenchPage();
119 List
<TraceSessionComponent
> sessions
= new ArrayList
<>(0);
121 // Check if one or more session are selected
122 ISelection selection
= page
.getSelection(ControlView
.ID
);
123 if (selection
instanceof StructuredSelection
) {
124 StructuredSelection structered
= ((StructuredSelection
) selection
);
125 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
126 Object element
= iterator
.next();
127 if (element
instanceof TraceSessionComponent
) {
128 // Add only TraceSessionComponents that are inactive and not destroyed
129 TraceSessionComponent session
= (TraceSessionComponent
) element
;
130 if ((session
.getSessionState() == TraceSessionState
.INACTIVE
) && (!session
.isDestroyed())) {
131 sessions
.add((TraceSessionComponent
)element
);
136 boolean isEnabled
= !sessions
.isEmpty();
141 fSessions
.addAll(sessions
);