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
;
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
.jface
.viewers
.ISelection
;
24 import org
.eclipse
.jface
.viewers
.TreeSelection
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
27 import org
.eclipse
.swt
.SWT
;
28 import org
.eclipse
.swt
.widgets
.Display
;
29 import org
.eclipse
.swt
.widgets
.MessageBox
;
30 import org
.eclipse
.swt
.widgets
.Shell
;
31 import org
.eclipse
.ui
.IEditorReference
;
32 import org
.eclipse
.ui
.IWorkbench
;
33 import org
.eclipse
.ui
.IWorkbenchPage
;
34 import org
.eclipse
.ui
.IWorkbenchPart
;
35 import org
.eclipse
.ui
.IWorkbenchWindow
;
36 import org
.eclipse
.ui
.PlatformUI
;
37 import org
.eclipse
.ui
.part
.FileEditorInput
;
40 * <b><u>DeleteExperimentHandler</u></b>
43 public class DeleteExperimentHandler
extends AbstractHandler
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
52 // Check if we are closing down
53 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
58 // Confirm the operation
59 Shell shell
= window
.getShell();
60 MessageBox confirmOperation
= new MessageBox(shell
, SWT
.ICON_QUESTION
| SWT
.CANCEL
| SWT
.OK
);
61 confirmOperation
.setText(Messages
.DeleteDialog_Title
);
62 confirmOperation
.setMessage(Messages
.DeleteExperimentHandler_Message
);
63 if (confirmOperation
.open() != SWT
.OK
) {
68 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
69 IWorkbenchPart part
= page
.getActivePart();
70 ISelection selection
= part
.getSite().getSelectionProvider().getSelection();
72 if (selection
instanceof TreeSelection
) {
73 TreeSelection sel
= (TreeSelection
) selection
;
74 Iterator
<Object
> iterator
= sel
.iterator();
75 while (iterator
.hasNext()) {
76 Object element
= iterator
.next();
77 if (element
instanceof TmfExperimentElement
) {
78 final TmfExperimentElement experiment
= (TmfExperimentElement
) element
;
79 IResource resource
= experiment
.getResource();
82 // Close the experiment if open
83 IFile file
= experiment
.getBookmarksFile();
84 FileEditorInput input
= new FileEditorInput(file
);
85 IWorkbench wb
= PlatformUI
.getWorkbench();
86 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
87 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
88 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
89 if (editorReference
.getEditorInput().equals(input
)) {
90 wbPage
.closeEditor(editorReference
.getEditor(false), false);
96 // Finally, delete the experiment
97 resource
.delete(true, null);
99 // Refresh the project
100 experiment
.getProject().refresh();
102 } catch (final CoreException e
) {
103 Display
.getDefault().asyncExec(new Runnable() {
106 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
107 mb
.setText(Messages
.DeleteTraceHandler_Error
+ ' ' + experiment
.getName());
108 mb
.setMessage(e
.getMessage());
112 Activator
.getDefault().logError("Error deleting experiment: " + experiment
.getName(), e
); //$NON-NLS-1$