Commit | Line | Data |
---|---|---|
339d539c PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 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 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.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.linuxtools.tmf.ui.project.model.TmfExperimentElement; | |
31 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
33 | import org.eclipse.linuxtools.tmf.ui.project.wizards.RenameFolderDialog; | |
34 | import org.eclipse.ui.IWorkbenchWindow; | |
35 | import org.eclipse.ui.PlatformUI; | |
36 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
37 | import org.eclipse.ui.handlers.HandlerUtil; | |
38 | ||
39 | /** | |
40 | * Handler for the Rename Folder command. | |
41 | */ | |
42 | public class RenameFolderHandler extends AbstractHandler { | |
43 | ||
44 | // ------------------------------------------------------------------------ | |
45 | // Execution | |
46 | // ------------------------------------------------------------------------ | |
47 | ||
48 | @Override | |
49 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
50 | ||
51 | // Check if we are closing down | |
52 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
53 | if (window == null) { | |
54 | return null; | |
55 | } | |
56 | ||
57 | ISelection selection = HandlerUtil.getCurrentSelection(event); | |
58 | TmfTraceFolder selectedFolder = null; | |
59 | if (selection instanceof IStructuredSelection) { | |
60 | Object element = ((IStructuredSelection) selection).getFirstElement(); | |
61 | if (element instanceof TmfTraceFolder) { | |
62 | selectedFolder = (TmfTraceFolder) element; | |
63 | } | |
64 | } | |
65 | if (selectedFolder == null) { | |
66 | return null; | |
67 | } | |
68 | final TmfTraceFolder oldFolder = selectedFolder; | |
69 | ||
70 | // Fire the Rename Folder dialog | |
71 | RenameFolderDialog dialog = new RenameFolderDialog(window.getShell(), oldFolder); | |
72 | dialog.open(); | |
73 | ||
74 | if (dialog.getReturnCode() != Window.OK) { | |
75 | return null; | |
76 | } | |
77 | ||
78 | final String newName = (String) dialog.getFirstResult(); | |
79 | ||
80 | IContainer parentFolder = oldFolder.getResource().getParent(); | |
81 | final TmfTraceFolder tracesFolder = oldFolder.getProject().getTracesFolder(); | |
82 | final IPath newFolderPath = parentFolder.getFullPath().append(newName); | |
83 | ||
84 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { | |
85 | @Override | |
86 | public void execute(IProgressMonitor monitor) throws CoreException { | |
87 | try { | |
88 | monitor.beginTask("", 1000); //$NON-NLS-1$ | |
89 | if (monitor.isCanceled()) { | |
90 | throw new OperationCanceledException(); | |
91 | } | |
92 | ||
93 | for (TmfTraceElement traceElement : oldFolder.getTraces()) { | |
94 | traceElement.closeEditors(); | |
95 | ||
96 | IPath relativePath = traceElement.getPath().makeRelativeTo(oldFolder.getPath()); | |
97 | String newElementPath = newFolderPath.makeRelativeTo(tracesFolder.getPath()).append(relativePath).toString(); | |
98 | traceElement.renameSupplementaryFolder(newElementPath); | |
99 | } | |
100 | ||
101 | oldFolder.getResource().move(newFolderPath, IResource.FORCE | IResource.SHALLOW, monitor); | |
102 | if (monitor.isCanceled()) { | |
103 | throw new OperationCanceledException(); | |
104 | } | |
105 | } finally { | |
106 | monitor.done(); | |
107 | } | |
108 | } | |
109 | }; | |
110 | ||
111 | try { | |
112 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
113 | } catch (InterruptedException e) { | |
114 | return null; | |
115 | } catch (InvocationTargetException e) { | |
116 | MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString()); | |
117 | return null; | |
118 | } | |
119 | ||
120 | /* We need to split the WorkspaceModifyOperation so that the new model | |
121 | * elements get created by the resource changed event */ | |
122 | operation = new WorkspaceModifyOperation() { | |
123 | @Override | |
124 | protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { | |
125 | ||
126 | IPath oldFolderElementPath = oldFolder.getPath().makeRelativeTo(tracesFolder.getPath()); | |
127 | IPath newFolderElementPath = oldFolderElementPath.removeLastSegments(1).append(newName); | |
128 | for (TmfExperimentElement experiment : oldFolder.getProject().getExperimentsFolder().getExperiments()) { | |
129 | for (TmfTraceElement oldTrace : experiment.getTraces()) { | |
130 | if (oldTrace.getElementPath().startsWith(oldFolderElementPath.toString())) { | |
131 | experiment.removeTrace(oldTrace); | |
132 | String relativePath = oldTrace.getElementPath().substring(oldFolderElementPath.toString().length() + 1); | |
133 | String newTraceElementPath = newFolderElementPath.append(relativePath).toString(); | |
134 | for (TmfTraceElement newTrace : tracesFolder.getTraces()) { | |
135 | if (newTrace.getElementPath().equals(newTraceElementPath)) { | |
136 | experiment.addTrace(newTrace); | |
137 | break; | |
138 | } | |
139 | } | |
140 | } | |
141 | } | |
142 | } | |
143 | } | |
144 | }; | |
145 | ||
146 | try { | |
147 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
148 | } catch (InterruptedException e) { | |
149 | return null; | |
150 | } catch (InvocationTargetException e) { | |
151 | MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString()); | |
152 | return null; | |
153 | } | |
154 | ||
155 | return null; | |
156 | } | |
157 | ||
158 | } |