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 | ||
18 | import org.eclipse.core.commands.AbstractHandler; | |
19 | import org.eclipse.core.commands.ExecutionEvent; | |
20 | import org.eclipse.core.commands.ExecutionException; | |
21 | import org.eclipse.core.runtime.IProgressMonitor; | |
22 | import org.eclipse.core.runtime.IStatus; | |
23 | import org.eclipse.core.runtime.Status; | |
24 | import org.eclipse.core.runtime.jobs.Job; | |
25 | import org.eclipse.jface.dialogs.MessageDialog; | |
26 | import org.eclipse.jface.viewers.ISelection; | |
27 | import org.eclipse.jface.viewers.StructuredSelection; | |
28 | import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin; | |
29 | import org.eclipse.linuxtools.lttng.ui.views.control.ControlView; | |
30 | import org.eclipse.linuxtools.lttng.ui.views.control.Messages; | |
31 | import org.eclipse.linuxtools.lttng.ui.views.control.model.TraceSessionState; | |
32 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent; | |
33 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionGroup; | |
34 | import org.eclipse.ui.IWorkbenchPage; | |
35 | import org.eclipse.ui.IWorkbenchPart; | |
36 | import org.eclipse.ui.IWorkbenchWindow; | |
37 | import 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 | */ | |
45 | public 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 | |
6503ae0f | 71 | if (!MessageDialog.openConfirm(window.getShell(), |
bbb3538a BH |
72 | Messages.TraceControl_DestroyConfirmationTitle, |
73 | Messages.TraceControl_DestroyConfirmationMessage)) { | |
74 | ||
6503ae0f BH |
75 | return null; |
76 | ||
bbb3538a | 77 | } |
6503ae0f BH |
78 | |
79 | Job job = new Job(Messages.TraceControl_DestroySessionJob) { | |
80 | @Override | |
81 | protected IStatus run(IProgressMonitor monitor) { | |
82 | try { | |
83 | // Make a copy of the list of sessions to avoid ConcurrentModificationException when iterating | |
84 | // over fSessions, since fSessions is modified in another thread triggered by the tree viewer refresh | |
85 | // after removing a session. | |
86 | TraceSessionComponent[] sessions = (TraceSessionComponent[])fSessions.toArray(new TraceSessionComponent[fSessions.size()]); | |
87 | ||
88 | for (int i = 0; i < sessions.length; i++) { | |
89 | // Destroy all selected sessions | |
90 | TraceSessionComponent session = sessions[i]; | |
91 | TraceSessionGroup sessionGroup = (TraceSessionGroup)session.getParent(); | |
92 | sessionGroup.destroySession(session, monitor); | |
93 | } | |
94 | } catch (ExecutionException e) { | |
95 | return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, e.toString()); | |
96 | } | |
97 | return Status.OK_STATUS; | |
98 | } | |
99 | }; | |
100 | job.setUser(true); | |
101 | job.schedule(); | |
102 | ||
bbb3538a BH |
103 | return null; |
104 | } | |
105 | ||
106 | /* | |
107 | * (non-Javadoc) | |
108 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
109 | */ | |
110 | @Override | |
111 | public boolean isEnabled() { | |
112 | fSessions.clear(); | |
113 | ||
114 | // Check if we are closing down | |
115 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
116 | if (window == null) { | |
117 | return false; | |
118 | } | |
119 | ||
120 | // Check if we are in the Project View | |
121 | IWorkbenchPage page = window.getActivePage(); | |
122 | if (page == null) { | |
123 | return false; | |
124 | } | |
125 | ||
126 | IWorkbenchPart part = page.getActivePart(); | |
127 | if (!(part instanceof ControlView)) { | |
128 | return false; | |
129 | } | |
130 | ||
131 | // Check if one or more session are selected | |
132 | ISelection selection = page.getSelection(ControlView.ID); | |
133 | if (selection instanceof StructuredSelection) { | |
134 | StructuredSelection structered = ((StructuredSelection) selection); | |
135 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
136 | Object element = (Object) iterator.next(); | |
137 | if (element instanceof TraceSessionComponent) { | |
138 | // Add only TraceSessionComponents that are inactive and not destroyed | |
139 | TraceSessionComponent session = (TraceSessionComponent) element; | |
140 | if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) { | |
141 | fSessions.add((TraceSessionComponent)element); | |
142 | } | |
143 | } | |
144 | } | |
145 | } | |
146 | return fSessions.size() > 0; | |
147 | } | |
148 | } |