Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010, 2011 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(); | |
1595249b | 72 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
abbdd66a | 73 | if (selectionProvider == null) { |
1595249b | 74 | return false; |
abbdd66a | 75 | } |
1595249b | 76 | ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
77 | |
78 | // Make sure there is only selection and that it is an experiment | |
79 | fTrace = null; | |
80 | if (selection instanceof TreeSelection) { | |
81 | TreeSelection sel = (TreeSelection) selection; | |
82 | // There should be only one item selected as per the plugin.xml | |
83 | Object element = sel.getFirstElement(); | |
84 | if (element instanceof TmfTraceElement) { | |
85 | fTrace = (TmfTraceElement) element; | |
86 | } | |
87 | } | |
88 | ||
828e5592 | 89 | return (fTrace != null); |
12c155f5 FC |
90 | } |
91 | ||
92 | // ------------------------------------------------------------------------ | |
93 | // Execution | |
94 | // ------------------------------------------------------------------------ | |
95 | ||
96 | @Override | |
97 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
98 | ||
99 | // Check if we are closing down | |
100 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
abbdd66a | 101 | if (window == null) { |
12c155f5 | 102 | return null; |
abbdd66a | 103 | } |
12c155f5 | 104 | |
828e5592 | 105 | // If trace is under an experiment, use the original trace from the traces folder |
5e4bf87d | 106 | fTrace = fTrace.getElementUnderTraceFolder(); |
828e5592 | 107 | |
12c155f5 FC |
108 | // Fire the Rename Trace dialog |
109 | Shell shell = window.getShell(); | |
110 | TmfTraceFolder traceFolder = (TmfTraceFolder) fTrace.getParent(); | |
111 | TmfTraceElement oldTrace = fTrace; | |
112 | RenameTraceDialog dialog = new RenameTraceDialog(shell, fTrace); | |
abbdd66a | 113 | if (dialog.open() != Window.OK) { |
12c155f5 | 114 | return null; |
abbdd66a | 115 | } |
12c155f5 FC |
116 | |
117 | // Locate the new trace object | |
118 | TmfTraceElement newTrace = null; | |
119 | String newTraceName = dialog.getNewTraceName(); | |
120 | for (ITmfProjectModelElement element : traceFolder.getChildren()) { | |
121 | if (element instanceof TmfTraceElement) { | |
122 | TmfTraceElement trace = (TmfTraceElement) element; | |
123 | if (trace.getName().equals(newTraceName)) { | |
124 | newTrace = trace; | |
125 | break; | |
126 | } | |
127 | } | |
128 | } | |
abbdd66a | 129 | if (newTrace == null) { |
12c155f5 | 130 | return null; |
abbdd66a | 131 | } |
12c155f5 FC |
132 | |
133 | List<WorkspaceModifyOperation> removeOps = new ArrayList<WorkspaceModifyOperation>(); | |
134 | TmfExperimentFolder experimentFolder = newTrace.getProject().getExperimentsFolder(); | |
135 | for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
136 | for (final ITmfProjectModelElement trace : experiment.getChildren()) { | |
137 | if (trace.equals(oldTrace)) { | |
138 | // Create a link to the renamed trace | |
139 | createTraceLink(newTrace, experiment); | |
abbdd66a | 140 | |
12c155f5 FC |
141 | // Queue the removal of the old trace link |
142 | removeOps.add(new WorkspaceModifyOperation() { | |
143 | @Override | |
144 | protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { | |
145 | experiment.removeChild(trace); | |
146 | trace.getResource().delete(true, null); | |
147 | experiment.refresh(); | |
148 | } | |
149 | }); | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
154 | for (WorkspaceModifyOperation operation : removeOps) { | |
155 | try { | |
156 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
157 | } catch (InterruptedException exception) { | |
158 | } catch (InvocationTargetException exception) { | |
159 | } catch (RuntimeException exception) { | |
160 | } | |
161 | } | |
abbdd66a | 162 | |
12c155f5 FC |
163 | return null; |
164 | } | |
165 | ||
abbdd66a AM |
166 | private static void createTraceLink(TmfTraceElement trace, |
167 | final ITmfProjectModelElement experiment) { | |
12c155f5 FC |
168 | try { |
169 | IResource resource = trace.getResource(); | |
170 | IPath location = resource.getLocation(); | |
828e5592 | 171 | // Get the trace properties for this resource |
e12ecd30 BH |
172 | String traceBundle = resource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE); |
173 | String traceTypeId = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE); | |
174 | String traceIcon = resource.getPersistentProperty(TmfCommonConstants.TRACEICON); | |
12c155f5 FC |
175 | if (resource instanceof IFolder) { |
176 | IFolder folder = ((IFolder) experiment.getResource()).getFolder(trace.getName()); | |
177 | if (ResourcesPlugin.getWorkspace().validateLinkLocation(folder, location).isOK()) { | |
178 | folder.createLink(location, IResource.REPLACE, null); | |
e12ecd30 BH |
179 | folder.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle); |
180 | folder.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId); | |
181 | folder.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon); | |
12c155f5 FC |
182 | } |
183 | else { | |
8fd82db5 | 184 | Activator.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location); //$NON-NLS-1$ |
12c155f5 FC |
185 | } |
186 | } | |
187 | else { | |
188 | IFile file = ((IFolder) experiment.getResource()).getFile(trace.getName()); | |
189 | if (ResourcesPlugin.getWorkspace().validateLinkLocation(file, location).isOK()) { | |
190 | file.createLink(location, IResource.REPLACE, null); | |
e12ecd30 BH |
191 | file.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle); |
192 | file.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId); | |
193 | file.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon); | |
12c155f5 FC |
194 | } |
195 | else { | |
8fd82db5 | 196 | Activator.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location); //$NON-NLS-1$ |
12c155f5 FC |
197 | } |
198 | } | |
199 | experiment.refresh(); | |
200 | } catch (CoreException e) { | |
8fd82db5 | 201 | Activator.getDefault().logError("Error renaming trace" + trace.getName(), e); //$NON-NLS-1$ |
12c155f5 FC |
202 | } |
203 | } | |
204 | ||
205 | } |