Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
c8422608 | 2 | * Copyright (c) 2009, 2013 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 | |
11 | *******************************************************************************/ | |
12 | ||
d34665f9 | 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 FC |
14 | |
15 | import java.util.Iterator; | |
16 | ||
17 | import org.eclipse.core.commands.AbstractHandler; | |
18 | import org.eclipse.core.commands.ExecutionEvent; | |
19 | import org.eclipse.core.commands.ExecutionException; | |
c4c81d91 | 20 | import org.eclipse.core.resources.IFile; |
12c155f5 FC |
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; | |
c4c81d91 PT |
32 | import org.eclipse.ui.IEditorReference; |
33 | import org.eclipse.ui.IWorkbench; | |
12c155f5 FC |
34 | import org.eclipse.ui.IWorkbenchPage; |
35 | import org.eclipse.ui.IWorkbenchPart; | |
36 | import org.eclipse.ui.IWorkbenchWindow; | |
37 | import org.eclipse.ui.PlatformUI; | |
c4c81d91 | 38 | import org.eclipse.ui.part.FileEditorInput; |
12c155f5 FC |
39 | |
40 | /** | |
41 | * <b><u>DeleteExperimentHandler</u></b> | |
42 | * <p> | |
43 | */ | |
44 | public class DeleteExperimentHandler extends AbstractHandler { | |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Execution | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
50 | @Override | |
51 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
52 | ||
53 | // Check if we are closing down | |
54 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
83f4e378 | 55 | if (window == null) { |
12c155f5 | 56 | return null; |
83f4e378 | 57 | } |
12c155f5 FC |
58 | |
59 | // Confirm the operation | |
60 | Shell shell = window.getShell(); | |
61 | MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); | |
62 | confirmOperation.setText(Messages.DeleteDialog_Title); | |
63 | confirmOperation.setMessage(Messages.DeleteExperimentHandler_Message); | |
83f4e378 | 64 | if (confirmOperation.open() != SWT.OK) { |
12c155f5 | 65 | return null; |
83f4e378 | 66 | } |
12c155f5 FC |
67 | |
68 | // Get the selection | |
69 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
70 | IWorkbenchPart part = page.getActivePart(); | |
d5210347 PT |
71 | if (part == null) { |
72 | return false; | |
73 | } | |
12c155f5 FC |
74 | ISelection selection = part.getSite().getSelectionProvider().getSelection(); |
75 | ||
76 | if (selection instanceof TreeSelection) { | |
77 | TreeSelection sel = (TreeSelection) selection; | |
12c155f5 FC |
78 | Iterator<Object> iterator = sel.iterator(); |
79 | while (iterator.hasNext()) { | |
80 | Object element = iterator.next(); | |
81 | if (element instanceof TmfExperimentElement) { | |
c4c81d91 | 82 | final TmfExperimentElement experiment = (TmfExperimentElement) element; |
12c155f5 | 83 | IResource resource = experiment.getResource(); |
c4c81d91 | 84 | |
12c155f5 | 85 | try { |
c4c81d91 PT |
86 | // Close the experiment if open |
87 | IFile file = experiment.getBookmarksFile(); | |
88 | FileEditorInput input = new FileEditorInput(file); | |
89 | IWorkbench wb = PlatformUI.getWorkbench(); | |
90 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { | |
91 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { | |
92 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { | |
93 | if (editorReference.getEditorInput().equals(input)) { | |
94 | wbPage.closeEditor(editorReference.getEditor(false), false); | |
95 | } | |
96 | } | |
97 | } | |
98 | } | |
99 | ||
99504bb8 GB |
100 | IPath path = resource.getLocation(); |
101 | if (path != null) { | |
102 | // Delete supplementary files | |
103 | experiment.deleteSupplementaryFolder(); | |
104 | } | |
105 | ||
c4c81d91 | 106 | // Finally, delete the experiment |
12c155f5 | 107 | resource.delete(true, null); |
c4c81d91 PT |
108 | |
109 | // Refresh the project | |
12c155f5 | 110 | experiment.getProject().refresh(); |
c4c81d91 PT |
111 | |
112 | } catch (final CoreException e) { | |
113 | Display.getDefault().asyncExec(new Runnable() { | |
114 | @Override | |
115 | public void run() { | |
116 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
117 | mb.setText(Messages.DeleteTraceHandler_Error + ' ' + experiment.getName()); | |
118 | mb.setMessage(e.getMessage()); | |
119 | mb.open(); | |
120 | } | |
121 | }); | |
8fd82db5 | 122 | Activator.getDefault().logError("Error deleting experiment: " + experiment.getName(), e); //$NON-NLS-1$ |
12c155f5 FC |
123 | } |
124 | } | |
125 | } | |
126 | } | |
127 | ||
128 | return null; | |
129 | } | |
130 | } |