tmf: Add missing @since annotations in Utils.TimeFormat
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteTraceHandler.java
CommitLineData
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 13package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
12c155f5 14
12c155f5
FC
15import java.util.Iterator;
16import java.util.LinkedList;
17import java.util.List;
18
19import org.eclipse.core.commands.AbstractHandler;
20import org.eclipse.core.commands.ExecutionEvent;
21import org.eclipse.core.commands.ExecutionException;
c4c81d91 22import org.eclipse.core.resources.IFile;
12c155f5
FC
23import org.eclipse.core.resources.IResource;
24import org.eclipse.core.runtime.CoreException;
25import org.eclipse.core.runtime.IPath;
26import org.eclipse.core.runtime.NullProgressMonitor;
27import org.eclipse.jface.viewers.ISelection;
1595249b 28import org.eclipse.jface.viewers.ISelectionProvider;
12c155f5 29import org.eclipse.jface.viewers.TreeSelection;
8fd82db5 30import org.eclipse.linuxtools.internal.tmf.ui.Activator;
12c155f5 31import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement;
c4c81d91 32import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
12c155f5
FC
33import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder;
34import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
35import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
36import org.eclipse.swt.SWT;
c4c81d91 37import org.eclipse.swt.widgets.Display;
12c155f5
FC
38import org.eclipse.swt.widgets.MessageBox;
39import org.eclipse.swt.widgets.Shell;
c4c81d91
PT
40import org.eclipse.ui.IEditorReference;
41import org.eclipse.ui.IWorkbench;
12c155f5
FC
42import org.eclipse.ui.IWorkbenchPage;
43import org.eclipse.ui.IWorkbenchPart;
44import org.eclipse.ui.IWorkbenchWindow;
45import org.eclipse.ui.PlatformUI;
c4c81d91 46import org.eclipse.ui.part.FileEditorInput;
12c155f5
FC
47
48/**
49 * <b><u>DeleteTraceHandler</u></b>
50 * <p>
51 */
52public 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)) {
9d869394 140 String location = path.toString();
12c155f5
FC
141 TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder();
142
143 // Propagate the removal to traces
144 for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
145 List<ITmfProjectModelElement> toRemove = new LinkedList<ITmfProjectModelElement>();
146 for (ITmfProjectModelElement child : experiment.getChildren()) {
147 if (child.getResource().getLocation().toString().equals(location)) {
148 toRemove.add(child);
149 }
150 }
151 for (ITmfProjectModelElement child : toRemove) {
c4c81d91
PT
152 // Close the experiment if open
153 file = ((TmfExperimentElement) experiment).getBookmarksFile();
154 input = new FileEditorInput(file);
155 for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
156 for (IWorkbenchPage wbPage : wbWindow.getPages()) {
157 for (IEditorReference editorReference : wbPage.getEditorReferences()) {
158 if (editorReference.getEditorInput().equals(input)) {
159 wbPage.closeEditor(editorReference.getEditor(false), false);
160 }
161 }
162 }
163 }
12c155f5
FC
164 experiment.removeChild(child);
165 child.getResource().delete(true, null);
166 }
167 }
a1e20e2e
PT
168
169 // Delete supplementary files
170 trace.deleteSupplementaryFolder();
12c155f5
FC
171 }
172
173 // Finally, delete the trace
174 resource.delete(true, new NullProgressMonitor());
5e4bf87d 175
5e4bf87d 176 // Refresh the project
12c155f5
FC
177 trace.getProject().refresh();
178
c4c81d91
PT
179 } catch (final CoreException e) {
180 Display.getDefault().asyncExec(new Runnable() {
181 @Override
182 public void run() {
183 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
184 mb.setText(Messages.DeleteTraceHandler_Error + ' ' + trace.getName());
185 mb.setMessage(e.getMessage());
186 mb.open();
187 }
188 });
8fd82db5 189 Activator.getDefault().logError("Error deleting trace: " + trace.getName(), e); //$NON-NLS-1$
12c155f5
FC
190 }
191 }
192 }
193
194 return null;
195 }
196}
This page took 0.042771 seconds and 5 git commands to generate.