Fix NullPointerException in command handlers
[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 if (part == null) {
71 return false;
72 }
73 ISelection selection = part.getSite().getSelectionProvider().getSelection();
74
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();
83
84 try {
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);
94 }
95 }
96 }
97 }
98
99 // Finally, delete the experiment
100 resource.delete(true, null);
101
102 // Refresh the project
103 experiment.getProject().refresh();
104
105 } catch (final CoreException e) {
106 Display.getDefault().asyncExec(new Runnable() {
107 @Override
108 public void run() {
109 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
110 mb.setText(Messages.DeleteTraceHandler_Error + ' ' + experiment.getName());
111 mb.setMessage(e.getMessage());
112 mb.open();
113 }
114 });
115 Activator.getDefault().logError("Error deleting experiment: " + experiment.getName(), e); //$NON-NLS-1$
116 }
117 }
118 }
119 }
120
121 return null;
122 }
123 }
This page took 0.033714 seconds and 6 git commands to generate.