Mercurial > projects > dwt-addons
annotate dwtx/jface/preference/IntegerFieldEditor.d @ 181:8116a58394c3
Fix validation message format error
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 18 Oct 2008 15:00:49 +0200 |
parents | 5df4896124c7 |
children |
rev | line source |
---|---|
34 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
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 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
10 * <sgandon@nds.com> - Fix for bug 109389 - IntegerFieldEditor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
11 * does not fire property change all the time |
34 | 12 * Port to the D programming language: |
13 * Frank Benoit <benoit@tionex.de> | |
14 *******************************************************************************/ | |
15 module dwtx.jface.preference.IntegerFieldEditor; | |
16 | |
17 import dwtx.jface.preference.StringFieldEditor; | |
18 | |
19 import dwt.widgets.Composite; | |
20 import dwt.widgets.Text; | |
21 import dwtx.jface.resource.JFaceResources; | |
22 | |
23 import dwt.dwthelper.utils; | |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
24 import tango.util.Convert; |
34 | 25 |
26 /** | |
27 * A field editor for an integer type preference. | |
28 */ | |
29 public class IntegerFieldEditor : StringFieldEditor { | |
30 private int minValidValue = 0; | |
31 | |
32 private int maxValidValue = int.max; | |
33 | |
34 private static const int DEFAULT_TEXT_LIMIT = 10; | |
35 | |
36 /** | |
37 * Creates a new integer field editor | |
38 */ | |
39 protected this() { | |
40 } | |
41 | |
42 /** | |
43 * Creates an integer field editor. | |
44 * | |
45 * @param name the name of the preference this field editor works on | |
46 * @param labelText the label text of the field editor | |
47 * @param parent the parent of the field editor's control | |
48 */ | |
49 public this(String name, String labelText, Composite parent) { | |
50 this(name, labelText, parent, DEFAULT_TEXT_LIMIT); | |
51 } | |
52 | |
53 /** | |
54 * Creates an integer field editor. | |
55 * | |
56 * @param name the name of the preference this field editor works on | |
57 * @param labelText the label text of the field editor | |
58 * @param parent the parent of the field editor's control | |
59 * @param textLimit the maximum number of characters in the text. | |
60 */ | |
61 public this(String name, String labelText, Composite parent, | |
62 int textLimit) { | |
63 init(name, labelText); | |
64 setTextLimit(textLimit); | |
65 setEmptyStringAllowed(false); | |
66 setErrorMessage(JFaceResources | |
67 .getString("IntegerFieldEditor.errorMessage"));//$NON-NLS-1$ | |
68 createControl(parent); | |
69 } | |
70 | |
71 /** | |
72 * Sets the range of valid values for this field. | |
73 * | |
74 * @param min the minimum allowed value (inclusive) | |
75 * @param max the maximum allowed value (inclusive) | |
76 */ | |
77 public void setValidRange(int min, int max) { | |
78 minValidValue = min; | |
79 maxValidValue = max; | |
80 setErrorMessage(JFaceResources.format( | |
81 "IntegerFieldEditor.errorMessageRange", //$NON-NLS-1$ | |
181
8116a58394c3
Fix validation message format error
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
82 min, max )); |
34 | 83 } |
84 | |
85 /* (non-Javadoc) | |
86 * Method declared on StringFieldEditor. | |
87 * Checks whether the entered String is a valid integer or not. | |
88 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
89 protected override bool checkState() { |
34 | 90 |
91 Text text = getTextControl(); | |
92 | |
93 if (text is null) { | |
94 return false; | |
95 } | |
96 | |
97 String numberString = text.getText(); | |
98 try { | |
99 int number = Integer.valueOf(numberString).intValue(); | |
100 if (number >= minValidValue && number <= maxValidValue) { | |
101 clearErrorMessage(); | |
102 return true; | |
103 } | |
104 | |
105 showErrorMessage(); | |
106 return false; | |
107 | |
108 } catch (NumberFormatException e1) { | |
109 showErrorMessage(); | |
110 } | |
111 | |
112 return false; | |
113 } | |
114 | |
115 /* (non-Javadoc) | |
116 * Method declared on FieldEditor. | |
117 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
118 protected override void doLoad() { |
34 | 119 Text text = getTextControl(); |
120 if (text !is null) { | |
121 int value = getPreferenceStore().getInt(getPreferenceName()); | |
122 text.setText( tango.text.convert.Integer.toString(value));//$NON-NLS-1$ | |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
123 oldValue = to!(String)( value ); //$NON-NLS-1$ |
34 | 124 } |
125 | |
126 } | |
127 | |
128 /* (non-Javadoc) | |
129 * Method declared on FieldEditor. | |
130 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
131 protected override void doLoadDefault() { |
34 | 132 Text text = getTextControl(); |
133 if (text !is null) { | |
134 int value = getPreferenceStore().getDefaultInt(getPreferenceName()); | |
135 text.setText(tango.text.convert.Integer.toString( value));//$NON-NLS-1$ | |
136 } | |
137 valueChanged(); | |
138 } | |
139 | |
140 /* (non-Javadoc) | |
141 * Method declared on FieldEditor. | |
142 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
34
diff
changeset
|
143 protected override void doStore() { |
34 | 144 Text text = getTextControl(); |
145 if (text !is null) { | |
146 Integer i = new Integer(text.getText()); | |
147 getPreferenceStore().setValue(getPreferenceName(), i.intValue()); | |
148 } | |
149 } | |
150 | |
151 /** | |
152 * Returns this field editor's current value as an integer. | |
153 * | |
154 * @return the value | |
155 * @exception NumberFormatException if the <code>String</code> does not | |
156 * contain a parsable integer | |
157 */ | |
158 public int getIntValue() { | |
159 return (new Integer(getStringValue())).intValue(); | |
160 } | |
161 } |