Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / RenameTraceHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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 * Francois Chouinard - Initial API and implementation
11 * Patrick Tasse - Add support for folder elements
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
15
16 import java.lang.reflect.InvocationTargetException;
17
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;
41
42 /**
43 * Handler for the Rename Trace command.
44 */
45 public class RenameTraceHandler extends AbstractHandler {
46
47 // ------------------------------------------------------------------------
48 // Execution
49 // ------------------------------------------------------------------------
50
51 @Override
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53
54 // Check if we are closing down
55 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
56 if (window == null) {
57 return null;
58 }
59
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;
66 }
67 }
68 if (selectedTrace == null) {
69 return null;
70 }
71
72 // If trace is under an experiment, use the original trace from the traces folder
73 final TmfTraceElement oldTrace = selectedTrace.getElementUnderTraceFolder();
74
75 RenameTraceDialog dialog = new RenameTraceDialog(window.getShell(), oldTrace);
76 if (dialog.open() != Window.OK) {
77 return null;
78 }
79
80 final TmfTraceFolder traceFolder = (TmfTraceFolder) oldTrace.getParent();
81 final String newName = (String) dialog.getFirstResult();
82
83 IFolder parentFolder = (IFolder) oldTrace.getParent().getResource();
84 final TmfTraceFolder tracesFolder = oldTrace.getProject().getTracesFolder();
85 final IPath newPath = parentFolder.getFullPath().append(newName);
86
87 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
88 @Override
89 public void execute(IProgressMonitor monitor) throws CoreException {
90 try {
91 monitor.beginTask("", 1000); //$NON-NLS-1$
92 if (monitor.isCanceled()) {
93 throw new OperationCanceledException();
94 }
95 // Close the trace if open
96 oldTrace.closeEditors();
97
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);
106 }
107 }
108 }
109
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();
115 }
116 } finally {
117 monitor.done();
118 }
119 }
120 };
121
122 try {
123 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
124 } catch (InterruptedException e) {
125 return null;
126 } catch (InvocationTargetException e) {
127 MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
128 return null;
129 }
130
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() {
134 @Override
135 protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
136
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)) {
142 newTrace = element;
143 break;
144 }
145 }
146 if (newTrace == null) {
147 return;
148 }
149
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);
156 }
157 }
158 }
159 }
160 };
161
162 try {
163 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
164 } catch (InterruptedException e) {
165 } catch (InvocationTargetException e) {
166 MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
167 }
168
169 return null;
170 }
171 }
This page took 0.035265 seconds and 5 git commands to generate.