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