tmf: Switch tmf.ui to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteExperimentSupplementaryFilesHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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 * Bernd Hufmann - Initial API and implementation
11 * Patrick Tasse - Close editors to release resources
12 * Marc-Andre Laperle - Fix NPE (Bug 416574)
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.HashMap;
20 import java.util.List;
21
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;
27 import org.eclipse.core.runtime.NullProgressMonitor;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.ISelectionProvider;
30 import org.eclipse.jface.viewers.TreeSelection;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
33 import org.eclipse.linuxtools.internal.tmf.ui.project.dialogs.SelectSupplementaryResourcesDialog;
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 /**
42 * Handler for Delete Supplementary Files command on experiment
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();
62 if (part == null) {
63 return false;
64 }
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();
77 List<IResource> resourcesList = new ArrayList<>();
78
79 if (element instanceof TmfExperimentElement) {
80
81 TmfExperimentElement experiment = (TmfExperimentElement) element;
82
83 IResource[] resources = experiment.getSupplementaryResources();
84 // List to know which resources belong to the experiment
85 List<IResource> experimentResources = Arrays.asList(resources);
86
87 resourcesList.addAll(experimentResources);
88
89 // Map to know which trace to close for each resource
90 HashMap<IResource, TmfTraceElement> traceMap = new HashMap<>();
91
92 for (TmfTraceElement aTrace : experiment.getTraces()) {
93
94 // If trace is under an experiment, use the original trace from the traces folder
95 aTrace = aTrace.getElementUnderTraceFolder();
96
97 // Add the trace supplementary resources for the dialog
98 resources = aTrace.getSupplementaryResources();
99 resourcesList.addAll(Arrays.asList(resources));
100
101 for (IResource resource : resources) {
102 traceMap.put(resource, aTrace);
103 }
104 }
105
106 SelectSupplementaryResourcesDialog dialog = new SelectSupplementaryResourcesDialog(window.getShell(), resourcesList.toArray(new IResource[resourcesList.size()]));
107
108 if (dialog.open() != Window.OK) {
109 return null;
110 }
111
112 IResource[] resourcesToDelete = dialog.getResources();
113
114 // Delete the selected resources
115 for (IResource resource : resourcesToDelete) {
116 if (experimentResources.contains(resource)) {
117 experiment.closeEditors();
118 } else {
119 traceMap.get(resource).closeEditors();
120 }
121
122 try {
123 resource.delete(true, new NullProgressMonitor());
124 } catch (CoreException e) {
125 Activator.getDefault().logError("Error deleting supplementary resource " + resource, e); //$NON-NLS-1$
126 }
127 }
128
129 IResource resource = experiment.getProject().getResource();
130 if (resource != null) {
131 try {
132 resource.refreshLocal(IResource.DEPTH_INFINITE, null);
133 } catch (CoreException e) {
134 Activator.getDefault().logError("Error refreshing resource " + resource, e); //$NON-NLS-1$
135 }
136 }
137 }
138 }
139 return null;
140 }
141 }
This page took 0.035481 seconds and 5 git commands to generate.