KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > PathVariableDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids <sdavids@gmx.de> - 19346, 42056
11  *******************************************************************************/

12 package org.eclipse.ui.internal.ide.dialogs;
13
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IPathVariableManager;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.jface.dialogs.IMessageProvider;
23 import org.eclipse.jface.dialogs.TitleAreaDialog;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.FormAttachment;
30 import org.eclipse.swt.layout.FormData;
31 import org.eclipse.swt.layout.FormLayout;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.DirectoryDialog;
37 import org.eclipse.swt.widgets.FileDialog;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
42
43 /**
44  * Dialog that prompts the user for defining a variable's name and value. It
45  * supports creating a new variable or editing an existing one. The difference
46  * between the two uses is just a matter of which messages to present to the
47  * user and whether the "Ok" button starts enabled or not.
48  */

49 public class PathVariableDialog extends TitleAreaDialog {
50
51     // UI widgets
52
private Button okButton;
53
54     private Label variableNameLabel;
55
56     private Label variableValueLabel;
57
58     private Text variableNameField;
59
60     private Text variableValueField;
61
62     private Button fileButton;
63
64     private Button folderButton;
65
66     /**
67      * This dialog type: <code>NEW_VARIABLE</code> or
68      * <code>EXISTING_VARIABLE</code>.
69      */

70     private int type;
71
72     /**
73      * The type of variable that can be edited in this dialog.
74      * <code>IResource.FILE</code> or <code>IResource.FOLDER</code>
75      */

76     private int variableType;
77
78     /**
79      * The name of the variable being edited.
80      */

81     private String JavaDoc variableName;
82
83     /**
84      * The value of the variable being edited.
85      */

86     private String JavaDoc variableValue;
87
88     /**
89      * The original name of the variable being edited. It is used when testing
90      * if the current variable's name is already in use.
91      */

92     private String JavaDoc originalName;
93
94     /**
95      * Used to select the proper message depending on the current mode
96      * (new/existing variable).
97      */

98     private boolean newVariable;
99
100     /**
101      * Reference to the path variable manager. It is used for validating
102      * variable names.
103      */

104     private IPathVariableManager pathVariableManager;
105
106     /**
107      * Set of variable names currently in use. Used when warning the user that
108      * the currently selected name is already in use by another variable.
109      */

110     private Set JavaDoc namesInUse;
111
112     /**
113      * The current validation status. Its value can be one of the following:<ul>
114      * <li><code>IMessageProvider.NONE</code> (default);</li>
115      * <li><code>IMessageProvider.WARNING</code>;</li>
116      * <li><code>IMessageProvider.ERROR</code>;</li>
117      * </ul>
118      * Used when validating the user input.
119      */

120     private int validationStatus;
121
122     /**
123      * The current validation message generated by the last
124      * call to a <code>validate</code> method.
125      */

126     private String JavaDoc validationMessage;
127
128     /**
129      * Whether a variable name has been entered.
130      */

131     private boolean nameEntered = false;
132
133     /**
134      * Whether a variable location has been entered.
135      */

136     private boolean locationEntered = false;
137
138     /**
139      * The standard message to be shown when there are no problems being
140      * reported.
141      */

142     final private String JavaDoc standardMessage;
143
144     /**
145      * Constant for defining this dialog as intended to create a new variable
146      * (value = 1).
147      */

148     public final static int NEW_VARIABLE = 1;
149
150     /**
151      * Constant for defining this dialog as intended to edit an existing
152      * variable (value = 2).
153      */

154     public final static int EXISTING_VARIABLE = 2;
155
156     /**
157      * Constructs a dialog for editing a new/existing path variable.
158      *
159      * @param parentShell the parent shell
160      * @param type the dialog type: <code>NEW_VARIABLE</code> or
161      * <code>EXISTING_VARIABLE</code>
162      * @param variableType the type of variable that can be edited in
163      * this dialog. <code>IResource.FILE</code> or <code>IResource.FOLDER</code>
164      * @param pathVariableManager a reference to the path variable manager
165      * @param namesInUse a set of variable names currently in use
166      */

167     public PathVariableDialog(Shell parentShell, int type, int variableType,
168             IPathVariableManager pathVariableManager, Set JavaDoc namesInUse) {
169         super(parentShell);
170         setShellStyle(getShellStyle() | SWT.RESIZE);
171         this.type = type;
172         this.newVariable = type == NEW_VARIABLE;
173         this.variableName = ""; //$NON-NLS-1$
174
this.variableValue = ""; //$NON-NLS-1$
175
this.variableType = variableType;
176         this.pathVariableManager = pathVariableManager;
177         this.namesInUse = namesInUse;
178
179         if (newVariable) {
180             this.standardMessage = IDEWorkbenchMessages.PathVariableDialog_message_newVariable;
181         } else {
182             this.standardMessage = IDEWorkbenchMessages.PathVariableDialog_message_existingVariable;
183         }
184     }
185
186     /**
187      * Configures this dialog's shell, setting the shell's text.
188      *
189      * @see org.eclipse.jface.window.Window#configureShell(Shell)
190      */

191     protected void configureShell(Shell shell) {
192         super.configureShell(shell);
193         if (newVariable) {
194             shell.setText(IDEWorkbenchMessages.PathVariableDialog_shellTitle_newVariable);
195         } else {
196             shell
197                     .setText(IDEWorkbenchMessages.PathVariableDialog_shellTitle_existingVariable);
198         }
199     }
200
201     /**
202      * Creates and returns the contents of this dialog (except for the button bar).
203      *
204      * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea
205      */

206     protected Control createDialogArea(Composite parent) {
207         // top level composite
208
Composite parentComposite = (Composite) super.createDialogArea(parent);
209
210         initializeDialogUnits(parentComposite);
211         
212         // creates dialog area composite
213
Composite contents = createComposite(parentComposite);
214
215         // creates and lay outs dialog area widgets
216
createWidgets(contents);
217
218         // validate possibly already incorrect variable definitions
219
if (type == EXISTING_VARIABLE) {
220             nameEntered = locationEntered = true;
221             validateVariableValue();
222         }
223
224         Dialog.applyDialogFont(parentComposite);
225         
226         return contents;
227     }
228
229     /**
230      * Creates and configures this dialog's main composite.
231      *
232      * @param parentComposite parent's composite
233      * @return this dialog's main composite
234      */

235     private Composite createComposite(Composite parentComposite) {
236         // creates a composite with standard margins and spacing
237
Composite contents = new Composite(parentComposite, SWT.NONE);
238
239         FormLayout layout = new FormLayout();
240
241         contents.setLayout(layout);
242         contents.setLayoutData(new GridData(GridData.FILL_BOTH));
243
244         if (newVariable) {
245             setTitle(IDEWorkbenchMessages.PathVariableDialog_dialogTitle_newVariable);
246         } else {
247             setTitle(IDEWorkbenchMessages.PathVariableDialog_dialogTitle_existingVariable);
248         }
249         setMessage(standardMessage);
250         return contents;
251     }
252
253     /**
254      * Creates widgets for this dialog.
255      *
256      * @param contents the parent composite where to create widgets
257      */

258     private void createWidgets(Composite contents) {
259         FormData data;
260
261         String JavaDoc nameLabelText = IDEWorkbenchMessages.PathVariableDialog_variableName;
262         String JavaDoc valueLabelText = IDEWorkbenchMessages.PathVariableDialog_variableValue;
263
264         // variable name label
265
variableNameLabel = new Label(contents, SWT.LEFT);
266         variableNameLabel.setText(nameLabelText);
267
268         data = new FormData();
269         data.top = new FormAttachment(0,
270                 convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN));
271         data.left = new FormAttachment(0,
272                 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN));
273         variableNameLabel.setLayoutData(data);
274      
275         // variable name field. Attachments done after all widgets created.
276
variableNameField = new Text(contents, SWT.SINGLE | SWT.BORDER);
277         variableNameField.setText(variableName);
278         variableNameField.addModifyListener(new ModifyListener() {
279             public void modifyText(ModifyEvent event) {
280                 variableNameModified();
281             }
282         });
283         
284         // variable value label
285
variableValueLabel = new Label(contents, SWT.LEFT);
286         variableValueLabel.setText(valueLabelText);
287
288         data = new FormData();
289         data.left = new FormAttachment(0,
290                 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN));
291         data.top = new FormAttachment(variableNameLabel,
292                 convertVerticalDLUsToPixels(5));
293         variableValueLabel.setLayoutData(data);
294
295         // variable value field. Attachments done after all widgets created.
296
variableValueField = new Text(contents, SWT.SINGLE | SWT.BORDER);
297         variableValueField.setText(variableValue);
298         variableValueField.addModifyListener(new ModifyListener() {
299             public void modifyText(ModifyEvent event) {
300                 variableValueModified();
301             }
302         });
303
304         // select file path button
305
fileButton = new Button(contents, SWT.PUSH);
306         fileButton.setText(IDEWorkbenchMessages.PathVariableDialog_file);
307         if ((variableType & IResource.FILE) == 0) {
308             fileButton.setEnabled(false);
309         }
310
311         data = setButtonFormLayoutData(fileButton);
312         data.top = new FormAttachment(variableValueLabel, 0, SWT.CENTER);
313         data.right = new FormAttachment(100,
314                 -convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN));
315         fileButton.setLayoutData(data);
316
317         fileButton.addSelectionListener(new SelectionAdapter() {
318             public void widgetSelected(SelectionEvent e) {
319                 selectFile();
320             }
321         });
322
323         // select folder path button
324
folderButton = new Button(contents, SWT.PUSH);
325         folderButton.setText(IDEWorkbenchMessages.PathVariableDialog_folder);
326         if ((variableType & IResource.FOLDER) == 0) {
327             folderButton.setEnabled(false);
328         }
329
330         data = setButtonFormLayoutData(folderButton);
331         data.top = new FormAttachment(fileButton, convertVerticalDLUsToPixels(2));
332         data.right = new FormAttachment(100,
333                 -convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN));
334         folderButton.setLayoutData(data);
335
336         folderButton.addSelectionListener(new SelectionAdapter() {
337             public void widgetSelected(SelectionEvent e) {
338                 selectFolder();
339             }
340         });
341         
342         // Attaching variable name and value fields to file and folder buttons,
343
// so do this now that those buttons have been created.
344

