Merge branch 'master'
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteTraceHandler.java
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.internal.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.ISelectionProvider;
29 import org.eclipse.jface.viewers.TreeSelection;
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();
59 if (window == null)
60 return false;
61
62 // Get the selection
63 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
64 IWorkbenchPart part = page.getActivePart();
65 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
66 if (selectionProvider == null)
67 return false;
68 ISelection selection = selectionProvider.getSelection();
69
70 // Make sure selection contains only traces
71 fSelection = null;
72 if (selection instanceof TreeSelection) {
73 fSelection = (TreeSelection) selection;
74 @SuppressWarnings("unchecked")
75 Iterator<Object> iterator = fSelection.iterator();
76 while (iterator.hasNext()) {
77 Object element = iterator.next();
78 if (!(element instanceof TmfTraceElement)) {
79 return false;
80 }
81 }
82 }
83
84 // If we get here, either nothing is selected or everything is a trace
85 return !selection.isEmpty();
86 }
87
88 // ------------------------------------------------------------------------
89 // Execution
90 // ------------------------------------------------------------------------
91
92 @Override
93 public Object execute(ExecutionEvent event) throws ExecutionException {
94
95 // Check if we are closing down
96 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
97 if (window == null)
98 return null;
99
100 // Confirm the operation
101 Shell shell = window.getShell();
102 MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK);
103 confirmOperation.setText(Messages.DeleteDialog_Title);
104 confirmOperation.setMessage(Messages.DeleteTraceHandler_Message);
105 if (confirmOperation.open() != SWT.OK)
106 return null;
107
108 @SuppressWarnings("unchecked")
109 Iterator<Object> iterator = fSelection.iterator();
110 while (iterator.hasNext()) {
111 Object element = iterator.next();
112 if (element instanceof TmfTraceElement) {
113 TmfTraceElement trace = (TmfTraceElement) element;
114 IResource resource = trace.getResource();
115 try {
116 IPath path = resource.getLocation();
117 if (path != null && (trace.getParent() instanceof TmfTraceFolder)) {
118 String location = path.toFile().getCanonicalPath();
119 TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder();
120
121 // Propagate the removal to traces
122 for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
123 List<ITmfProjectModelElement> toRemove = new LinkedList<ITmfProjectModelElement>();
124 for (ITmfProjectModelElement child : experiment.getChildren()) {
125 if (child.getResource().getLocation().toString().equals(location)) {
126 toRemove.add(child);
127 }
128 }
129 for (ITmfProjectModelElement child : toRemove) {
130 experiment.removeChild(child);
131 child.getResource().delete(true, null);
132 }
133 }
134 }
135
136 // Finally, delete the trace
137 resource.delete(true, new NullProgressMonitor());
138
139 // Delete supplementary files
140 trace.deleteSupplementaryFolder();
141
142 // Refresh the project
143 trace.getProject().refresh();
144
145 } catch (IOException e1) {
146 e1.printStackTrace();
147 } catch (CoreException e) {
148 e.printStackTrace();
149 }
150 }
151 }
152
153 return null;
154 }
155 }
This page took 0.036713 seconds and 6 git commands to generate.