1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.ui.wizards;
26
27 import org.eclipse.core.resources.*;
28 import org.eclipse.core.runtime.*;
29 import org.eclipse.osgi.util.NLS;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.*;
32 import org.eclipse.swt.layout.*;
33 import org.eclipse.swt.widgets.*;
34 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
35 import org.eclipse.ui.internal.ide.misc.ContainerSelectionGroup;
36
37 public class WSMLResourceContainerGroup implements Listener {
38
39
40 public static final int PROBLEM_NONE = 0;
41 public static final int PROBLEM_RESOURCE_EMPTY = 1;
42 public static final int PROBLEM_RESOURCE_EXIST = 2;
43 public static final int PROBLEM_RESOURCE_CONTAINS_SEPARATOR = 3;
44 public static final int PROBLEM_PATH_INVALID = 4;
45 public static final int PROBLEM_CONTAINER_EMPTY = 5;
46 public static final int PROBLEM_PROJECT_DOES_NOT_EXIST = 6;
47 public static final int PROBLEM_NAME_INVALID = 7;
48 public static final int PROBLEM_PATH_OCCUPIED = 8;
49
50
51 private Listener client;
52
53
54 private boolean allowExistingResources = false;
55
56
57 private String resourceType = IDEWorkbenchMessages.ResourceGroup_resource;
58
59
60 private String problemMessage = "";
61
62 private int problemType = PROBLEM_NONE;
63
64
65 private ContainerSelectionGroup containerGroup;
66
67 private Text resourceNameField;
68
69 public Button useIRIforName;
70
71 /***
72 * Create an instance of the group to allow the user
73 * to enter/select a container and specify a resource
74 * name.
75 *
76 * @param parent composite widget to parent the group
77 * @param client object interested in changes to the group's fields value
78 * @param resourceFieldLabel label to use in front of the resource name field
79 * @param resourceType one word, in lowercase, to describe the resource to the user (file, folder, project)
80 * @param showClosedProjects whether or not to show closed projects
81 * @param heightHint height hint for the container selection widget group
82 */
83 public WSMLResourceContainerGroup(Composite parent, Listener client, String resourceType,
84 int heightHint, int widthHint) {
85
86 this.resourceType = resourceType;
87 createContents(parent, heightHint, widthHint);
88 this.client = client;
89 }
90
91 /***
92 * Returns a boolean indicating whether all controls in this group
93 * contain valid values.
94 *
95 * @return boolean
96 */
97 public boolean areAllValuesValid() {
98 return problemType == PROBLEM_NONE;
99 }
100
101 /***
102 * Creates this object's visual components.
103 *
104 * @param parent org.eclipse.swt.widgets.Composite
105 * @param heightHint height hint for the container selection widget group
106 */
107 protected void createContents(Composite parent, int heightHint, int widthHint) {
108
109 Composite composite = new Composite(parent, SWT.NONE);
110 GridLayout layout = new GridLayout();
111 layout.marginWidth = 0;
112 layout.marginHeight = 0;
113 composite.setLayout(layout);
114 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
115
116
117 if (heightHint == SWT.DEFAULT)
118 containerGroup = new ContainerSelectionGroup(composite, this, true,
119 null, false);
120 else
121 containerGroup = new ContainerSelectionGroup(composite, this, true,
122 null, false, heightHint, widthHint);
123
124
125 Composite nameGroup = new Composite(composite, SWT.NONE);
126 nameGroup.setLayout(new GridLayout(2, false));
127 nameGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL
128 | GridData.GRAB_HORIZONTAL));
129
130 Label label = new Label(nameGroup, SWT.NONE);
131 label.setText("File name:");
132
133
134 resourceNameField = new Text(nameGroup, SWT.BORDER);
135 resourceNameField.addListener(SWT.Modify, this);
136 resourceNameField.setLayoutData(
137 new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
138
139 useIRIforName = new Button(nameGroup, SWT.CHECK);
140 useIRIforName.setText("Use IRI to generate filename");
141 GridData data = new GridData();
142 data.horizontalSpan = 2;
143 useIRIforName.setLayoutData(data);
144 useIRIforName.addSelectionListener(new SelectionAdapter() {
145 public void widgetSelected(SelectionEvent event) {
146 resourceNameField.setEnabled(useIRIforName.getSelection() == false);
147 }
148 });
149 useIRIforName.addListener(SWT.Selection, this);
150
151 validateControls();
152 }
153
154 /***
155 * Returns the path of the currently selected container
156 * or null if no container has been selected. Note that
157 * the container may not exist yet if the user entered
158 * a new container name in the field.
159 */
160 public IPath getContainerFullPath() {
161 return containerGroup.getContainerFullPath();
162 }
163
164 /***
165 * Returns an error message indicating the current problem with the value
166 * of a control in the group, or an empty message if all controls in the
167 * group contain valid values.
168 *
169 * @return java.lang.String
170 */
171 public String getProblemMessage() {
172 return problemMessage;
173 }
174
175 /***
176 * Returns the type of problem with the value of a control
177 * in the group.
178 *
179 * @return one of the PROBLEM_* constants
180 */
181 public int getProblemType() {
182 return problemType;
183 }
184
185 /***
186 * Returns a string that is the path of the currently selected
187 * container. Returns an empty string if no container has been
188 * selected.
189 */
190 public String getResource() {
191 return resourceNameField.getText();
192 }
193
194 /***
195 * Handles events for all controls in the group.
196 *
197 * @param e org.eclipse.swt.widgets.Event
198 */
199 public void handleEvent(Event e) {
200 validateControls();
201 if (client != null) {
202 client.handleEvent(e);
203 }
204 }
205
206 /***
207 * Sets the flag indicating whether existing resources are permitted.
208 */
209 public void setAllowExistingResources(boolean value) {
210 allowExistingResources = value;
211 }
212
213 /***
214 * Sets the value of this page's container.
215 *
216 * @param path Full path to the container.
217 */
218 public void setContainerFullPath(IPath path) {
219 IResource initial = ResourcesPlugin.getWorkspace().getRoot()
220 .findMember(path);
221 if (initial != null) {
222 if (!(initial instanceof IContainer)) {
223 initial = initial.getParent();
224 }
225 containerGroup.setSelectedContainer((IContainer) initial);
226 }
227 validateControls();
228 }
229
230 /***
231 * Gives focus to the resource name field and selects its contents
232 */
233 public void setFocus() {
234
235 resourceNameField.setSelection(0, resourceNameField.getText().length());
236 resourceNameField.setFocus();
237 }
238
239 /***
240 * Sets the value of this page's resource name.
241 *
242 * @param value new value
243 */
244 public void setResource(String value) {
245 resourceNameField.setText(value);
246 validateControls();
247 }
248
249 /***
250 * Returns a <code>boolean</code> indicating whether a container name represents
251 * a valid container resource in the workbench. An error message is stored for
252 * future reference if the name does not represent a valid container.
253 *
254 * @return <code>boolean</code> indicating validity of the container name
255 */
256 protected boolean validateContainer() {
257 IPath path = containerGroup.getContainerFullPath();
258 if (path == null) {
259 problemType = PROBLEM_CONTAINER_EMPTY;
260 problemMessage = IDEWorkbenchMessages.ResourceGroup_folderEmpty;
261 return false;
262 }
263 IWorkspace workspace = ResourcesPlugin.getWorkspace();
264 String projectName = path.segment(0);
265 if (projectName == null
266 || !workspace.getRoot().getProject(projectName).exists()) {
267 problemType = PROBLEM_PROJECT_DOES_NOT_EXIST;
268 problemMessage = IDEWorkbenchMessages.ResourceGroup_noProject;
269 return false;
270 }
271
272 IWorkspaceRoot root = workspace.getRoot();
273 while (path.segmentCount() > 1) {
274 if (root.getFile(path).exists()) {
275 problemType = PROBLEM_PATH_OCCUPIED;
276 problemMessage = NLS.bind(IDEWorkbenchMessages.ResourceGroup_pathOccupied, path.makeRelative());
277 return false;
278 }
279 path = path.removeLastSegments(1);
280 }
281 return true;
282 }
283
284 /***
285 * Validates the values for each of the group's controls. If an invalid
286 * value is found then a descriptive error message is stored for later
287 * reference. Returns a boolean indicating the validity of all of the
288 * controls in the group.
289 */
290 protected boolean validateControls() {
291
292 if (containerGroup == null) {
293 return false;
294 }
295 problemType = PROBLEM_NONE;
296 problemMessage = "";
297
298 if (!validateContainer() || !validateResourceName())
299 return false;
300
301 if (useIRIforName.getSelection() == false) {
302 IPath path = containerGroup.getContainerFullPath().append(
303 resourceNameField.getText());
304 return validateFullResourcePath(path);
305 }
306 return true;
307 }
308
309 /***
310 * Returns a <code>boolean</code> indicating whether the specified resource
311 * path represents a valid new resource in the workbench. An error message
312 * is stored for future reference if the path does not represent a valid
313 * new resource path.
314 *
315 * @param resourcePath the path to validate
316 * @return <code>boolean</code> indicating validity of the resource path
317 */
318 protected boolean validateFullResourcePath(IPath resourcePath) {
319 IWorkspace workspace = ResourcesPlugin.getWorkspace();
320
321 IStatus result = workspace.validatePath(resourcePath.toString(),
322 IResource.FOLDER);
323 if (!result.isOK()) {
324 problemType = PROBLEM_PATH_INVALID;
325 problemMessage = result.getMessage();
326 return false;
327 }
328
329 if (!allowExistingResources
330 && (workspace.getRoot().getFolder(resourcePath).exists() || workspace
331 .getRoot().getFile(resourcePath).exists())) {
332 problemType = PROBLEM_RESOURCE_EXIST;
333 problemMessage = IDEWorkbenchMessages.ResourceGroup_nameExists;
334 return false;
335 }
336 return true;
337 }
338
339 /***
340 * Returns a <code>boolean</code> indicating whether the resource name rep-
341 * resents a valid resource name in the workbench. An error message is stored
342 * for future reference if the name does not represent a valid resource name.
343 *
344 * @return <code>boolean</code> indicating validity of the resource name
345 */
346 protected boolean validateResourceName() {
347
348 if (useIRIforName.getSelection() == true) {
349 return true;
350 }
351
352 String resourceName = resourceNameField.getText();
353
354 if (resourceName.equals("")) {
355 problemType = PROBLEM_RESOURCE_EMPTY;
356 problemMessage = NLS.bind(IDEWorkbenchMessages.ResourceGroup_emptyName, resourceType);
357 return false;
358 }
359
360 if (!(new Path("")).isValidPath(resourceName)) {
361 problemType = PROBLEM_NAME_INVALID;
362 problemMessage = NLS.bind(IDEWorkbenchMessages.ResourceGroup_invalidFilename, resourceName);
363 return false;
364 }
365 return true;
366 }
367
368 }
369
370
371
372
373
374
375
376
377
378
379