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