Internalize some classes and fix a pile of 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, 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 // Get the selection
69 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
70 IWorkbenchPart part = page.getActivePart();
71 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
72 if (selectionProvider == null)
73 return false;
74 ISelection selection = selectionProvider.getSelection();
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
87 return (fTrace != null);
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
102 // If trace is under an experiment, use the original trace from the traces folder
103 fTrace = fTrace.getElementUnderTraceFolder();
104
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();
165 // Get the trace properties for this resource
166 String traceBundle = resource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE);
167 String traceTypeId = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
168 String traceIcon = resource.getPersistentProperty(TmfCommonConstants.TRACEICON);
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);
173 folder.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
174 folder.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
175 folder.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
176 }
177 else {
178 Activator.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location); //$NON-NLS-1$
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);
185 file.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
186 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
187 file.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
188 }
189 else {
190 Activator.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location); //$NON-NLS-1$
191 }
192 }
193 experiment.refresh();
194 } catch (CoreException e) {
195 Activator.getDefault().logError("Error renaming trace" + trace.getName(), e); //$NON-NLS-1$
196 }
197 }
198
199 }
This page took 0.034887 seconds and 5 git commands to generate.