Mercurial > projects > dwt-addons
comparison dwtx/jface/preference/FileFieldEditor.d @ 34:b3c8e32d406f
preference
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 05 Apr 2008 01:45:47 +0200 |
parents | |
children | ea8ff534f622 |
comparison
equal
deleted
inserted
replaced
33:f25582573129 | 34:b3c8e32d406f |
---|---|
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 * 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 } | |
61 | |
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 */ | |
71 public this(String name, String labelText, | |
72 bool enforceAbsolute, Composite parent) { | |
73 init(name, labelText); | |
74 this.enforceAbsolute = enforceAbsolute; | |
75 setErrorMessage(JFaceResources | |
76 .getString("FileFieldEditor.errorMessage"));//$NON-NLS-1$ | |
77 setChangeButtonText(JFaceResources.getString("openBrowse"));//$NON-NLS-1$ | |
78 setValidateStrategy(VALIDATE_ON_FOCUS_LOST); | |
79 createControl(parent); | |
80 } | |
81 | |
82 /* (non-Javadoc) | |
83 * Method declared on StringButtonFieldEditor. | |
84 * Opens the file chooser dialog and returns the selected file. | |
85 */ | |
86 protected String changePressed() { | |
87 auto f = new FilePath(getTextControl().getText()); | |
88 if (!f.exists()) { | |
89 f = cast(FilePath)null; | |
90 } | |
91 auto d = getFile(f); | |
92 if (d is null) { | |
93 return null; | |
94 } | |
95 | |
96 return FileSystem.toAbsolute( d ).toString(); | |
97 } | |
98 | |
99 /* (non-Javadoc) | |
100 * Method declared on StringFieldEditor. | |
101 * Checks whether the text input field specifies an existing file. | |
102 */ | |
103 protected bool checkState() { | |
104 | |
105 String msg = null; | |
106 | |
107 String path = getTextControl().getText(); | |
108 if (path !is null) { | |
109 path = path.trim(); | |
110 } else { | |
111 path = "";//$NON-NLS-1$ | |
112 } | |
113 if (path.length is 0) { | |
114 if (!isEmptyStringAllowed()) { | |
115 msg = getErrorMessage(); | |
116 } | |
117 } else { | |
118 auto file = new FilePath(path); | |
119 if (/+file.isFile()+/ file.exists && !file.isFolder ) { | |
120 if (enforceAbsolute && !file.isAbsolute()) { | |
121 msg = JFaceResources | |
122 .getString("FileFieldEditor.errorMessage2");//$NON-NLS-1$ | |
123 } | |
124 } else { | |
125 msg = getErrorMessage(); | |
126 } | |
127 } | |
128 | |
129 if (msg !is null) { // error | |
130 showErrorMessage(msg); | |
131 return false; | |
132 } | |
133 | |
134 // OK! | |
135 clearErrorMessage(); | |
136 return true; | |
137 } | |
138 | |
139 /** | |
140 * Helper to open the file chooser dialog. | |
141 * @param startingDirectory the directory to open the dialog on. | |
142 * @return File The File the user selected or <code>null</code> if they | |
143 * do not. | |
144 */ | |
145 private FilePath getFile(FilePath startingDirectory) { | |
146 | |
147 FileDialog dialog = new FileDialog(getShell(), DWT.OPEN); | |
148 if (startingDirectory !is null) { | |
149 dialog.setFileName(startingDirectory.path()); | |
150 } | |
151 if (extensions !is null) { | |
152 dialog.setFilterExtensions(extensions); | |
153 } | |
154 String file = dialog.open(); | |
155 if (file !is null) { | |
156 file = file.trim(); | |
157 if (file.length > 0) { | |
158 return new FilePath(file); | |
159 } | |
160 } | |
161 | |
162 return null; | |
163 } | |
164 | |
165 /** | |
166 * Sets this file field editor's file extension filter. | |
167 * | |
168 * @param extensions a list of file extension, or <code>null</code> | |
169 * to set the filter to the system's default value | |
170 */ | |
171 public void setFileExtensions(String[] extensions) { | |
172 this.extensions = extensions; | |
173 } | |
174 } |