1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011 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();
69 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
70 IWorkbenchPart part
= page
.getActivePart();
71 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
72 if (selectionProvider
== null)
74 ISelection selection
= selectionProvider
.getSelection();
76 // Make sure there is only selection and that it is an experiment
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
;
87 return (fTrace
!= null);
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
97 // Check if we are closing down
98 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
102 // If trace is under an experiment, use the original trace from the traces folder
103 fTrace
= fTrace
.getElementUnderTraceFolder();
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
)
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
)) {
125 if (newTrace
== null)
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
);
136 // Queue the removal of the old trace link
137 removeOps
.add(new WorkspaceModifyOperation() {
139 protected void execute(IProgressMonitor monitor
) throws CoreException
, InvocationTargetException
, InterruptedException
{
140 experiment
.removeChild(trace
);
141 trace
.getResource().delete(true, null);
142 experiment
.refresh();
149 for (WorkspaceModifyOperation operation
: removeOps
) {
151 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
152 } catch (InterruptedException exception
) {
153 } catch (InvocationTargetException exception
) {
154 } catch (RuntimeException exception
) {
161 private void createTraceLink(TmfTraceElement trace
, final ITmfProjectModelElement experiment
) {
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
);
178 Activator
.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location
); //$NON-NLS-1$
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
);
190 Activator
.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location
); //$NON-NLS-1$
193 experiment
.refresh();
194 } catch (CoreException e
) {
195 Activator
.getDefault().logError("Error renaming trace" + trace
.getName(), e
); //$NON-NLS-1$