Commit | Line | Data |
---|---|---|
5e4bf87d | 1 | /******************************************************************************* |
a72a6830 | 2 | * Copyright (c) 2012, 2013 Ericsson |
abbdd66a | 3 | * |
5e4bf87d BH |
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 | |
abbdd66a | 8 | * |
5e4bf87d | 9 | * Contributors: |
f854e05e MAL |
10 | * Bernd Hufmann - Initial API and implementation |
11 | * Patrick Tasse - Close editors to release resources | |
12 | * Marc-Andre Laperle - Fix NPE (Bug 416574) | |
5e4bf87d BH |
13 | *******************************************************************************/ |
14 | ||
15 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; | |
16 | ||
e12ecd30 BH |
17 | import java.util.ArrayList; |
18 | import java.util.Arrays; | |
a72a6830 | 19 | import java.util.HashMap; |
e12ecd30 BH |
20 | import java.util.List; |
21 | ||
5e4bf87d BH |
22 | import org.eclipse.core.commands.AbstractHandler; |
23 | import org.eclipse.core.commands.ExecutionEvent; | |
24 | import org.eclipse.core.commands.ExecutionException; | |
25 | import org.eclipse.core.resources.IResource; | |
26 | import org.eclipse.core.runtime.CoreException; | |
e12ecd30 | 27 | import org.eclipse.core.runtime.NullProgressMonitor; |
5e4bf87d BH |
28 | import org.eclipse.jface.viewers.ISelection; |
29 | import org.eclipse.jface.viewers.ISelectionProvider; | |
30 | import org.eclipse.jface.viewers.TreeSelection; | |
e12ecd30 | 31 | import org.eclipse.jface.window.Window; |
8fd82db5 | 32 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
e12ecd30 | 33 | import org.eclipse.linuxtools.internal.tmf.ui.project.dialogs.SelectSupplementaryResourcesDialog; |
5e4bf87d BH |
34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
35 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
36 | import org.eclipse.ui.IWorkbenchPage; | |
37 | import org.eclipse.ui.IWorkbenchPart; | |
38 | import org.eclipse.ui.IWorkbenchWindow; | |
39 | import org.eclipse.ui.PlatformUI; | |
40 | ||
41 | /** | |
a72a6830 | 42 | * Handler for Delete Supplementary Files command on experiment |
5e4bf87d BH |
43 | */ |
44 | public class DeleteExperimentSupplementaryFilesHandler extends AbstractHandler { | |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Execution | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
50 | @Override | |
51 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
52 | ||
53 | // Check if we are closing down | |
54 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
55 | if (window == null) { | |
56 | return null; | |
57 | } | |
58 | ||
59 | // Get the selection | |
60 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
61 | IWorkbenchPart part = page.getActivePart(); | |
d5210347 PT |
62 | if (part == null) { |
63 | return false; | |
64 | } | |
5e4bf87d BH |
65 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
66 | if (selectionProvider == null) { | |
67 | return false; | |
68 | } | |
69 | ||
70 | ISelection selection = selectionProvider.getSelection(); | |
71 | ||
72 | // Make sure there is only selection and that it is an experiment | |
73 | if (selection instanceof TreeSelection) { | |
74 | TreeSelection sel = (TreeSelection) selection; | |
75 | // There should be only one item selected as per the plugin.xml | |
76 | Object element = sel.getFirstElement(); | |
507b1336 | 77 | List<IResource> resourcesList = new ArrayList<>(); |
abbdd66a | 78 | |
5e4bf87d BH |
79 | if (element instanceof TmfExperimentElement) { |
80 | ||
a72a6830 | 81 | TmfExperimentElement experiment = (TmfExperimentElement) element; |
5e4bf87d | 82 | |
a72a6830 | 83 | IResource[] resources = experiment.getSupplementaryResources(); |
f854e05e MAL |
84 | // List to know which resources belong to the experiment |
85 | List<IResource> experimentResources = Arrays.asList(resources); | |
86 | ||
87 | resourcesList.addAll(experimentResources); | |
99504bb8 | 88 | |
a72a6830 | 89 | // Map to know which trace to close for each resource |
507b1336 | 90 | HashMap<IResource, TmfTraceElement> traceMap = new HashMap<>(); |
a72a6830 PT |
91 | |
92 | for (TmfTraceElement aTrace : experiment.getTraces()) { | |
e12ecd30 | 93 | |
5e4bf87d BH |
94 | // If trace is under an experiment, use the original trace from the traces folder |
95 | aTrace = aTrace.getElementUnderTraceFolder(); | |
e12ecd30 | 96 | |
a72a6830 | 97 | // Add the trace supplementary resources for the dialog |
99504bb8 | 98 | resources = aTrace.getSupplementaryResources(); |
e12ecd30 | 99 | resourcesList.addAll(Arrays.asList(resources)); |
a72a6830 PT |
100 | |
101 | for (IResource resource : resources) { | |
102 | traceMap.put(resource, aTrace); | |
103 | } | |
e12ecd30 BH |
104 | } |
105 | ||
106 | SelectSupplementaryResourcesDialog dialog = new SelectSupplementaryResourcesDialog(window.getShell(), resourcesList.toArray(new IResource[resourcesList.size()])); | |
abbdd66a | 107 | |
e12ecd30 BH |
108 | if (dialog.open() != Window.OK) { |
109 | return null; | |
110 | } | |
111 | ||
112 | IResource[] resourcesToDelete = dialog.getResources(); | |
113 | ||
a72a6830 PT |
114 | // Delete the selected resources |
115 | for (IResource resource : resourcesToDelete) { | |
f854e05e MAL |
116 | if (experimentResources.contains(resource)) { |
117 | experiment.closeEditors(); | |
118 | } else { | |
119 | traceMap.get(resource).closeEditors(); | |
120 | } | |
121 | ||
e12ecd30 | 122 | try { |
a72a6830 | 123 | resource.delete(true, new NullProgressMonitor()); |
e12ecd30 | 124 | } catch (CoreException e) { |
a72a6830 | 125 | Activator.getDefault().logError("Error deleting supplementary resource " + resource, e); //$NON-NLS-1$ |
e12ecd30 | 126 | } |
5e4bf87d BH |
127 | } |
128 | ||
a72a6830 | 129 | IResource resource = experiment.getProject().getResource(); |
5e4bf87d BH |
130 | if (resource != null) { |
131 | try { | |
abbdd66a | 132 | resource.refreshLocal(IResource.DEPTH_INFINITE, null); |
5e4bf87d | 133 | } catch (CoreException e) { |
8fd82db5 | 134 | Activator.getDefault().logError("Error refreshing resource " + resource, e); //$NON-NLS-1$ |
5e4bf87d BH |
135 | } |
136 | } | |
137 | } | |
138 | } | |
139 | return null; | |
140 | } | |
141 | } |