tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteExperimentHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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 * Francois Chouinard - Initial API and implementation
11 * Patrick Tasse - Close editors to release resources
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
15
16 import java.util.Iterator;
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.resources.IResource;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.TreeSelection;
26 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
27 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.MessageBox;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchPart;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PlatformUI;
36
37 /**
38 * <b><u>DeleteExperimentHandler</u></b>
39 * <p>
40 */
41 public class DeleteExperimentHandler extends AbstractHandler {
42
43 // ------------------------------------------------------------------------
44 // Execution
45 // ------------------------------------------------------------------------
46
47 @Override
48 public Object execute(ExecutionEvent event) throws ExecutionException {
49
50 // Check if we are closing down
51 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
52 if (window == null) {
53 return null;
54 }
55
56 // Confirm the operation
57 Shell shell = window.getShell();
58 MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK);
59 confirmOperation.setText(Messages.DeleteDialog_Title);
60 confirmOperation.setMessage(Messages.DeleteExperimentHandler_Message);
61 if (confirmOperation.open() != SWT.OK) {
62 return null;
63 }
64
65 // Get the selection
66 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
67 IWorkbenchPart part = page.getActivePart();
68 if (part == null) {
69 return Boolean.FALSE;
70 }
71 ISelection selection = part.getSite().getSelectionProvider().getSelection();
72
73 if (selection instanceof TreeSelection) {
74 TreeSelection sel = (TreeSelection) selection;
75 Iterator<Object> iterator = sel.iterator();
76 while (iterator.hasNext()) {
77 Object element = iterator.next();
78 if (element instanceof TmfExperimentElement) {
79 final TmfExperimentElement experiment = (TmfExperimentElement) element;
80 IResource resource = experiment.getResource();
81
82 try {
83 // Close the experiment if open
84 experiment.closeEditors();
85
86 IPath path = resource.getLocation();
87 if (path != null) {
88 // Delete supplementary files
89 experiment.deleteSupplementaryFolder();
90 }
91
92 // Finally, delete the experiment
93 resource.delete(true, null);
94
95 } catch (final CoreException e) {
96 Display.getDefault().asyncExec(new Runnable() {
97 @Override
98 public void run() {
99 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
100 mb.setText(Messages.DeleteTraceHandler_Error + ' ' + experiment.getName());
101 mb.setMessage(e.getMessage());
102 mb.open();
103 }
104 });
105 Activator.getDefault().logError("Error deleting experiment: " + experiment.getName(), e); //$NON-NLS-1$
106 }
107 }
108 }
109 }
110
111 return null;
112 }
113 }
This page took 0.050056 seconds and 5 git commands to generate.