tmf: log when an error message is displayed in a MessageBox
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / handlers / RenameTraceHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 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 * Francois Chouinard - Initial API and implementation
11 * Patrick Tasse - Add support for folder elements
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.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.viewers.ISelection;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
33 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
34 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
35 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
36 import org.eclipse.tracecompass.tmf.ui.project.model.TraceUtils;
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;
42
43 /**
44 * Handler for the Rename Trace command.
45 */
46 public class RenameTraceHandler extends AbstractHandler {
47
48 // ------------------------------------------------------------------------
49 // Execution
50 // ------------------------------------------------------------------------
51
52 @Override
53 public Object execute(ExecutionEvent event) throws ExecutionException {
54
55 // Check if we are closing down
56 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
57 if (window == null) {
58 return null;
59 }
60
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;
67 }
68 }
69 if (selectedTrace == null) {
70 return null;
71 }
72
73 // If trace is under an experiment, use the original trace from the traces folder
74 final TmfTraceElement oldTrace = selectedTrace.getElementUnderTraceFolder();
75
76 RenameTraceDialog dialog = new RenameTraceDialog(window.getShell(), oldTrace);
77 if (dialog.open() != Window.OK) {
78 return null;
79 }
80
81 final TmfTraceFolder traceFolder = (TmfTraceFolder) oldTrace.getParent();
82 final String newName = (String) dialog.getFirstResult();
83
84 IFolder parentFolder = (IFolder) oldTrace.getParent().getResource();
85 final TmfTraceFolder tracesFolder = oldTrace.getProject().getTracesFolder();
86 final IPath newPath = parentFolder.getFullPath().append(newName);
87
88 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
89 @Override
90 public void execute(IProgressMonitor monitor) throws CoreException {
91 try {
92 monitor.beginTask("", 1000); //$NON-NLS-1$
93 if (monitor.isCanceled()) {
94 throw new OperationCanceledException();
95 }
96 // Close the trace if open
97 Display.getDefault().syncExec(new Runnable() {
98 @Override
99 public void run() {
100 oldTrace.closeEditors();
101 }
102 });
103
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);
112 }
113 }
114 }
115
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();
121 }
122 } finally {
123 monitor.done();
124 }
125 }
126 };
127
128 try {
129 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
130 } catch (InterruptedException e) {
131 return null;
132 } catch (InvocationTargetException e) {
133 TraceUtils.displayErrorMsg(e.toString(), e.getTargetException().toString());
134 return null;
135 }
136
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() {
140 @Override
141 protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
142
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)) {
148 newTrace = element;
149 break;
150 }
151 }
152 if (newTrace == null) {
153 return;
154 }
155
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);
162 }
163 }
164 }
165 }
166 };
167
168 try {
169 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
170 } catch (InterruptedException e) {
171 } catch (InvocationTargetException e) {
172 TraceUtils.displayErrorMsg(e.toString(), e.getTargetException().toString());
173 }
174
175 return null;
176 }
177 }
This page took 0.037223 seconds and 6 git commands to generate.