Mercurial > projects > dwt-addons
annotate dwtx/jface/preference/FileFieldEditor.d @ 70:46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 01:36:46 +0200 |
parents | 3ed358ab21c9 |
children | 0168c579a3f9 |
rev | line source |
---|---|
34 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others. |
34 | 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.preference.FileFieldEditor; | |
14 | |
15 import dwtx.jface.preference.StringButtonFieldEditor; | |
16 // import java.io.File; | |
17 | |
18 import dwt.DWT; | |
19 import dwt.widgets.Composite; | |
20 import dwt.widgets.FileDialog; | |
21 import dwtx.jface.resource.JFaceResources; | |
22 | |
23 import dwt.dwthelper.utils; | |
24 import tango.io.FilePath; | |
25 import tango.io.FileSystem; | |
26 | |
27 /** | |
28 * A field editor for a file path type preference. A standard file | |
29 * dialog appears when the user presses the change button. | |
30 */ | |
31 public class FileFieldEditor : StringButtonFieldEditor { | |
32 | |
33 /** | |
34 * List of legal file extension suffixes, or <code>null</code> | |
35 * for system defaults. | |
36 */ | |
37 private String[] extensions = null; | |
38 | |
39 /** | |
40 * Indicates whether the path must be absolute; | |
41 * <code>false</code> by default. | |
42 */ | |
43 private bool enforceAbsolute = false; | |
44 | |
45 /** | |
46 * Creates a new file field editor | |
47 */ | |
48 protected this() { | |
49 } | |
50 | |
51 /** | |
52 * Creates a file field editor. | |
53 * | |
54 * @param name the name of the preference this field editor works on | |
55 * @param labelText the label text of the field editor | |
56 * @param parent the parent of the field editor's control | |
57 */ | |
58 public this(String name, String labelText, Composite parent) { | |
59 this(name, labelText, false, parent); | |
60 } | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
61 |
34 | 62 /** |
63 * Creates a file field editor. | |
64 * | |
65 * @param name the name of the preference this field editor works on | |
66 * @param labelText the label text of the field editor | |
67 * @param enforceAbsolute <code>true</code> if the file path | |
68 * must be absolute, and <code>false</code> otherwise | |
69 * @param parent the parent of the field editor's control | |
70 */ | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
71 public this(String name, String labelText, bool enforceAbsolute, Composite parent) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
72 this(name, labelText, enforceAbsolute, VALIDATE_ON_FOCUS_LOST, parent); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
73 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
74 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
75 * Creates a file field editor. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
76 * |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
77 * @param name the name of the preference this field editor works on |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
78 * @param labelText the label text of the field editor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
79 * @param enforceAbsolute <code>true</code> if the file path |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
80 * must be absolute, and <code>false</code> otherwise |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
81 * @param validationStrategy either {@link StringButtonFieldEditor#VALIDATE_ON_KEY_STROKE} |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
82 * to perform on the fly checking, or {@link StringButtonFieldEditor#VALIDATE_ON_FOCUS_LOST} |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
83 * (the default) to perform validation only after the text has been typed in |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
84 * @param parent the parent of the field editor's control. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
85 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
86 * @see StringButtonFieldEditor#VALIDATE_ON_KEY_STROKE |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
87 * @see StringButtonFieldEditor#VALIDATE_ON_FOCUS_LOST |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
88 */ |
34 | 89 public this(String name, String labelText, |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
90 bool enforceAbsolute, int validationStrategy, Composite parent) { |
34 | 91 init(name, labelText); |
92 this.enforceAbsolute = enforceAbsolute; | |
93 setErrorMessage(JFaceResources | |
94 .getString("FileFieldEditor.errorMessage"));//$NON-NLS-1$ | |
95 setChangeButtonText(JFaceResources.getString("openBrowse"));//$NON-NLS-1$ | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
66
diff
changeset
|
96 setValidateStrategy(validationStrategy); |
34 | 97 createControl(parent); |
98 } | |
99 | |
100 /* (non-Javadoc) | |
101 * Method declared on StringButtonFieldEditor. | |
102 * Opens the file chooser dialog and returns the selected file. | |
103 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
104 protected override String changePressed() { |
34 | 105 auto f = new FilePath(getTextControl().getText()); |
106 if (!f.exists()) { | |
107 f = cast(FilePath)null; | |
108 } | |
109 auto d = getFile(f); | |
110 if (d is null) { | |
111 return null; | |
112 } | |
113 | |
66
3ed358ab21c9
The FieldEditors shall show Paths in native look
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
114 return FileSystem.toAbsolute( d ).native.toString(); |
34 | 115 } |
116 | |
117 /* (non-Javadoc) | |
118 * Method declared on StringFieldEditor. | |
119 * Checks whether the text input field specifies an existing file. | |
120 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
121 protected override bool checkState() { |
34 | 122 |
123 String msg = null; | |
124 | |
125 String path = getTextControl().getText(); | |
126 if (path !is null) { | |
127 path = path.trim(); | |
128 } else { | |
129 path = "";//$NON-NLS-1$ | |
130 } | |
131 if (path.length is 0) { | |
132 if (!isEmptyStringAllowed()) { | |
133 msg = getErrorMessage(); | |
134 } | |
135 } else { | |
136 auto file = new FilePath(path); | |
137 if (/+file.isFile()+/ file.exists && !file.isFolder ) { | |
138 if (enforceAbsolute && !file.isAbsolute()) { | |
139 msg = JFaceResources | |
140 .getString("FileFieldEditor.errorMessage2");//$NON-NLS-1$ | |
141 } | |
142 } else { | |
143 msg = getErrorMessage(); | |
144 } | |
145 } | |
146 | |
147 if (msg !is null) { // error | |
148 showErrorMessage(msg); | |
149 return false; | |
150 } | |
151 | |
152 // OK! | |
153 clearErrorMessage(); | |
154 return true; | |
155 } | |
156 | |
157 /** | |
158 * Helper to open the file chooser dialog. | |
159 * @param startingDirectory the directory to open the dialog on. | |
160 * @return File The File the user selected or <code>null</code> if they | |
161 * do not. | |
162 */ | |
163 private FilePath getFile(FilePath startingDirectory) { | |
164 | |
165 FileDialog dialog = new FileDialog(getShell(), DWT.OPEN); | |
166 if (startingDirectory !is null) { | |
167 dialog.setFileName(startingDirectory.path()); | |
168 } | |
169 if (extensions !is null) { | |
170 dialog.setFilterExtensions(extensions); | |
171 } | |
172 String file = dialog.open(); | |
173 if (file !is null) { | |
174 file = file.trim(); | |
175 if (file.length > 0) { | |
176 return new FilePath(file); | |
177 } | |
178 } | |
179 | |
180 return null; | |
181 } | |
182 | |
183 /** | |
184 * Sets this file field editor's file extension filter. | |
185 * | |
186 * @param extensions a list of file extension, or <code>null</code> | |
187 * to set the filter to the system's default value | |
188 */ | |
189 public void setFileExtensions(String[] extensions) { | |
190 this.extensions = extensions; | |
191 } | |
192 } |