Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2009, 2014 Ericsson |
83f4e378 | 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 | |
83f4e378 | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
a72a6830 | 11 | * Patrick Tasse - Close editors to release resources |
12c155f5 FC |
12 | *******************************************************************************/ |
13 | ||
d34665f9 | 14 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 FC |
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; | |
99504bb8 | 23 | import org.eclipse.core.runtime.IPath; |
12c155f5 FC |
24 | import org.eclipse.jface.viewers.ISelection; |
25 | import org.eclipse.jface.viewers.TreeSelection; | |
8fd82db5 | 26 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
12c155f5 FC |
27 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
28 | import org.eclipse.swt.SWT; | |
c4c81d91 | 29 | import org.eclipse.swt.widgets.Display; |
12c155f5 FC |
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(); | |
83f4e378 | 52 | if (window == null) { |
12c155f5 | 53 | return null; |
83f4e378 | 54 | } |
12c155f5 FC |
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); | |
83f4e378 | 61 | if (confirmOperation.open() != SWT.OK) { |
12c155f5 | 62 | return null; |
83f4e378 | 63 | } |
12c155f5 FC |
64 | |
65 | // Get the selection | |
66 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
67 | IWorkbenchPart part = page.getActivePart(); | |
d5210347 | 68 | if (part == null) { |
0126a8ca | 69 | return Boolean.FALSE; |
d5210347 | 70 | } |
12c155f5 FC |
71 | ISelection selection = part.getSite().getSelectionProvider().getSelection(); |
72 | ||
73 | if (selection instanceof TreeSelection) { | |
74 | TreeSelection sel = (TreeSelection) selection; | |
12c155f5 FC |
75 | Iterator<Object> iterator = sel.iterator(); |
76 | while (iterator.hasNext()) { | |
77 | Object element = iterator.next(); | |
78 | if (element instanceof TmfExperimentElement) { | |
c4c81d91 | 79 | final TmfExperimentElement experiment = (TmfExperimentElement) element; |
12c155f5 | 80 | IResource resource = experiment.getResource(); |
c4c81d91 | 81 | |
12c155f5 | 82 | try { |
c4c81d91 | 83 | // Close the experiment if open |
a72a6830 | 84 | experiment.closeEditors(); |
c4c81d91 | 85 | |
99504bb8 GB |
86 | IPath path = resource.getLocation(); |
87 | if (path != null) { | |
88 | // Delete supplementary files | |
89 | experiment.deleteSupplementaryFolder(); | |
90 | } | |
91 | ||
c4c81d91 | 92 | // Finally, delete the experiment |
12c155f5 | 93 | resource.delete(true, null); |
c4c81d91 | 94 | |
c4c81d91 PT |
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 | }); | |
8fd82db5 | 105 | Activator.getDefault().logError("Error deleting experiment: " + experiment.getName(), e); //$NON-NLS-1$ |
12c155f5 FC |
106 | } |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | return null; | |
112 | } | |
113 | } |