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();
72 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
73 if (selectionProvider
== null) {
76 ISelection selection
= selectionProvider
.getSelection();
78 // Make sure selection contains only traces
80 if (selection
instanceof TreeSelection
) {
81 fSelection
= (TreeSelection
) selection
;
82 Iterator
<Object
> iterator
= fSelection
.iterator();
83 while (iterator
.hasNext()) {
84 Object element
= iterator
.next();
85 if (!(element
instanceof TmfTraceElement
)) {
91 // If we get here, either nothing is selected or everything is a trace
92 return !selection
.isEmpty();
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
102 // Check if we are closing down
103 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
104 if (window
== null) {
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
);
113 if (confirmOperation
.open() != SWT
.OK
) {
117 Iterator
<Object
> iterator
= fSelection
.iterator();
118 while (iterator
.hasNext()) {
119 Object element
= iterator
.next();
120 if (element
instanceof TmfTraceElement
) {
121 final TmfTraceElement trace
= (TmfTraceElement
) element
;
122 IResource resource
= trace
.getResource();
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);
138 IPath path
= resource
.getLocation();
139 if (path
!= null && (trace
.getParent() instanceof TmfTraceFolder
)) {
140 String location
= path
.toString();
141 TmfExperimentFolder experimentFolder
= trace
.getProject().getExperimentsFolder();
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
)) {
151 for (ITmfProjectModelElement child
: toRemove
) {
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);
164 experiment
.removeChild(child
);
165 child
.getResource().delete(true, null);
169 // Delete supplementary files
170 trace
.deleteSupplementaryFolder();
173 // Finally, delete the trace
174 resource
.delete(true, new NullProgressMonitor());
176 // Refresh the project
177 trace
.getProject().refresh();
179 } catch (final CoreException e
) {
180 Display
.getDefault().asyncExec(new Runnable() {
183 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
184 mb
.setText(Messages
.DeleteTraceHandler_Error
+ ' ' + trace
.getName());
185 mb
.setMessage(e
.getMessage());
189 Activator
.getDefault().logError("Error deleting trace: " + trace
.getName(), e
); //$NON-NLS-1$