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(); | |
1595249b | 72 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
6256d8ad | 73 | if (selectionProvider == null) { |
1595249b | 74 | return false; |
6256d8ad | 75 | } |
1595249b | 76 | ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
77 | |
78 | // Make sure selection contains only traces | |
79 | fSelection = null; | |
80 | if (selection instanceof TreeSelection) { | |
81 | fSelection = (TreeSelection) selection; | |
12c155f5 FC |
82 | Iterator<Object> iterator = fSelection.iterator(); |
83 | while (iterator.hasNext()) { | |
84 | Object element = iterator.next(); | |
85 | if (!(element instanceof TmfTraceElement)) { | |
86 | return false; | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | // If we get here, either nothing is selected or everything is a trace | |
92 | return !selection.isEmpty(); | |
93 | } | |
94 | ||
95 | // ------------------------------------------------------------------------ | |
96 | // Execution | |
97 | // ------------------------------------------------------------------------ | |
98 | ||
99 | @Override | |
100 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
101 | ||
102 | // Check if we are closing down | |
103 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 104 | if (window == null) { |
12c155f5 | 105 | return null; |
6256d8ad | 106 | } |
12c155f5 FC |
107 | |
108 | // Confirm the operation | |
109 | Shell shell = window.getShell(); | |
110 | MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); | |
111 | confirmOperation.setText(Messages.DeleteDialog_Title); | |
112 | confirmOperation.setMessage(Messages.DeleteTraceHandler_Message); | |
6256d8ad | 113 | if (confirmOperation.open() != SWT.OK) { |
12c155f5 | 114 | return null; |
6256d8ad | 115 | } |
12c155f5 | 116 | |
12c155f5 FC |
117 | Iterator<Object> iterator = fSelection.iterator(); |
118 | while (iterator.hasNext()) { | |
119 | Object element = iterator.next(); | |
120 | if (element instanceof TmfTraceElement) { | |
c4c81d91 | 121 | final TmfTraceElement trace = (TmfTraceElement) element; |
12c155f5 FC |
122 | IResource resource = trace.getResource(); |
123 | try { | |
c4c81d91 PT |
124 | // Close the trace if open |
125 | IFile file = trace.getBookmarksFile(); | |
126 | FileEditorInput input = new FileEditorInput(file); | |
127 | IWorkbench wb = PlatformUI.getWorkbench(); | |
128 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { | |
129 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { | |
130 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { | |
131 | if (editorReference.getEditorInput().equals(input)) { | |
132 | wbPage.closeEditor(editorReference.getEditor(false), false); | |
133 | } | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
12c155f5 FC |
138 | IPath path = resource.getLocation(); |
139 | if (path != null && (trace.getParent() instanceof TmfTraceFolder)) { | |
12c155f5 FC |
140 | TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder(); |
141 | ||
142 | // Propagate the removal to traces | |
143 | for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
144 | List<ITmfProjectModelElement> toRemove = new LinkedList<ITmfProjectModelElement>(); | |
145 | for (ITmfProjectModelElement child : experiment.getChildren()) { | |
81fe3479 | 146 | if (child.getName().equals(trace.getName())) { |
12c155f5 FC |
147 | toRemove.add(child); |
148 | } | |
149 | } | |
150 | for (ITmfProjectModelElement child : toRemove) { | |
c4c81d91 PT |
151 | // Close the experiment if open |
152 | file = ((TmfExperimentElement) experiment).getBookmarksFile(); | |
153 | input = new FileEditorInput(file); | |
154 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { | |
155 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { | |
156 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { | |
157 | if (editorReference.getEditorInput().equals(input)) { | |
158 | wbPage.closeEditor(editorReference.getEditor(false), false); | |
159 | } | |
160 | } | |
161 | } | |
162 | } | |
12c155f5 FC |
163 | experiment.removeChild(child); |
164 | child.getResource().delete(true, null); | |
165 | } | |
166 | } | |
a1e20e2e PT |
167 | |
168 | // Delete supplementary files | |
169 | trace.deleteSupplementaryFolder(); | |
12c155f5 FC |
170 | } |
171 | ||
172 | // Finally, delete the trace | |
173 | resource.delete(true, new NullProgressMonitor()); | |
5e4bf87d | 174 | |
5e4bf87d | 175 | // Refresh the project |
12c155f5 FC |
176 | trace.getProject().refresh(); |
177 | ||
c4c81d91 PT |
178 | } catch (final CoreException e) { |
179 | Display.getDefault().asyncExec(new Runnable() { | |
180 | @Override | |
181 | public void run() { | |
182 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
183 | mb.setText(Messages.DeleteTraceHandler_Error + ' ' + trace.getName()); | |
184 | mb.setMessage(e.getMessage()); | |
185 | mb.open(); | |
186 | } | |
187 | }); | |
8fd82db5 | 188 | Activator.getDefault().logError("Error deleting trace: " + trace.getName(), e); //$NON-NLS-1$ |
12c155f5 FC |
189 | } |
190 | } | |
191 | } | |
192 | ||
193 | return null; | |
194 | } | |
195 | } |