Source code: javax/microedition/lcdui/TextBox.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package javax.microedition.lcdui;
21
22 import com.barteo.emulator.device.DeviceFactory;
23 import com.barteo.emulator.device.InputMethod;
24 import com.barteo.emulator.device.InputMethodEvent;
25 import com.barteo.emulator.device.InputMethodListener;
26
27
28 public class TextBox extends Screen
29 {
30
31 TextField tf;
32
33 InputMethodListener inputMethodListener = new InputMethodListener()
34 {
35
36 public void caretPositionChanged(InputMethodEvent event)
37 {
38 setCaretPosition(event.getCaret());
39 tf.setCaretVisible(true);
40 repaint();
41 }
42
43 public void inputMethodTextChanged(InputMethodEvent event)
44 {
45 tf.setCaretVisible(false);
46 tf.setString(event.getText());
47 repaint();
48 }
49 };
50
51
52 public TextBox(String title, String text, int maxSize, int constraints)
53 {
54 super(title);
55 tf = new TextField(null, text, maxSize, constraints);
56 }
57
58
59 public String getString()
60 {
61 return tf.getString();
62 }
63
64
65 public void setString(String text)
66 {
67 tf.setString(text);
68 }
69
70
71 public int getChars(char[] data)
72 {
73 return tf.getChars(data);
74 }
75
76
77 public void setChars(char[] data, int offset, int length)
78 {
79 tf.setChars(data, offset, length);
80 }
81
82
83 public void insert(String src, int position)
84 {
85 tf.insert(src, position);
86 }
87
88
89 public void insert(char[] data, int offset, int length, int position)
90 {
91 tf.insert(data, offset, length, position);
92 }
93
94
95 public void delete(int offset, int length)
96 {
97 tf.delete(offset, length);
98 }
99
100
101 public int getMaxSize()
102 {
103 return tf.getMaxSize();
104 }
105
106
107 public int setMaxSize(int maxSize)
108 {
109 return tf.setMaxSize(maxSize);
110 }
111
112
113 public int size()
114 {
115 return tf.size();
116 }
117
118
119 public int getCaretPosition()
120 {
121 return tf.getCaretPosition();
122 }
123
124
125 public void setConstraints(int constraints)
126 {
127 tf.setConstraints(constraints);
128 }
129
130
131 public int getConstraints()
132 {
133 return tf.getConstraints();
134 }
135
136
137 void hideNotify()
138 {
139 DeviceFactory.getDevice().getInputMethod().removeInputMethodListener(inputMethodListener);
140 super.hideNotify();
141 }
142
143
144 int paintContent(Graphics g)
145 {
146 g.translate(0, viewPortY);
147 g.drawRect(1, 1,
148 DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 3, viewPortHeight - 3);
149 g.setClip(3, 3,
150 DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 6, viewPortHeight - 6);
151 g.translate(3, 3);
152 g.translate(0, -viewPortY);
153 tf.paintContent(g);
154
155 return tf.stringComponent.getHeight() + 6;
156 }
157
158
159 void setCaretPosition(int position)
160 {
161 tf.setCaretPosition(position);
162
163 StringComponent tmp = tf.stringComponent;
164 if (tmp.getCharPositionY(position) < viewPortY) {
165 viewPortY = tmp.getCharPositionY(position);
166 } else if (tmp.getCharPositionY(position) + tmp.getCharHeight() > viewPortY + viewPortHeight - 6) {
167 viewPortY = tmp.getCharPositionY(position) + tmp.getCharHeight() - (viewPortHeight - 6);
168 }
169 }
170
171
172 int traverse(int gameKeyCode, int top, int bottom)
173 {
174 int traverse = tf.traverse(gameKeyCode, top, bottom, true);
175 if (traverse == Item.OUTOFITEM) {
176 return 0;
177 } else {
178 return traverse;
179 }
180 }
181
182
183 void showNotify()
184 {
185 super.showNotify();
186 InputMethod inputMethod = DeviceFactory.getDevice().getInputMethod();
187 inputMethod.setInputMethodListener(inputMethodListener);
188 inputMethod.setConstraints(getConstraints());
189 inputMethod.setText(getString());
190 inputMethod.setMaxSize(getMaxSize());
191 setCaretPosition(getString().length());
192 tf.setCaretVisible(true);
193 }
194
195 }