Commit | Line | Data |
---|---|---|
5e4bf87d | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 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: |
a72a6830 PT |
10 | * Bernd Hufmann - Initial API and implementation |
11 | * Patrick Tasse - Close editors to release resources | |
5e4bf87d BH |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; | |
15 | ||
0245c524 | 16 | import java.lang.reflect.InvocationTargetException; |
099a269e PT |
17 | import java.util.ArrayList; |
18 | import java.util.Arrays; | |
19 | import java.util.Comparator; | |
20 | import java.util.HashSet; | |
21 | import java.util.Iterator; | |
22 | import java.util.List; | |
23 | import java.util.Set; | |
24 | ||
5e4bf87d BH |
25 | import org.eclipse.core.commands.AbstractHandler; |
26 | import org.eclipse.core.commands.ExecutionEvent; | |
27 | import org.eclipse.core.commands.ExecutionException; | |
099a269e | 28 | import org.eclipse.core.resources.IProject; |
5e4bf87d BH |
29 | import org.eclipse.core.resources.IResource; |
30 | import org.eclipse.core.runtime.CoreException; | |
0245c524 BH |
31 | import org.eclipse.core.runtime.IProgressMonitor; |
32 | import org.eclipse.core.runtime.OperationCanceledException; | |
33 | import org.eclipse.core.runtime.SubMonitor; | |
34 | import org.eclipse.jface.dialogs.MessageDialog; | |
5e4bf87d | 35 | import org.eclipse.jface.viewers.ISelection; |
099a269e | 36 | import org.eclipse.jface.viewers.IStructuredSelection; |
e12ecd30 | 37 | import org.eclipse.jface.window.Window; |
8fd82db5 | 38 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
e12ecd30 | 39 | import org.eclipse.linuxtools.internal.tmf.ui.project.dialogs.SelectSupplementaryResourcesDialog; |
0245c524 | 40 | import org.eclipse.linuxtools.internal.tmf.ui.project.operations.TmfWorkspaceModifyOperation; |
099a269e PT |
41 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfCommonProjectElement; |
42 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; | |
5e4bf87d | 43 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
0245c524 | 44 | import org.eclipse.osgi.util.NLS; |
6b44794a | 45 | import org.eclipse.swt.widgets.Display; |
5e4bf87d BH |
46 | import org.eclipse.ui.IWorkbenchWindow; |
47 | import org.eclipse.ui.PlatformUI; | |
099a269e PT |
48 | import org.eclipse.ui.handlers.HandlerUtil; |
49 | ||
50 | import com.google.common.collect.Multimap; | |
51 | import com.google.common.collect.TreeMultimap; | |
5e4bf87d BH |
52 | |
53 | /** | |
a72a6830 | 54 | * Handler for Delete Supplementary Files command on trace |
5e4bf87d BH |
55 | */ |
56 | public class DeleteTraceSupplementaryFilesHandler extends AbstractHandler { | |
57 | ||
099a269e PT |
58 | // ------------------------------------------------------------------------ |
59 | // Inner classes | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
62 | private class ElementComparator implements Comparator<TmfCommonProjectElement> { | |
63 | @Override | |
64 | public int compare(TmfCommonProjectElement e1, TmfCommonProjectElement e2) { | |
65 | return e1.getPath().toString().compareTo(e2.getPath().toString()); | |
66 | } | |
67 | } | |
68 | ||
69 | private class ResourceComparator implements Comparator<IResource> { | |
70 | @Override | |
71 | public int compare(IResource r1, IResource r2) { | |
72 | return r1.getFullPath().toString().compareTo(r2.getFullPath().toString()); | |
73 | } | |
74 | } | |
75 | ||
5e4bf87d BH |
76 | // ------------------------------------------------------------------------ |
77 | // Execution | |
78 | // ------------------------------------------------------------------------ | |
79 | ||
80 | @Override | |
81 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
82 | ||
83 | // Check if we are closing down | |
84 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
abbdd66a | 85 | if (window == null) { |
5e4bf87d | 86 | return null; |
abbdd66a | 87 | } |
5e4bf87d BH |
88 | |
89 | // Get the selection | |
099a269e PT |
90 | ISelection selection = HandlerUtil.getCurrentSelection(event); |
91 | if (!(selection instanceof IStructuredSelection)) { | |
92 | return null; | |
5e4bf87d | 93 | } |
099a269e PT |
94 | final Multimap<TmfCommonProjectElement, IResource> resourceMap = |
95 | TreeMultimap.create(new ElementComparator(), new ResourceComparator()); | |
96 | final Iterator<Object> iterator = ((IStructuredSelection) selection).iterator(); | |
e12ecd30 | 97 | |
099a269e PT |
98 | while (iterator.hasNext()) { |
99 | Object element = iterator.next(); | |
5e4bf87d BH |
100 | if (element instanceof TmfTraceElement) { |
101 | TmfTraceElement trace = (TmfTraceElement) element; | |
102 | // If trace is under an experiment, use the original trace from the traces folder | |
103 | trace = trace.getElementUnderTraceFolder(); | |
099a269e PT |
104 | for (IResource resource : trace.getSupplementaryResources()) { |
105 | resourceMap.put(trace, resource); | |
106 | } | |
e12ecd30 | 107 | |
099a269e PT |
108 | } else if (element instanceof TmfExperimentElement) { |
109 | TmfExperimentElement experiment = (TmfExperimentElement) element; | |
110 | for (IResource resource : experiment.getSupplementaryResources()) { | |
111 | resourceMap.put(experiment, resource); | |
112 | } | |
113 | for (TmfTraceElement trace : experiment.getTraces()) { | |
114 | // If trace is under an experiment, use the original trace from the traces folder | |
115 | trace = trace.getElementUnderTraceFolder(); | |
116 | for (IResource resource : trace.getSupplementaryResources()) { | |
117 | resourceMap.put(trace, resource); | |
118 | } | |
e12ecd30 | 119 | } |
099a269e PT |
120 | } |
121 | } | |
122 | ||
0245c524 | 123 | final SelectSupplementaryResourcesDialog dialog = |
7daa4edf | 124 | new SelectSupplementaryResourcesDialog(window.getShell(), resourceMap); |
099a269e PT |
125 | if (dialog.open() != Window.OK) { |
126 | return null; | |
127 | } | |
e12ecd30 | 128 | |
0245c524 BH |
129 | TmfWorkspaceModifyOperation operation = new TmfWorkspaceModifyOperation() { |
130 | @Override | |
131 | public void execute(IProgressMonitor monitor) throws CoreException { | |
e12ecd30 | 132 | |
0245c524 BH |
133 | Set<IProject> projectsToRefresh = new HashSet<>(); |
134 | ||
135 | // Delete the resources that were selected | |
136 | List<IResource> allResourcesToDelete = Arrays.asList(dialog.getResources()); | |
137 | ||
138 | SubMonitor subMonitor = SubMonitor.convert(monitor, allResourcesToDelete.size()); | |
139 | ||
6b44794a | 140 | for (final TmfCommonProjectElement element : resourceMap.keySet()) { |
0245c524 BH |
141 | if (monitor.isCanceled()) { |
142 | throw new OperationCanceledException(); | |
143 | } | |
144 | List<IResource> traceResourcesToDelete = new ArrayList<>(resourceMap.get(element)); | |
145 | traceResourcesToDelete.retainAll(allResourcesToDelete); | |
146 | if (!traceResourcesToDelete.isEmpty()) { | |
147 | subMonitor.setTaskName(NLS.bind(Messages.DeleteSupplementaryFiles_DeletionTask, element.getElementPath())); | |
148 | // Delete the selected resources | |
6b44794a MK |
149 | Display.getDefault().syncExec(new Runnable() { |
150 | @Override | |
151 | public void run() { | |
152 | element.closeEditors(); | |
153 | } | |
154 | }); | |
0245c524 BH |
155 | element.deleteSupplementaryResources(traceResourcesToDelete.toArray(new IResource[0])); |
156 | projectsToRefresh.add(element.getProject().getResource()); | |
157 | } | |
158 | subMonitor.worked(traceResourcesToDelete.size()); | |
159 | } | |
160 | ||
161 | subMonitor = SubMonitor.convert(monitor, projectsToRefresh.size()); | |
5e4bf87d | 162 | |
0245c524 BH |
163 | // Refresh projects |
164 | Iterator<IProject> projectIterator = projectsToRefresh.iterator(); | |
165 | while (projectIterator.hasNext()) { | |
166 | if (monitor.isCanceled()) { | |
167 | throw new OperationCanceledException(); | |
168 | } | |
169 | IProject project = projectIterator.next(); | |
170 | subMonitor.setTaskName(NLS.bind(Messages.DeleteSupplementaryFiles_ProjectRefreshTask, project.getName())); | |
171 | try { | |
172 | project.refreshLocal(IResource.DEPTH_INFINITE, null); | |
173 | } catch (CoreException e) { | |
174 | Activator.getDefault().logError("Error refreshing project " + project, e); //$NON-NLS-1$ | |
175 | } | |
176 | subMonitor.worked(1); | |
177 | } | |
178 | } | |
179 | }; | |
180 | ||
181 | try { | |
182 | PlatformUI.getWorkbench().getProgressService().run(true, true, operation); | |
183 | } catch (InterruptedException e) { | |
184 | return null; | |
185 | } catch (InvocationTargetException e) { | |
186 | MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString()); | |
187 | return null; | |
188 | } | |
5e4bf87d BH |
189 | return null; |
190 | } | |
191 | ||
192 | } |