Commit | Line | Data |
---|---|---|
12c155f5 FC |
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 | ||
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; | |
33 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; | |
828e5592 | 34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
12c155f5 FC |
35 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; |
36 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
37 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
38 | import org.eclipse.linuxtools.tmf.ui.project.wizards.RenameTraceDialog; | |
39 | import org.eclipse.swt.widgets.Shell; | |
40 | import org.eclipse.ui.IWorkbenchPage; | |
41 | import org.eclipse.ui.IWorkbenchPart; | |
42 | import org.eclipse.ui.IWorkbenchWindow; | |
43 | import org.eclipse.ui.PlatformUI; | |
44 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
45 | ||
46 | /** | |
47 | * <b><u>RenameTraceHandler</u></b> | |
48 | * <p> | |
49 | * TODO: Implement me. Please. | |
50 | */ | |
51 | public class RenameTraceHandler extends AbstractHandler { | |
52 | ||
53 | private TmfTraceElement fTrace = null; | |
54 | ||
55 | // ------------------------------------------------------------------------ | |
56 | // isEnabled | |
57 | // ------------------------------------------------------------------------ | |
58 | ||
59 | @Override | |
60 | public boolean isEnabled() { | |
61 | ||
62 | // Check if we are closing down | |
63 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
64 | if (window == null) | |
65 | return false; | |
66 | ||
67 | // Get the selection | |
68 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
69 | IWorkbenchPart part = page.getActivePart(); | |
1595249b FC |
70 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
71 | if (selectionProvider == null) | |
72 | return false; | |
73 | ISelection selection = selectionProvider.getSelection(); | |
12c155f5 FC |
74 | |
75 | // Make sure there is only selection and that it is an experiment | |
76 | fTrace = null; | |
77 | if (selection instanceof TreeSelection) { | |
78 | TreeSelection sel = (TreeSelection) selection; | |
79 | // There should be only one item selected as per the plugin.xml | |
80 | Object element = sel.getFirstElement(); | |
81 | if (element instanceof TmfTraceElement) { | |
82 | fTrace = (TmfTraceElement) element; | |
83 | } | |
84 | } | |
85 | ||
828e5592 | 86 | return (fTrace != null); |
12c155f5 FC |
87 | } |
88 | ||
89 | // ------------------------------------------------------------------------ | |
90 | // Execution | |
91 | // ------------------------------------------------------------------------ | |
92 | ||
93 | @Override | |
94 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
95 | ||
96 | // Check if we are closing down | |
97 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
98 | if (window == null) | |
99 | return null; | |
100 | ||
828e5592 PT |
101 | // If trace is under an experiment, use the original trace from the traces folder |
102 | if (fTrace.getParent() instanceof TmfExperimentElement) { | |
103 | for (TmfTraceElement trace : fTrace.getProject().getTracesFolder().getTraces()) { | |
104 | if (trace.getName().equals(fTrace.getName())) { | |
105 | fTrace = trace; | |
106 | break; | |
107 | } | |
108 | } | |
109 | } | |
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); | |
116 | if (dialog.open() != Window.OK) | |
117 | return null; | |
118 | ||
119 | // Locate the new trace object | |
120 | TmfTraceElement newTrace = null; | |
121 | String newTraceName = dialog.getNewTraceName(); | |
122 | for (ITmfProjectModelElement element : traceFolder.getChildren()) { | |
123 | if (element instanceof TmfTraceElement) { | |
124 | TmfTraceElement trace = (TmfTraceElement) element; | |
125 | if (trace.getName().equals(newTraceName)) { | |
126 | newTrace = trace; | |
127 | break; | |
128 | } | |
129 | } | |
130 | } | |
131 | if (newTrace == null) | |
132 | return null; | |
133 | ||
134 | List<WorkspaceModifyOperation> removeOps = new ArrayList<WorkspaceModifyOperation>(); | |
135 | TmfExperimentFolder experimentFolder = newTrace.getProject().getExperimentsFolder(); | |
136 | for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
137 | for (final ITmfProjectModelElement trace : experiment.getChildren()) { | |
138 | if (trace.equals(oldTrace)) { | |
139 | // Create a link to the renamed trace | |
140 | createTraceLink(newTrace, experiment); | |
141 | ||
142 | // Queue the removal of the old trace link | |
143 | removeOps.add(new WorkspaceModifyOperation() { | |
144 | @Override | |
145 | protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { | |
146 | experiment.removeChild(trace); | |
147 | trace.getResource().delete(true, null); | |
148 | experiment.refresh(); | |
149 | } | |
150 | }); | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
155 | for (WorkspaceModifyOperation operation : removeOps) { | |
156 | try { | |
157 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
158 | } catch (InterruptedException exception) { | |
159 | } catch (InvocationTargetException exception) { | |
160 | } catch (RuntimeException exception) { | |
161 | } | |
162 | } | |
163 | ||
164 | return null; | |
165 | } | |
166 | ||
167 | private void createTraceLink(TmfTraceElement trace, final ITmfProjectModelElement experiment) { | |
168 | try { | |
169 | IResource resource = trace.getResource(); | |
170 | IPath location = resource.getLocation(); | |
828e5592 PT |
171 | // Get the trace properties for this resource |
172 | String traceBundle = resource.getPersistentProperty(TmfTraceElement.TRACEBUNDLE); | |
173 | String traceTypeId = resource.getPersistentProperty(TmfTraceElement.TRACETYPE); | |
174 | String traceIcon = resource.getPersistentProperty(TmfTraceElement.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); | |
828e5592 PT |
179 | folder.setPersistentProperty(TmfTraceElement.TRACEBUNDLE, traceBundle); |
180 | folder.setPersistentProperty(TmfTraceElement.TRACETYPE, traceTypeId); | |
181 | folder.setPersistentProperty(TmfTraceElement.TRACEICON, traceIcon); | |
12c155f5 FC |
182 | } |
183 | else { | |
184 | System.out.println("Invalid Trace Location"); //$NON-NLS-1$ | |
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); | |
828e5592 PT |
191 | file.setPersistentProperty(TmfTraceElement.TRACEBUNDLE, traceBundle); |
192 | file.setPersistentProperty(TmfTraceElement.TRACETYPE, traceTypeId); | |
193 | file.setPersistentProperty(TmfTraceElement.TRACEICON, traceIcon); | |
12c155f5 FC |
194 | } |
195 | else { | |
196 | System.out.println("Invalid Trace Location"); //$NON-NLS-1$ | |
197 | } | |
198 | } | |
199 | experiment.refresh(); | |
200 | } catch (CoreException e) { | |
201 | e.printStackTrace(); | |
202 | } | |
203 | } | |
204 | ||
205 | } |