tmf: add progress feedback to delete traces execution
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteTraceHandler.java
CommitLineData
12c155f5 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2009, 2014 Ericsson, École Polytechnique de Montréal
6256d8ad 3 *
12c155f5
FC
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
6256d8ad 8 *
12c155f5
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
a72a6830 11 * Patrick Tasse - Close editors to release resources
beb19106 12 * Geneviève Bastien - Moved the delete code to element model's classes
12c155f5
FC
13 *******************************************************************************/
14
d34665f9 15package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
12c155f5 16
1e62a46b 17import java.lang.reflect.InvocationTargetException;
12c155f5 18import java.util.Iterator;
12c155f5
FC
19
20import org.eclipse.core.commands.AbstractHandler;
21import org.eclipse.core.commands.ExecutionEvent;
22import org.eclipse.core.commands.ExecutionException;
1e62a46b
BH
23import org.eclipse.core.resources.IWorkspace;
24import org.eclipse.core.resources.IWorkspaceRunnable;
25import org.eclipse.core.resources.ResourcesPlugin;
12c155f5 26import org.eclipse.core.runtime.CoreException;
1e62a46b
BH
27import org.eclipse.core.runtime.IProgressMonitor;
28import org.eclipse.core.runtime.OperationCanceledException;
29import org.eclipse.core.runtime.SubMonitor;
30import org.eclipse.jface.dialogs.MessageDialog;
31import org.eclipse.jface.operation.IRunnableWithProgress;
12c155f5 32import org.eclipse.jface.viewers.ISelection;
1595249b 33import org.eclipse.jface.viewers.ISelectionProvider;
12c155f5 34import org.eclipse.jface.viewers.TreeSelection;
8fd82db5 35import org.eclipse.linuxtools.internal.tmf.ui.Activator;
12c155f5 36import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
12c155f5 37import org.eclipse.swt.SWT;
c4c81d91 38import org.eclipse.swt.widgets.Display;
12c155f5
FC
39import org.eclipse.swt.widgets.MessageBox;
40import org.eclipse.swt.widgets.Shell;
41import org.eclipse.ui.IWorkbenchPage;
42import org.eclipse.ui.IWorkbenchPart;
43import org.eclipse.ui.IWorkbenchWindow;
44import org.eclipse.ui.PlatformUI;
45
46/**
47 * <b><u>DeleteTraceHandler</u></b>
48 * <p>
49 */
50public class DeleteTraceHandler extends AbstractHandler {
51
52 private TreeSelection fSelection = null;
53
54 // ------------------------------------------------------------------------
55 // Validation
56 // ------------------------------------------------------------------------
57
58 @Override
59 public boolean isEnabled() {
60
61 // Check if we are closing down
62 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
6256d8ad 63 if (window == null) {
12c155f5 64 return false;
6256d8ad 65 }
12c155f5
FC
66
67 // Get the selection
68 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
69 IWorkbenchPart part = page.getActivePart();
d5210347
PT
70 if (part == null) {
71 return false;
72 }
1595249b 73 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
6256d8ad 74 if (selectionProvider == null) {
1595249b 75 return false;
6256d8ad 76 }
1595249b 77 ISelection selection = selectionProvider.getSelection();
12c155f5
FC
78
79 // Make sure selection contains only traces
80 fSelection = null;
81 if (selection instanceof TreeSelection) {
82 fSelection = (TreeSelection) selection;
12c155f5
FC
83 Iterator<Object> iterator = fSelection.iterator();
84 while (iterator.hasNext()) {
85 Object element = iterator.next();
86 if (!(element instanceof TmfTraceElement)) {
87 return false;
88 }
89 }
90 }
91
92 // If we get here, either nothing is selected or everything is a trace
93 return !selection.isEmpty();
94 }
95
96 // ------------------------------------------------------------------------
97 // Execution
98 // ------------------------------------------------------------------------
99
100 @Override
101 public Object execute(ExecutionEvent event) throws ExecutionException {
102
103 // Check if we are closing down
104 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
6256d8ad 105 if (window == null) {
12c155f5 106 return null;
6256d8ad 107 }
12c155f5
FC
108
109 // Confirm the operation
110 Shell shell = window.getShell();
111 MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK);
112 confirmOperation.setText(Messages.DeleteDialog_Title);
113 confirmOperation.setMessage(Messages.DeleteTraceHandler_Message);
6256d8ad 114 if (confirmOperation.open() != SWT.OK) {
12c155f5 115 return null;
6256d8ad 116 }
12c155f5 117
1e62a46b
BH
118 final Iterator<Object> iterator = fSelection.iterator();
119 final int nbTraces = fSelection.size();
120
121 SelectTraceOperation operation = new SelectTraceOperation() {
122 @Override
123 public void execute(IProgressMonitor monitor) throws CoreException {
124 SubMonitor subMonitor = SubMonitor.convert(monitor, nbTraces);
125
126 while (iterator.hasNext()) {
127 if (monitor.isCanceled()) {
128 throw new OperationCanceledException();
129 }
130 Object element = iterator.next();
131 if (element instanceof TmfTraceElement) {
132 final TmfTraceElement trace = (TmfTraceElement) element;
133 subMonitor.setTaskName(Messages.DeleteTraceHandler_TaskName + " " + trace.getElementPath()); //$NON-NLS-1$
134 try {
135 trace.delete(null);
136 } catch (final CoreException e) {
137 Display.getDefault().asyncExec(new Runnable() {
138 @Override
139 public void run() {
140 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
141 mb.setText(Messages.DeleteTraceHandler_Error + ' ' + trace.getName());
142 mb.setMessage(e.getMessage());
143 mb.open();
144 }
145 });
146 Activator.getDefault().logError("Error deleting trace: " + trace.getName(), e); //$NON-NLS-1$
c4c81d91 147 }
1e62a46b
BH
148 }
149 subMonitor.setTaskName(""); //$NON-NLS-1$
150 subMonitor.worked(1);
12c155f5 151 }
1e62a46b
BH
152 }
153 };
154
155 try {
156 PlatformUI.getWorkbench().getProgressService().run(true, true, operation);
157 } catch (InterruptedException e) {
158 return null;
159 } catch (InvocationTargetException e) {
160 MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
161 return null;
162 }
163 return null;
164 }
165
166 private abstract class SelectTraceOperation implements IRunnableWithProgress {
167 @Override
168 public synchronized final void run(IProgressMonitor monitor)
169 throws InvocationTargetException, InterruptedException {
170 final InvocationTargetException[] iteHolder = new InvocationTargetException[1];
171 try {
172 IWorkspaceRunnable workspaceRunnable = new IWorkspaceRunnable() {
173 @Override
174 public void run(IProgressMonitor pm) throws CoreException {
175 try {
176 execute(pm);
177 } catch (InvocationTargetException e) {
178 // Pass it outside the workspace runnable
179 iteHolder[0] = e;
180 } catch (InterruptedException e) {
181 // Re-throw as OperationCanceledException, which will be
182 // caught and re-thrown as InterruptedException below.
183 throw new OperationCanceledException(e.getMessage());
184 }
185 // CoreException and OperationCanceledException are propagated
186 }
187 };
188
189 IWorkspace workspace = ResourcesPlugin.getWorkspace();
190 workspace.run(workspaceRunnable, workspace.getRoot(), IWorkspace.AVOID_UPDATE, monitor);
191 } catch (CoreException e) {
192 throw new InvocationTargetException(e);
193 } catch (OperationCanceledException e) {
194 throw new InterruptedException(e.getMessage());
195 }
196 // Re-throw the InvocationTargetException, if any occurred
197 if (iteHolder[0] != null) {
198 throw iteHolder[0];
12c155f5
FC
199 }
200 }
201
1e62a46b 202 protected abstract void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException;
12c155f5 203 }
1e62a46b 204}
This page took 0.062122 seconds and 5 git commands to generate.