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