345         // the larger label will be used in the left attachments for the fields
346
Label largerLabel = nameLabelText.length() > valueLabelText.length() ? variableNameLabel
347                 : variableValueLabel;
348  
349         data = new FormData();
350         data.left = new FormAttachment(largerLabel,
351                 convertHorizontalDLUsToPixels(5));
352         data.right = new FormAttachment(fileButton, -convertHorizontalDLUsToPixels(5));
353         data.top = new FormAttachment(variableNameLabel,
354                 convertVerticalDLUsToPixels(5), SWT.CENTER);
355         variableNameField.setLayoutData(data);
356         
357
358         data = new FormData();
359         data.left = new FormAttachment(largerLabel,
360                 convertHorizontalDLUsToPixels(5));
361         data.right = new FormAttachment(fileButton, -convertHorizontalDLUsToPixels(5));
362         data.top = new FormAttachment(variableValueLabel, 0, SWT.CENTER);
363         variableValueField.setLayoutData(data);
364
365   
366  
367     }
368
369     /**
370      * Sets the <code>FormData</code> on the specified button to be one that is
371      * spaced for the current dialog page units. The method
372      * <code>initializeDialogUnits</code> must be called once before calling this
373      * method for the first time.
374      *
375      * @param button the button to set the <code>FormData</code>
376      * @return the <code>FormData</code> set on the specified button
377      */

