Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 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 - Copied and adapted from NewFolderDialog | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.wizards; | |
14 | ||
15 | import java.lang.reflect.InvocationTargetException; | |
16 | ||
17 | import org.eclipse.core.resources.IContainer; | |
18 | import org.eclipse.core.resources.IResource; | |
19 | import org.eclipse.core.resources.IWorkspace; | |
20 | import org.eclipse.core.runtime.CoreException; | |
21 | import org.eclipse.core.runtime.IPath; | |
22 | import org.eclipse.core.runtime.IProgressMonitor; | |
23 | import org.eclipse.core.runtime.IStatus; | |
24 | import org.eclipse.core.runtime.OperationCanceledException; | |
25 | import org.eclipse.core.runtime.Path; | |
26 | import org.eclipse.core.runtime.Status; | |
27 | import org.eclipse.jface.dialogs.IDialogConstants; | |
28 | import org.eclipse.jface.dialogs.MessageDialog; | |
29 | import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin; | |
30 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; | |
31 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
33 | import org.eclipse.osgi.util.NLS; | |
34 | import org.eclipse.swt.SWT; | |
35 | import org.eclipse.swt.graphics.Font; | |
36 | import org.eclipse.swt.layout.GridData; | |
37 | import org.eclipse.swt.layout.GridLayout; | |
38 | import org.eclipse.swt.widgets.Composite; | |
39 | import org.eclipse.swt.widgets.Control; | |
40 | import org.eclipse.swt.widgets.Event; | |
41 | import org.eclipse.swt.widgets.Label; | |
42 | import org.eclipse.swt.widgets.Listener; | |
43 | import org.eclipse.swt.widgets.Shell; | |
44 | import org.eclipse.swt.widgets.Text; | |
45 | import org.eclipse.ui.PlatformUI; | |
46 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
47 | import org.eclipse.ui.dialogs.SelectionStatusDialog; | |
48 | ||
49 | /** | |
50 | * <b><u>RenameTraceDialog</u></b> | |
51 | * <p> | |
52 | */ | |
53 | public class RenameTraceDialog extends SelectionStatusDialog { | |
54 | ||
55 | // ------------------------------------------------------------------------ | |
56 | // Members | |
57 | // ------------------------------------------------------------------------ | |
58 | ||
59 | private final TmfTraceElement fTrace; | |
60 | private Text fNewTraceNameText; | |
61 | private String fNewTraceName; | |
62 | private IContainer fTraceFolder; | |
63 | private TmfProjectElement fProject; | |
64 | ||
65 | // ------------------------------------------------------------------------ | |
66 | // Constructor | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
69 | public RenameTraceDialog(Shell shell, TmfTraceElement trace) { | |
70 | super(shell); | |
71 | fTrace = trace; | |
72 | TmfTraceFolder folder = (TmfTraceFolder) trace.getParent(); | |
73 | fTraceFolder = folder.getResource(); | |
74 | fProject = trace.getProject(); | |
75 | setTitle(Messages.RenameTraceDialog_DialogTitle); | |
76 | setStatusLineAboveButtons(true); | |
77 | } | |
78 | ||
79 | // ------------------------------------------------------------------------ | |
80 | // Dialog | |
81 | // ------------------------------------------------------------------------ | |
82 | ||
83 | @Override | |
84 | protected Control createDialogArea(Composite parent) { | |
85 | Composite composite = (Composite) super.createDialogArea(parent); | |
86 | composite.setLayout(new GridLayout()); | |
87 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
88 | ||
89 | createNewTraceNameGroup(composite); | |
90 | return composite; | |
91 | } | |
92 | ||
93 | private void createNewTraceNameGroup(Composite parent) { | |
94 | Font font = parent.getFont(); | |
95 | Composite folderGroup = new Composite(parent, SWT.NONE); | |
96 | GridLayout layout = new GridLayout(); | |
97 | layout.numColumns = 2; | |
98 | folderGroup.setLayout(layout); | |
99 | folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
100 | ||
101 | // Old trace name label | |
102 | Label oldTraceLabel = new Label(folderGroup, SWT.NONE); | |
103 | oldTraceLabel.setFont(font); | |
104 | oldTraceLabel.setText(Messages.RenameTraceDialog_TraceName); | |
105 | ||
106 | // Old trace name field | |
107 | Text oldTraceName = new Text(folderGroup, SWT.BORDER); | |
108 | GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
109 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
110 | oldTraceName.setLayoutData(data); | |
111 | oldTraceName.setFont(font); | |
112 | oldTraceName.setText(fTrace.getName()); | |
113 | oldTraceName.setEnabled(false); | |
114 | ||
115 | // New trace name label | |
116 | Label newTaceLabel = new Label(folderGroup, SWT.NONE); | |
117 | newTaceLabel.setFont(font); | |
118 | newTaceLabel.setText(Messages.RenameTraceDialog_TraceNewName); | |
119 | ||
120 | // New trace name entry field | |
121 | fNewTraceNameText = new Text(folderGroup, SWT.BORDER); | |
122 | data = new GridData(GridData.FILL_HORIZONTAL); | |
123 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
124 | fNewTraceNameText.setLayoutData(data); | |
125 | fNewTraceNameText.setFont(font); | |
126 | fNewTraceNameText.addListener(SWT.Modify, new Listener() { | |
127 | @Override | |
128 | public void handleEvent(Event event) { | |
129 | validateNewTraceName(); | |
130 | } | |
131 | }); | |
132 | } | |
133 | ||
134 | public String getNewTraceName() { | |
135 | return fNewTraceName; | |
136 | } | |
137 | ||
138 | private void validateNewTraceName() { | |
139 | ||
140 | fNewTraceName = fNewTraceNameText.getText(); | |
141 | IWorkspace workspace = fTraceFolder.getWorkspace(); | |
142 | IStatus nameStatus = workspace.validateName(fNewTraceName, IResource.FOLDER); | |
143 | ||
144 | if ("".equals(fNewTraceName)) { //$NON-NLS-1$ | |
145 | updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR, | |
146 | Messages.Dialog_EmptyNameError, null)); | |
147 | return; | |
148 | } | |
149 | ||
150 | if (nameStatus.isOK() == false) { | |
151 | updateStatus(nameStatus); | |
152 | return; | |
153 | } | |
154 | ||
155 | IPath path = new Path(fNewTraceName); | |
156 | if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) { | |
157 | updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR, | |
158 | Messages.Dialog_ExistingNameError, null)); | |
159 | return; | |
160 | } | |
161 | ||
162 | updateStatus(new Status(IStatus.OK, TmfUiPlugin.PLUGIN_ID, "")); //$NON-NLS-1$ | |
163 | } | |
164 | ||
165 | // ------------------------------------------------------------------------ | |
166 | // SelectionStatusDialog | |
167 | // ------------------------------------------------------------------------ | |
168 | ||
169 | @Override | |
170 | protected void computeResult() { | |
171 | } | |
172 | ||
173 | @Override | |
174 | public void create() { | |
175 | super.create(); | |
176 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
177 | } | |
178 | ||
179 | @Override | |
180 | protected void okPressed() { | |
181 | IResource trace = renameTrace(fNewTraceNameText.getText()); | |
182 | if (trace == null) { | |
183 | return; | |
184 | } | |
185 | setSelectionResult(new IResource[] { trace }); | |
186 | super.okPressed(); | |
187 | ||
188 | if (fProject != null) { | |
189 | fProject.refresh(); | |
190 | } | |
191 | } | |
192 | ||
193 | private IResource renameTrace(String newName) { | |
194 | ||
195 | IPath oldPath = fTrace.getResource().getFullPath(); | |
196 | final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$ | |
197 | ||
198 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { | |
199 | @Override | |
200 | public void execute(IProgressMonitor monitor) throws CoreException { | |
201 | try { | |
202 | monitor.beginTask("", 1000); //$NON-NLS-1$ | |
203 | if (monitor.isCanceled()) { | |
204 | throw new OperationCanceledException(); | |
205 | } | |
206 | fTrace.getResource().move(newPath, IResource.FORCE | IResource.SHALLOW, null); | |
207 | if (monitor.isCanceled()) { | |
208 | throw new OperationCanceledException(); | |
209 | } | |
210 | } finally { | |
211 | monitor.done(); | |
212 | } | |
213 | } | |
214 | }; | |
215 | ||
216 | try { | |
217 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
218 | } catch (InterruptedException exception) { | |
219 | return null; | |
220 | } catch (InvocationTargetException exception) { | |
221 | MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$ | |
222 | return null; | |
223 | } catch (RuntimeException exception) { | |
224 | return null; | |
225 | } | |
226 | ||
227 | return fTrace.getResource(); | |
228 | } | |
229 | ||
230 | } |