Commit | Line | Data |
---|---|---|
abfad0aa FC |
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 | ||
9ccc6d01 | 44 | @Override |
abfad0aa FC |
45 | public boolean isEnabled() { |
46 | ||
47 | // Check if we are closing down | |
48 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
49 | if (window == null) | |
50 | return false; | |
51 | ||
52 | // Check if a trace is selected | |
53 | IWorkbenchPage page = window.getActivePage(); | |
54 | if (!(page.getActivePart() instanceof ProjectView)) | |
55 | return false; | |
56 | ||
57 | // Check if a trace is selected | |
58 | ISelection selection = page.getSelection(ProjectView.ID); | |
59 | if (selection instanceof StructuredSelection) { | |
60 | Object element = ((StructuredSelection) selection).getFirstElement(); | |
61 | fTrace = (element instanceof TmfTraceNode) ? (TmfTraceNode) element : null; | |
62 | } | |
63 | ||
64 | return (fTrace != null); | |
65 | } | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // Execution | |
69 | // ------------------------------------------------------------------------ | |
70 | ||
71 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
72 | ||
73 | IResource resource = fTrace.getResource(); | |
74 | ITmfProjectTreeNode parent = fTrace.getParent(); | |
75 | try { | |
76 | if (!(parent instanceof TmfExperimentNode)) { | |
77 | // Delete the trace from every experiment where it appears | |
78 | TmfExperimentFolderNode experimentFolder = fTrace.getProject().getExperimentsFolder(); | |
79 | for (ITmfProjectTreeNode experiment : experimentFolder.getChildren()) { | |
80 | for (int i = 0; i < experiment.getChildren().size(); i++) { | |
81 | TmfTraceNode trace = (TmfTraceNode) experiment.getChildren().get(i); | |
82 | if (trace.getResource().getLocation().equals(resource.getLocation())) { | |
83 | experiment.removeChild(trace); | |
84 | experiment.refresh(); | |
85 | trace.getResource().delete(true, null); | |
86 | } | |
87 | } | |
88 | } | |
89 | } | |
90 | parent.removeChild(fTrace); | |
91 | parent.refresh(); | |
92 | resource.delete(true, null); | |
93 | } catch (CoreException e) { | |
94 | e.printStackTrace(); | |
95 | } | |
96 | ||
97 | return null; | |
98 | } | |
99 | ||
100 | } |