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