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