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