My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 68 attachment: TextBox.java (5.8 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* MicroEmulator
* Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
*
* It is licensed under the following two licenses as alternatives:
* 1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
* 2. Apache License (the "AL") Version 2.0
*
* You may not use this file except in compliance with at least one of
* the above two licenses.
*
* You may obtain a copy of the LGPL at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*
* You may obtain a copy of the AL at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the LGPL or the AL for the specific language governing permissions and
* limitations.
*/

package javax.microedition.lcdui;

import org.microemu.device.DeviceFactory;
import org.microemu.device.InputMethod;
import org.microemu.device.InputMethodEvent;
import org.microemu.device.InputMethodListener;
import org.microemu.device.ui.TextBoxUI;

//TODO implement pointer events
public class TextBox extends Screen {

TextField tf;

InputMethodListener inputMethodListener = new InputMethodListener() {

public void caretPositionChanged(InputMethodEvent event) {
setCaretPosition(event.getCaret());
tf.setCaretVisible(true);
repaint();
}

public void inputMethodTextChanged(InputMethodEvent event) {
tf.setCaretVisible(false);
tf.setString(event.getText(), event.getCaret());
repaint();
}

public int getCaretPosition() {
return TextBox.this.getCaretPosition();
}

public String getText() {
return TextBox.this.getString();
}

public int getConstraints() {
return TextBox.this.getConstraints();
}
};

public TextBox(String title, String text, int maxSize, int constraints) {
super(title);

tf = new TextField(null, text, maxSize, constraints);

super.setUI(DeviceFactory.getDevice().getUIFactory().createTextBoxUI(this));
}

public void delete(int offset, int length) {
// tf.delete(offset, length);
//
// modified by Malcolm Bryant, 29 August 2010
//
if (ui != null && ui.getClass().getName().equals("org.microemu.android.device.ui.AndroidTextBoxUI")) {
((TextBoxUI) ui).delete(offset, length);
} else {
tf.delete(offset, length);
}
}

public int getCaretPosition() {
if (ui != null && ui.getClass().getName().equals("org.microemu.android.device.ui.AndroidTextBoxUI")) {
return ((TextBoxUI) ui).getCaretPosition();
} else {
return tf.getCaretPosition();
}
}

public int getChars(char[] data) {
return tf.getChars(data);
}

public int getConstraints() {
return tf.getConstraints();
}

public int getMaxSize() {
return tf.getMaxSize();
}

public String getString() {
if (ui != null && ui.getClass().getName().equals("org.microemu.android.device.ui.AndroidTextBoxUI")) {
return ((TextBoxUI) ui).getString();
} else {
return tf.getString();
}
}

public void insert(char[] data, int offset, int length, int position) {
tf.insert(data, offset, length, position);
}

public void insert(String src, int position) {
if (ui != null && ui.getClass().getName().equals("org.microemu.android.device.ui.AndroidTextBoxUI")) {
((TextBoxUI) ui).insert(src, position);
} else {
tf.insert(src, position);
}
}

public void setChars(char[] data, int offset, int length) {
tf.setChars(data, offset, length);
}

public void setConstraints(int constraints) {
tf.setConstraints(constraints);
}

public void setInitialInputMode(String characterSubset) {
// TODO implement
}

public int setMaxSize(int maxSize) {
return tf.setMaxSize(maxSize);
}

public void setString(String text) {
if (ui != null && ui.getClass().getName().equals("org.microemu.android.device.ui.AndroidTextBoxUI")) {
((TextBoxUI) ui).setString(text);
} else {
tf.setString(text);
}
}

public void setTicker(Ticker ticker) {
// TODO implement
}

public void setTitle(String s) {
super.setTitle(s);
}

public int size() {
// return tf.size();
//
// modified by Malcolm Bryant, 29 August 2010
//
if (ui != null && ui.getClass().getName().equals("org.microemu.android.device.ui.AndroidTextBoxUI")) {
return ((TextBoxUI) ui).getString().length();
} else {
return tf.size();
}
}

void hideNotify() {
DeviceFactory.getDevice().getInputMethod().removeInputMethodListener(inputMethodListener);
super.hideNotify();
}

int paintContent(Graphics g) {
g.translate(0, viewPortY);
g.drawRect(1, 1, getWidth() - 3, viewPortHeight - 3);
g.setClip(3, 3, getWidth() - 6, viewPortHeight - 6);
g.translate(3, 3);
g.translate(0, -viewPortY);
tf.paintContent(g);

return tf.stringComponent.getHeight() + 6;
}

void setCaretPosition(int position) {
tf.setCaretPosition(position);

StringComponent tmp = tf.stringComponent;
if (tmp.getCharPositionY(position) < viewPortY) {
viewPortY = tmp.getCharPositionY(position);
} else if (tmp.getCharPositionY(position) + tmp.getCharHeight() > viewPortY + viewPortHeight - 6) {
viewPortY = tmp.getCharPositionY(position) + tmp.getCharHeight() - (viewPortHeight - 6);
}
}

void showNotify() {
super.showNotify();
InputMethod inputMethod = DeviceFactory.getDevice().getInputMethod();
inputMethod.setInputMethodListener(inputMethodListener);
inputMethod.setMaxSize(getMaxSize());
setCaretPosition(getString().length());
tf.setCaretVisible(true);
}

int traverse(int gameKeyCode, int top, int bottom) {
int traverse = tf.traverse(gameKeyCode, top, bottom, true);
if (traverse == Item.OUTOFITEM) {
return 0;
} else {
return traverse;
}
}

}
Powered by Google Project Hosting