comparison dwt/widgets/FileDialog.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 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 module dwt.widgets.FileDialog;
12
13 import dwt.dwthelper.utils;
14
15
16 import dwt.DWT;
17 import dwt.DWTException;
18 import dwt.internal.cocoa.NSArray;
19 import dwt.internal.cocoa.NSOpenPanel;
20 import dwt.internal.cocoa.NSSavePanel;
21 import dwt.internal.cocoa.NSString;
22 import dwt.internal.cocoa.OS;
23
24 /**
25 * Instances of this class allow the user to navigate
26 * the file system and select or enter a file name.
27 * <dl>
28 * <dt><b>Styles:</b></dt>
29 * <dd>SAVE, OPEN, MULTI</dd>
30 * <dt><b>Events:</b></dt>
31 * <dd>(none)</dd>
32 * </dl>
33 * <p>
34 * Note: Only one of the styles SAVE and OPEN may be specified.
35 * </p><p>
36 * IMPORTANT: This class is intended to be subclassed <em>only</em>
37 * within the DWT implementation.
38 * </p>
39 */
40 public class FileDialog extends Dialog {
41 String [] filterNames = new String [0];
42 String [] filterExtensions = new String [0];
43 String [] fileNames = new String[0];
44 String filterPath = "", fileName = "";
45 int filterIndex = -1;
46 bool overwrite = true; //TODO: if setOverwrite(false) is implemented, change default to false for consistency
47 static final char EXTENSION_SEPARATOR = ';';
48
49 /**
50 * Constructs a new instance of this class given only its parent.
51 *
52 * @param parent a shell which will be the parent of the new instance
53 *
54 * @exception IllegalArgumentException <ul>
55 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
56 * </ul>
57 * @exception DWTException <ul>
58 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
59 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
60 * </ul>
61 */
62 public FileDialog (Shell parent) {
63 this (parent, DWT.APPLICATION_MODAL);
64 }
65
66 /**
67 * Constructs a new instance of this class given its parent
68 * and a style value describing its behavior and appearance.
69 * <p>
70 * The style value is either one of the style constants defined in
71 * class <code>DWT</code> which is applicable to instances of this
72 * class, or must be built by <em>bitwise OR</em>'ing together
73 * (that is, using the <code>int</code> "|" operator) two or more
74 * of those <code>DWT</code> style constants. The class description
75 * lists the style constants that are applicable to the class.
76 * Style bits are also inherited from superclasses.
77 * </p>
78 *
79 * @param parent a shell which will be the parent of the new instance
80 * @param style the style of dialog to construct
81 *
82 * @exception IllegalArgumentException <ul>
83 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
84 * </ul>
85 * @exception DWTException <ul>
86 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
87 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
88 * </ul>
89 */
90 public FileDialog (Shell parent, int style) {
91 super (parent, style);
92 checkSubclass ();
93 }
94
95 /**
96 * Returns the path of the first file that was
97 * selected in the dialog relative to the filter path, or an
98 * empty string if no such file has been selected.
99 *
100 * @return the relative path of the file
101 */
102 public String getFileName () {
103 return fileName;
104 }
105
106 /**
107 * Returns a (possibly empty) array with the paths of all files
108 * that were selected in the dialog relative to the filter path.
109 *
110 * @return the relative paths of the files
111 */
112 public String [] getFileNames () {
113 return fileNames;
114 }
115
116 /**
117 * Returns the file extensions which the dialog will
118 * use to filter the files it shows.
119 *
120 * @return the file extensions filter
121 */
122 public String [] getFilterExtensions () {
123 return filterExtensions;
124 }
125
126 /**
127 * Get the 0-based index of the file extension filter
128 * which was selected by the user, or -1 if no filter
129 * was selected.
130 * <p>
131 * This is an index into the FilterExtensions array and
132 * the FilterNames array.
133 * </p>
134 *
135 * @return index the file extension filter index
136 *
137 * @see #getFilterExtensions
138 * @see #getFilterNames
139 *
140 * @since 3.4
141 */
142 public int getFilterIndex () {
143 return filterIndex;
144 }
145
146 /**
147 * Returns the names that describe the filter extensions
148 * which the dialog will use to filter the files it shows.
149 *
150 * @return the list of filter names
151 */
152 public String [] getFilterNames () {
153 return filterNames;
154 }
155
156 /**
157 * Returns the directory path that the dialog will use, or an empty
158 * string if this is not set. File names in this path will appear
159 * in the dialog, filtered according to the filter extensions.
160 *
161 * @return the directory path string
162 *
163 * @see #setFilterExtensions
164 */
165 public String getFilterPath () {
166 return filterPath;
167 }
168
169 /**
170 * Returns the flag that the dialog will use to
171 * determine whether to prompt the user for file
172 * overwrite if the selected file already exists.
173 *
174 * @return true if the dialog will prompt for file overwrite, false otherwise
175 *
176 * @since 3.4
177 */
178 public bool getOverwrite () {
179 return overwrite;
180 }
181
182 /**
183 * Makes the dialog visible and brings it to the front
184 * of the display.
185 *
186 * @return a string describing the absolute path of the first selected file,
187 * or null if the dialog was cancelled or an error occurred
188 *
189 * @exception DWTException <ul>
190 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
191 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
192 * </ul>
193 */
194 public String open () {
195 String fullPath = null;
196 fileNames = new String [0];
197 NSSavePanel panel;
198 if ((style & DWT.SAVE) !is 0) {
199 NSSavePanel savePanel = NSSavePanel.savePanel();
200 panel = savePanel;
201 } else {
202 NSOpenPanel openPanel = NSOpenPanel.openPanel();
203 openPanel.setAllowsMultipleSelection((style & DWT.MULTI) !is 0);
204 panel = openPanel;
205 }
206 if (filterPath !is null) panel.setDirectory(NSString.stringWith(filterPath));
207 panel.setTitle(NSString.stringWith(title !is null ? title : ""));
208 int response = panel.runModal();
209 if (response is OS.NSFileHandlingPanelOKButton) {
210 NSString filename = panel.filename();
211 char[] buffer = new char[filename.length()];
212 filename.getCharacters_(buffer);
213 fullPath = new String(buffer);
214 if ((style & DWT.SAVE) is 0) {
215 NSArray filenames = ((NSOpenPanel)panel).filenames();
216 int count = filenames.count();
217 fileNames = new String[count];
218 for (int i = 0; i < count; i++) {
219 filename = new NSString(filenames.objectAtIndex(i));
220 buffer = new char[filename.length()];
221 filename.getCharacters_(buffer);
222 fileNames[i] = new String(buffer);
223 }
224 }
225 filterIndex = -1;
226 }
227 return fullPath;
228 }
229
230 /**
231 * Set the initial filename which the dialog will
232 * select by default when opened to the argument,
233 * which may be null. The name will be prefixed with
234 * the filter path when one is supplied.
235 *
236 * @param string the file name
237 */
238 public void setFileName (String string) {
239 fileName = string;
240 }
241
242 /**
243 * Set the file extensions which the dialog will
244 * use to filter the files it shows to the argument,
245 * which may be null.
246 * <p>
247 * The strings are platform specific. For example, on
248 * Windows, an extension filter string is typically of
249 * the form "*.extension", where "*.*" matches all files.
250 * </p>
251 *
252 * @param extensions the file extension filter
253 *
254 * @see #setFilterNames to specify the user-friendly
255 * names corresponding to the extensions
256 */
257 public void setFilterExtensions (String [] extensions) {
258 filterExtensions = extensions;
259 }
260
261 /**
262 * Set the 0-based index of the file extension filter
263 * which the dialog will use initially to filter the files
264 * it shows to the argument.
265 * <p>
266 * This is an index into the FilterExtensions array and
267 * the FilterNames array.
268 * </p>
269 *
270 * @param index the file extension filter index
271 *
272 * @see #setFilterExtensions
273 * @see #setFilterNames
274 *
275 * @since 3.4
276 */
277 public void setFilterIndex (int index) {
278 filterIndex = index;
279 }
280
281 /**
282 * Sets the the names that describe the filter extensions
283 * which the dialog will use to filter the files it shows
284 * to the argument, which may be null.
285 * <p>
286 * Each name is a user-friendly short description shown for
287 * its corresponding filter. The <code>names</code> array must
288 * be the same length as the <code>extensions</code> array.
289 * </p>
290 *
291 * @param names the list of filter names, or null for no filter names
292 *
293 * @see #setFilterExtensions
294 */
295 public void setFilterNames (String [] names) {
296 filterNames = names;
297 }
298
299 /**
300 * Sets the directory path that the dialog will use
301 * to the argument, which may be null. File names in this
302 * path will appear in the dialog, filtered according
303 * to the filter extensions. If the string is null,
304 * then the operating system's default filter path
305 * will be used.
306 * <p>
307 * Note that the path string is platform dependent.
308 * For convenience, either '/' or '\' can be used
309 * as a path separator.
310 * </p>
311 *
312 * @param string the directory path
313 *
314 * @see #setFilterExtensions
315 */
316 public void setFilterPath (String string) {
317 filterPath = string;
318 }
319
320 /**
321 * Sets the flag that the dialog will use to
322 * determine whether to prompt the user for file
323 * overwrite if the selected file already exists.
324 *
325 * @param overwrite true if the dialog will prompt for file overwrite, false otherwise
326 *
327 * @since 3.4
328 */
329 public void setOverwrite (bool overwrite) {
330 //TODO: May be able to implement this with private NSSavePanel method (BOOL)_overwriteExistingFileCheck:(NSString *)filename
331 /* See bug 223703 */
332 //this.overwrite = overwrite;
333 }
334 }