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