KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > DirectoryFieldEditor


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  *******************************************************************************/

11 package org.eclipse.jface.preference;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.DirectoryDialog;
19
20 /**
21  * A field editor for a directory path type preference. A standard directory
22  * dialog appears when the user presses the change button.
23  */

24 public class DirectoryFieldEditor extends StringButtonFieldEditor {
25     /**
26      * Creates a new directory field editor
27      */

28     protected DirectoryFieldEditor() {
29     }
30
31     /**
32      * Creates a directory field editor.
33      *
34      * @param name the name of the preference this field editor works on
35      * @param labelText the label text of the field editor
36      * @param parent the parent of the field editor's control
37      */

38     public DirectoryFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
39         init(name, labelText);
40         setErrorMessage(JFaceResources
41                 .getString("DirectoryFieldEditor.errorMessage"));//$NON-NLS-1$
42
setChangeButtonText(JFaceResources.getString("openBrowse"));//$NON-NLS-1$
43
setValidateStrategy(VALIDATE_ON_FOCUS_LOST);
44         createControl(parent);
45     }
46
47     /* (non-Javadoc)
48      * Method declared on StringButtonFieldEditor.
49      * Opens the directory chooser dialog and returns the selected directory.
50      */

51     protected String JavaDoc changePressed() {
52         File JavaDoc f = new File JavaDoc(getTextControl().getText());
53         if (!f.exists()) {
54             f = null;
55         }
56         File JavaDoc d = getDirectory(f);
57         if (d == null) {
58             return null;
59         }
60
61         return d.getAbsolutePath();
62     }
63
64     /* (non-Javadoc)
65      * Method declared on StringFieldEditor.
66      * Checks whether the text input field contains a valid directory.
67      */

68     protected boolean doCheckState() {
69         String JavaDoc fileName = getTextControl().getText();
70         fileName = fileName.trim();
71         if (fileName.length() == 0 && isEmptyStringAllowed()) {
72             return true;
73         }
74         File JavaDoc file = new File JavaDoc(fileName);
75         return file.isDirectory();
76     }
77
78     /**
79      * Helper that opens the directory chooser dialog.
80      * @param startingDirectory The directory the dialog will open in.
81      * @return File File or <code>null</code>.
82      *
83      */

84     private File JavaDoc getDirectory(File JavaDoc startingDirectory) {
85
86         DirectoryDialog fileDialog = new DirectoryDialog(getShell(), SWT.OPEN);
87         if (startingDirectory != null) {
88             fileDialog.setFilterPath(startingDirectory.getPath());
89         }
90         String JavaDoc dir = fileDialog.open();
91         if (dir != null) {
92             dir = dir.trim();
93             if (dir.length() > 0) {
94                 return new File JavaDoc(dir);
95             }
96         }
97
98         return null;
99     }
100 }
101
Popular Tags