1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 * Patrick Tasse - Close editors to release resources
12 * Marc-Andre Laperle - Fix NPE (Bug 416574)
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Arrays
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
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
;
42 * Handler for Delete Supplementary Files command on experiment
44 public class DeleteExperimentSupplementaryFilesHandler
extends AbstractHandler
{
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
53 // Check if we are closing down
54 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
60 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
61 IWorkbenchPart part
= page
.getActivePart();
65 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
66 if (selectionProvider
== null) {
70 ISelection selection
= selectionProvider
.getSelection();
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
<>();
79 if (element
instanceof TmfExperimentElement
) {
81 TmfExperimentElement experiment
= (TmfExperimentElement
) element
;
83 IResource
[] resources
= experiment
.getSupplementaryResources();
84 // List to know which resources belong to the experiment
85 List
<IResource
> experimentResources
= Arrays
.asList(resources
);
87 resourcesList
.addAll(experimentResources
);
89 // Map to know which trace to close for each resource
90 HashMap
<IResource
, TmfTraceElement
> traceMap
= new HashMap
<>();
92 for (TmfTraceElement aTrace
: experiment
.getTraces()) {
94 // If trace is under an experiment, use the original trace from the traces folder
95 aTrace
= aTrace
.getElementUnderTraceFolder();
97 // Add the trace supplementary resources for the dialog
98 resources
= aTrace
.getSupplementaryResources();
99 resourcesList
.addAll(Arrays
.asList(resources
));
101 for (IResource resource
: resources
) {
102 traceMap
.put(resource
, aTrace
);
106 SelectSupplementaryResourcesDialog dialog
= new SelectSupplementaryResourcesDialog(window
.getShell(), resourcesList
.toArray(new IResource
[resourcesList
.size()]));
108 if (dialog
.open() != Window
.OK
) {
112 IResource
[] resourcesToDelete
= dialog
.getResources();
114 // Delete the selected resources
115 for (IResource resource
: resourcesToDelete
) {
116 if (experimentResources
.contains(resource
)) {
117 experiment
.closeEditors();
119 traceMap
.get(resource
).closeEditors();
123 resource
.delete(true, new NullProgressMonitor());
124 } catch (CoreException e
) {
125 Activator
.getDefault().logError("Error deleting supplementary resource " + resource
, e
); //$NON-NLS-1$
129 IResource resource
= experiment
.getProject().getResource();
130 if (resource
!= null) {
132 resource
.refreshLocal(IResource
.DEPTH_INFINITE
, null);
133 } catch (CoreException e
) {
134 Activator
.getDefault().logError("Error refreshing resource " + resource
, e
); //$NON-NLS-1$