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