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
CommitLineData
12c155f5 1/*******************************************************************************
2f5c2df5 2 * Copyright (c) 2009, 2015 Ericsson
abbdd66a 3 *
12c155f5
FC
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
abbdd66a 8 *
12c155f5
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
339d539c 11 * Patrick Tasse - Add support for folder elements
12c155f5
FC
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.internal.tmf.ui.project.handlers;
12c155f5
FC
15
16import java.lang.reflect.InvocationTargetException;
12c155f5
FC
17
18import org.eclipse.core.commands.AbstractHandler;
19import org.eclipse.core.commands.ExecutionEvent;
20import org.eclipse.core.commands.ExecutionException;
21import org.eclipse.core.resources.IFile;
22import org.eclipse.core.resources.IFolder;
23import org.eclipse.core.resources.IResource;
12c155f5
FC
24import org.eclipse.core.runtime.CoreException;
25import org.eclipse.core.runtime.IPath;
26import org.eclipse.core.runtime.IProgressMonitor;
339d539c 27import org.eclipse.core.runtime.OperationCanceledException;
12c155f5 28import org.eclipse.jface.viewers.ISelection;
339d539c 29import org.eclipse.jface.viewers.IStructuredSelection;
12c155f5 30import org.eclipse.jface.window.Window;
2f5c2df5 31import org.eclipse.swt.widgets.Display;
2bdf0193
AM
32import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
33import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
34import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
35import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
8e25df71 36import org.eclipse.tracecompass.tmf.ui.project.model.TraceUtils;
2bdf0193 37import org.eclipse.tracecompass.tmf.ui.project.wizards.RenameTraceDialog;
12c155f5
FC
38import org.eclipse.ui.IWorkbenchWindow;
39import org.eclipse.ui.PlatformUI;
40import org.eclipse.ui.actions.WorkspaceModifyOperation;
339d539c 41import org.eclipse.ui.handlers.HandlerUtil;
12c155f5
FC
42
43/**
339d539c 44 * Handler for the Rename Trace command.
12c155f5
FC
45 */
46public class RenameTraceHandler extends AbstractHandler {
47
d5efe032 48 // ------------------------------------------------------------------------
339d539c 49 // Execution
12c155f5
FC
50 // ------------------------------------------------------------------------
51
52 @Override
339d539c 53 public Object execute(ExecutionEvent event) throws ExecutionException {
12c155f5
FC
54
55 // Check if we are closing down
56 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
abbdd66a 57 if (window == null) {
339d539c 58 return null;
abbdd66a 59 }
12c155f5 60
339d539c
PT
61 ISelection selection = HandlerUtil.getCurrentSelection(event);
62 TmfTraceElement selectedTrace = null;
63 if (selection instanceof IStructuredSelection) {
64 Object element = ((IStructuredSelection) selection).getFirstElement();
12c155f5 65 if (element instanceof TmfTraceElement) {
339d539c 66 selectedTrace = (TmfTraceElement) element;
12c155f5
FC
67 }
68 }
339d539c 69 if (selectedTrace == null) {
12c155f5 70 return null;
abbdd66a 71 }
12c155f5 72
828e5592 73 // If trace is under an experiment, use the original trace from the traces folder
339d539c 74 final TmfTraceElement oldTrace = selectedTrace.getElementUnderTraceFolder();
828e5592 75
339d539c 76 RenameTraceDialog dialog = new RenameTraceDialog(window.getShell(), oldTrace);
abbdd66a 77 if (dialog.open() != Window.OK) {
12c155f5 78 return null;
abbdd66a 79 }
12c155f5 80
339d539c
PT
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);
12c155f5 87
f537c959
PT
88 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
89 @Override
339d539c
PT
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
2f5c2df5
PT
97 Display.getDefault().syncExec(new Runnable() {
98 @Override
99 public void run() {
100 oldTrace.closeEditors();
101 }
102 });
339d539c
PT
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 }
12c155f5 113 }
f537c959 114 }
339d539c
PT
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();
12c155f5
FC
124 }
125 }
f537c959 126 };
12c155f5 127
f537c959
PT
128 try {
129 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
339d539c
PT
130 } catch (InterruptedException e) {
131 return null;
132 } catch (InvocationTargetException e) {
8e25df71 133 TraceUtils.displayErrorMsg(e.toString(), e.getTargetException().toString());
339d539c 134 return null;
12c155f5 135 }
abbdd66a 136
339d539c
PT
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 {
12c155f5 142
339d539c
PT
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;
0ac777c8 150 }
12c155f5 151 }
339d539c
PT
152 if (newTrace == null) {
153 return;
12c155f5 154 }
339d539c
PT
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 }
0ac777c8 163 }
12c155f5 164 }
12c155f5 165 }
339d539c
PT
166 };
167
168 try {
169 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
170 } catch (InterruptedException e) {
171 } catch (InvocationTargetException e) {
8e25df71 172 TraceUtils.displayErrorMsg(e.toString(), e.getTargetException().toString());
12c155f5 173 }
12c155f5 174
339d539c
PT
175 return null;
176 }
12c155f5 177}
This page took 0.111599 seconds and 5 git commands to generate.