1 107 108 package swingwt.awt; 109 110 import org.eclipse.swt.widgets.*; 111 import org.eclipse.swt.*; 112 import org.eclipse.swt.events.*; 113 114 import swingwtx.swing.*; 115 import java.util.*; 116 117 public class Window extends Container { 118 119 protected Shell peer = null; 120 protected Display display = null; 121 protected Vector windowListeners = new Vector(); 122 protected boolean isClosed = false; 123 protected boolean isIcon = false; 124 125 protected JRootPane rootPane = null; 126 127 128 protected final static int FRAME_DECORATION_HEIGHT = 24; 129 130 protected final static int FRAME_DECORATION_WIDTH = 4; 131 132 private Object retval = null; 133 private Container owner = null; 134 135 136 GraphicsConfiguration graphicsConfig = null; 137 138 public Window(Frame owner) { 139 this.owner = owner; 140 graphicsConfig = (owner == null ? null : owner.getGraphicsConfiguration()); 141 create(); 142 } 143 public Window(Window owner) { 144 this.owner = owner; 145 graphicsConfig = (owner == null ? null : owner.getGraphicsConfiguration()); 146 create(); 147 } 148 public Window(Window owner, GraphicsConfiguration gc) { 149 this.owner = owner; 150 graphicsConfig = (owner == null ? null : owner.getGraphicsConfiguration()); 151 create(); 152 } 153 154 Window(Window owner, boolean isModal) 157 { 158 this.owner = owner; 159 graphicsConfig = (owner == null ? null : owner.getGraphicsConfiguration()); 160 161 int frameType = SWT.DIALOG_TRIM; 162 if (isModal) frameType |= SWT.APPLICATION_MODAL; 163 else frameType |= SWT.MODELESS; 164 165 create(frameType); 166 } 167 168 protected void create() { 169 170 int SWTFrameType = SWT.NO_TRIM; 171 172 if (this instanceof Dialog) 173 SWTFrameType = SWT.DIALOG_TRIM; 174 else if (this instanceof Frame) 175 SWTFrameType = SWT.SHELL_TRIM; 176 177 create(SWTFrameType); 178 } 179 180 188 protected void create(final int SWTFrameType) { 189 SwingWTUtils.incrementWindowReferences(); 190 SwingUtilities.invokeSync(new Runnable () { 191 public void run() { 192 createImpl(SWTFrameType); 193 } 194 }); 195 } 196 197 205 private void createImpl(int SWTFrameType) { 206 display = SwingWTUtils.getDisplay(); 207 208 if (owner == null) 209 peer = new Shell(display, SWTFrameType); 210 else 211 peer = new Shell( (Shell)owner.getPeer(), SWTFrameType); 212 213 composite = peer; 214 peer.setLayout(new swingwt.awt.swtcustomlayout.SWTBorderLayout()); 215 rootPane = new JRootPane(this); 216 try { rootPane.setSwingWTParent(this); } catch (Exception e) { e.printStackTrace(); } 217 peer.setLayoutData(swingwt.awt.swtcustomlayout.SWTBorderLayout.CENTER); 218 registerWindowEvents(); 219 } 220 221 222 public Component add(swingwt.awt.Component c) { rootPane.getContentPane().add(c); return c; } 223 224 public void add(swingwt.awt.Component c, Object layoutModifier) { rootPane.getContentPane().add(c, layoutModifier); } 225 226 public void remove(swingwt.awt.Component c) { rootPane.getContentPane().remove(c); } 227 228 public LayoutManager getLayout() { 229 return rootPane.getContentPane().getLayout(); 230 } 231 232 public void addMouseListener(swingwt.awt.event.MouseListener l) { 233 rootPane.getContentPane().addMouseListener(l); 234 } 235 236 public void removeMouseListener(swingwt.awt.event.MouseListener l) { 237 rootPane.getContentPane().removeMouseListener(l); 238 } 239 240 public void setLayout(LayoutManager l) { 241 rootPane.getContentPane().setLayout(l); 242 } 243 244 public void setLayout(LayoutManager2 l) { 245 rootPane.getContentPane().setLayout(l); 246 } 247 248 public void pack() { 249 final Window me = this; 250 SwingUtilities.invokeSync(new Runnable () { 251 public void run() { 252 Dimension d = rootPane.getContentPane().getPreferredSize(); 253 d.width += FRAME_DECORATION_WIDTH; 254 d.height += FRAME_DECORATION_HEIGHT; 255 peer.setSize( d.width, d.height ); 256 } 257 }); 258 } 259 public void setLocation(final int x, final int y) { 260 SwingUtilities.invokeSync(new Runnable () { 261 public void run() { 262 peer.setLocation(x, y); 263 } 264 }); 265 } 266 267 public void setLocationRelativeTo(Component c) { 268 Dimension paneSize = getSize(); 270 Dimension screenSize = getToolkit().getScreenSize(); 271 setLocation((screenSize.width - paneSize.width) / 2, 272 (screenSize.height - paneSize.height) / 2); 273 } 274 275 public Dimension getSize() { 276 SwingUtilities.invokeSync(new Runnable () { 277 public void run() { 278 retval = new Dimension(peer.getBounds().width, peer.getBounds().height); 279 } 280 }); 281 return (Dimension) retval; 282 } 283 284 public Rectangle getBounds() { 285 SwingUtilities.invokeSync(new Runnable () { 286 public void run() { 287 retval = new Rectangle(peer.getLocation().x, peer.getLocation().y, peer.getSize().x, peer.getSize().y); 288 } 289 }); 290 return (Rectangle) retval; 291 } 292 293 public void setSize(final int width, final int height) { 294 SwingUtilities.invokeSync(new Runnable () { 295 public void run() { 296 peer.setSize(width, height); 297 } 298 }); 299 } 300 public void setSize(final Dimension d) { 301 SwingUtilities.invokeSync(new Runnable () { 302 public void run() { 303 peer.setSize(d.width, d.height); 304 } 305 }); 306 } 307 public void setBounds(final int x, final int y, final int width, final int height) { 308 SwingUtilities.invokeSync(new Runnable () { 309 public void run() { 310 peer.setLocation(x, y); peer.setSize(width, height); 311 } 312 }); 313 } 314 315 public void setBounds(final Rectangle r) { 316 SwingUtilities.invokeSync(new Runnable () { 317 public void run() { 318 peer.setLocation(r.x, r.y); peer.setSize(r.width, r.height); 319 } 320 }); 321 } 322 323 public boolean isActive() { 324 final boolean[] ret = new boolean[1]; 325 SwingUtilities.invokeSync(new Runnable () { 326 public void run() { 327 ret[0] = (SwingWTUtils.getDisplay().getActiveShell() == peer); 328 } 329 }); 330 return ret[0]; 331 } 332 333 public void show() { invalidate(); setVisible(true); } 334 public void hide() { setVisible(false); } 335 public void addWindowListener(swingwt.awt.event.WindowListener l) { windowListeners.add(l); } 336 public void removeWindowListener(swingwt.awt.event.WindowListener l) { windowListeners.remove(l); } 337 338 public void setVisible(final boolean b) { 339 SwingUtilities.invokeSync(new Runnable () { 340 public void run() { 341 if (b) { 342 peer.open(); 343 peer.layout(); processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_OPENED); 345 346 if (!SwingWTUtils.isWindows()) repaintFix(); 348 } 349 else { 350 if (isClosed) return; 353 peer.close(); 354 } 355 } 356 }); 357 } 358 359 public String getTitle() { 360 SwingUtilities.invokeSync(new Runnable () { 361 public void run() { 362 retval = peer.getText(); 363 } 364 }); 365 return retval.toString(); 366 } 367 368 public void setTitle(final String s) { 369 if (!peer.isDisposed()) { 370 SwingUtilities.invokeSync(new Runnable () { 371 public void run() { 372 peer.setText(s); 373 } 374 }); 375 } 376 } 377 378 public Image getIconImage() { 379 final Image[] ret = new Image[1]; 380 SwingUtilities.invokeSync(new Runnable () { 381 public void run() { 382 ret[0] = new Image(); 383 ret[0].image = peer.getImage(); 384 } 385 }); 386 return ret[0]; 387 } 388 389 public void setIconImage(final Image icon) { 390 SwingUtilities.invokeSync(new Runnable () { 391 public void run() { 392 peer.setImage(icon.image); 393 } 394 }); 395 } 396 397 public void dispose() { 398 SwingUtilities.invokeSync(new Runnable () { 399 public void run() { 400 peer.dispose(); 401 } 402 }); 403 super.dispose(); 404 } 405 406 public void setJMenuBar(JMenuBar menu) { 407 rootPane.setJMenuBar(menu); 408 } 409 410 public JMenuBar getJMenuBar() { 411 return rootPane.getJMenuBar(); 412 } 413 414 public void setMenuBar(MenuBar menu) { 415 rootPane.setMenuBar(menu); 416 } 417 418 public MenuBar getMenuBar() { 419 return rootPane.getMenuBar(); 420 } 421 422 public void setDefaultButton(JButton button) { 423 rootPane.setDefaultButton(button); 424 } 425 426 public JButton getDefaultButton() { 427 return rootPane.getDefaultButton(); 428 } 429 430 public void registerWindowEvents() { 431 432 peer.addShellListener(new ShellListener() { 433 public void shellActivated(ShellEvent e) { 434 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_ACTIVATED); 435 } 436 public void shellClosed(ShellEvent e) { 437 isClosed = true; 438 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_CLOSING); 439 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_CLOSED); 440 SwingWTUtils.decrementWindowReferences(); 441 } 442 public void shellDeactivated(ShellEvent e) { 443 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_DEACTIVATED); 444 } 445 public void shellDeiconified(ShellEvent e) { 446 isIcon = false; 447 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_DEICONIFIED); 448 } 449 public void shellIconified(ShellEvent e) { 450 isIcon = true; 451 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_ICONIFIED); 452 } 453 }); 454 } 455 456 protected void processWindowEvent(int id) { 457 swingwt.awt.event.WindowEvent we = new swingwt.awt.event.WindowEvent(this, id); 458 Iterator i = windowListeners.iterator(); 459 while (i.hasNext()) { 460 swingwt.awt.event.WindowListener l = (swingwt.awt.event.WindowListener) i.next(); 461 switch(id) { 462 case (swingwt.awt.event.WindowEvent.WINDOW_ACTIVATED): l.windowActivated(we); break; 463 case (swingwt.awt.event.WindowEvent.WINDOW_CLOSED): l.windowClosed(we); break; 464 case (swingwt.awt.event.WindowEvent.WINDOW_CLOSING): l.windowClosing(we); break; 465 case (swingwt.awt.event.WindowEvent.WINDOW_DEACTIVATED): l.windowDeactivated(we); break; 466 case (swingwt.awt.event.WindowEvent.WINDOW_DEICONIFIED): l.windowDeiconified(we); break; 467 case (swingwt.awt.event.WindowEvent.WINDOW_ICONIFIED): l.windowIconified(we); break; 468 case (swingwt.awt.event.WindowEvent.WINDOW_OPENED): l.windowOpened(we); break; 469 } 470 } 471 } 472 473 protected void processWindowEvent(swingwt.awt.event.WindowEvent e) { 474 swingwt.awt.event.WindowEvent we = e; 475 Iterator i = windowListeners.iterator(); 476 while (i.hasNext()) { 477 swingwt.awt.event.WindowListener l = (swingwt.awt.event.WindowListener) i.next(); 478 switch(e.getID()) { 479 case (swingwt.awt.event.WindowEvent.WINDOW_ACTIVATED): l.windowActivated(we); break; 480 case (swingwt.awt.event.WindowEvent.WINDOW_CLOSED): l.windowClosed(we); break; 481 case (swingwt.awt.event.WindowEvent.WINDOW_CLOSING): l.windowClosing(we); break; 482 case (swingwt.awt.event.WindowEvent.WINDOW_DEACTIVATED): l.windowDeactivated(we); break; 483 case (swingwt.awt.event.WindowEvent.WINDOW_DEICONIFIED): l.windowDeiconified(we); break; 484 case (swingwt.awt.event.WindowEvent.WINDOW_ICONIFIED): l.windowIconified(we); break; 485 case (swingwt.awt.event.WindowEvent.WINDOW_OPENED): l.windowOpened(we); break; 486 } 487 } 488 } 489 490 protected void dispatchEvents() { 491 while (!peer.isDisposed()) { 492 try { 493 if (!display.readAndDispatch()) { 494 display.sleep(); 497 } 498 if (SwingWTUtils.isRetardDispatchThread()) { 500 try { 501 Thread.sleep(SwingWTUtils.getRetardationInterval()); 502 } 503 catch (InterruptedException e) {} 504 } 505 } 506 catch (Error e) { 511 } 512 catch (Exception e) { 513 e.printStackTrace(); 514 } 515 } 516 } 517 518 519 public void toFront() { 520 SwingUtilities.invokeSync(new Runnable () { 521 public void run() { 522 if (SwingWTUtils.isSWTControlAvailable(peer)) 523 peer.forceActive(); 524 } 525 }); 526 } 527 528 529 public void toBack() { 530 SwingUtilities.invokeSync(new Runnable () { 531 public void run() { 532 if (SwingWTUtils.isSWTControlAvailable(peer)) { 533 Shell[] s = SwingWTUtils.getDisplay().getShells(); 535 for (int i = 0; i < s.length; i++) { 536 if (!s[i].equals(peer)) 537 s[i].forceActive(); 538 } 539 } 540 } 541 }); 542 } 543 544 549 protected void repaintFix() { 550 551 SwingUtilities.invokeIn(new Runnable () { 552 public void run() { 553 if (SwingWTUtils.isSWTControlAvailable(peer)) 554 peer.setSize(peer.getSize().x + 1, peer.getSize().y + 1); 555 } 556 }, 1); 557 } 558 559 public Control getPeer() { return peer; } 560 public void setSwingWTParent(Composite parent) throws Exception { throw new Exception ("Window can't have parent."); } 561 562 } 563 564 | Popular Tags |