1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
15 import java
.util
.Iterator
;
16 import java
.util
.LinkedList
;
17 import java
.util
.List
;
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
.IFile
;
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
.internal
.tmf
.ui
.Activator
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.MessageBox
;
39 import org
.eclipse
.swt
.widgets
.Shell
;
40 import org
.eclipse
.ui
.IEditorReference
;
41 import org
.eclipse
.ui
.IWorkbench
;
42 import org
.eclipse
.ui
.IWorkbenchPage
;
43 import org
.eclipse
.ui
.IWorkbenchPart
;
44 import org
.eclipse
.ui
.IWorkbenchWindow
;
45 import org
.eclipse
.ui
.PlatformUI
;
46 import org
.eclipse
.ui
.part
.FileEditorInput
;
49 * <b><u>DeleteTraceHandler</u></b>
52 public class DeleteTraceHandler
extends AbstractHandler
{
54 private TreeSelection fSelection
= null;
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 public boolean isEnabled() {
63 // Check if we are closing down
64 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
70 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
71 IWorkbenchPart part
= page
.getActivePart();
75 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
76 if (selectionProvider
== null) {
79 ISelection selection
= selectionProvider
.getSelection();
81 // Make sure selection contains only traces
83 if (selection
instanceof TreeSelection
) {
84 fSelection
= (TreeSelection
) selection
;
85 Iterator
<Object
> iterator
= fSelection
.iterator();
86 while (iterator
.hasNext()) {
87 Object element
= iterator
.next();
88 if (!(element
instanceof TmfTraceElement
)) {
94 // If we get here, either nothing is selected or everything is a trace
95 return !selection
.isEmpty();
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
105 // Check if we are closing down
106 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
107 if (window
== null) {
111 // Confirm the operation
112 Shell shell
= window
.getShell();
113 MessageBox confirmOperation
= new MessageBox(shell
, SWT
.ICON_QUESTION
| SWT
.CANCEL
| SWT
.OK
);
114 confirmOperation
.setText(Messages
.DeleteDialog_Title
);
115 confirmOperation
.setMessage(Messages
.DeleteTraceHandler_Message
);
116 if (confirmOperation
.open() != SWT
.OK
) {
120 Iterator
<Object
> iterator
= fSelection
.iterator();
121 while (iterator
.hasNext()) {
122 Object element
= iterator
.next();
123 if (element
instanceof TmfTraceElement
) {
124 final TmfTraceElement trace
= (TmfTraceElement
) element
;
125 IResource resource
= trace
.getResource();
127 // Close the trace if open
128 IFile file
= trace
.getBookmarksFile();
129 FileEditorInput input
= new FileEditorInput(file
);
130 IWorkbench wb
= PlatformUI
.getWorkbench();
131 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
132 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
133 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
134 if (editorReference
.getEditorInput().equals(input
)) {
135 wbPage
.closeEditor(editorReference
.getEditor(false), false);
141 IPath path
= resource
.getLocation();
142 if (path
!= null && (trace
.getParent() instanceof TmfTraceFolder
)) {
143 TmfExperimentFolder experimentFolder
= trace
.getProject().getExperimentsFolder();
145 // Propagate the removal to traces
146 for (ITmfProjectModelElement experiment
: experimentFolder
.getChildren()) {
147 List
<ITmfProjectModelElement
> toRemove
= new LinkedList
<ITmfProjectModelElement
>();
148 for (ITmfProjectModelElement child
: experiment
.getChildren()) {
149 if (child
.getName().equals(trace
.getName())) {
153 for (ITmfProjectModelElement child
: toRemove
) {
154 // Close the experiment if open
155 file
= ((TmfExperimentElement
) experiment
).getBookmarksFile();
156 input
= new FileEditorInput(file
);
157 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
158 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
159 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
160 if (editorReference
.getEditorInput().equals(input
)) {
161 wbPage
.closeEditor(editorReference
.getEditor(false), false);
166 experiment
.removeChild(child
);
167 child
.getResource().delete(true, null);
171 // Delete supplementary files
172 trace
.deleteSupplementaryFolder();
175 // Finally, delete the trace
176 resource
.delete(true, new NullProgressMonitor());
178 // Refresh the project
179 trace
.getProject().refresh();
181 } catch (final CoreException e
) {
182 Display
.getDefault().asyncExec(new Runnable() {
185 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
186 mb
.setText(Messages
.DeleteTraceHandler_Error
+ ' ' + trace
.getName());
187 mb
.setMessage(e
.getMessage());
191 Activator
.getDefault().logError("Error deleting trace: " + trace
.getName(), e
); //$NON-NLS-1$