Mercurial > projects > dwt-addons
annotate dwtx/jface/preference/DirectoryFieldEditor.d @ 66:3ed358ab21c9
The FieldEditors shall show Paths in native look
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 15 Apr 2008 06:48:52 +0200 |
parents | 076d0807c32a |
children | 0168c579a3f9 |
rev | line source |
---|---|
34 | 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.DirectoryFieldEditor; | |
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.DirectoryDialog; | |
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 directory path type preference. A standard directory | |
29 * dialog appears when the user presses the change button. | |
30 */ | |
31 public class DirectoryFieldEditor : StringButtonFieldEditor { | |
32 /** | |
33 * Creates a new directory field editor | |
34 */ | |
35 protected this() { | |
36 } | |
37 | |
38 /** | |
39 * Creates a directory field editor. | |
40 * | |
41 * @param name the name of the preference this field editor works on | |
42 * @param labelText the label text of the field editor | |
43 * @param parent the parent of the field editor's control | |
44 */ | |
45 public this(String name, String labelText, Composite parent) { | |
46 init(name, labelText); | |
47 setErrorMessage(JFaceResources | |
48 .getString("DirectoryFieldEditor.errorMessage"));//$NON-NLS-1$ | |
49 setChangeButtonText(JFaceResources.getString("openBrowse"));//$NON-NLS-1$ | |
50 setValidateStrategy(VALIDATE_ON_FOCUS_LOST); | |
51 createControl(parent); | |
52 } | |
53 | |
54 /* (non-Javadoc) | |
55 * Method declared on StringButtonFieldEditor. | |
56 * Opens the directory chooser dialog and returns the selected directory. | |
57 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
58 protected override String changePressed() { |
34 | 59 auto f = new FilePath(getTextControl().getText()); |
60 if (!f.exists()) { | |
61 f = cast(FilePath)null; | |
62 } | |
63 auto d = getDirectory(f); | |
64 if (d is null) { | |
65 return null; | |
66 } | |
67 | |
66
3ed358ab21c9
The FieldEditors shall show Paths in native look
Frank Benoit <benoit@tionex.de>
parents:
45
diff
changeset
|
68 return FileSystem.toAbsolute( d ).native.toString; |
34 | 69 } |
70 | |
71 /* (non-Javadoc) | |
72 * Method declared on StringFieldEditor. | |
73 * Checks whether the text input field contains a valid directory. | |
74 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
75 protected override bool doCheckState() { |
34 | 76 String fileName = getTextControl().getText(); |
77 fileName = fileName.trim(); | |
78 if (fileName.length is 0 && isEmptyStringAllowed()) { | |
79 return true; | |
80 } | |
81 auto file = new FilePath(fileName); | |
45
076d0807c32a
fix exception from file.isFolder, test first if exists.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
82 return file.exists() && file.isFolder(); |
34 | 83 } |
84 | |
85 /** | |
86 * Helper that opens the directory chooser dialog. | |
87 * @param startingDirectory The directory the dialog will open in. | |
88 * @return File File or <code>null</code>. | |
89 * | |
90 */ | |
91 private FilePath getDirectory(FilePath startingDirectory) { | |
92 | |
93 DirectoryDialog fileDialog = new DirectoryDialog(getShell(), DWT.OPEN); | |
94 if (startingDirectory !is null) { | |
95 fileDialog.setFilterPath(startingDirectory.path); | |
96 } | |
97 String dir = fileDialog.open(); | |
98 if (dir !is null) { | |
99 dir = dir.trim(); | |
100 if (dir.length > 0) { | |
101 return new FilePath(dir); | |
102 } | |
103 } | |
104 | |
105 return null; | |
106 } | |
107 } |