KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileDialog;
19
20 /**
21  * A field editor for a file path type preference. A standard file
22  * dialog appears when the user presses the change button.
23  */

24 public class FileFieldEditor extends StringButtonFieldEditor {
25
26     /**
27      * List of legal file extension suffixes, or <code>null</code>
28      * for system defaults.
29      */

30     private String JavaDoc[] extensions = null;
31
32     /**
33      * Indicates whether the path must be absolute;
34      * <code>false</code> by default.
35      */

36     private boolean enforceAbsolute = false;
37
38     /**
39      * Creates a new file field editor
40      */

41     protected FileFieldEditor() {
42     }
43
44     /**
45      * Creates a file field editor.
46      *
47      * @param name the name of the preference this field editor works on
48      * @param labelText the label text of the field editor
49      * @param parent the parent of the field editor's control
50      */

51     public FileFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
52         this(name, labelText, false, parent);
53     }
54
55     /**
56      * Creates a file field editor.
57      *
58      * @param name the name of the preference this field editor works on
59      * @param labelText the label text of the field editor
60      * @param enforceAbsolute <code>true</code> if the file path
61      * must be absolute, and <code>false</code> otherwise
62      * @param parent the parent of the field editor's control
63      */

64     public FileFieldEditor(String JavaDoc name, String JavaDoc labelText,
65             boolean enforceAbsolute, Composite parent) {
66         init(name, labelText);
67         this.enforceAbsolute = enforceAbsolute;
68         setErrorMessage(JFaceResources
69                 .getString("FileFieldEditor.errorMessage"));//$NON-NLS-1$
70
setChangeButtonText(JFaceResources.getString("openBrowse"));//$NON-NLS-1$
71
setValidateStrategy(VALIDATE_ON_FOCUS_LOST);
72         createControl(parent);
73     }
74
75     /* (non-Javadoc)
76      * Method declared on StringButtonFieldEditor.
77      * Opens the file chooser dialog and returns the selected file.
78      */

79     protected String JavaDoc changePressed() {
80         File JavaDoc f = new File JavaDoc(getTextControl().getText());
81         if (!f.exists()) {
82             f = null;
83         }
84         File JavaDoc d = getFile(f);
85         if (d == null) {
86             return null;
87         }
88
89         return d.getAbsolutePath();
90     }
91
92     /* (non-Javadoc)
93      * Method declared on StringFieldEditor.
94      * Checks whether the text input field specifies an existing file.
95      */

96     protected boolean checkState() {
97
98         String JavaDoc msg = null;
99
100         String JavaDoc path = getTextControl().getText();
101         if (path != null) {
102             path = path.trim();
103         } else {
104             path = "";//$NON-NLS-1$
105
}
106         if (path.length() == 0) {
107             if (!isEmptyStringAllowed()) {
108                 msg = getErrorMessage();
109             }
110         } else {
111             File JavaDoc file = new File JavaDoc(path);
112             if (file.isFile()) {
113                 if (enforceAbsolute && !file.isAbsolute()) {
114                     msg = JFaceResources
115                             .getString("FileFieldEditor.errorMessage2");//$NON-NLS-1$
116
}
117             } else {
118                 msg = getErrorMessage();
119             }
120         }
121
122         if (msg != null) { // error
123
showErrorMessage(msg);
124             return false;
125         }
126
127         // OK!
128
clearErrorMessage();
129         return true;
130     }
131
132     /**
133      * Helper to open the file chooser dialog.
134      * @param startingDirectory the directory to open the dialog on.
135      * @return File The File the user selected or <code>null</code> if they
136      * do not.
137      */

138     private File JavaDoc getFile(File JavaDoc startingDirectory) {
139
140         FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
141         if (startingDirectory != null) {
142             dialog.setFileName(startingDirectory.getPath());
143         }
144         if (extensions != null) {
145             dialog.setFilterExtensions(extensions);
146         }
147         String JavaDoc file = dialog.open();
148         if (file != null) {
149             file = file.trim();
150             if (file.length() > 0) {
151                 return new File JavaDoc(file);
152             }
153         }
154
155         return null;
156     }
157
158     /**
159      * Sets this file field editor's file extension filter.
160      *
161      * @param extensions a list of file extension, or <code>null</code>
162      * to set the filter to the system's default value
163      */

164     public void setFileExtensions(String JavaDoc[] extensions) {
165         this.extensions = extensions;
166     }
167 }
168
Popular Tags