KickJava   Java API By Example, From Geeks To Geeks.

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


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
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20  
21 package javax.microedition.lcdui;
22
23 import com.barteo.emulator.device.DeviceFactory;
24 import com.barteo.emulator.device.InputMethod;
25 import com.barteo.emulator.device.InputMethodEvent;
26 import com.barteo.emulator.device.InputMethodListener;
27
28
29 public class TextBox extends Screen
30 {
31
32     TextField tf;
33
34     InputMethodListener inputMethodListener = new InputMethodListener()
35     {
36
37         public void caretPositionChanged(InputMethodEvent event)
38         {
39             setCaretPosition(event.getCaret());
40             tf.setCaretVisible(true);
41             repaint();
42         }
43
44         public void inputMethodTextChanged(InputMethodEvent event)
45         {
46             tf.setCaretVisible(false);
47             tf.setString(event.getText());
48             repaint();
49         }
50     };
51
52
53     public TextBox(String title, String text, int maxSize, int constraints)
54     {
55         super(title);
56         tf = new TextField(null, text, maxSize, constraints);
57     }
58
59
60     public String getString()
61     {
62         return tf.getString();
63     }
64
65
66     public void setString(String text)
67     {
68         tf.setString(text);
69     }
70
71
72     public int getChars(char[] data)
73     {
74         return tf.getChars(data);
75     }
76
77
78     public void setChars(char[] data, int offset, int length)
79     {
80         tf.setChars(data, offset, length);
81     }
82
83
84     public void insert(String src, int position)
85     {
86         tf.insert(src, position);
87     }
88
89
90     public void insert(char[] data, int offset, int length, int position)
91     {
92         tf.insert(data, offset, length, position);
93     }
94
95
96     public void delete(int offset, int length)
97     {
98         tf.delete(offset, length);
99     }
100
101
102     public int getMaxSize()
103     {
104         return tf.getMaxSize();
105     }
106
107
108     public int setMaxSize(int maxSize)
109     {
110         return tf.setMaxSize(maxSize);
111     }
112
113
114     public int size()
115     {
116         return tf.size();
117     }
118
119
120     public int getCaretPosition()
121     {
122         return tf.getCaretPosition();
123     }
124
125
126     public void setConstraints(int constraints)
127     {
128         tf.setConstraints(constraints);
129     }
130
131
132     public int getConstraints()
133     {
134         return tf.getConstraints();
135     }
136
137
138     void hideNotify()
139     {
140         DeviceFactory.getDevice().getInputMethod().removeInputMethodListener(inputMethodListener);
141         super.hideNotify();
142     }
143
144
145     int paintContent(Graphics g)
146     {
147         g.translate(0, viewPortY);
148         g.drawRect(1, 1,
149         DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 3, viewPortHeight - 3);
150         g.setClip(3, 3,
151         DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 6, viewPortHeight - 6);
152         g.translate(3, 3);
153         g.translate(0, -viewPortY);
154         tf.paintContent(g);
155
156         return tf.stringComponent.getHeight() + 6;
157     }
158
159
160     void setCaretPosition(int position)
161     {
162         tf.setCaretPosition(position);
163         
164         StringComponent tmp = tf.stringComponent;
165         if (tmp.getCharPositionY(position) < viewPortY) {
166             viewPortY = tmp.getCharPositionY(position);
167         } else if (tmp.getCharPositionY(position) + tmp.getCharHeight() > viewPortY + viewPortHeight - 6) {
168             viewPortY = tmp.getCharPositionY(position) + tmp.getCharHeight() - (viewPortHeight - 6);
169         }
170     }
171
172
173     int traverse(int gameKeyCode, int top, int bottom)
174     {
175         int traverse = tf.traverse(gameKeyCode, top, bottom, true);
176         if (traverse == Item.OUTOFITEM) {
177             return 0;
178         } else {
179         return traverse;
180         }
181     }
182
183
184     void showNotify()
185     {
186         super.showNotify();
187     InputMethod inputMethod = DeviceFactory.getDevice().getInputMethod();
188         inputMethod.setInputMethodListener(inputMethodListener);
189     inputMethod.setConstraints(getConstraints());
190         inputMethod.setText(getString());
191         inputMethod.setMaxSize(getMaxSize());
192         setCaretPosition(getString().length());
193         tf.setCaretVisible(true);
194     }
195
196 }
197
Popular Tags