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();
70 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
71 IWorkbenchPart part
= page
.getActivePart();
72 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
73 if (selectionProvider
== null) {
76 ISelection selection
= selectionProvider
.getSelection();
78 // Make sure there is only selection and that it is an experiment
80 if (selection
instanceof TreeSelection
) {
81 TreeSelection sel
= (TreeSelection
) selection
;
82 // There should be only one item selected as per the plugin.xml
83 Object element
= sel
.getFirstElement();
84 if (element
instanceof TmfTraceElement
) {
85 fTrace
= (TmfTraceElement
) element
;
89 return (fTrace
!= null);
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
99 // Check if we are closing down
100 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
101 if (window
== null) {
105 // If trace is under an experiment, use the original trace from the traces folder
106 fTrace
= fTrace
.getElementUnderTraceFolder();
108 // Fire the Rename Trace dialog
109 Shell shell
= window
.getShell();
110 TmfTraceFolder traceFolder
= (TmfTraceFolder
) fTrace
.getParent();
111 TmfTraceElement oldTrace
= fTrace
;
112 RenameTraceDialog dialog
= new RenameTraceDialog(shell
, fTrace
);
113 if (dialog
.open() != Window
.OK
) {
117 // Locate the new trace object
118 TmfTraceElement newTrace
= null;
119 String newTraceName
= dialog
.getNewTraceName();
120 for (ITmfProjectModelElement element
: traceFolder
.getChildren()) {
121 if (element
instanceof TmfTraceElement
) {
122 TmfTraceElement trace
= (TmfTraceElement
) element
;
123 if (trace
.getName().equals(newTraceName
)) {
129 if (newTrace
== null) {
133 List
<WorkspaceModifyOperation
> removeOps
= new ArrayList
<WorkspaceModifyOperation
>();
134 TmfExperimentFolder experimentFolder
= newTrace
.getProject().getExperimentsFolder();
135 for (final ITmfProjectModelElement experiment
: experimentFolder
.getChildren()) {
136 for (final ITmfProjectModelElement trace
: experiment
.getChildren()) {
137 if (trace
.getName().equals(oldTrace
.getName())) {
138 // Create a link to the renamed trace
139 createTraceLink(newTrace
, experiment
);
141 // Queue the removal of the old trace link
142 removeOps
.add(new WorkspaceModifyOperation() {
144 protected void execute(IProgressMonitor monitor
) throws CoreException
, InvocationTargetException
, InterruptedException
{
145 experiment
.removeChild(trace
);
146 trace
.getResource().delete(true, null);
147 experiment
.refresh();
154 for (WorkspaceModifyOperation operation
: removeOps
) {
156 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
157 } catch (InterruptedException exception
) {
158 } catch (InvocationTargetException exception
) {
159 } catch (RuntimeException exception
) {
166 private static void createTraceLink(TmfTraceElement trace
,
167 final ITmfProjectModelElement experiment
) {
169 IResource resource
= trace
.getResource();
170 IPath location
= resource
.getLocation();
171 // Get the trace properties for this resource
172 String traceBundle
= resource
.getPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
);
173 String traceTypeId
= resource
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
174 String traceIcon
= resource
.getPersistentProperty(TmfCommonConstants
.TRACEICON
);
175 if (resource
instanceof IFolder
) {
176 IFolder folder
= ((IFolder
) experiment
.getResource()).getFolder(trace
.getName());
177 if (ResourcesPlugin
.getWorkspace().validateLinkLocation(folder
, location
).isOK()) {
178 folder
.createLink(location
, IResource
.REPLACE
, null);
179 folder
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, traceBundle
);
180 folder
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceTypeId
);
181 folder
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, traceIcon
);
184 Activator
.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location
); //$NON-NLS-1$
188 IFile file
= ((IFolder
) experiment
.getResource()).getFile(trace
.getName());
189 if (ResourcesPlugin
.getWorkspace().validateLinkLocation(file
, location
).isOK()) {
190 file
.createLink(location
, IResource
.REPLACE
, null);
191 file
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, traceBundle
);
192 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceTypeId
);
193 file
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, traceIcon
);
196 Activator
.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location
); //$NON-NLS-1$
199 experiment
.refresh();
200 } catch (CoreException e
) {
201 Activator
.getDefault().logError("Error renaming trace" + trace
.getName(), e
); //$NON-NLS-1$