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; | |
0f0d17bb | 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 | 101 | // If trace is under an experiment, use the original trace from the traces folder |
5e4bf87d | 102 | fTrace = fTrace.getElementUnderTraceFolder(); |
828e5592 | 103 | |
12c155f5 FC |
104 | // Fire the Rename Trace dialog |
105 | Shell shell = window.getShell(); | |
106 | TmfTraceFolder traceFolder = (TmfTraceFolder) fTrace.getParent(); | |
107 | TmfTraceElement oldTrace = fTrace; | |
108 | RenameTraceDialog dialog = new RenameTraceDialog(shell, fTrace); | |
109 | if (dialog.open() != Window.OK) | |
110 | return null; | |
111 | ||
112 | // Locate the new trace object | |
113 | TmfTraceElement newTrace = null; | |
114 | String newTraceName = dialog.getNewTraceName(); | |
115 | for (ITmfProjectModelElement element : traceFolder.getChildren()) { | |
116 | if (element instanceof TmfTraceElement) { | |
117 | TmfTraceElement trace = (TmfTraceElement) element; | |
118 | if (trace.getName().equals(newTraceName)) { | |
119 | newTrace = trace; | |
120 | break; | |
121 | } | |
122 | } | |
123 | } | |
124 | if (newTrace == null) | |
125 | return null; | |
126 | ||
127 | List<WorkspaceModifyOperation> removeOps = new ArrayList<WorkspaceModifyOperation>(); | |
128 | TmfExperimentFolder experimentFolder = newTrace.getProject().getExperimentsFolder(); | |
129 | for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
130 | for (final ITmfProjectModelElement trace : experiment.getChildren()) { | |
131 | if (trace.equals(oldTrace)) { | |
132 | // Create a link to the renamed trace | |
133 | createTraceLink(newTrace, experiment); | |
134 | ||
135 | // Queue the removal of the old trace link | |
136 | removeOps.add(new WorkspaceModifyOperation() { | |
137 | @Override | |
138 | protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { | |
139 | experiment.removeChild(trace); | |
140 | trace.getResource().delete(true, null); | |
141 | experiment.refresh(); | |
142 | } | |
143 | }); | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | for (WorkspaceModifyOperation operation : removeOps) { | |
149 | try { | |
150 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
151 | } catch (InterruptedException exception) { | |
152 | } catch (InvocationTargetException exception) { | |
153 | } catch (RuntimeException exception) { | |
154 | } | |
155 | } | |
156 | ||
157 | return null; | |
158 | } | |
159 | ||
160 | private void createTraceLink(TmfTraceElement trace, final ITmfProjectModelElement experiment) { | |
161 | try { | |
162 | IResource resource = trace.getResource(); | |
163 | IPath location = resource.getLocation(); | |
828e5592 | 164 | // Get the trace properties for this resource |
0f0d17bb AM |
165 | String traceBundle = resource.getPersistentProperty(TmfTraceElement.TRACEBUNDLE); |
166 | String traceTypeId = resource.getPersistentProperty(TmfTraceElement.TRACETYPE); | |
167 | String traceIcon = resource.getPersistentProperty(TmfTraceElement.TRACEICON); | |
12c155f5 FC |
168 | if (resource instanceof IFolder) { |
169 | IFolder folder = ((IFolder) experiment.getResource()).getFolder(trace.getName()); | |
170 | if (ResourcesPlugin.getWorkspace().validateLinkLocation(folder, location).isOK()) { | |
171 | folder.createLink(location, IResource.REPLACE, null); | |
0f0d17bb AM |
172 | folder.setPersistentProperty(TmfTraceElement.TRACEBUNDLE, traceBundle); |
173 | folder.setPersistentProperty(TmfTraceElement.TRACETYPE, traceTypeId); | |
174 | folder.setPersistentProperty(TmfTraceElement.TRACEICON, traceIcon); | |
12c155f5 FC |
175 | } |
176 | else { | |
177 | System.out.println("Invalid Trace Location"); //$NON-NLS-1$ | |
178 | } | |
179 | } | |
180 | else { | |
181 | IFile file = ((IFolder) experiment.getResource()).getFile(trace.getName()); | |
182 | if (ResourcesPlugin.getWorkspace().validateLinkLocation(file, location).isOK()) { | |
183 | file.createLink(location, IResource.REPLACE, null); | |
0f0d17bb AM |
184 | file.setPersistentProperty(TmfTraceElement.TRACEBUNDLE, traceBundle); |
185 | file.setPersistentProperty(TmfTraceElement.TRACETYPE, traceTypeId); | |
186 | file.setPersistentProperty(TmfTraceElement.TRACEICON, traceIcon); | |
12c155f5 FC |
187 | } |
188 | else { | |
189 | System.out.println("Invalid Trace Location"); //$NON-NLS-1$ | |
190 | } | |
191 | } | |
192 | experiment.refresh(); | |
193 | } catch (CoreException e) { | |
194 | e.printStackTrace(); | |
195 | } | |
196 | } | |
197 | ||
198 | } |