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();
73 ISelection selection
= part
.getSite().getSelectionProvider().getSelection();
75 if (selection
instanceof TreeSelection
) {
76 TreeSelection sel
= (TreeSelection
) selection
;
77 Iterator
<Object
> iterator
= sel
.iterator();
78 while (iterator
.hasNext()) {
79 Object element
= iterator
.next();
80 if (element
instanceof TmfExperimentElement
) {
81 final TmfExperimentElement experiment
= (TmfExperimentElement
) element
;
82 IResource resource
= experiment
.getResource();
85 // Close the experiment if open
86 IFile file
= experiment
.getBookmarksFile();
87 FileEditorInput input
= new FileEditorInput(file
);
88 IWorkbench wb
= PlatformUI
.getWorkbench();
89 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
90 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
91 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
92 if (editorReference
.getEditorInput().equals(input
)) {
93 wbPage
.closeEditor(editorReference
.getEditor(false), false);
99 // Finally, delete the experiment
100 resource
.delete(true, null);
102 // Refresh the project
103 experiment
.getProject().refresh();
105 } catch (final CoreException e
) {
106 Display
.getDefault().asyncExec(new Runnable() {
109 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
110 mb
.setText(Messages
.DeleteTraceHandler_Error
+ ' ' + experiment
.getName());
111 mb
.setMessage(e
.getMessage());
115 Activator
.getDefault().logError("Error deleting experiment: " + experiment
.getName(), e
); //$NON-NLS-1$