cbbfb8546a1c365dd2bc4003f62632ab9bef72fc
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / handlers / RenameFolderHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.ui.project.handlers;
14
15 import java.lang.reflect.InvocationTargetException;
16
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;
39
40 /**
41 * Handler for the Rename Folder command.
42 */
43 public class RenameFolderHandler extends AbstractHandler {
44
45 // ------------------------------------------------------------------------
46 // Execution
47 // ------------------------------------------------------------------------
48
49 @Override
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51
52 // Check if we are closing down
53 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
54 if (window == null) {
55 return null;
56 }
57
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;
64 }
65 }
66 if (selectedFolder == null) {
67 return null;
68 }
69 final TmfTraceFolder oldFolder = selectedFolder;
70
71 // Fire the Rename Folder dialog
72 RenameFolderDialog dialog = new RenameFolderDialog(window.getShell(), oldFolder);
73 dialog.open();
74
75 if (dialog.getReturnCode() != Window.OK) {
76 return null;
77 }
78
79 final String newName = (String) dialog.getFirstResult();
80
81 IContainer parentFolder = oldFolder.getResource().getParent();
82 final TmfTraceFolder tracesFolder = oldFolder.getProject().getTracesFolder();
83 final IPath newFolderPath = parentFolder.getFullPath().append(newName);
84
85 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
86 @Override
87 public void execute(IProgressMonitor monitor) throws CoreException {
88 try {
89 monitor.beginTask("", 1000); //$NON-NLS-1$
90 if (monitor.isCanceled()) {
91 throw new OperationCanceledException();
92 }
93
94 for (final TmfTraceElement traceElement : oldFolder.getTraces()) {
95 Display.getDefault().syncExec(new Runnable() {
96 @Override
97 public void run() {
98 traceElement.closeEditors();
99 }
100 });
101
102 IPath relativePath = traceElement.getPath().makeRelativeTo(oldFolder.getPath());
103 String newElementPath = newFolderPath.makeRelativeTo(tracesFolder.getPath()).append(relativePath).toString();
104 traceElement.renameSupplementaryFolder(newElementPath);
105 }
106
107 oldFolder.getResource().move(newFolderPath, IResource.FORCE | IResource.SHALLOW, monitor);
108 if (monitor.isCanceled()) {
109 throw new OperationCanceledException();
110 }
111 } finally {
112 monitor.done();
113 }
114 }
115 };
116
117 try {
118 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
119 } catch (InterruptedException e) {
120 return null;
121 } catch (InvocationTargetException e) {
122 MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
123 return null;
124 }
125
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() {
129 @Override
130 protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
131
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);
143 break;
144 }
145 }
146 }
147 }
148 }
149 }
150 };
151
152 try {
153 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
154 } catch (InterruptedException e) {
155 return null;
156 } catch (InvocationTargetException e) {
157 MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
158 return null;
159 }
160
161 return null;
162 }
163
164 }
This page took 0.035291 seconds and 4 git commands to generate.