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
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 **********************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.views.control.handlers;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
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;
36
37 /**
38 * <b><u>DestroySessionHandler</u></b>
39 * <p>
40 * Command handler implementation to destroy one or more trace sessions.
41 * </p>
42 */
43 public class DestroySessionHandler extends BaseControlViewHandler {
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48 /**
49 * The list of session components the command is to be executed on.
50 */
51 private List<TraceSessionComponent> fSessions = new ArrayList<TraceSessionComponent>();
52
53 // ------------------------------------------------------------------------
54 // Operations
55 // ------------------------------------------------------------------------
56 /*
57 * (non-Javadoc)
58 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
59 */
60 @Override
61 public Object execute(ExecutionEvent event) throws ExecutionException {
62
63 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
64
65 if (window == null) {
66 return false;
67 }
68 // Get user confirmation
69 if (!MessageDialog.openConfirm(window.getShell(),
70 Messages.TraceControl_DestroyConfirmationTitle,
71 Messages.TraceControl_DestroyConfirmationMessage)) {
72
73 return null;
74
75 }
76
77 Job job = new Job(Messages.TraceControl_DestroySessionJob) {
78 @Override
79 protected IStatus run(IProgressMonitor monitor) {
80 try {
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()]);
85
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);
91 }
92 } catch (ExecutionException e) {
93 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, e.toString());
94 }
95 return Status.OK_STATUS;
96 }
97 };
98 job.setUser(true);
99 job.schedule();
100
101 return null;
102 }
103
104 /*
105 * (non-Javadoc)
106 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
107 */
108 @Override
109 public boolean isEnabled() {
110 // Get workbench page for the Control View
111 IWorkbenchPage page = getWorkbenchPage();
112 if (page == null) {
113 return false;
114 }
115 fSessions.clear();
116
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);
128 }
129 }
130 }
131 }
132 return fSessions.size() > 0;
133 }
134 }
This page took 0.033808 seconds and 6 git commands to generate.