lttng: Add a diagram showing the dependencies between plugins
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / handlers / DestroySessionHandler.java
CommitLineData
bbb3538a 1/**********************************************************************
60ae41e1 2 * Copyright (c) 2012, 2014 Ericsson
cfdb727a 3 *
bbb3538a
BH
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
cfdb727a
AM
8 *
9 * Contributors:
bbb3538a
BH
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
115b4a01 12package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers;
bbb3538a
BH
13
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.List;
17
bbb3538a
BH
18import org.eclipse.core.commands.ExecutionEvent;
19import org.eclipse.core.commands.ExecutionException;
20import org.eclipse.core.runtime.IProgressMonitor;
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.Status;
23import org.eclipse.core.runtime.jobs.Job;
bbb3538a
BH
24import org.eclipse.jface.viewers.ISelection;
25import org.eclipse.jface.viewers.StructuredSelection;
9315aeee 26import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceSessionState;
115b4a01
BH
27import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
28import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
115b4a01
BH
29import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IConfirmDialog;
30import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory;
9315aeee 31import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
115b4a01
BH
32import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
33import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionGroup;
bbb3538a 34import org.eclipse.ui.IWorkbenchPage;
bbb3538a
BH
35import org.eclipse.ui.IWorkbenchWindow;
36import org.eclipse.ui.PlatformUI;
37
38/**
bbb3538a
BH
39 * <p>
40 * Command handler implementation to destroy one or more trace sessions.
41 * </p>
cfdb727a 42 *
dbd4432d 43 * @author Bernd Hufmann
bbb3538a 44 */
498704b3 45public class DestroySessionHandler extends BaseControlViewHandler {
bbb3538a
BH
46
47 // ------------------------------------------------------------------------
48 // Attributes
49 // ------------------------------------------------------------------------
50 /**
cfdb727a 51 * The list of session components the command is to be executed on.
bbb3538a 52 */
e0838ca1 53 private final List<TraceSessionComponent> fSessions = new ArrayList<>();
cfdb727a 54
bbb3538a
BH
55 // ------------------------------------------------------------------------
56 // Operations
57 // ------------------------------------------------------------------------
11252342 58
bbb3538a
BH
59 @Override
60 public Object execute(ExecutionEvent event) throws ExecutionException {
61
62 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
63
64 if (window == null) {
65 return false;
66 }
67 // Get user confirmation
d132bcc7 68 IConfirmDialog dialog = TraceControlDialogFactory.getInstance().getConfirmDialog();
cfdb727a
AM
69 if (!dialog.openConfirm(window.getShell(),
70 Messages.TraceControl_DestroyConfirmationTitle,
bbb3538a
BH
71 Messages.TraceControl_DestroyConfirmationMessage)) {
72
6503ae0f 73 return null;
bbb3538a 74 }
6503ae0f
BH
75
76 Job job = new Job(Messages.TraceControl_DestroySessionJob) {
77 @Override
78 protected IStatus run(IProgressMonitor monitor) {
79 try {
cfdb727a
AM
80 // Make a copy of the list of sessions to avoid ConcurrentModificationException when iterating
81 // over fSessions, since fSessions is modified in another thread triggered by the tree viewer refresh
6503ae0f 82 // after removing a session.
cfdb727a 83 TraceSessionComponent[] sessions = fSessions.toArray(new TraceSessionComponent[fSessions.size()]);
6503ae0f
BH
84
85 for (int i = 0; i < sessions.length; i++) {
86 // Destroy all selected sessions
87 TraceSessionComponent session = sessions[i];
88 TraceSessionGroup sessionGroup = (TraceSessionGroup)session.getParent();
89 sessionGroup.destroySession(session, monitor);
90 }
91 } catch (ExecutionException e) {
cfdb727a
AM
92 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_DestroySessionFailure, e);
93 }
6503ae0f
BH
94 return Status.OK_STATUS;
95 }
96 };
97 job.setUser(true);
98 job.schedule();
99
bbb3538a
BH
100 return null;
101 }
102
bbb3538a
BH
103 @Override
104 public boolean isEnabled() {
498704b3
BH
105 // Get workbench page for the Control View
106 IWorkbenchPage page = getWorkbenchPage();
bbb3538a
BH
107 if (page == null) {
108 return false;
109 }
498704b3 110 fSessions.clear();
bbb3538a
BH
111
112 // Check if one or more session are selected
113 ISelection selection = page.getSelection(ControlView.ID);
114 if (selection instanceof StructuredSelection) {
115 StructuredSelection structered = ((StructuredSelection) selection);
116 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
cfdb727a 117 Object element = iterator.next();
bbb3538a
BH
118 if (element instanceof TraceSessionComponent) {
119 // Add only TraceSessionComponents that are inactive and not destroyed
120 TraceSessionComponent session = (TraceSessionComponent) element;
121 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
122 fSessions.add((TraceSessionComponent)element);
123 }
124 }
125 }
126 }
c56972bb 127 return !fSessions.isEmpty();
bbb3538a
BH
128 }
129}
This page took 0.059552 seconds and 5 git commands to generate.