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