1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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
.linuxtools
.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
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
.RenameTraceDialog
;
37 import org
.eclipse
.ui
.IWorkbenchWindow
;
38 import org
.eclipse
.ui
.PlatformUI
;
39 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
40 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
43 * Handler for the Rename Trace command.
45 public class RenameTraceHandler
extends AbstractHandler
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
54 // Check if we are closing down
55 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
60 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
61 TmfTraceElement selectedTrace
= null;
62 if (selection
instanceof IStructuredSelection
) {
63 Object element
= ((IStructuredSelection
) selection
).getFirstElement();
64 if (element
instanceof TmfTraceElement
) {
65 selectedTrace
= (TmfTraceElement
) element
;
68 if (selectedTrace
== null) {
72 // If trace is under an experiment, use the original trace from the traces folder
73 final TmfTraceElement oldTrace
= selectedTrace
.getElementUnderTraceFolder();
75 RenameTraceDialog dialog
= new RenameTraceDialog(window
.getShell(), oldTrace
);
76 if (dialog
.open() != Window
.OK
) {
80 final TmfTraceFolder traceFolder
= (TmfTraceFolder
) oldTrace
.getParent();
81 final String newName
= (String
) dialog
.getFirstResult();
83 IFolder parentFolder
= (IFolder
) oldTrace
.getParent().getResource();
84 final TmfTraceFolder tracesFolder
= oldTrace
.getProject().getTracesFolder();
85 final IPath newPath
= parentFolder
.getFullPath().append(newName
);
87 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
89 public void execute(IProgressMonitor monitor
) throws CoreException
{
91 monitor
.beginTask("", 1000); //$NON-NLS-1$
92 if (monitor
.isCanceled()) {
93 throw new OperationCanceledException();
95 // Close the trace if open
96 oldTrace
.closeEditors();
98 if (oldTrace
.getResource() instanceof IFolder
) {
99 IFolder folder
= (IFolder
) oldTrace
.getResource();
100 IFile bookmarksFile
= oldTrace
.getBookmarksFile();
101 if (bookmarksFile
.exists()) {
102 IFile newBookmarksFile
= folder
.getFile(bookmarksFile
.getName().replace(oldTrace
.getName(), newName
));
103 if (!newBookmarksFile
.exists()) {
104 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
105 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, monitor
);
110 String newElementPath
= newPath
.makeRelativeTo(tracesFolder
.getPath()).toString();
111 oldTrace
.renameSupplementaryFolder(newElementPath
);
112 oldTrace
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, monitor
);
113 if (monitor
.isCanceled()) {
114 throw new OperationCanceledException();
123 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
124 } catch (InterruptedException e
) {
126 } catch (InvocationTargetException e
) {
127 MessageDialog
.openError(window
.getShell(), e
.toString(), e
.getTargetException().toString());
131 /* We need to split the WorkspaceModifyOperation so that the new model
132 * elements get created by the resource changed event */
133 operation
= new WorkspaceModifyOperation() {
135 protected void execute(IProgressMonitor monitor
) throws CoreException
, InvocationTargetException
, InterruptedException
{
137 // Locate the new trace object
138 TmfTraceElement newTrace
= null;
139 String newElementPath
= oldTrace
.getParent().getPath().append(newName
).makeRelativeTo(tracesFolder
.getPath()).toString();
140 for (TmfTraceElement element
: traceFolder
.getTraces()) {
141 if (element
.getElementPath().equals(newElementPath
)) {
146 if (newTrace
== null) {
150 TmfExperimentFolder experimentFolder
= newTrace
.getProject().getExperimentsFolder();
151 for (final TmfExperimentElement experiment
: experimentFolder
.getExperiments()) {
152 for (final TmfTraceElement expTrace
: experiment
.getTraces()) {
153 if (expTrace
.getElementPath().equals(oldTrace
.getElementPath())) {
154 experiment
.removeTrace(expTrace
);
155 experiment
.addTrace(newTrace
);
163 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
164 } catch (InterruptedException e
) {
165 } catch (InvocationTargetException e
) {
166 MessageDialog
.openError(window
.getShell(), e
.toString(), e
.getTargetException().toString());