tmf/lttng: Update 2014 copyrights
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteTraceHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson, École Polytechnique de Montréal
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 * Patrick Tasse - Close editors to release resources
12 * Geneviève Bastien - Moved the delete code to element model's classes
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
16
17 import java.util.Iterator;
18
19 import org.eclipse.core.commands.AbstractHandler;
20 import org.eclipse.core.commands.ExecutionEvent;
21 import org.eclipse.core.commands.ExecutionException;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.ISelectionProvider;
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.TmfTraceElement;
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.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchPart;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PlatformUI;
36
37 /**
38 * <b><u>DeleteTraceHandler</u></b>
39 * <p>
40 */
41 public class DeleteTraceHandler extends AbstractHandler {
42
43 private TreeSelection fSelection = null;
44
45 // ------------------------------------------------------------------------
46 // Validation
47 // ------------------------------------------------------------------------
48
49 @Override
50 public boolean isEnabled() {
51
52 // Check if we are closing down
53 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
54 if (window == null) {
55 return false;
56 }
57
58 // Get the selection
59 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
60 IWorkbenchPart part = page.getActivePart();
61 if (part == null) {
62 return false;
63 }
64 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
65 if (selectionProvider == null) {
66 return false;
67 }
68 ISelection selection = selectionProvider.getSelection();
69
70 // Make sure selection contains only traces
71 fSelection = null;
72 if (selection instanceof TreeSelection) {
73 fSelection = (TreeSelection) selection;
74 Iterator<Object> iterator = fSelection.iterator();
75 while (iterator.hasNext()) {
76 Object element = iterator.next();
77 if (!(element instanceof TmfTraceElement)) {
78 return false;
79 }
80 }
81 }
82
83 // If we get here, either nothing is selected or everything is a trace
84 return !selection.isEmpty();
85 }
86
87 // ------------------------------------------------------------------------
88 // Execution
89 // ------------------------------------------------------------------------
90
91 @Override
92 public Object execute(ExecutionEvent event) throws ExecutionException {
93
94 // Check if we are closing down
95 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
96 if (window == null) {
97 return null;
98 }
99
100 // Confirm the operation
101 Shell shell = window.getShell();
102 MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK);
103 confirmOperation.setText(Messages.DeleteDialog_Title);
104 confirmOperation.setMessage(Messages.DeleteTraceHandler_Message);
105 if (confirmOperation.open() != SWT.OK) {
106 return null;
107 }
108
109 Iterator<Object> iterator = fSelection.iterator();
110 while (iterator.hasNext()) {
111 Object element = iterator.next();
112 if (element instanceof TmfTraceElement) {
113 final TmfTraceElement trace = (TmfTraceElement) element;
114 try {
115 trace.delete(null);
116 } catch (final CoreException e) {
117 Display.getDefault().asyncExec(new Runnable() {
118 @Override
119 public void run() {
120 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
121 mb.setText(Messages.DeleteTraceHandler_Error + ' ' + trace.getName());
122 mb.setMessage(e.getMessage());
123 mb.open();
124 }
125 });
126 Activator.getDefault().logError("Error deleting trace: " + trace.getName(), e); //$NON-NLS-1$
127 }
128 }
129 }
130
131 return null;
132 }
133 }
This page took 0.034406 seconds and 5 git commands to generate.