Merge branch 'master' into TmfTrace-new
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteAction.java
1 /*******************************************************************************
2 * Copyright (c) 2012 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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
14
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.NotEnabledException;
17 import org.eclipse.core.commands.NotHandledException;
18 import org.eclipse.core.commands.common.NotDefinedException;
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
24 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
25 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.handlers.IHandlerService;
28
29 public class DeleteAction extends Action {
30
31 private static final String DELETE_TRACE_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.project.trace.delete"; //$NON-NLS-1$
32 private static final String DELETE_EXPERIMENT_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.project.experiment.delete"; //$NON-NLS-1$
33
34 private IWorkbenchPage page;
35 private ISelectionProvider selectionProvider;
36 private boolean tracesSelected;
37 private boolean experimentsSelected;
38
39 /**
40 * Default constructor
41 * @param page the workbench page
42 * @param selectionProvider the selection provider
43 */
44 public DeleteAction(IWorkbenchPage page, ISelectionProvider selectionProvider) {
45 this.page = page;
46 this.selectionProvider = selectionProvider;
47 }
48
49 /*
50 * (non-Javadoc)
51 * @see org.eclipse.jface.action.Action#isEnabled()
52 */
53 @Override
54 public boolean isEnabled() {
55 ISelection selection = selectionProvider.getSelection();
56 if (!selection.isEmpty()) {
57 tracesSelected = false;
58 experimentsSelected = false;
59 IStructuredSelection sSelection = (IStructuredSelection) selection;
60 for (Object aSelection : sSelection.toList()) {
61 if (aSelection instanceof TmfTraceElement) {
62 tracesSelected = true;
63 }
64 if (aSelection instanceof TmfExperimentElement) {
65 experimentsSelected = true;
66 }
67 }
68 if (tracesSelected && experimentsSelected) {
69 return false;
70 }
71 return (tracesSelected || experimentsSelected);
72 }
73 return false;
74 }
75
76 /*
77 * (non-Javadoc)
78 * @see org.eclipse.jface.action.Action#run()
79 */
80 @Override
81 public void run() {
82 try {
83 IHandlerService handlerService = (IHandlerService) page.getActivePart().getSite().getService(IHandlerService.class);
84 if (tracesSelected) {
85 handlerService.executeCommand(DELETE_TRACE_COMMAND_ID, null);
86 } else if (experimentsSelected) {
87 handlerService.executeCommand(DELETE_EXPERIMENT_COMMAND_ID, null);
88 }
89 } catch (ExecutionException e) {
90 TmfUiPlugin.getDefault().logError("Error deleting resources", e); //$NON-NLS-1$
91 } catch (NotDefinedException e) {
92 TmfUiPlugin.getDefault().logError("Error deleting resources", e); //$NON-NLS-1$
93 } catch (NotEnabledException e) {
94 TmfUiPlugin.getDefault().logError("Error deleting resources", e); //$NON-NLS-1$
95 } catch (NotHandledException e) {
96 TmfUiPlugin.getDefault().logError("Error deleting resources", e); //$NON-NLS-1$
97 }
98 }
99
100 }
This page took 0.038147 seconds and 6 git commands to generate.