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; | |
22 | import org.eclipse.core.resources.IResource; | |
23 | import org.eclipse.core.runtime.CoreException; | |
24 | import org.eclipse.core.runtime.IPath; | |
25 | import org.eclipse.core.runtime.NullProgressMonitor; | |
26 | import org.eclipse.jface.viewers.ISelection; | |
1595249b | 27 | import org.eclipse.jface.viewers.ISelectionProvider; |
12c155f5 | 28 | import org.eclipse.jface.viewers.TreeSelection; |
8fd82db5 | 29 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
12c155f5 FC |
30 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
31 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; | |
32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
33 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
34 | import org.eclipse.swt.SWT; | |
35 | import org.eclipse.swt.widgets.MessageBox; | |
36 | import org.eclipse.swt.widgets.Shell; | |
37 | import org.eclipse.ui.IWorkbenchPage; | |
38 | import org.eclipse.ui.IWorkbenchPart; | |
39 | import org.eclipse.ui.IWorkbenchWindow; | |
40 | import org.eclipse.ui.PlatformUI; | |
41 | ||
42 | /** | |
43 | * <b><u>DeleteTraceHandler</u></b> | |
44 | * <p> | |
45 | */ | |
46 | public class DeleteTraceHandler extends AbstractHandler { | |
47 | ||
48 | private TreeSelection fSelection = null; | |
49 | ||
50 | // ------------------------------------------------------------------------ | |
51 | // Validation | |
52 | // ------------------------------------------------------------------------ | |
53 | ||
54 | @Override | |
55 | public boolean isEnabled() { | |
56 | ||
57 | // Check if we are closing down | |
58 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 59 | if (window == null) { |
12c155f5 | 60 | return false; |
6256d8ad | 61 | } |
12c155f5 FC |
62 | |
63 | // Get the selection | |
64 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
65 | IWorkbenchPart part = page.getActivePart(); | |
1595249b | 66 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
6256d8ad | 67 | if (selectionProvider == null) { |
1595249b | 68 | return false; |
6256d8ad | 69 | } |
1595249b | 70 | ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
71 | |
72 | // Make sure selection contains only traces | |
73 | fSelection = null; | |
74 | if (selection instanceof TreeSelection) { | |
75 | fSelection = (TreeSelection) selection; | |
12c155f5 FC |
76 | Iterator<Object> iterator = fSelection.iterator(); |
77 | while (iterator.hasNext()) { | |
78 | Object element = iterator.next(); | |
79 | if (!(element instanceof TmfTraceElement)) { | |
80 | return false; | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | // If we get here, either nothing is selected or everything is a trace | |
86 | return !selection.isEmpty(); | |
87 | } | |
88 | ||
89 | // ------------------------------------------------------------------------ | |
90 | // Execution | |
91 | // ------------------------------------------------------------------------ | |
92 | ||
93 | @Override | |
94 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
95 | ||
96 | // Check if we are closing down | |
97 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 98 | if (window == null) { |
12c155f5 | 99 | return null; |
6256d8ad | 100 | } |
12c155f5 FC |
101 | |
102 | // Confirm the operation | |
103 | Shell shell = window.getShell(); | |
104 | MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); | |
105 | confirmOperation.setText(Messages.DeleteDialog_Title); | |
106 | confirmOperation.setMessage(Messages.DeleteTraceHandler_Message); | |
6256d8ad | 107 | if (confirmOperation.open() != SWT.OK) { |
12c155f5 | 108 | return null; |
6256d8ad | 109 | } |
12c155f5 | 110 | |
12c155f5 FC |
111 | Iterator<Object> iterator = fSelection.iterator(); |
112 | while (iterator.hasNext()) { | |
113 | Object element = iterator.next(); | |
114 | if (element instanceof TmfTraceElement) { | |
115 | TmfTraceElement trace = (TmfTraceElement) element; | |
116 | IResource resource = trace.getResource(); | |
117 | try { | |
118 | IPath path = resource.getLocation(); | |
119 | if (path != null && (trace.getParent() instanceof TmfTraceFolder)) { | |
9d869394 | 120 | String location = path.toString(); |
12c155f5 FC |
121 | TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder(); |
122 | ||
123 | // Propagate the removal to traces | |
124 | for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
125 | List<ITmfProjectModelElement> toRemove = new LinkedList<ITmfProjectModelElement>(); | |
126 | for (ITmfProjectModelElement child : experiment.getChildren()) { | |
127 | if (child.getResource().getLocation().toString().equals(location)) { | |
128 | toRemove.add(child); | |
129 | } | |
130 | } | |
131 | for (ITmfProjectModelElement child : toRemove) { | |
132 | experiment.removeChild(child); | |
133 | child.getResource().delete(true, null); | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | // Finally, delete the trace | |
139 | resource.delete(true, new NullProgressMonitor()); | |
5e4bf87d BH |
140 | |
141 | // Delete supplementary files | |
e12ecd30 | 142 | trace.deleteSupplementaryFolder(); |
5e4bf87d BH |
143 | |
144 | // Refresh the project | |
12c155f5 FC |
145 | trace.getProject().refresh(); |
146 | ||
12c155f5 | 147 | } catch (CoreException e) { |
8fd82db5 | 148 | Activator.getDefault().logError("Error deleting trace: " + trace.getName(), e); //$NON-NLS-1$ |
12c155f5 FC |
149 | } |
150 | } | |
151 | } | |
152 | ||
153 | return null; | |
154 | } | |
155 | } |