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 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.handlers; | |
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; | |
34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; | |
35 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
36 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
37 | import org.eclipse.linuxtools.tmf.ui.project.wizards.RenameTraceDialog; | |
38 | import org.eclipse.swt.widgets.Shell; | |
39 | import org.eclipse.ui.IWorkbenchPage; | |
40 | import org.eclipse.ui.IWorkbenchPart; | |
41 | import org.eclipse.ui.IWorkbenchWindow; | |
42 | import org.eclipse.ui.PlatformUI; | |
43 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
44 | ||
45 | /** | |
46 | * <b><u>RenameTraceHandler</u></b> | |
47 | * <p> | |
48 | * TODO: Implement me. Please. | |
49 | */ | |
50 | public class RenameTraceHandler extends AbstractHandler { | |
51 | ||
52 | private TmfTraceElement fTrace = null; | |
53 | ||
54 | // ------------------------------------------------------------------------ | |
55 | // isEnabled | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
58 | @Override | |
59 | public boolean isEnabled() { | |
60 | ||
61 | // Check if we are closing down | |
62 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
63 | if (window == null) | |
64 | return false; | |
65 | ||
66 | // Get the selection | |
67 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
68 | IWorkbenchPart part = page.getActivePart(); | |
1595249b FC |
69 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
70 | if (selectionProvider == null) | |
71 | return false; | |
72 | ISelection selection = selectionProvider.getSelection(); | |
12c155f5 FC |
73 | |
74 | // Make sure there is only selection and that it is an experiment | |
75 | fTrace = null; | |
76 | if (selection instanceof TreeSelection) { | |
77 | TreeSelection sel = (TreeSelection) selection; | |
78 | // There should be only one item selected as per the plugin.xml | |
79 | Object element = sel.getFirstElement(); | |
80 | if (element instanceof TmfTraceElement) { | |
81 | fTrace = (TmfTraceElement) element; | |
82 | } | |
83 | } | |
84 | ||
85 | // We only enable opening from the Traces folder for now | |
86 | return (fTrace != null && fTrace.getParent() instanceof TmfTraceFolder); | |
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 | ||
101 | // Fire the Rename Trace dialog | |
102 | Shell shell = window.getShell(); | |
103 | TmfTraceFolder traceFolder = (TmfTraceFolder) fTrace.getParent(); | |
104 | TmfTraceElement oldTrace = fTrace; | |
105 | RenameTraceDialog dialog = new RenameTraceDialog(shell, fTrace); | |
106 | if (dialog.open() != Window.OK) | |
107 | return null; | |
108 | ||
109 | // Locate the new trace object | |
110 | TmfTraceElement newTrace = null; | |
111 | String newTraceName = dialog.getNewTraceName(); | |
112 | for (ITmfProjectModelElement element : traceFolder.getChildren()) { | |
113 | if (element instanceof TmfTraceElement) { | |
114 | TmfTraceElement trace = (TmfTraceElement) element; | |
115 | if (trace.getName().equals(newTraceName)) { | |
116 | newTrace = trace; | |
117 | break; | |
118 | } | |
119 | } | |
120 | } | |
121 | if (newTrace == null) | |
122 | return null; | |
123 | ||
124 | List<WorkspaceModifyOperation> removeOps = new ArrayList<WorkspaceModifyOperation>(); | |
125 | TmfExperimentFolder experimentFolder = newTrace.getProject().getExperimentsFolder(); | |
126 | for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
127 | for (final ITmfProjectModelElement trace : experiment.getChildren()) { | |
128 | if (trace.equals(oldTrace)) { | |
129 | // Create a link to the renamed trace | |
130 | createTraceLink(newTrace, experiment); | |
131 | ||
132 | // Queue the removal of the old trace link | |
133 | removeOps.add(new WorkspaceModifyOperation() { | |
134 | @Override | |
135 | protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { | |
136 | experiment.removeChild(trace); | |
137 | trace.getResource().delete(true, null); | |
138 | experiment.refresh(); | |
139 | } | |
140 | }); | |
141 | } | |
142 | } | |
143 | } | |
144 | ||
145 | for (WorkspaceModifyOperation operation : removeOps) { | |
146 | try { | |
147 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
148 | } catch (InterruptedException exception) { | |
149 | } catch (InvocationTargetException exception) { | |
150 | } catch (RuntimeException exception) { | |
151 | } | |
152 | } | |
153 | ||
154 | return null; | |
155 | } | |
156 | ||
157 | private void createTraceLink(TmfTraceElement trace, final ITmfProjectModelElement experiment) { | |
158 | try { | |
159 | IResource resource = trace.getResource(); | |
160 | IPath location = resource.getLocation(); | |
161 | if (resource instanceof IFolder) { | |
162 | IFolder folder = ((IFolder) experiment.getResource()).getFolder(trace.getName()); | |
163 | if (ResourcesPlugin.getWorkspace().validateLinkLocation(folder, location).isOK()) { | |
164 | folder.createLink(location, IResource.REPLACE, null); | |
165 | } | |
166 | else { | |
167 | System.out.println("Invalid Trace Location"); //$NON-NLS-1$ | |
168 | } | |
169 | } | |
170 | else { | |
171 | IFile file = ((IFolder) experiment.getResource()).getFile(trace.getName()); | |
172 | if (ResourcesPlugin.getWorkspace().validateLinkLocation(file, location).isOK()) { | |
173 | file.createLink(location, IResource.REPLACE, null); | |
174 | } | |
175 | else { | |
176 | System.out.println("Invalid Trace Location"); //$NON-NLS-1$ | |
177 | } | |
178 | } | |
179 | experiment.refresh(); | |
180 | } catch (CoreException e) { | |
181 | e.printStackTrace(); | |
182 | } | |
183 | } | |
184 | ||
185 | } |