Close trace and improve error handling on delete and rename commands
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteExperimentHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
14
15 import java.util.Iterator;
16
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;
38
39 /**
40 * <b><u>DeleteExperimentHandler</u></b>
41 * <p>
42 */
43 public class DeleteExperimentHandler extends AbstractHandler {
44
45 // ------------------------------------------------------------------------
46 // Execution
47 // ------------------------------------------------------------------------
48
49 @Override
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51
52 // Check if we are closing down
53 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
54 if (window == null) {
55 return null;
56 }
57
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) {
64 return null;
65 }
66
67 // Get the selection
68 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
69 IWorkbenchPart part = page.getActivePart();
70 ISelection selection = part.getSite().getSelectionProvider().getSelection();
71
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();
80
81 try {
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);
91 }
92 }
93 }
94 }
95
96 // Finally, delete the experiment
97 resource.delete(true, null);
98
99 // Refresh the project
100 experiment.getProject().refresh();
101
102 } catch (final CoreException e) {
103 Display.getDefault().asyncExec(new Runnable() {
104 @Override
105 public void run() {
106 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
107 mb.setText(Messages.DeleteTraceHandler_Error + ' ' + experiment.getName());
108 mb.setMessage(e.getMessage());
109 mb.open();
110 }
111 });
112 Activator.getDefault().logError("Error deleting experiment: " + experiment.getName(), e); //$NON-NLS-1$
113 }
114 }
115 }
116 }
117
118 return null;
119 }
120 }
This page took 0.034115 seconds and 6 git commands to generate.