1 2 23 24 package javax.microedition.lcdui; 25 26 import java.util.Enumeration; 27 import java.util.Vector; 28 29 import javax.microedition.midlet.MIDlet; 30 31 import com.barteo.emulator.CommandManager; 32 import com.barteo.emulator.DisplayAccess; 33 import com.barteo.emulator.MIDletBridge; 34 import com.barteo.emulator.device.DeviceFactory; 35 36 public class Display 37 { 38 private static EventDispatcher eventDispatcher = null; 39 private static TickerPaint tickerPaint = null; 40 41 private Displayable current = null; 42 private Displayable nextScreen = null; 43 44 private DisplayAccessor accessor = null; 45 46 private Object paintLock = new Object(); 47 private boolean repaintPending = false; 48 49 private class DisplayAccessor implements DisplayAccess 50 { 51 Display display; 52 53 DisplayAccessor(Display d) 54 { 55 display = d; 56 } 57 58 public void commandAction(Command cmd) 59 { 60 if (current == null) { 61 return; 62 } 63 CommandListener listener = current.getCommandListener(); 64 if (listener == null) { 65 return; 66 } 67 listener.commandAction(cmd, current); 68 } 69 70 public Display getDisplay() 71 { 72 return display; 73 } 74 75 public void keyPressed(int keyCode) 76 { 77 if (current != null) { 78 current.keyPressed(keyCode); 79 } 80 } 81 82 public void keyReleased(int keyCode) 83 { 84 if (current != null) { 85 current.keyReleased(keyCode); 86 } 87 } 88 89 public void paint(Graphics g) 90 { 91 if (current != null) { 92 current.paint(g); 93 g.translate(-g.getTranslateX(), -g.getTranslateY()); 94 synchronized (paintLock) { 95 repaintPending = false; 96 paintLock.notify(); 97 } 98 } 99 } 100 101 public Displayable getCurrent() 102 { 103 return getDisplay().getCurrent(); 104 } 105 106 public void setCurrent(Displayable d) 107 { 108 getDisplay().setCurrent(d); 109 } 110 111 public void updateCommands() 112 { 113 getDisplay().updateCommands(); 114 } 115 116 public void clean() 117 { 118 if (current != null) { 119 current.hideNotify(); 120 } 121 } 122 } 123 124 private class AlertTimeout implements Runnable 125 { 126 int time; 127 128 AlertTimeout(int time) 129 { 130 this.time = time; 131 } 132 133 public void run() 134 { 135 try { 136 Thread.sleep(time); 137 } catch (InterruptedException ex) { 138 ex.printStackTrace(); 139 } 140 141 setCurrent(nextScreen); 142 } 143 } 144 145 private class TickerPaint implements Runnable 146 { 147 private Display currentDisplay = null; 148 149 public void setCurrentDisplay(Display currentDisplay) 150 { 151 this.currentDisplay = currentDisplay; 152 } 153 154 public void run() 155 { 156 while (true) { 157 if (currentDisplay != null && currentDisplay.current != null && currentDisplay.current instanceof Screen) { 158 Ticker ticker = ((Screen) currentDisplay.current).getTicker(); 159 if (ticker != null) { 160 synchronized (ticker) { 161 if (ticker.resetTextPosTo != -1) { 162 ticker.textPos = ticker.resetTextPosTo; 163 ticker.resetTextPosTo = -1; 164 } 165 ticker.textPos -= Ticker.PAINT_MOVE; 166 } 167 currentDisplay.repaint(); 168 } 169 } 170 try { 171 Thread.sleep(Ticker.PAINT_TIMEOUT); 172 } catch (InterruptedException ex) { 173 ex.printStackTrace(); 174 tickerPaint = null; 175 } 176 } 177 } 178 } 179 180 private class EventDispatcher implements Runnable 181 { 182 private Vector events = new Vector(); 183 184 public void add(Runnable r) 185 { 186 synchronized (paintLock) { 187 events.addElement(r); 188 paintLock.notify(); 189 } 190 } 191 192 public void run() 193 { 194 Vector jobs; 195 196 while (true) { 197 jobs = null; 198 synchronized (paintLock) { 199 if (!repaintPending) { 200 if (events.size() > 0) { 201 jobs = (Vector) events.clone(); 202 events.removeAllElements(); 203 } 204 } 205 } 206 207 if (jobs != null) { 208 for (Enumeration en = jobs.elements(); en.hasMoreElements();) { 209 ((Runnable) en.nextElement()).run(); 210 } 211 } 212 213 try { 214 synchronized (paintLock) { 215 if (events.size() == 0) { 216 paintLock.wait(); 217 } 218 } 219 } catch (InterruptedException ex) { 220 ex.printStackTrace(); 221 eventDispatcher = null; 222 } 223 } 224 } 225 } 226 227 228 Display() 229 { 230 accessor = new DisplayAccessor(this); 231 232 if (eventDispatcher == null) { 233 eventDispatcher = new EventDispatcher(); 234 new Thread(eventDispatcher, "EventDispatcher").start(); 235 } 236 if (tickerPaint == null) { 237 tickerPaint = new TickerPaint(); 238 new Thread(tickerPaint, "TickerPaint").start(); 239 } 240 } 241 242 243 public void callSerially(Runnable r) 244 { 245 eventDispatcher.add(r); 246 } 247 248 249 public int numColors() 250 { 251 return DeviceFactory.getDevice().getDeviceDisplay().numColors(); 252 } 253 254 255 public static Display getDisplay(MIDlet m) 256 { 257 Display result; 258 259 if (MIDletBridge.getMIDletAccess(m).getDisplayAccess() == null) { 260 result = new Display(); 261 MIDletBridge.getMIDletAccess(m).setDisplayAccess(result.accessor); 262 } else { 263 result = MIDletBridge.getMIDletAccess(m).getDisplayAccess().getDisplay(); 264 } 265 266 tickerPaint.setCurrentDisplay(result); 267 268 return result; 269 } 270 271 272 public Displayable getCurrent() 273 { 274 return current; 275 } 276 277 278 public boolean isColor() 279 { 280 return DeviceFactory.getDevice().getDeviceDisplay().isColor(); 281 } 282 283 284 public void setCurrent(Displayable nextDisplayable) 285 { 286 if (nextDisplayable != null) { 287 if (current != null) { 288 current.hideNotify(this); 289 } 290 291 if (nextDisplayable instanceof Alert) { 292 setCurrent((Alert) nextDisplayable, current); 293 return; 294 } 295 296 current = nextDisplayable; 297 current.showNotify(this); 298 setScrollUp(false); 299 setScrollDown(false); 300 updateCommands(); 301 302 current.repaint(); 303 } 304 } 305 306 307 public void setCurrent(Alert alert, Displayable nextDisplayable) 308 { 309 nextScreen = nextDisplayable; 310 311 current = alert; 312 313 current.showNotify(this); 314 updateCommands(); 315 current.repaint(); 316 317 if (alert.getTimeout() != Alert.FOREVER) { 318 AlertTimeout at = new AlertTimeout(alert.getTimeout()); 319 Thread t = new Thread(at); 320 t.start(); 321 } 322 } 323 324 325 void clearAlert() 326 { 327 setCurrent(nextScreen); 328 } 329 330 331 static int getGameAction(int keyCode) 332 { 333 return DeviceFactory.getDevice().getGameAction(keyCode); 334 } 335 336 337 static int getKeyCode(int gameAction) 338 { 339 return DeviceFactory.getDevice().getKeyCode(gameAction); 340 } 341 342 343 boolean isShown(Displayable d) 344 { 345 if (current == null || current != d) { 346 return false; 347 } else { 348 return true; 349 } 350 } 351 352 353 void repaint(Displayable d) 354 { 355 if (current == d) { 356 synchronized (paintLock) { 357 repaintPending = true; 358 } 359 DeviceFactory.getDevice().getDeviceDisplay().repaint(); 360 } 361 } 362 363 364 void setScrollDown(boolean state) 365 { 366 DeviceFactory.getDevice().getDeviceDisplay().setScrollDown(state); 367 } 368 369 370 void setScrollUp(boolean state) 371 { 372 DeviceFactory.getDevice().getDeviceDisplay().setScrollUp(state); 373 } 374 375 376 void updateCommands() 377 { 378 if (current == null) { 379 CommandManager.getInstance().updateCommands(null); 380 } else { 381 CommandManager.getInstance().updateCommands(current.getCommands()); 382 } 383 387 MIDletBridge.notifySoftkeyLabelsChanged(); 388 repaint(); 389 } 390 391 392 private void repaint() 393 { 394 if (current != null) { 395 repaint(current); 396 } 397 } 398 399 } 400 | Popular Tags |