1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
15 import java
.lang
.reflect
.InvocationTargetException
;
16 import java
.util
.ArrayList
;
17 import java
.util
.List
;
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
;
48 * <b><u>RenameTraceHandler</u></b>
50 * TODO: Implement me. Please.
52 public class RenameTraceHandler
extends AbstractHandler
{
54 private TmfTraceElement fTrace
= null;
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 public boolean isEnabled() {
63 // Check if we are closing down
64 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
70 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
71 IWorkbenchPart part
= page
.getActivePart();
75 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
76 if (selectionProvider
== null) {
79 ISelection selection
= selectionProvider
.getSelection();
81 // Make sure there is only selection and that it is an experiment
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
;
92 return (fTrace
!= null);
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
102 // Check if we are closing down
103 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
104 if (window
== null) {
108 // If trace is under an experiment, use the original trace from the traces folder
109 fTrace
= fTrace
.getElementUnderTraceFolder();
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
) {
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
)) {
132 if (newTrace
== null) {
136 List
<WorkspaceModifyOperation
> removeOps
= new ArrayList
<WorkspaceModifyOperation
>();
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
);
144 // Queue the removal of the old trace link
145 removeOps
.add(new WorkspaceModifyOperation() {
147 protected void execute(IProgressMonitor monitor
) throws CoreException
, InvocationTargetException
, InterruptedException
{
148 experiment
.removeChild(trace
);
149 trace
.getResource().delete(true, null);
150 experiment
.refresh();
157 for (WorkspaceModifyOperation operation
: removeOps
) {
159 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
160 } catch (InterruptedException exception
) {
161 } catch (InvocationTargetException exception
) {
162 } catch (RuntimeException exception
) {
169 private static void createTraceLink(TmfTraceElement trace
,
170 final ITmfProjectModelElement experiment
) {
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
);
187 Activator
.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location
); //$NON-NLS-1$
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
);
199 Activator
.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location
); //$NON-NLS-1$
202 experiment
.refresh();
203 } catch (CoreException e
) {
204 Activator
.getDefault().logError("Error renaming trace" + trace
.getName(), e
); //$NON-NLS-1$