|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package javax.microedition.lcdui; |
|
18 |
|
19 import javax.microedition.lcdui.EventDispatcher.LCDUIEvent; |
|
20 |
|
21 import org.eclipse.swt.graphics.Point; |
|
22 import org.eclipse.swt.graphics.Rectangle; |
|
23 import org.eclipse.swt.internal.extension.CanvasExtension; |
|
24 import org.eclipse.swt.widgets.Control; |
|
25 |
|
26 /** |
|
27 * Implementation of LCDUI abstract <code>CustomItem</code> class. |
|
28 */ |
|
29 public abstract class CustomItem extends Item { |
|
30 |
|
31 protected static final int NONE = 0; |
|
32 protected static final int TRAVERSE_HORIZONTAL = 1; |
|
33 protected static final int TRAVERSE_VERTICAL = 2; |
|
34 |
|
35 protected static final int KEY_PRESS = 4; |
|
36 protected static final int KEY_RELEASE = 8; |
|
37 protected static final int KEY_REPEAT = 0x10; |
|
38 |
|
39 protected static final int POINTER_PRESS = 0x20; |
|
40 protected static final int POINTER_RELEASE = 0x40; |
|
41 protected static final int POINTER_DRAG = 0x80; |
|
42 |
|
43 /** |
|
44 * Constant used by layouter to repaint CustomItem's rectangle section. |
|
45 */ |
|
46 static final int UPDATE_REPAINT_RECT = 4; |
|
47 |
|
48 private boolean cleanupNeeded; |
|
49 private int contentWidth; |
|
50 private int contentHeight; |
|
51 |
|
52 private boolean repaintPending; |
|
53 private int repaintX1; |
|
54 private int repaintY1; |
|
55 private int repaintX2; |
|
56 private int repaintY2; |
|
57 private Object repaintLock; |
|
58 private Object cleanupLock; |
|
59 private Object resizeLock; |
|
60 |
|
61 boolean bufferFlush; |
|
62 |
|
63 CustomItemLayouter layouter; |
|
64 |
|
65 /** |
|
66 * Constructor. |
|
67 * |
|
68 * @param label Label of CustomItem. |
|
69 */ |
|
70 protected CustomItem(String label) { |
|
71 repaintLock = new Object(); |
|
72 cleanupLock = new Object(); |
|
73 resizeLock = new Object(); |
|
74 setLabel(label); |
|
75 } |
|
76 |
|
77 /** |
|
78 * Get the game action associated with the key code. |
|
79 * |
|
80 * @param keyCode key code |
|
81 * @return game action bound to the key |
|
82 */ |
|
83 public int getGameAction(int keyCode) { |
|
84 return KeyTable.getGameAction(keyCode); |
|
85 } |
|
86 |
|
87 /** |
|
88 * Get the interaction modes available on this device. |
|
89 * |
|
90 * @return bitmask of available interaction modes. |
|
91 */ |
|
92 protected final int getInteractionModes() { |
|
93 // TODO: check final interaction modes |
|
94 return TRAVERSE_HORIZONTAL | TRAVERSE_VERTICAL |
|
95 | KEY_PRESS | KEY_RELEASE |
|
96 | POINTER_PRESS | POINTER_DRAG | POINTER_RELEASE; |
|
97 } |
|
98 |
|
99 /** |
|
100 * Invalidates CustomItem. |
|
101 */ |
|
102 protected final void invalidate() { |
|
103 updateParent(UPDATE_SIZE_CHANGED); |
|
104 } |
|
105 |
|
106 /** |
|
107 * Requests repainting of CustomItem. |
|
108 */ |
|
109 protected final void repaint() { |
|
110 repaint(0, 0, contentWidth, contentHeight); |
|
111 } |
|
112 |
|
113 /** |
|
114 * Requests repainting of the specified area in CustomItem. |
|
115 * |
|
116 * @param aX |
|
117 * @param aY |
|
118 * @param aWidth |
|
119 * @param aHeight |
|
120 */ |
|
121 protected final void repaint(int aX, int aY, int aWidth, int aHeight) { |
|
122 Rectangle rect = new Rectangle(aX, aY, aWidth, aHeight); |
|
123 // From here it goes to updateItem() |
|
124 updateParent(UPDATE_REPAINT_RECT, rect); |
|
125 } |
|
126 |
|
127 /** |
|
128 * Callback method for traverse-in event. |
|
129 * |
|
130 * @param direction |
|
131 * @param viewportWidth |
|
132 * @param viewportHeight |
|
133 * @param visRect if the CustomItem supports internal traversal and keeps |
|
134 * focus, visRect represents the visible rectangle (focused area) |
|
135 * @return true if internal traversal has occurred, false otherwise |
|
136 */ |
|
137 protected boolean traverse(int direction, int viewportWidth, |
|
138 int viewportHeight, int[] visRect) { |
|
139 return false; |
|
140 } |
|
141 |
|
142 /** |
|
143 * Callback method for traverse-out event. |
|
144 */ |
|
145 protected void traverseOut() { |
|
146 } |
|
147 |
|
148 /** |
|
149 * Callback method for key pressed event. |
|
150 */ |
|
151 protected void keyPressed(int aKeyCode) { |
|
152 } |
|
153 |
|
154 /** |
|
155 * Callback method for key released event. |
|
156 */ |
|
157 protected void keyReleased(int aKeyCode) { |
|
158 } |
|
159 |
|
160 /** |
|
161 * Callback method for key repeated event. |
|
162 */ |
|
163 protected void keyRepeated(int aKeyCode) { |
|
164 } |
|
165 |
|
166 /** |
|
167 * Callback method for pointer pressed event. |
|
168 */ |
|
169 protected void pointerPressed(int aX, int aY) { |
|
170 } |
|
171 |
|
172 /** |
|
173 * Callback method for pointer released event. |
|
174 */ |
|
175 protected void pointerReleased(int aX, int aY) { |
|
176 } |
|
177 |
|
178 /** |
|
179 * Callback method for pointer dragged event. |
|
180 */ |
|
181 protected void pointerDragged(int aX, int aY) { |
|
182 } |
|
183 |
|
184 /** |
|
185 * Callback method when item gets shown. |
|
186 */ |
|
187 protected void showNotify() { |
|
188 } |
|
189 |
|
190 /** |
|
191 * Callback method when item gets hidden. |
|
192 */ |
|
193 protected void hideNotify() { |
|
194 } |
|
195 |
|
196 /** |
|
197 * Callback method for content area size change event. |
|
198 */ |
|
199 protected void sizeChanged(int aWidth, int aHeight) { |
|
200 } |
|
201 |
|
202 /** |
|
203 * Abstract method. |
|
204 * |
|
205 * @param graphics |
|
206 * @param aWidth |
|
207 * @param aHeight |
|
208 */ |
|
209 protected abstract void paint(Graphics graphics, int aWidth, int aHeight); |
|
210 |
|
211 /** |
|
212 * Should return the minimum width of the content area. |
|
213 */ |
|
214 protected abstract int getMinContentWidth(); |
|
215 |
|
216 /** |
|
217 * Should return the minimum height of the content area. |
|
218 */ |
|
219 protected abstract int getMinContentHeight(); |
|
220 |
|
221 /** |
|
222 * Should return the preferred width of the content area. |
|
223 */ |
|
224 protected abstract int getPrefContentWidth(int aHeight); |
|
225 |
|
226 /** |
|
227 * Should return the preferred height of the content area. |
|
228 */ |
|
229 protected abstract int getPrefContentHeight(int aWidth); |
|
230 |
|
231 /** |
|
232 * Calculates minimum size of this item. |
|
233 * |
|
234 * @return Minimum size. |
|
235 */ |
|
236 Point calculateMinimumSize() { |
|
237 return CustomItemLayouter.calculateMinimumBounds(this); |
|
238 } |
|
239 |
|
240 /** |
|
241 * Calculates preferred size of this item. |
|
242 * |
|
243 * @return Preferred size. |
|
244 */ |
|
245 Point calculatePreferredSize() { |
|
246 return CustomItemLayouter.calculatePreferredBounds(this); |
|
247 } |
|
248 |
|
249 /** |
|
250 * Set the size of the content area. |
|
251 * |
|
252 * @param newWidth |
|
253 * @param newHeight |
|
254 */ |
|
255 void internalHandleSizeChanged(int newWidth, int newHeight) { |
|
256 if (contentWidth != newWidth || contentHeight != newHeight) { |
|
257 synchronized(resizeLock) { |
|
258 contentWidth = newWidth; |
|
259 contentHeight = newHeight; |
|
260 } |
|
261 EventDispatcher eventDispatcher = EventDispatcher.instance(); |
|
262 LCDUIEvent event = eventDispatcher.newEvent(LCDUIEvent.CUSTOMITEM_SIZECHANGED, layouter.dfi.getForm()); |
|
263 event.item = this; |
|
264 eventDispatcher.postEvent(event); |
|
265 synchronized(cleanupLock) { |
|
266 cleanupNeeded = true; |
|
267 } |
|
268 repaint(); |
|
269 } |
|
270 } |
|
271 |
|
272 /* |
|
273 * Note that if you call this and getContentHeight() from a non-UI thread |
|
274 * then it must be made sure size doesn't change between the calls. |
|
275 */ |
|
276 int getContentWidth() { |
|
277 return contentWidth; |
|
278 } |
|
279 |
|
280 /* |
|
281 * Note that if you call this and getContentHeight() from a non-UI thread |
|
282 * then it must be made sure size doesn't change between the calls. |
|
283 */ |
|
284 int getContentHeight() { |
|
285 return contentHeight; |
|
286 } |
|
287 |
|
288 boolean isFocusable() { |
|
289 return true; |
|
290 } |
|
291 |
|
292 /* |
|
293 * This gets called when Form contents are modified. |
|
294 */ |
|
295 void setParent(Screen parent) { |
|
296 super.setParent(parent); |
|
297 if(parent == null) { |
|
298 // Item was removed from a Form |
|
299 layouter = null; |
|
300 } else { |
|
301 // Item was added to a Form |
|
302 layouter = ((CustomItemLayouter)((Form)parent).getLayoutPolicy().getLayouter(this)); |
|
303 } |
|
304 } |
|
305 |
|
306 private boolean invalidate(int x, int y, int width, int height) { |
|
307 // Regularize bounds |
|
308 final int x1 = x; |
|
309 final int y1 = y; |
|
310 final int x2 = x + width; |
|
311 final int y2 = y + height; |
|
312 |
|
313 // Union the current and new damaged rects |
|
314 final boolean valid = ((repaintX2 - repaintX1) <= 0) && ((repaintY2 - repaintY1) <= 0); |
|
315 if (!valid) { |
|
316 repaintX1 = Math.min(repaintX1, x1); |
|
317 repaintY1 = Math.min(repaintY1, y1); |
|
318 repaintX2 = Math.max(repaintX2, x2); |
|
319 repaintY2 = Math.max(repaintY2, y2); |
|
320 } else { |
|
321 repaintX1 = x1; |
|
322 repaintY1 = y1; |
|
323 repaintX2 = x2; |
|
324 repaintY2 = y2; |
|
325 } |
|
326 |
|
327 // UI thread can change the size at any time. Must make sure it doesn't |
|
328 // change between reading the width and the height. |
|
329 final int w; |
|
330 final int h; |
|
331 synchronized(resizeLock) { |
|
332 w = contentWidth; |
|
333 h = contentHeight; |
|
334 } |
|
335 |
|
336 // Clip to bounds |
|
337 repaintX1 = repaintX1 > 0 ? repaintX1 : 0; |
|
338 repaintY1 = repaintY1 > 0 ? repaintY1 : 0; |
|
339 repaintX2 = repaintX2 < w ? repaintX2 : w; |
|
340 repaintY2 = repaintY2 < h ? repaintY2 : h; |
|
341 |
|
342 return valid; |
|
343 } |
|
344 |
|
345 /* |
|
346 * Note the control passed as a parameter can change. It must not be stored |
|
347 * to the CustomItem. Adding and removing Form item is blocked for the |
|
348 * duration of the call. |
|
349 */ |
|
350 void updateItem(Rectangle rect, Control control) { |
|
351 // Paint callback event is posted without any invalid area info. |
|
352 // Invalid area info is kept in the member variables. Only one event |
|
353 // per Canvas is added to the queue. If there are more repaint() calls |
|
354 // before the already posted event has been served those are merged |
|
355 // to the invalid area in the member variables without posting a new |
|
356 // event. |
|
357 synchronized(repaintLock) { |
|
358 if (invalidate(rect.x, rect.y, rect.width, rect.height)) { |
|
359 // Note that repaintPending doesn't mean that there's a repaint |
|
360 // event in the queue. It means that the invalid area is going |
|
361 // to be repainted and there's no need to add a new event. |
|
362 // It's possible that the repaint event has already been |
|
363 // removed from the queue but repaintPending is still true. In |
|
364 // that case it's currently being processed and we can still |
|
365 // add to the invalid area. |
|
366 if (!repaintPending) { |
|
367 EventDispatcher eventDispatcher = EventDispatcher.instance(); |
|
368 LCDUIEvent event = eventDispatcher.newEvent( |
|
369 LCDUIEvent.CUSTOMITEM_PAINT_MIDLET_REQUEST, layouter.dfi.getForm()); |
|
370 event.widget = control; |
|
371 event.item = this; |
|
372 eventDispatcher.postEvent(event); |
|
373 repaintPending = true; |
|
374 } |
|
375 } |
|
376 } |
|
377 } |
|
378 |
|
379 /* |
|
380 * Dispatcher thread thread calls. Operations changing Form content are |
|
381 * blocked for the duration of the method call. It has been checked that |
|
382 * the eSWT widget has not been disposed and that the CustomItem has not |
|
383 * been removed from the Form. |
|
384 */ |
|
385 void doCallback(final LCDUIEvent event) { |
|
386 switch(event.type) { |
|
387 case LCDUIEvent.CUSTOMITEM_PAINT_MIDLET_REQUEST: |
|
388 case LCDUIEvent.CUSTOMITEM_PAINT_NATIVE_REQUEST: |
|
389 doPaintCallback(event); |
|
390 break; |
|
391 case LCDUIEvent.CUSTOMITEM_HIDENOTIFY: |
|
392 hideNotify(); |
|
393 break; |
|
394 case LCDUIEvent.CUSTOMITEM_SHOWNOTIFY: |
|
395 showNotify(); |
|
396 break; |
|
397 case LCDUIEvent.CUSTOMITEM_KEYPRESSED: |
|
398 keyPressed(event.keyCode); |
|
399 break; |
|
400 case LCDUIEvent.CUSTOMITEM_KEYREPEATED: |
|
401 keyRepeated(event.keyCode); |
|
402 break; |
|
403 case LCDUIEvent.CUSTOMITEM_KEYRELEASED: |
|
404 keyReleased(event.keyCode); |
|
405 break; |
|
406 case LCDUIEvent.CUSTOMITEM_POINTERDRAGGED: |
|
407 pointerDragged(event.x, event.y); |
|
408 break; |
|
409 case LCDUIEvent.CUSTOMITEM_POINTERPRESSED: |
|
410 pointerPressed(event.x, event.y); |
|
411 break; |
|
412 case LCDUIEvent.CUSTOMITEM_POINTERRELEASED: |
|
413 pointerReleased(event.x, event.y); |
|
414 break; |
|
415 case LCDUIEvent.CUSTOMITEM_SIZECHANGED: |
|
416 sizeChanged(event.width, event.height); |
|
417 break; |
|
418 default: |
|
419 super.doCallback(event); |
|
420 } |
|
421 } |
|
422 |
|
423 /* |
|
424 * Dispatcher thread calls. |
|
425 */ |
|
426 void doPaintCallback(final LCDUIEvent event) { |
|
427 // Decide the area going to be painted by the callback. |
|
428 final int redrawNowX; |
|
429 final int redrawNowY; |
|
430 final int redrawNowW; |
|
431 final int redrawNowH; |
|
432 // Before this thread obtains the repaintLock any repaint() calls |
|
433 // will still be adding to the invalid area that is going to be |
|
434 // painted by this callback. |
|
435 synchronized(repaintLock) { |
|
436 if(event.type == LCDUIEvent.CUSTOMITEM_PAINT_NATIVE_REQUEST) { |
|
437 // Merge with possibly existing repaint() requests |
|
438 invalidate(event.x, event.y, event.width, event.height); |
|
439 } else { |
|
440 // Need to add a new event to the queue in subsequent repaint() |
|
441 // calls. |
|
442 repaintPending = false; |
|
443 } |
|
444 |
|
445 // Store the current area to be painted |
|
446 redrawNowX = repaintX1; |
|
447 redrawNowY = repaintY1; |
|
448 redrawNowW = repaintX2-repaintX1; |
|
449 redrawNowH = repaintY2-repaintY1; |
|
450 |
|
451 // After releasing the lock the repaint() calls will start with |
|
452 // new invalid area. |
|
453 repaintX1 = repaintX2 = repaintY1 = repaintY2 = 0; |
|
454 |
|
455 // Don't do the callback if there's nothing to paint |
|
456 if(!((redrawNowW > 0) && (redrawNowH > 0))) { |
|
457 return; |
|
458 } |
|
459 } |
|
460 |
|
461 // Prepare the GC's buffer if not done yet |
|
462 if (layouter.graphics.getCommandBuffer() == null) { |
|
463 layouter.graphics.initBuffered(this, event.x, event.y, event.width, event.height); |
|
464 } |
|
465 |
|
466 // Clean the background if dirty, buffer the operations. |
|
467 synchronized(cleanupLock) { |
|
468 if (cleanupNeeded && layouter.noBackground) { |
|
469 // Must be made sure that size doesn't change between reading |
|
470 // the width and the height. |
|
471 int contentWidth, contentHeight; |
|
472 synchronized(resizeLock) { |
|
473 contentWidth = this.contentWidth; |
|
474 contentHeight = this.contentHeight; |
|
475 } |
|
476 |
|
477 layouter.graphics.setClip(0, 0, contentWidth, contentHeight); |
|
478 layouter.graphics.cleanBackground(new Rectangle(0, 0, contentWidth, contentHeight)); |
|
479 cleanupNeeded = false; |
|
480 } |
|
481 } |
|
482 |
|
483 // Clip must define the invalid area |
|
484 layouter.graphics.setClip(redrawNowX, redrawNowY, redrawNowW, redrawNowH); |
|
485 |
|
486 // The callback |
|
487 paint(layouter.graphics, contentWidth, contentHeight); |
|
488 |
|
489 // Wait until the UI thread is available. Then in the UI thread |
|
490 // synchronously send a paint event. |
|
491 ESWTUIThreadRunner.safeSyncExec(new Runnable() { |
|
492 public void run() { |
|
493 if(event.widget.isDisposed()) { |
|
494 return; |
|
495 } |
|
496 bufferFlush = true; |
|
497 ((CanvasExtension) event.widget) |
|
498 .redrawNow(redrawNowX, redrawNowY, redrawNowW, redrawNowH); |
|
499 layouter.graphics.resetCommandBuffer(); |
|
500 bufferFlush = false; |
|
501 } |
|
502 }); |
|
503 } |
|
504 } |