Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010, 2011 Ericsson | |
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 | |
11 | *******************************************************************************/ | |
12 | ||
d34665f9 | 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 | 14 | |
12c155f5 FC |
15 | import java.util.Iterator; |
16 | import java.util.LinkedList; | |
17 | import java.util.List; | |
18 | ||
19 | import org.eclipse.core.commands.AbstractHandler; | |
20 | import org.eclipse.core.commands.ExecutionEvent; | |
21 | import org.eclipse.core.commands.ExecutionException; | |
c4c81d91 | 22 | import org.eclipse.core.resources.IFile; |
12c155f5 FC |
23 | import org.eclipse.core.resources.IResource; |
24 | import org.eclipse.core.runtime.CoreException; | |
25 | import org.eclipse.core.runtime.IPath; | |
26 | import org.eclipse.core.runtime.NullProgressMonitor; | |
27 | import org.eclipse.jface.viewers.ISelection; | |
1595249b | 28 | import org.eclipse.jface.viewers.ISelectionProvider; |
12c155f5 | 29 | import org.eclipse.jface.viewers.TreeSelection; |
8fd82db5 | 30 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
12c155f5 | 31 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
c4c81d91 | 32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
12c155f5 FC |
33 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; |
34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
35 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
36 | import org.eclipse.swt.SWT; | |
c4c81d91 | 37 | import org.eclipse.swt.widgets.Display; |
12c155f5 FC |
38 | import org.eclipse.swt.widgets.MessageBox; |
39 | import org.eclipse.swt.widgets.Shell; | |
c4c81d91 PT |
40 | import org.eclipse.ui.IEditorReference; |
41 | import org.eclipse.ui.IWorkbench; | |
12c155f5 FC |
42 | import org.eclipse.ui.IWorkbenchPage; |
43 | import org.eclipse.ui.IWorkbenchPart; | |
44 | import org.eclipse.ui.IWorkbenchWindow; | |
45 | import org.eclipse.ui.PlatformUI; | |
c4c81d91 | 46 | import org.eclipse.ui.part.FileEditorInput; |
12c155f5 FC |
47 | |
48 | /** | |
49 | * <b><u>DeleteTraceHandler</u></b> | |
50 | * <p> | |
51 | */ | |
52 | public class DeleteTraceHandler extends AbstractHandler { | |
53 | ||
54 | private TreeSelection fSelection = null; | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Validation | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | @Override | |
61 | public boolean isEnabled() { | |
62 | ||
63 | // Check if we are closing down | |
64 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 65 | if (window == null) { |
12c155f5 | 66 | return false; |
6256d8ad | 67 | } |
12c155f5 FC |
68 | |
69 | // Get the selection | |
70 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
71 | IWorkbenchPart part = page.getActivePart(); | |
d5210347 PT |
72 | if (part == null) { |
73 | return false; | |
74 | } | |
1595249b | 75 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
6256d8ad | 76 | if (selectionProvider == null) { |
1595249b | 77 | return false; |
6256d8ad | 78 | } |
1595249b | 79 | ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
80 | |
81 | // Make sure selection contains only traces | |
82 | fSelection = null; | |
83 | if (selection instanceof TreeSelection) { | |
84 | fSelection = (TreeSelection) selection; | |
12c155f5 FC |
85 | Iterator<Object> iterator = fSelection.iterator(); |
86 | while (iterator.hasNext()) { | |
87 | Object element = iterator.next(); | |
88 | if (!(element instanceof TmfTraceElement)) { | |
89 | return false; | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | // If we get here, either nothing is selected or everything is a trace | |
95 | return !selection.isEmpty(); | |
96 | } | |
97 | ||
98 | // ------------------------------------------------------------------------ | |
99 | // Execution | |
100 | // ------------------------------------------------------------------------ | |
101 | ||
102 | @Override | |
103 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
104 | ||
105 | // Check if we are closing down | |
106 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 107 | if (window == null) { |
12c155f5 | 108 | return null; |
6256d8ad | 109 | } |
12c155f5 FC |
110 | |
111 | // Confirm the operation | |
112 | Shell shell = window.getShell(); | |
113 | MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); | |
114 | confirmOperation.setText(Messages.DeleteDialog_Title); | |
115 | confirmOperation.setMessage(Messages.DeleteTraceHandler_Message); | |
6256d8ad | 116 | if (confirmOperation.open() != SWT.OK) { |
12c155f5 | 117 | return null; |
6256d8ad | 118 | } |
12c155f5 | 119 | |
12c155f5 FC |
120 | Iterator<Object> iterator = fSelection.iterator(); |
121 | while (iterator.hasNext()) { | |
122 | Object element = iterator.next(); | |
123 | if (element instanceof TmfTraceElement) { | |
c4c81d91 | 124 | final TmfTraceElement trace = (TmfTraceElement) element; |
12c155f5 FC |
125 | IResource resource = trace.getResource(); |
126 | try { | |
c4c81d91 PT |
127 | // Close the trace if open |
128 | IFile file = trace.getBookmarksFile(); | |
129 | FileEditorInput input = new FileEditorInput(file); | |
130 | IWorkbench wb = PlatformUI.getWorkbench(); | |
131 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { | |
132 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { | |
133 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { | |
134 | if (editorReference.getEditorInput().equals(input)) { | |
135 | wbPage.closeEditor(editorReference.getEditor(false), false); | |
136 | } | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
12c155f5 FC |
141 | IPath path = resource.getLocation(); |
142 | if (path != null && (trace.getParent() instanceof TmfTraceFolder)) { | |
12c155f5 FC |
143 | TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder(); |
144 | ||
145 | // Propagate the removal to traces | |
146 | for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
147 | List<ITmfProjectModelElement> toRemove = new LinkedList<ITmfProjectModelElement>(); | |
148 | for (ITmfProjectModelElement child : experiment.getChildren()) { | |
81fe3479 | 149 | if (child.getName().equals(trace.getName())) { |
12c155f5 FC |
150 | toRemove.add(child); |
151 | } | |
152 | } | |
153 | for (ITmfProjectModelElement child : toRemove) { | |
c4c81d91 PT |
154 | // Close the experiment if open |
155 | file = ((TmfExperimentElement) experiment).getBookmarksFile(); | |
156 | input = new FileEditorInput(file); | |
157 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { | |
158 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { | |
159 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { | |
160 | if (editorReference.getEditorInput().equals(input)) { | |
161 | wbPage.closeEditor(editorReference.getEditor(false), false); | |
162 | } | |
163 | } | |
164 | } | |
165 | } | |
12c155f5 FC |
166 | experiment.removeChild(child); |
167 | child.getResource().delete(true, null); | |
168 | } | |
169 | } | |
a1e20e2e PT |
170 | |
171 | // Delete supplementary files | |
172 | trace.deleteSupplementaryFolder(); | |
12c155f5 FC |
173 | } |
174 | ||
175 | // Finally, delete the trace | |
176 | resource.delete(true, new NullProgressMonitor()); | |
5e4bf87d | 177 | |
5e4bf87d | 178 | // Refresh the project |
12c155f5 FC |
179 | trace.getProject().refresh(); |
180 | ||
c4c81d91 PT |
181 | } catch (final CoreException e) { |
182 | Display.getDefault().asyncExec(new Runnable() { | |
183 | @Override | |
184 | public void run() { | |
185 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
186 | mb.setText(Messages.DeleteTraceHandler_Error + ' ' + trace.getName()); | |
187 | mb.setMessage(e.getMessage()); | |
188 | mb.open(); | |
189 | } | |
190 | }); | |
8fd82db5 | 191 | Activator.getDefault().logError("Error deleting trace: " + trace.getName(), e); //$NON-NLS-1$ |
12c155f5 FC |
192 | } |
193 | } | |
194 | } | |
195 | ||
196 | return null; | |
197 | } | |
198 | } |