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
.io
.IOException
;
16 import java
.util
.Iterator
;
17 import java
.util
.LinkedList
;
18 import java
.util
.List
;
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
.internal
.tmf
.ui
.Activator
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
35 import org
.eclipse
.swt
.SWT
;
36 import org
.eclipse
.swt
.widgets
.MessageBox
;
37 import org
.eclipse
.swt
.widgets
.Shell
;
38 import org
.eclipse
.ui
.IWorkbenchPage
;
39 import org
.eclipse
.ui
.IWorkbenchPart
;
40 import org
.eclipse
.ui
.IWorkbenchWindow
;
41 import org
.eclipse
.ui
.PlatformUI
;
44 * <b><u>DeleteTraceHandler</u></b>
47 public class DeleteTraceHandler
extends AbstractHandler
{
49 private TreeSelection fSelection
= null;
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 public boolean isEnabled() {
58 // Check if we are closing down
59 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
64 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
65 IWorkbenchPart part
= page
.getActivePart();
66 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
67 if (selectionProvider
== null)
69 ISelection selection
= selectionProvider
.getSelection();
71 // Make sure selection contains only traces
73 if (selection
instanceof TreeSelection
) {
74 fSelection
= (TreeSelection
) selection
;
75 @SuppressWarnings("unchecked")
76 Iterator
<Object
> iterator
= fSelection
.iterator();
77 while (iterator
.hasNext()) {
78 Object element
= iterator
.next();
79 if (!(element
instanceof TmfTraceElement
)) {
85 // If we get here, either nothing is selected or everything is a trace
86 return !selection
.isEmpty();
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
94 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
96 // Check if we are closing down
97 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
101 // Confirm the operation
102 Shell shell
= window
.getShell();
103 MessageBox confirmOperation
= new MessageBox(shell
, SWT
.ICON_QUESTION
| SWT
.CANCEL
| SWT
.OK
);
104 confirmOperation
.setText(Messages
.DeleteDialog_Title
);
105 confirmOperation
.setMessage(Messages
.DeleteTraceHandler_Message
);
106 if (confirmOperation
.open() != SWT
.OK
)
109 @SuppressWarnings("unchecked")
110 Iterator
<Object
> iterator
= fSelection
.iterator();
111 while (iterator
.hasNext()) {
112 Object element
= iterator
.next();
113 if (element
instanceof TmfTraceElement
) {
114 TmfTraceElement trace
= (TmfTraceElement
) element
;
115 IResource resource
= trace
.getResource();
117 IPath path
= resource
.getLocation();
118 if (path
!= null && (trace
.getParent() instanceof TmfTraceFolder
)) {
119 String location
= path
.toFile().getCanonicalPath();
120 TmfExperimentFolder experimentFolder
= trace
.getProject().getExperimentsFolder();
122 // Propagate the removal to traces
123 for (ITmfProjectModelElement experiment
: experimentFolder
.getChildren()) {
124 List
<ITmfProjectModelElement
> toRemove
= new LinkedList
<ITmfProjectModelElement
>();
125 for (ITmfProjectModelElement child
: experiment
.getChildren()) {
126 if (child
.getResource().getLocation().toString().equals(location
)) {
130 for (ITmfProjectModelElement child
: toRemove
) {
131 experiment
.removeChild(child
);
132 child
.getResource().delete(true, null);
137 // Finally, delete the trace
138 resource
.delete(true, new NullProgressMonitor());
140 // Delete supplementary files
141 trace
.deleteSupplementaryFolder();
143 // Refresh the project
144 trace
.getProject().refresh();
146 } catch (IOException e
) {
147 Activator
.getDefault().logError("Error deleting trace: " + trace
.getName(), e
); //$NON-NLS-1$
148 } catch (CoreException e
) {
149 Activator
.getDefault().logError("Error deleting trace: " + trace
.getName(), e
); //$NON-NLS-1$