378     private FormData setButtonFormLayoutData(Button button) {
379         FormData data = new FormData();
380         int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
381         data.width = Math.max(widthHint, button.computeSize(SWT.DEFAULT,
382                 SWT.DEFAULT, true).x);
383         button.setLayoutData(data);
384         return data;
385     }
386
387     /**
388      * Fires validations (variable name first) and updates enabled state for the
389      * "Ok" button accordingly.
390      */

391     private void variableNameModified() {
392         // updates and validates the variable name
393
variableName = variableNameField.getText();
394         validationStatus = IMessageProvider.NONE;
395         okButton.setEnabled(validateVariableName() && validateVariableValue());
396         nameEntered = true;
397     }
398
399     /**
400      * Fires validations (variable value first) and updates enabled state for the
401      * "Ok" button accordingly.
402      */

403     private void variableValueModified() {
404         // updates and validates the variable value
405
variableValue = variableValueField.getText().trim();
406         validationStatus = IMessageProvider.NONE;
407         okButton.setEnabled(validateVariableValue() && validateVariableName());
408         locationEntered = true;
409     }
410
411     /**
412      * Opens a dialog where the user can select a folder path.
413      */

414     private void selectFolder() {
415         DirectoryDialog dialog = new DirectoryDialog(getShell());
416         dialog.setText(IDEWorkbenchMessages.PathVariableDialog_selectFolderTitle);
417         dialog.setMessage(IDEWorkbenchMessages.PathVariableDialog_selectFolderMessage);
418         dialog.setFilterPath(variableValue);
419         String JavaDoc res = dialog.open();
420         if (res != null) {
421             variableValue = new Path(res).makeAbsolute().toOSString();
422             variableValueField.setText(variableValue);
423         }
424     }
425
426     /**
427      * Opens a dialog where the user can select a file path.
428      */

