Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
c8422608 | 2 | * Copyright (c) 2009, 2013 Ericsson |
abbdd66a | 3 | * |
12c155f5 FC |
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 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
d34665f9 | 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 FC |
14 | |
15 | import java.lang.reflect.InvocationTargetException; | |
16 | import java.util.ArrayList; | |
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.IFile; | |
23 | import org.eclipse.core.resources.IFolder; | |
24 | import org.eclipse.core.resources.IResource; | |
25 | import org.eclipse.core.resources.ResourcesPlugin; | |
26 | import org.eclipse.core.runtime.CoreException; | |
27 | import org.eclipse.core.runtime.IPath; | |
28 | import org.eclipse.core.runtime.IProgressMonitor; | |
29 | import org.eclipse.jface.viewers.ISelection; | |
1595249b | 30 | import org.eclipse.jface.viewers.ISelectionProvider; |
12c155f5 FC |
31 | import org.eclipse.jface.viewers.TreeSelection; |
32 | import org.eclipse.jface.window.Window; | |
8fd82db5 | 33 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
e12ecd30 | 34 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
12c155f5 FC |
35 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
36 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; | |
37 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
38 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
39 | import org.eclipse.linuxtools.tmf.ui.project.wizards.RenameTraceDialog; | |
40 | import org.eclipse.swt.widgets.Shell; | |
41 | import org.eclipse.ui.IWorkbenchPage; | |
42 | import org.eclipse.ui.IWorkbenchPart; | |
43 | import org.eclipse.ui.IWorkbenchWindow; | |
44 | import org.eclipse.ui.PlatformUI; | |
45 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
46 | ||
47 | /** | |
48 | * <b><u>RenameTraceHandler</u></b> | |
49 | * <p> | |
50 | * TODO: Implement me. Please. | |
51 | */ | |
52 | public class RenameTraceHandler extends AbstractHandler { | |
53 | ||
54 | private TmfTraceElement fTrace = null; | |
abbdd66a | 55 | |
12c155f5 FC |
56 | // ------------------------------------------------------------------------ |
57 | // isEnabled | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | @Override | |
61 | public boolean isEnabled() { | |
62 | ||
63 | // Check if we are closing down | |
64 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
abbdd66a | 65 | if (window == null) { |
12c155f5 | 66 | return false; |
abbdd66a | 67 | } |
12c155f5 FC |
68 | |
69 | // Get the selection | |
70 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
71 | IWorkbenchPart part = page.getActivePart(); | |
d5210347 PT |
72 | if (part == null) { |
73 | return false; | |
74 | } | |
1595249b | 75 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
abbdd66a | 76 | if (selectionProvider == null) { |
1595249b | 77 | return false; |
abbdd66a | 78 | } |
1595249b | 79 | ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
80 | |
81 | // Make sure there is only selection and that it is an experiment | |
82 | fTrace = null; | |
83 | if (selection instanceof TreeSelection) { | |
84 | TreeSelection sel = (TreeSelection) selection; | |
85 | // There should be only one item selected as per the plugin.xml | |
86 | Object element = sel.getFirstElement(); | |
87 | if (element instanceof TmfTraceElement) { | |
88 | fTrace = (TmfTraceElement) element; | |
89 | } | |
90 | } | |
91 | ||
828e5592 | 92 | return (fTrace != null); |
12c155f5 FC |
93 | } |
94 | ||
95 | // ------------------------------------------------------------------------ | |
96 | // Execution | |
97 | // ------------------------------------------------------------------------ | |
98 | ||
99 | @Override | |
100 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
101 | ||
102 | // Check if we are closing down | |
103 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
abbdd66a | 104 | if (window == null) { |
12c155f5 | 105 | return null; |
abbdd66a | 106 | } |
12c155f5 | 107 | |
828e5592 | 108 | // If trace is under an experiment, use the original trace from the traces folder |
5e4bf87d | 109 | fTrace = fTrace.getElementUnderTraceFolder(); |
828e5592 | 110 | |
12c155f5 FC |
111 | // Fire the Rename Trace dialog |
112 | Shell shell = window.getShell(); | |
113 | TmfTraceFolder traceFolder = (TmfTraceFolder) fTrace.getParent(); | |
114 | TmfTraceElement oldTrace = fTrace; | |
115 | RenameTraceDialog dialog = new RenameTraceDialog(shell, fTrace); | |
abbdd66a | 116 | if (dialog.open() != Window.OK) { |
12c155f5 | 117 | return null; |
abbdd66a | 118 | } |
12c155f5 FC |
119 | |
120 | // Locate the new trace object | |
121 | TmfTraceElement newTrace = null; | |
122 | String newTraceName = dialog.getNewTraceName(); | |
123 | for (ITmfProjectModelElement element : traceFolder.getChildren()) { | |
124 | if (element instanceof TmfTraceElement) { | |
125 | TmfTraceElement trace = (TmfTraceElement) element; | |
126 | if (trace.getName().equals(newTraceName)) { | |
127 | newTrace = trace; | |
128 | break; | |
129 | } | |
130 | } | |
131 | } | |
abbdd66a | 132 | if (newTrace == null) { |
12c155f5 | 133 | return null; |
abbdd66a | 134 | } |
12c155f5 | 135 | |
507b1336 | 136 | List<WorkspaceModifyOperation> removeOps = new ArrayList<>(); |
12c155f5 FC |
137 | TmfExperimentFolder experimentFolder = newTrace.getProject().getExperimentsFolder(); |
138 | for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
139 | for (final ITmfProjectModelElement trace : experiment.getChildren()) { | |
81fe3479 | 140 | if (trace.getName().equals(oldTrace.getName())) { |
12c155f5 FC |
141 | // Create a link to the renamed trace |
142 | createTraceLink(newTrace, experiment); | |
abbdd66a | 143 | |
12c155f5 FC |
144 | // Queue the removal of the old trace link |
145 | removeOps.add(new WorkspaceModifyOperation() { | |
146 | @Override | |
147 | protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { | |
148 | experiment.removeChild(trace); | |
149 | trace.getResource().delete(true, null); | |
150 | experiment.refresh(); | |
151 | } | |
152 | }); | |
153 | } | |
154 | } | |
155 | } | |
156 | ||
157 | for (WorkspaceModifyOperation operation : removeOps) { | |
158 | try { | |
159 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
160 | } catch (InterruptedException exception) { | |
161 | } catch (InvocationTargetException exception) { | |
162 | } catch (RuntimeException exception) { | |
163 | } | |
164 | } | |
abbdd66a | 165 | |
12c155f5 FC |
166 | return null; |
167 | } | |
168 | ||
abbdd66a AM |
169 | private static void createTraceLink(TmfTraceElement trace, |
170 | final ITmfProjectModelElement experiment) { | |
12c155f5 FC |
171 | try { |
172 | IResource resource = trace.getResource(); | |
173 | IPath location = resource.getLocation(); | |
828e5592 | 174 | // Get the trace properties for this resource |
e12ecd30 BH |
175 | String traceBundle = resource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE); |
176 | String traceTypeId = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE); | |
177 | String traceIcon = resource.getPersistentProperty(TmfCommonConstants.TRACEICON); | |
12c155f5 FC |
178 | if (resource instanceof IFolder) { |
179 | IFolder folder = ((IFolder) experiment.getResource()).getFolder(trace.getName()); | |
180 | if (ResourcesPlugin.getWorkspace().validateLinkLocation(folder, location).isOK()) { | |
181 | folder.createLink(location, IResource.REPLACE, null); | |
e12ecd30 BH |
182 | folder.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle); |
183 | folder.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId); | |
184 | folder.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon); | |
12c155f5 FC |
185 | } |
186 | else { | |
8fd82db5 | 187 | Activator.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location); //$NON-NLS-1$ |
12c155f5 FC |
188 | } |
189 | } | |
190 | else { | |
191 | IFile file = ((IFolder) experiment.getResource()).getFile(trace.getName()); | |
192 | if (ResourcesPlugin.getWorkspace().validateLinkLocation(file, location).isOK()) { | |
193 | file.createLink(location, IResource.REPLACE, null); | |
e12ecd30 BH |
194 | file.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle); |
195 | file.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId); | |
196 | file.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon); | |
12c155f5 FC |
197 | } |
198 | else { | |
8fd82db5 | 199 | Activator.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location); //$NON-NLS-1$ |
12c155f5 FC |
200 | } |
201 | } | |
202 | experiment.refresh(); | |
203 | } catch (CoreException e) { | |
8fd82db5 | 204 | Activator.getDefault().logError("Error renaming trace" + trace.getName(), e); //$NON-NLS-1$ |
12c155f5 FC |
205 | } |
206 | } | |
207 | ||
208 | } |