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