429     private void selectFile() {
430         FileDialog dialog = new FileDialog(getShell());
431         dialog.setText(IDEWorkbenchMessages.PathVariableDialog_selectFileTitle);
432         dialog.setFilterPath(variableValue);
433         String JavaDoc res = dialog.open();
434         if (res != null) {
435             variableValue = new Path(res).makeAbsolute().toOSString();
436             variableValueField.setText(variableValue);
437         }
438     }
439
440     /**
441      * Adds buttons to this dialog's button bar.
442      *
443      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar
444      */

445     protected void createButtonsForButtonBar(Composite parent) {
446         okButton = createButton(parent, IDialogConstants.OK_ID,
447                 IDialogConstants.OK_LABEL, true);
448         okButton.setEnabled(type == EXISTING_VARIABLE);
449
450         createButton(parent, IDialogConstants.CANCEL_ID,
451                 IDialogConstants.CANCEL_LABEL, false);
452     }
453
454     /**
455      * Validates the current variable name, and updates this dialog's message.
456      *
457      * @return true if the name is valid, false otherwise
458      */

459     private boolean validateVariableName() {
460         boolean allowFinish = false;
461
462         // if the current validationStatus is ERROR, no additional validation applies
463
if (validationStatus == IMessageProvider.ERROR) {
464             return false;
465         }
466
467         // assumes everything will be ok
468
String JavaDoc message = standardMessage;
469         int newValidationStatus = IMessageProvider.NONE;
470
471         if (variableName.length() == 0) {
472             // the variable name is empty
473
if (nameEntered) {
474                 // a name was entered before and is now empty
475
newValidationStatus = IMessageProvider.ERROR;
476                 message = IDEWorkbenchMessages.PathVariableDialog_variableNameEmptyMessage;
477             }
478         } else {
479             IStatus status = pathVariableManager.validateName(variableName);
480             if (!status.isOK()) {
481                 // the variable name is not valid
482
newValidationStatus = IMessageProvider.ERROR;
483                 message = status.getMessage();
484             } else if (namesInUse.contains(variableName)
485                     && !variableName.equals(originalName)) {
486                 // the variable name is already in use
487
message = IDEWorkbenchMessages.PathVariableDialog_variableAlreadyExistsMessage;
488                 newValidationStatus = IMessageProvider.ERROR;
489             } else {
490                 allowFinish = true;
491             }
492         }
493
494         // overwrite the current validation status / message only if everything is ok (clearing them)
495
// or if we have a more serious problem than the current one
496
if (validationStatus == IMessageProvider.NONE
497                 || newValidationStatus == IMessageProvider.ERROR) {
498             validationStatus = newValidationStatus;
499             validationMessage = message;
500         }
501         // only set the message here if it is not going to be set in
502
// validateVariableValue to avoid flashing.
503
if (allowFinish == false) {
504             setMessage(validationMessage, validationStatus);
505         }
506         return allowFinish;
507     }
508
509     /**
510      * Validates the current variable value, and updates this dialog's message.
511      *
512      * @return true if the value is valid, false otherwise
513      */

