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