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