Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / DestroySessionHandler.java
CommitLineData
bbb3538a
BH
1/**********************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12package org.eclipse.linuxtools.lttng.ui.views.control.handlers;
13
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.List;
17
18import org.eclipse.core.commands.AbstractHandler;
19import org.eclipse.core.commands.ExecutionEvent;
20import org.eclipse.core.commands.ExecutionException;
21import org.eclipse.core.runtime.IProgressMonitor;
22import org.eclipse.core.runtime.IStatus;
23import org.eclipse.core.runtime.Status;
24import org.eclipse.core.runtime.jobs.Job;
25import org.eclipse.jface.dialogs.MessageDialog;
26import org.eclipse.jface.viewers.ISelection;
27import org.eclipse.jface.viewers.StructuredSelection;
28import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
29import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
30import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
31import org.eclipse.linuxtools.lttng.ui.views.control.model.TraceSessionState;
32import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent;
33import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionGroup;
34import org.eclipse.ui.IWorkbenchPage;
35import org.eclipse.ui.IWorkbenchPart;
36import org.eclipse.ui.IWorkbenchWindow;
37import org.eclipse.ui.PlatformUI;
38
39/**
40 * <b><u>DestroySessionHandler</u></b>
41 * <p>
42 * Command handler implementation to destroy one or more trace sessions.
43 * </p>
44 */
45public class DestroySessionHandler extends AbstractHandler {
46
47 // ------------------------------------------------------------------------
48 // Attributes
49 // ------------------------------------------------------------------------
50 /**
51 * The list of session components the command is to be executed on.
52 */
53 private List<TraceSessionComponent> fSessions = new ArrayList<TraceSessionComponent>();
54
55 // ------------------------------------------------------------------------
56 // Operations
57 // ------------------------------------------------------------------------
58 /*
59 * (non-Javadoc)
60 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
61 */
62 @Override
63 public Object execute(ExecutionEvent event) throws ExecutionException {
64
65 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
66
67 if (window == null) {
68 return false;
69 }
70 // Get user confirmation
71 if (MessageDialog.openConfirm(window.getShell(),
72 Messages.TraceControl_DestroyConfirmationTitle,
73 Messages.TraceControl_DestroyConfirmationMessage)) {
74
75 Job job = new Job(Messages.TraceControl_DestroySessionJob) {
76 @Override
77 protected IStatus run(IProgressMonitor monitor) {
78 try {
79 for (Iterator<TraceSessionComponent> iterator = fSessions.iterator(); iterator.hasNext();) {
80 // Destroy all selected sessions
81 TraceSessionComponent session = (TraceSessionComponent) iterator.next();
82 TraceSessionGroup sessionGroup = (TraceSessionGroup)session.getParent();
83 sessionGroup.destroySession(session.getName(), monitor);
84 }
85 } catch (ExecutionException e) {
86 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, e.toString());
87 }
88 return Status.OK_STATUS;
89 }
90 };
91 job.setUser(true);
92 job.schedule();
93 }
94 return null;
95 }
96
97 /*
98 * (non-Javadoc)
99 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
100 */
101 @Override
102 public boolean isEnabled() {
103 fSessions.clear();
104
105 // Check if we are closing down
106 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
107 if (window == null) {
108 return false;
109 }
110
111 // Check if we are in the Project View
112 IWorkbenchPage page = window.getActivePage();
113 if (page == null) {
114 return false;
115 }
116
117 IWorkbenchPart part = page.getActivePart();
118 if (!(part instanceof ControlView)) {
119 return false;
120 }
121
122 // Check if one or more session are selected
123 ISelection selection = page.getSelection(ControlView.ID);
124 if (selection instanceof StructuredSelection) {
125 StructuredSelection structered = ((StructuredSelection) selection);
126 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
127 Object element = (Object) iterator.next();
128 if (element instanceof TraceSessionComponent) {
129 // Add only TraceSessionComponents that are inactive and not destroyed
130 TraceSessionComponent session = (TraceSessionComponent) element;
131 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
132 fSessions.add((TraceSessionComponent)element);
133 }
134 }
135 }
136 }
137 return fSessions.size() > 0;
138 }
139}
This page took 0.036269 seconds and 5 git commands to generate.