Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010, 2011 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.handlers; | |
14 | ||
15 | import java.io.IOException; | |
16 | import java.util.Iterator; | |
17 | import java.util.LinkedList; | |
18 | import java.util.List; | |
19 | ||
20 | import org.eclipse.core.commands.AbstractHandler; | |
21 | import org.eclipse.core.commands.ExecutionEvent; | |
22 | import org.eclipse.core.commands.ExecutionException; | |
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; | |
28 | import org.eclipse.jface.viewers.TreeSelection; | |
29 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; | |
30 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; | |
31 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
33 | import org.eclipse.swt.SWT; | |
34 | import org.eclipse.swt.widgets.MessageBox; | |
35 | import org.eclipse.swt.widgets.Shell; | |
36 | import org.eclipse.ui.IWorkbenchPage; | |
37 | import org.eclipse.ui.IWorkbenchPart; | |
38 | import org.eclipse.ui.IWorkbenchWindow; | |
39 | import org.eclipse.ui.PlatformUI; | |
40 | ||
41 | /** | |
42 | * <b><u>DeleteTraceHandler</u></b> | |
43 | * <p> | |
44 | */ | |
45 | public class DeleteTraceHandler extends AbstractHandler { | |
46 | ||
47 | private TreeSelection fSelection = null; | |
48 | ||
49 | // ------------------------------------------------------------------------ | |
50 | // Validation | |
51 | // ------------------------------------------------------------------------ | |
52 | ||
53 | @Override | |
54 | public boolean isEnabled() { | |
55 | ||
56 | // Check if we are closing down | |
57 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
58 | if (window == null) | |
59 | return false; | |
60 | ||
61 | // Get the selection | |
62 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
63 | IWorkbenchPart part = page.getActivePart(); | |
64 | ISelection selection = part.getSite().getSelectionProvider().getSelection(); | |
65 | ||
66 | // Make sure selection contains only traces | |
67 | fSelection = null; | |
68 | if (selection instanceof TreeSelection) { | |
69 | fSelection = (TreeSelection) selection; | |
70 | @SuppressWarnings("unchecked") | |
71 | Iterator<Object> iterator = fSelection.iterator(); | |
72 | while (iterator.hasNext()) { | |
73 | Object element = iterator.next(); | |
74 | if (!(element instanceof TmfTraceElement)) { | |
75 | return false; | |
76 | } | |
77 | } | |
78 | } | |
79 | ||
80 | // If we get here, either nothing is selected or everything is a trace | |
81 | return !selection.isEmpty(); | |
82 | } | |
83 | ||
84 | // ------------------------------------------------------------------------ | |
85 | // Execution | |
86 | // ------------------------------------------------------------------------ | |
87 | ||
88 | @Override | |
89 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
90 | ||
91 | // Check if we are closing down | |
92 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
93 | if (window == null) | |
94 | return null; | |
95 | ||
96 | // Confirm the operation | |
97 | Shell shell = window.getShell(); | |
98 | MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); | |
99 | confirmOperation.setText(Messages.DeleteDialog_Title); | |
100 | confirmOperation.setMessage(Messages.DeleteTraceHandler_Message); | |
101 | if (confirmOperation.open() != SWT.OK) | |
102 | return null; | |
103 | ||
104 | @SuppressWarnings("unchecked") | |
105 | Iterator<Object> iterator = fSelection.iterator(); | |
106 | while (iterator.hasNext()) { | |
107 | Object element = iterator.next(); | |
108 | if (element instanceof TmfTraceElement) { | |
109 | TmfTraceElement trace = (TmfTraceElement) element; | |
110 | IResource resource = trace.getResource(); | |
111 | try { | |
112 | IPath path = resource.getLocation(); | |
113 | if (path != null && (trace.getParent() instanceof TmfTraceFolder)) { | |
114 | String location = path.toFile().getCanonicalPath(); | |
115 | TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder(); | |
116 | ||
117 | // Propagate the removal to traces | |
118 | for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
119 | List<ITmfProjectModelElement> toRemove = new LinkedList<ITmfProjectModelElement>(); | |
120 | for (ITmfProjectModelElement child : experiment.getChildren()) { | |
121 | if (child.getResource().getLocation().toString().equals(location)) { | |
122 | toRemove.add(child); | |
123 | } | |
124 | } | |
125 | for (ITmfProjectModelElement child : toRemove) { | |
126 | experiment.removeChild(child); | |
127 | child.getResource().delete(true, null); | |
128 | } | |
129 | } | |
130 | } | |
131 | ||
132 | // Finally, delete the trace | |
133 | resource.delete(true, new NullProgressMonitor()); | |
134 | trace.getProject().refresh(); | |
135 | ||
136 | } catch (IOException e1) { | |
137 | e1.printStackTrace(); | |
138 | } catch (CoreException e) { | |
139 | e.printStackTrace(); | |
140 | } | |
141 | } | |
142 | } | |
143 | ||
144 | return null; | |
145 | } | |
146 | } |