Use supplementary directory for state history tree
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / RenameTraceHandler.java
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.internal.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;
30 import org.eclipse.jface.viewers.ISelectionProvider;
31 import org.eclipse.jface.viewers.TreeSelection;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
34 import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement;
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();
70 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
71 if (selectionProvider == null)
72 return false;
73 ISelection selection = selectionProvider.getSelection();
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
86 return (fTrace != null);
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 // If trace is under an experiment, use the original trace from the traces folder
102 fTrace = fTrace.getElementUnderTraceFolder();
103
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();
164 // Get the trace properties for this resource
165 String traceBundle = resource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE);
166 String traceTypeId = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
167 String traceIcon = resource.getPersistentProperty(TmfCommonConstants.TRACEICON);
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);
172 folder.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
173 folder.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
174 folder.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
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);
184 file.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
185 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
186 file.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
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 }
This page took 0.03539 seconds and 6 git commands to generate.