2010-07-12 Francois Chouinard <fchouinar@gmail.com> Contribution for Bug319428...
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / project / handlers / DeleteTraceHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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.tmf.ui.views.project.handlers;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.linuxtools.tmf.ui.views.project.ProjectView;
23 import org.eclipse.linuxtools.tmf.ui.views.project.model.ITmfProjectTreeNode;
24 import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfExperimentFolderNode;
25 import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfExperimentNode;
26 import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfTraceNode;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.IWorkbenchWindow;
29 import org.eclipse.ui.PlatformUI;
30
31 /**
32 * <b><u>DeleteTraceHandler</u></b>
33 * <p>
34 * TODO: Implement me. Please.
35 */
36 public class DeleteTraceHandler extends AbstractHandler {
37
38 private TmfTraceNode fTrace = null;
39
40 // ------------------------------------------------------------------------
41 // Validation
42 // ------------------------------------------------------------------------
43
44 public boolean isEnabled() {
45
46 // Check if we are closing down
47 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
48 if (window == null)
49 return false;
50
51 // Check if a trace is selected
52 IWorkbenchPage page = window.getActivePage();
53 if (!(page.getActivePart() instanceof ProjectView))
54 return false;
55
56 // Check if a trace is selected
57 ISelection selection = page.getSelection(ProjectView.ID);
58 if (selection instanceof StructuredSelection) {
59 Object element = ((StructuredSelection) selection).getFirstElement();
60 fTrace = (element instanceof TmfTraceNode) ? (TmfTraceNode) element : null;
61 }
62
63 return (fTrace != null);
64 }
65
66 // ------------------------------------------------------------------------
67 // Execution
68 // ------------------------------------------------------------------------
69
70 public Object execute(ExecutionEvent event) throws ExecutionException {
71
72 IResource resource = fTrace.getResource();
73 ITmfProjectTreeNode parent = fTrace.getParent();
74 try {
75 if (!(parent instanceof TmfExperimentNode)) {
76 // Delete the trace from every experiment where it appears
77 TmfExperimentFolderNode experimentFolder = fTrace.getProject().getExperimentsFolder();
78 for (ITmfProjectTreeNode experiment : experimentFolder.getChildren()) {
79 for (int i = 0; i < experiment.getChildren().size(); i++) {
80 TmfTraceNode trace = (TmfTraceNode) experiment.getChildren().get(i);
81 if (trace.getResource().getLocation().equals(resource.getLocation())) {
82 experiment.removeChild(trace);
83 experiment.refresh();
84 trace.getResource().delete(true, null);
85 }
86 }
87 }
88 }
89 parent.removeChild(fTrace);
90 parent.refresh();
91 resource.delete(true, null);
92 } catch (CoreException e) {
93 e.printStackTrace();
94 }
95
96 return null;
97 }
98
99 }
This page took 0.033011 seconds and 5 git commands to generate.