KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > microedition > lcdui > TextField


1
2 /*
3  * MicroEmulator
4  * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your
9  * option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contributor(s):
21  * 3GLab
22  * Robert Helmer
23  */

24
25 package javax.microedition.lcdui;
26
27 import com.barteo.emulator.device.DeviceFactory;
28 import com.barteo.emulator.device.InputMethod;
29 import com.barteo.emulator.device.InputMethodListener;
30 import com.barteo.emulator.device.InputMethodEvent;
31
32 public class TextField extends Item
33 {
34     public static final int ANY = 0;
35     public static final int EMAILADDR = 1;
36     public static final int NUMERIC = 2;
37     public static final int PHONENUMBER = 3;
38     public static final int URL = 4;
39
40     public static final int PASSWORD = 0x10000;
41     public static final int CONSTRAINT_MASK = 0xffff;
42
43     StringComponent stringComponent;
44     
45     private String field;
46     private int caret;
47     private boolean caretVisible;
48     private int maxSize;
49     private int constraints;
50
51     private TextBox tb = null;
52
53     private InputMethodListener inputMethodListener = new InputMethodListener()
54     {
55         public void caretPositionChanged(InputMethodEvent event)
56         {
57             setCaretPosition(event.getCaret());
58             setCaretVisible(true);
59             repaint();
60         }
61
62         public void inputMethodTextChanged(InputMethodEvent event)
63         {
64             setCaretVisible(false);
65             setString(event.getText());
66             repaint();
67         }
68     };
69
70     
71     public TextField(String label, String text, int maxSize, int constraints)
72     {
73         super(label);
74         if (maxSize <= 0) {
75             throw new IllegalArgumentException();
76         }
77         setConstraints(constraints);
78         this.maxSize = maxSize;
79         stringComponent = new StringComponent();
80         if (text != null) {
81             setString(text);
82         } else {
83             setString("");
84         }
85         stringComponent.setWidth(DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 8);
86         setCaretPosition(getString().length());
87         setCaretVisible(false);
88     }
89
90     
91     public String getString()
92     {
93         return field;
94     }
95
96     
97     public void setString(String text)
98     {
99         validate(text);
100         if (text == null) {
101             field = "";
102             stringComponent.setText("");
103         } else {
104             if (text.length() > maxSize) {
105                 throw new IllegalArgumentException();
106             }
107             field = text;
108             if ((constraints & PASSWORD) == 0) {
109                 stringComponent.setText(text);
110             } else {
111                 StringBuffer sb = new StringBuffer();
112                 for (int i = 0; i < text.length(); i++) {
113                     sb.append('*');
114                 }
115                 stringComponent.setText(sb.toString());
116             }
117         }
118         repaint();
119     }
120
121     
122     public int getChars(char[] data)
123     {
124         if (data.length < field.length()) {
125             throw new ArrayIndexOutOfBoundsException();
126         }
127         getString().getChars(0, field.length(), data, 0);
128
129         return field.length();
130     }
131
132     
133     public void setChars(char[] data, int offset, int length)
134     {
135         if (data == null) {
136             setString("");
137         } else {
138             if (length > maxSize) {
139                 throw new IllegalArgumentException();
140             }
141             String newtext = new String(data, offset, length);
142             validate(newtext);
143             setString(newtext);
144         }
145         repaint();
146     }
147
148     
149     public void insert(String src, int position)
150     {
151         validate(src);
152         if (field.length() + src.length() > maxSize) {
153             throw new IllegalArgumentException();
154         }
155         String newtext = "";
156         if (position > 0) {
157             newtext = getString().substring(0, position);
158         }
159         newtext += src;
160         if (position < field.length()) {
161             newtext += getString().substring(position + 1);
162         }
163         setString(newtext);
164         repaint();
165     }
166
167     
168     public void insert(char[] data, int offset, int length, int position)
169     {
170         if (offset + length > data.length) {
171             throw new ArrayIndexOutOfBoundsException();
172         }
173         insert(new String(data, offset, length), position);
174     }
175
176     
177     public void delete(int offset, int length)
178     {
179         if (offset + length > field.length()) {
180             throw new StringIndexOutOfBoundsException();
181         }
182         String newtext = "";
183         if (offset > 0) {
184             newtext = getString().substring(0, offset);
185         }
186         if (offset + length < field.length()) {
187             newtext += getString().substring(offset + length);
188         }
189         setString(newtext);
190         repaint();
191     }
192
193     
194     public int getMaxSize()
195     {
196         return maxSize;
197     }
198
199     
200     public int setMaxSize(int maxSize)
201     {
202         if (maxSize <= 0) {
203             throw new IllegalArgumentException();
204         }
205         if (field.length() > maxSize) {
206             setString(getString().substring(0, maxSize));
207         }
208         this.maxSize = maxSize;
209         return maxSize;
210     }
211
212     
213     public int size()
214     {
215         return field.length();
216     }
217
218     
219     public int getCaretPosition()
220     {
221         return caret;
222     }
223
224     
225     public void setConstraints(int constraints)
226     {
227         if ((constraints & TextField.CONSTRAINT_MASK) < ANY
228             || (constraints & TextField.CONSTRAINT_MASK) > URL) {
229             throw new IllegalArgumentException("constraints is an illegal value");
230         }
231         this.constraints = constraints;
232     }
233
234     
235     public int getConstraints()
236     {
237         return constraints;
238     }
239
240     
241     public void setLabel(String label)
242     {
243         super.setLabel(label);
244     }
245
246     
247     boolean isFocusable()
248     {
249         return true;
250     }
251
252     
253     int getHeight()
254     {
255         return super.getHeight() + stringComponent.getHeight() + 8;
256     }
257
258     
259     int paint(Graphics g)
260     {
261         super.paintContent(g);
262
263         g.translate(0, super.getHeight());
264         if (hasFocus()) {
265             g.drawRect(
266                     1, 1,
267                     DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 3, stringComponent.getHeight() + 4);
268         }
269         g.translate(3, 3);
270         paintContent(g);
271         g.translate(-3, -3);
272         g.translate(0, -super.getHeight());
273
274         return getHeight();
275     }
276
277     
278     void paintContent(Graphics g)
279     {
280         stringComponent.paint(g);
281         if (caretVisible) {
282             int x_pos = stringComponent.getCharPositionX(caret);
283             int y_pos = stringComponent.getCharPositionY(caret);
284             g.drawLine(x_pos, y_pos, x_pos, y_pos + Font.getDefaultFont().getHeight());
285         }
286     }
287
288     
289     void setCaretPosition(int position)
290     {
291         caret = position;
292     }
293
294     
295     void setCaretVisible(boolean state)
296     {
297         caretVisible = state;
298     }
299
300     
301     int traverse(int gameKeyCode, int top, int bottom, boolean action)
302     {
303         if (gameKeyCode == Canvas.UP) {
304             if (top > 0) {
305                 return -top;
306             } else {
307                 return Item.OUTOFITEM;
308             }
309         }
310         if (gameKeyCode == Canvas.DOWN) {
311             if (getHeight() > bottom) {
312                 return getHeight() - bottom;
313             } else {
314                 return Item.OUTOFITEM;
315             }
316         }
317
318         return 0;
319     }
320
321     
322     void setFocus(boolean hasFocus)
323     {
324         super.setFocus(hasFocus);
325         if (hasFocus) {
326             // register input listener
327
InputMethod inputMethod = DeviceFactory.getDevice().getInputMethod();
328             inputMethod.setInputMethodListener(inputMethodListener);
329             inputMethod.setConstraints(getConstraints());
330             inputMethod.setText(getString());
331             inputMethod.setMaxSize(getMaxSize());
332             setCaretVisible(true);
333         } else {
334             // unregister input listener
335
DeviceFactory.getDevice().getInputMethod().removeInputMethodListener(inputMethodListener);
336             setCaretVisible(false);
337         }
338     }
339
340     
341     void validate(String text)
342     {
343         // text is illegal for the specified constraints so IllegalArgumentException
344
if ((constraints & CONSTRAINT_MASK) == ANY) {
345             return;
346         }
347         /*
348          * if ((constraints & CONSTRAINT_MASK) == NUMERIC) {
349          * try {
350          * int tmp = Integer.parseInt(text);
351          * } catch (NumberFormatException e) {
352          * throw new IllegalArgumentException(
353          * "TextField limited to numeric values: text = " + text);
354          * }
355          * }
356          */

357
358         /* @todo add more constraints checking */
359     }
360
361 }
362
Popular Tags