514     private boolean validateVariableValue() {
515         boolean allowFinish = false;
516
517         // if the current validationStatus is ERROR, no additional validation applies
518
if (validationStatus == IMessageProvider.ERROR) {
519             return false;
520         }
521
522         // assumes everything will be ok
523
String JavaDoc message = standardMessage;
524         int newValidationStatus = IMessageProvider.NONE;
525
526         if (variableValue.length() == 0) {
527             // the variable value is empty
528
if (locationEntered) {
529                 // a location value was entered before and is now empty
530
newValidationStatus = IMessageProvider.ERROR;
531                 message = IDEWorkbenchMessages.PathVariableDialog_variableValueEmptyMessage;
532             }
533         } else if (!Path.EMPTY.isValidPath(variableValue)) {
534             // the variable value is an invalid path
535
message = IDEWorkbenchMessages.PathVariableDialog_variableValueInvalidMessage;
536             newValidationStatus = IMessageProvider.ERROR;
537         } else if (!new Path(variableValue).isAbsolute()) {
538             // the variable value is a relative path
539
message = IDEWorkbenchMessages.PathVariableDialog_pathIsRelativeMessage;
540             newValidationStatus = IMessageProvider.ERROR;
541         } else if (!IDEResourceInfoUtils.exists(variableValue)) {
542             // the path does not exist (warning)
543
message = IDEWorkbenchMessages.PathVariableDialog_pathDoesNotExistMessage;
544             newValidationStatus = IMessageProvider.WARNING;
545             allowFinish = true;
546         } else {
547             allowFinish = true;
548         }
549
550         // overwrite the current validation status / message only if everything is ok (clearing them)
551
// or if we have a more serious problem than the current one
552
if (validationStatus == IMessageProvider.NONE
553                 || newValidationStatus > validationStatus) {
554             validationStatus = newValidationStatus;
555             validationMessage = message;
556         }
557         setMessage(validationMessage, validationStatus);
558         return allowFinish;
559     }
560
561     /**
562      * Returns the variable name.
563      *
564      * @return the variable name
565      */

566     public String JavaDoc getVariableName() {
567         return variableName;
568     }
569
570     /**
571      * Returns the variable value.
572      *
573      * @return the variable value
574      */

575     public String JavaDoc getVariableValue() {
576         return variableValue;
577     }
578
579     /**
580      * Sets the variable name.
581      *
582      * @param variableName the new variable name
583      */

584     public void setVariableName(String JavaDoc variableName) {
585         this.variableName = variableName.trim();
586         this.originalName = this.variableName;
587     }
588
589     /**
590      * Sets the variable value.
591      *
592      * @param variableValue the new variable value
593      */

594     public void setVariableValue(String JavaDoc variableValue) {
595         this.variableValue = variableValue;
596     }
597
598 }
599
Popular Tags