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