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