1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package javax.microedition.lcdui;
25
26 import java.util.Vector;
27
28 import org.microemu.device.DeviceFactory;
29
30 public abstract class Item
31 {
32
33 static final int OUTOFITEM = Integer.MAX_VALUE;
34
35 public static final int LAYOUT_DEFAULT = 0x0000;
36
37 public static final int LAYOUT_LEFT = 0x0001;
38 public static final int LAYOUT_RIGHT = 0x0002;
39 public static final int LAYOUT_CENTER = 0x0003;
40
41 public static final int LAYOUT_TOP = 0x0010;
42 public static final int LAYOUT_BOTTOM = 0x0020;
43 public static final int LAYOUT_VCENTER = 0x0030;
44
45 public static final int LAYOUT_NEWLINE_BEFORE = 0x0100;
46 public static final int LAYOUT_NEWLINE_AFTER = 0x0200;
47
48 public static final int LAYOUT_SHRINK = 0x0400;
49 public static final int LAYOUT_EXPAND = 0x0800;
50 public static final int LAYOUT_VSHRINK = 0x1000;
51 public static final int LAYOUT_VEXPAND = 0x2000;
52
53 public static final int LAYOUT_2 = 0x4000;
54
55
56 public static final int PLAIN = 0;
57 public static final int HYPERLINK = 1;
58 public static final int BUTTON = 2;
59
60 StringComponent labelComponent;
61 Screen owner = null;
62 private boolean focus = false;
63
64
65 int layout;
66 Vector commands;
67 Command defaultCommand;
68 ItemCommandListener commandListener;
69
70
71
72
73 private int prefWidth, prefHeight;
74
75
76 Item(String label)
77 {
78 labelComponent = new StringComponent(label);
79 commands = new Vector();
80 }
81
82 public void addCommand(Command cmd) {
83 if (cmd == null)
84 throw new NullPointerException();
85
86 if (!commands.contains(cmd)) {
87
88 boolean inserted = false;
89
90 for (int i = 0; i < commands.size(); i++) {
91 if (cmd.getPriority() < ((Command)commands.elementAt(i)).getPriority()) {
92 commands.insertElementAt(cmd, i);
93 inserted = true;
94 break;
95 }
96 }
97 if (!inserted) {
98
99 commands.addElement(cmd);
100 }
101 repaintOwner();
102 }
103
104 }
105
106 public String getLabel()
107 {
108 return labelComponent.getText();
109 }
110
111 public int getLayout() {
112 return layout;
113 }
114
115 public int getMinimumHeight() {
116 if (labelComponent != null)
117 return labelComponent.getHeight();
118 else
119 return 0;
120 }
121
122 public int getMinimumWidth() {
123 return getMaximumWidth();
124 }
125
126 public int getPreferredHeight() {
127 int ret = prefHeight;
128 int min = getMinimumHeight();
129 int max = getMaximumHeight();
130
131 if (ret == -1)
132 return min;
133
134 if (ret < min)
135 ret = min;
136 else if (ret > max)
137 ret = max;
138 return ret;
139 }
140
141 public int getPreferredWidth() {
142 int ret = prefWidth;
143 int min = getMinimumWidth();
144 int max = getMaximumWidth();
145
146 if (ret == -1)
147 return max;
148
149 if (ret < min)
150 ret = min;
151 else if (ret > max)
152 ret = max;
153 return ret;
154 }
155
156 public void notifyStateChanged() {
157 Screen owner = getOwner();
158 if (owner != null && owner instanceof Form) {
159 Form form = (Form) owner;
160 form.fireItemStateListener(this);
161 }
162
163 }
164
165 public void removeCommand(Command cmd) {
166 commands.removeElement(cmd);
167 if (defaultCommand == cmd)
168 defaultCommand = null;
169 repaintOwner();
170 }
171
172 public void setDefaultCommand(Command cmd) {
173 this.defaultCommand = cmd;
174 if (cmd != null) {
175
176
177 if (commands.contains(cmd))
178 addCommand(cmd);
179 else
180 repaintOwner();
181 } else {
182 repaintOwner();
183 }
184 }
185
186 public void setItemCommandListener(ItemCommandListener l) {
187 this.commandListener = l;
188 }
189
190 public void setLabel(String label)
191 {
192 labelComponent.setText(label);
193 repaint();
194 }
195
196 public void setLayout(int layout) {
197
198
199
200
201
202
203
204 if ((( (layout & LAYOUT_SHRINK) != 0) &&
205 ((layout & LAYOUT_EXPAND) != 0)) ||
206 ( ((layout & LAYOUT_VSHRINK) != 0) &&
207 (layout & LAYOUT_VEXPAND) != 0) )
208 throw new IllegalArgumentException(
209 "Bad combination of layout policies");
210 this.layout = layout;
211 repaint();
212 }
213
214 public void setPreferredSize(int width, int height) {
215 if (width < -1 || height < -1) {
216 throw new IllegalArgumentException();
217 }
218 this.prefWidth = width;
219 this.prefHeight = height;
220 repaint();
221 }
222
223
224
225
226
227
228 void repaintOwner() {
229 Screen owner = getOwner();
230 if (owner != null)
231 owner.repaint();
232 }
233
234 int getHeight()
235 {
236 return labelComponent.getHeight();
237 }
238
239
240 boolean isFocusable()
241 {
242 return false;
243 }
244
245
246 void keyPressed(int keyCode)
247 {
248 }
249
250
251 abstract int paint(Graphics g);
252
253
254 void paintContent(Graphics g)
255 {
256 labelComponent.paint(g);
257 }
258
259
260 void repaint()
261 {
262 if (owner != null) {
263 owner.repaint();
264 }
265 }
266
267
268 boolean hasFocus()
269 {
270 return focus;
271 }
272
273
274 void setFocus(boolean state)
275 {
276 focus = state;
277 }
278
279
280 Screen getOwner()
281 {
282 return owner;
283 }
284
285
286 void setOwner(Screen owner)
287 {
288 this.owner = owner;
289
290 if (owner == null) {
291 setFocus(false);
292 }
293 }
294
295
296 boolean select()
297 {
298
299
300
301 if (defaultCommand != null && commandListener != null) {
302 commandListener.commandAction(defaultCommand, this);
303 return true;
304 } else {
305 return false;
306 }
307 }
308
309
310 int traverse(int gameKeyCode, int top, int bottom, boolean action)
311 {
312 return 0;
313 }
314
315 int getMaximumHeight() {
316 if (owner != null) {
317 return owner.getHeight() * 10;
318 } else {
319 return DeviceFactory.getDevice().getDeviceDisplay().getHeight() * 10;
320 }
321 }
322
323 int getMaximumWidth() {
324 if (owner != null) {
325 return owner.getWidth() - 3;
326 } else {
327 return DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 3;
328 }
329 }
330
331 ItemCommandListener getItemCommandListener() {
332 return this.commandListener;
333 }
334 }