Merge branch 'master'
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteExperimentSupplementaryFilesHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011 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.internal.tmf.ui.project.handlers;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18
19 import org.eclipse.core.commands.AbstractHandler;
20 import org.eclipse.core.commands.ExecutionEvent;
21 import org.eclipse.core.commands.ExecutionException;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.TreeSelection;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
32 import org.eclipse.linuxtools.internal.tmf.ui.project.dialogs.SelectSupplementaryResourcesDialog;
33 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
34 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
35 import org.eclipse.ui.IWorkbenchPage;
36 import org.eclipse.ui.IWorkbenchPart;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.PlatformUI;
39
40 /**
41 * <b><u>DeleteExperimentSupplementaryFilesHandler</u></b>
42 * <p>
43 * TODO: Implement me. Please.
44 */
45 public class DeleteExperimentSupplementaryFilesHandler extends AbstractHandler {
46
47 // ------------------------------------------------------------------------
48 // Execution
49 // ------------------------------------------------------------------------
50
51 @Override
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53
54 // Check if we are closing down
55 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
56 if (window == null) {
57 return null;
58 }
59
60 // Get the selection
61 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
62 IWorkbenchPart part = page.getActivePart();
63 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
64 if (selectionProvider == null) {
65 return false;
66 }
67
68 ISelection selection = selectionProvider.getSelection();
69
70 // Make sure there is only selection and that it is an experiment
71 if (selection instanceof TreeSelection) {
72 TreeSelection sel = (TreeSelection) selection;
73 // There should be only one item selected as per the plugin.xml
74 Object element = sel.getFirstElement();
75 List<IResource> resourcesList = new ArrayList<IResource>();
76
77 if (element instanceof TmfExperimentElement) {
78
79 TmfExperimentElement trace = (TmfExperimentElement) element;
80
81 for (TmfTraceElement aTrace : trace.getTraces()) {
82
83 // If trace is under an experiment, use the original trace from the traces folder
84 aTrace = aTrace.getElementUnderTraceFolder();
85
86 // Delete the selected resources
87 IResource[] resources = aTrace.getSupplementaryResources();
88 resourcesList.addAll(Arrays.asList(resources));
89 }
90
91 SelectSupplementaryResourcesDialog dialog = new SelectSupplementaryResourcesDialog(window.getShell(), resourcesList.toArray(new IResource[resourcesList.size()]));
92
93 if (dialog.open() != Window.OK) {
94 return null;
95 }
96
97 IResource[] resourcesToDelete = dialog.getResources();
98
99 for (int i = 0; i < resourcesToDelete.length; i++) {
100 try {
101 resourcesToDelete[i].delete(true, new NullProgressMonitor());
102 } catch (CoreException e) {
103 TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error deleting supplementary resource " + resourcesToDelete[i], e)); //$NON-NLS-1$
104 }
105 }
106
107 IResource resource = trace.getProject().getResource();
108 if (resource != null) {
109 try {
110 if (resource != null) {
111 resource.refreshLocal(IResource.DEPTH_INFINITE, null);
112 }
113 } catch (CoreException e) {
114 TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error refreshing resource " + resource, e)); //$NON-NLS-1$
115 }
116 }
117 }
118 }
119 return null;
120 }
121 }
This page took 0.033574 seconds and 6 git commands to generate.