1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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
;
17 import org
.eclipse
.core
.commands
.AbstractHandler
;
18 import org
.eclipse
.core
.commands
.ExecutionEvent
;
19 import org
.eclipse
.core
.commands
.ExecutionException
;
20 import org
.eclipse
.core
.resources
.IFile
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.core
.runtime
.IPath
;
24 import org
.eclipse
.jface
.viewers
.ISelection
;
25 import org
.eclipse
.jface
.viewers
.TreeSelection
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
28 import org
.eclipse
.swt
.SWT
;
29 import org
.eclipse
.swt
.widgets
.Display
;
30 import org
.eclipse
.swt
.widgets
.MessageBox
;
31 import org
.eclipse
.swt
.widgets
.Shell
;
32 import org
.eclipse
.ui
.IEditorReference
;
33 import org
.eclipse
.ui
.IWorkbench
;
34 import org
.eclipse
.ui
.IWorkbenchPage
;
35 import org
.eclipse
.ui
.IWorkbenchPart
;
36 import org
.eclipse
.ui
.IWorkbenchWindow
;
37 import org
.eclipse
.ui
.PlatformUI
;
38 import org
.eclipse
.ui
.part
.FileEditorInput
;
41 * <b><u>DeleteExperimentHandler</u></b>
44 public class DeleteExperimentHandler
extends AbstractHandler
{
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
53 // Check if we are closing down
54 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
59 // Confirm the operation
60 Shell shell
= window
.getShell();
61 MessageBox confirmOperation
= new MessageBox(shell
, SWT
.ICON_QUESTION
| SWT
.CANCEL
| SWT
.OK
);
62 confirmOperation
.setText(Messages
.DeleteDialog_Title
);
63 confirmOperation
.setMessage(Messages
.DeleteExperimentHandler_Message
);
64 if (confirmOperation
.open() != SWT
.OK
) {
69 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
70 IWorkbenchPart part
= page
.getActivePart();
74 ISelection selection
= part
.getSite().getSelectionProvider().getSelection();
76 if (selection
instanceof TreeSelection
) {
77 TreeSelection sel
= (TreeSelection
) selection
;
78 Iterator
<Object
> iterator
= sel
.iterator();
79 while (iterator
.hasNext()) {
80 Object element
= iterator
.next();
81 if (element
instanceof TmfExperimentElement
) {
82 final TmfExperimentElement experiment
= (TmfExperimentElement
) element
;
83 IResource resource
= experiment
.getResource();
86 // Close the experiment if open
87 IFile file
= experiment
.getBookmarksFile();
88 FileEditorInput input
= new FileEditorInput(file
);
89 IWorkbench wb
= PlatformUI
.getWorkbench();
90 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
91 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
92 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
93 if (editorReference
.getEditorInput().equals(input
)) {
94 wbPage
.closeEditor(editorReference
.getEditor(false), false);
100 IPath path
= resource
.getLocation();
102 // Delete supplementary files
103 experiment
.deleteSupplementaryFolder();
106 // Finally, delete the experiment
107 resource
.delete(true, null);
109 // Refresh the project
110 experiment
.getProject().refresh();
112 } catch (final CoreException e
) {
113 Display
.getDefault().asyncExec(new Runnable() {
116 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
117 mb
.setText(Messages
.DeleteTraceHandler_Error
+ ' ' + experiment
.getName());
118 mb
.setMessage(e
.getMessage());
122 Activator
.getDefault().logError("Error deleting experiment: " + experiment
.getName(), e
); //$NON-NLS-1$