Commit | Line | Data |
---|---|---|
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 | **********************************************************************/ | |
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 | ||
bbb3538a BH |
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; | |
bbb3538a BH |
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 | */ | |
498704b3 | 43 | public class DestroySessionHandler extends BaseControlViewHandler { |
bbb3538a BH |
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 | |
6503ae0f | 69 | if (!MessageDialog.openConfirm(window.getShell(), |
bbb3538a BH |
70 | Messages.TraceControl_DestroyConfirmationTitle, |
71 | Messages.TraceControl_DestroyConfirmationMessage)) { | |
72 | ||
6503ae0f BH |
73 | return null; |
74 | ||
bbb3538a | 75 | } |
6503ae0f BH |
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 | ||
bbb3538a BH |
101 | return null; |
102 | } | |
103 | ||
104 | /* | |
105 | * (non-Javadoc) | |
106 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
107 | */ | |
108 | @Override | |
109 | public boolean isEnabled() { | |
498704b3 BH |
110 | // Get workbench page for the Control View |
111 | IWorkbenchPage page = getWorkbenchPage(); | |
bbb3538a BH |
112 | if (page == null) { |
113 | return false; | |
114 | } | |
498704b3 | 115 | fSessions.clear(); |
bbb3538a BH |
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 | } |