1 /*******************************************************************************
2 * Copyright (c) 2014, 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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.handlers
;
15 import java
.lang
.reflect
.InvocationTargetException
;
17 import org
.eclipse
.core
.commands
.AbstractHandler
;
18 import org
.eclipse
.core
.commands
.ExecutionEvent
;
19 import org
.eclipse
.core
.commands
.ExecutionException
;
20 import org
.eclipse
.core
.resources
.IContainer
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.core
.runtime
.IPath
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
26 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
27 import org
.eclipse
.jface
.viewers
.ISelection
;
28 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
29 import org
.eclipse
.jface
.window
.Window
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
32 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
33 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
34 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.wizards
.RenameFolderDialog
;
35 import org
.eclipse
.ui
.IWorkbenchWindow
;
36 import org
.eclipse
.ui
.PlatformUI
;
37 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
38 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
41 * Handler for the Rename Folder command.
43 public class RenameFolderHandler
extends AbstractHandler
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
52 // Check if we are closing down
53 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
58 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
59 TmfTraceFolder selectedFolder
= null;
60 if (selection
instanceof IStructuredSelection
) {
61 Object element
= ((IStructuredSelection
) selection
).getFirstElement();
62 if (element
instanceof TmfTraceFolder
) {
63 selectedFolder
= (TmfTraceFolder
) element
;
66 if (selectedFolder
== null) {
69 final TmfTraceFolder oldFolder
= selectedFolder
;
71 // Fire the Rename Folder dialog
72 RenameFolderDialog dialog
= new RenameFolderDialog(window
.getShell(), oldFolder
);
75 if (dialog
.getReturnCode() != Window
.OK
) {
79 final String newName
= (String
) dialog
.getFirstResult();
81 IContainer parentFolder
= oldFolder
.getResource().getParent();
82 final TmfTraceFolder tracesFolder
= oldFolder
.getProject().getTracesFolder();
83 final IPath newFolderPath
= parentFolder
.getFullPath().append(newName
);
85 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
87 public void execute(IProgressMonitor monitor
) throws CoreException
{
89 monitor
.beginTask("", 1000); //$NON-NLS-1$
90 if (monitor
.isCanceled()) {
91 throw new OperationCanceledException();
94 for (final TmfTraceElement traceElement
: oldFolder
.getTraces()) {
95 Display
.getDefault().syncExec(new Runnable() {
98 traceElement
.closeEditors();
102 IPath relativePath
= traceElement
.getPath().makeRelativeTo(oldFolder
.getPath());
103 String newElementPath
= newFolderPath
.makeRelativeTo(tracesFolder
.getPath()).append(relativePath
).toString();
104 traceElement
.renameSupplementaryFolder(newElementPath
);
107 oldFolder
.getResource().move(newFolderPath
, IResource
.FORCE
| IResource
.SHALLOW
, monitor
);
108 if (monitor
.isCanceled()) {
109 throw new OperationCanceledException();
118 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
119 } catch (InterruptedException e
) {
121 } catch (InvocationTargetException e
) {
122 MessageDialog
.openError(window
.getShell(), e
.toString(), e
.getTargetException().toString());
126 /* We need to split the WorkspaceModifyOperation so that the new model
127 * elements get created by the resource changed event */
128 operation
= new WorkspaceModifyOperation() {
130 protected void execute(IProgressMonitor monitor
) throws CoreException
, InvocationTargetException
, InterruptedException
{
132 IPath oldFolderElementPath
= oldFolder
.getPath().makeRelativeTo(tracesFolder
.getPath());
133 IPath newFolderElementPath
= oldFolderElementPath
.removeLastSegments(1).append(newName
);
134 for (TmfExperimentElement experiment
: oldFolder
.getProject().getExperimentsFolder().getExperiments()) {
135 for (TmfTraceElement oldTrace
: experiment
.getTraces()) {
136 if (oldTrace
.getElementPath().startsWith(oldFolderElementPath
.toString())) {
137 experiment
.removeTrace(oldTrace
);
138 String relativePath
= oldTrace
.getElementPath().substring(oldFolderElementPath
.toString().length() + 1);
139 String newTraceElementPath
= newFolderElementPath
.append(relativePath
).toString();
140 for (TmfTraceElement newTrace
: tracesFolder
.getTraces()) {
141 if (newTrace
.getElementPath().equals(newTraceElementPath
)) {
142 experiment
.addTrace(newTrace
);
153 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
154 } catch (InterruptedException e
) {
156 } catch (InvocationTargetException e
) {
157 MessageDialog
.openError(window
.getShell(), e
.toString(), e
.getTargetException().toString());