1 /*******************************************************************************
2 * Copyright (c) 2009, 2015 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 * Patrick Tasse - Add support for folder elements
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.handlers
;
16 import java
.lang
.reflect
.InvocationTargetException
;
18 import org
.eclipse
.core
.commands
.AbstractHandler
;
19 import org
.eclipse
.core
.commands
.ExecutionEvent
;
20 import org
.eclipse
.core
.commands
.ExecutionException
;
21 import org
.eclipse
.core
.resources
.IFile
;
22 import org
.eclipse
.core
.resources
.IFolder
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.core
.runtime
.IPath
;
26 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
27 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
28 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
29 import org
.eclipse
.jface
.viewers
.ISelection
;
30 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
31 import org
.eclipse
.jface
.window
.Window
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
34 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
35 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.wizards
.RenameTraceDialog
;
38 import org
.eclipse
.ui
.IWorkbenchWindow
;
39 import org
.eclipse
.ui
.PlatformUI
;
40 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
41 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
44 * Handler for the Rename Trace command.
46 public class RenameTraceHandler
extends AbstractHandler
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
53 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
55 // Check if we are closing down
56 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
61 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
62 TmfTraceElement selectedTrace
= null;
63 if (selection
instanceof IStructuredSelection
) {
64 Object element
= ((IStructuredSelection
) selection
).getFirstElement();
65 if (element
instanceof TmfTraceElement
) {
66 selectedTrace
= (TmfTraceElement
) element
;
69 if (selectedTrace
== null) {
73 // If trace is under an experiment, use the original trace from the traces folder
74 final TmfTraceElement oldTrace
= selectedTrace
.getElementUnderTraceFolder();
76 RenameTraceDialog dialog
= new RenameTraceDialog(window
.getShell(), oldTrace
);
77 if (dialog
.open() != Window
.OK
) {
81 final TmfTraceFolder traceFolder
= (TmfTraceFolder
) oldTrace
.getParent();
82 final String newName
= (String
) dialog
.getFirstResult();
84 IFolder parentFolder
= (IFolder
) oldTrace
.getParent().getResource();
85 final TmfTraceFolder tracesFolder
= oldTrace
.getProject().getTracesFolder();
86 final IPath newPath
= parentFolder
.getFullPath().append(newName
);
88 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
90 public void execute(IProgressMonitor monitor
) throws CoreException
{
92 monitor
.beginTask("", 1000); //$NON-NLS-1$
93 if (monitor
.isCanceled()) {
94 throw new OperationCanceledException();
96 // Close the trace if open
97 Display
.getDefault().syncExec(new Runnable() {
100 oldTrace
.closeEditors();
104 if (oldTrace
.getResource() instanceof IFolder
) {
105 IFolder folder
= (IFolder
) oldTrace
.getResource();
106 IFile bookmarksFile
= oldTrace
.getBookmarksFile();
107 if (bookmarksFile
.exists()) {
108 IFile newBookmarksFile
= folder
.getFile(bookmarksFile
.getName().replace(oldTrace
.getName(), newName
));
109 if (!newBookmarksFile
.exists()) {
110 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
111 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, monitor
);
116 String newElementPath
= newPath
.makeRelativeTo(tracesFolder
.getPath()).toString();
117 oldTrace
.renameSupplementaryFolder(newElementPath
);
118 oldTrace
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, monitor
);
119 if (monitor
.isCanceled()) {
120 throw new OperationCanceledException();
129 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
130 } catch (InterruptedException e
) {
132 } catch (InvocationTargetException e
) {
133 MessageDialog
.openError(window
.getShell(), e
.toString(), e
.getTargetException().toString());
137 /* We need to split the WorkspaceModifyOperation so that the new model
138 * elements get created by the resource changed event */
139 operation
= new WorkspaceModifyOperation() {
141 protected void execute(IProgressMonitor monitor
) throws CoreException
, InvocationTargetException
, InterruptedException
{
143 // Locate the new trace object
144 TmfTraceElement newTrace
= null;
145 String newElementPath
= oldTrace
.getParent().getPath().append(newName
).makeRelativeTo(tracesFolder
.getPath()).toString();
146 for (TmfTraceElement element
: traceFolder
.getTraces()) {
147 if (element
.getElementPath().equals(newElementPath
)) {
152 if (newTrace
== null) {
156 TmfExperimentFolder experimentFolder
= newTrace
.getProject().getExperimentsFolder();
157 for (final TmfExperimentElement experiment
: experimentFolder
.getExperiments()) {
158 for (final TmfTraceElement expTrace
: experiment
.getTraces()) {
159 if (expTrace
.getElementPath().equals(oldTrace
.getElementPath())) {
160 experiment
.removeTrace(expTrace
);
161 experiment
.addTrace(newTrace
);
169 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
170 } catch (InterruptedException e
) {
171 } catch (InvocationTargetException e
) {
172 MessageDialog
.openError(window
.getShell(), e
.toString(), e
.getTargetException().toString());