1 7 package java.awt; 8 9 import java.awt.peer.DialogPeer; 10 import java.awt.event.*; 11 import java.io.ObjectOutputStream ; 12 import java.io.ObjectInputStream ; 13 import java.io.IOException ; 14 import javax.accessibility.*; 15 import sun.awt.AppContext; 16 import sun.awt.SunToolkit; 17 import sun.awt.PeerEvent; 18 import java.security.AccessController ; 19 import java.security.PrivilegedAction ; 20 import java.util.concurrent.atomic.AtomicLong ; 21 22 76 public class Dialog extends Window { 77 78 static { 79 80 Toolkit.loadLibraries(); 81 if (!GraphicsEnvironment.isHeadless()) { 82 initIDs(); 83 } 84 } 85 86 94 boolean resizable = true; 95 96 97 109 boolean undecorated = false; 110 111 121 boolean modal; 122 123 131 String title; 132 133 private transient boolean keepBlocking = false; 134 135 private static final String base = "dialog"; 136 private static int nameCounter = 0; 137 138 141 private static final long serialVersionUID = 5920926903803293709L; 142 143 158 public Dialog(Frame owner) { 159 this(owner, "", false); 160 } 161 162 177 public Dialog(Frame owner, boolean modal) { 178 this(owner, "", modal); 179 } 180 181 199 public Dialog(Frame owner, String title) { 200 this(owner, title, false); 201 } 202 203 222 public Dialog(Frame owner, String title, boolean modal) { 223 super(owner); 224 225 this.title = title; 226 this.modal = modal; 227 SunToolkit.checkAndSetPolicy(this, false); 228 } 229 230 251 public Dialog(Frame owner, String title, boolean modal, 252 GraphicsConfiguration gc) { 253 super(owner, gc); 254 255 this.title = title; 256 this.modal = modal; 257 SunToolkit.checkAndSetPolicy(this, false); 258 } 259 260 270 public Dialog(Dialog owner) { 271 this(owner, "", false); 272 } 273 274 287 public Dialog(Dialog owner, String title) { 288 this(owner, title, false); 289 } 290 291 309 public Dialog(Dialog owner, String title, boolean modal) { 310 super(owner); 311 312 this.title = title; 313 this.modal = modal; 314 SunToolkit.checkAndSetPolicy(this, false); 315 } 316 317 342 public Dialog(Dialog owner, String title, boolean modal, 343 GraphicsConfiguration gc) { 344 super(owner, gc); 345 346 this.title = title; 347 this.modal = modal; 348 SunToolkit.checkAndSetPolicy(this, false); 349 } 350 351 355 String constructComponentName() { 356 synchronized (getClass()) { 357 return base + nameCounter++; 358 } 359 } 360 361 370 public void addNotify() { 371 synchronized (getTreeLock()) { 372 if (parent != null && parent.getPeer() == null) { 373 parent.addNotify(); 374 } 375 376 if (peer == null) { 377 peer = getToolkit().createDialog(this); 378 } 379 super.addNotify(); 380 } 381 } 382 383 393 public boolean isModal() { 394 return modal; 395 } 396 397 402 public void setModal(boolean b) { 403 this.modal = b; 404 } 405 406 413 public String getTitle() { 414 return title; 415 } 416 417 423 public void setTitle(String title) { 424 String oldTitle = this.title; 425 426 synchronized(this) { 427 this.title = title; 428 DialogPeer peer = (DialogPeer)this.peer; 429 if (peer != null) { 430 peer.setTitle(title); 431 } 432 } 433 firePropertyChange("title", oldTitle, title); 434 } 435 436 439 private boolean conditionalShow(Component toFocus, AtomicLong time) { 440 boolean retval; 441 442 synchronized (getTreeLock()) { 443 if (peer == null) { 444 addNotify(); 445 } 446 validate(); 447 if (visible) { 448 toFront(); 449 retval = false; 450 } else { 451 visible = retval = true; 452 if (toFocus != null && time != null && isFocusable() && isEnabled()) 453 { 454 time.set(Toolkit.getEventQueue().getMostRecentEventTimeEx()); 457 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 458 enqueueKeyEvents(time.get(), toFocus); 459 } 460 461 peer.show(); 463 for (int i = 0; i < ownedWindowList.size(); i++) { 464 Window child = ownedWindowList.elementAt(i).get(); 465 if ((child != null) && child.showWithParent) { 466 child.show(); 467 child.showWithParent = false; 468 } } Window.updateChildFocusableWindowState(this); 471 472 createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, 473 this, parent, 474 HierarchyEvent.SHOWING_CHANGED, 475 Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)); 476 } 477 if (retval && (componentListener != null || 478 (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 || 479 Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK))) { 480 ComponentEvent e = 481 new ComponentEvent(this, ComponentEvent.COMPONENT_SHOWN); 482 Toolkit.getEventQueue().postEvent(e); 483 } 484 } 485 486 if (retval && (state & OPENED) == 0) { 487 postWindowEvent(WindowEvent.WINDOW_OPENED); 488 state |= OPENED; 489 } 490 491 return retval; 492 } 493 494 498 transient private AppContext showAppContext; 499 500 504 @Deprecated 505 public void show() { 506 beforeFirstShow = false; 507 if (!isModal()) { 508 conditionalShow(null, null); 509 } else { 510 keepBlocking = true; 514 515 showAppContext = AppContext.getAppContext(); 519 520 AtomicLong time = new AtomicLong (); 523 Component predictedFocusOwner = getMostRecentFocusOwner(); 524 525 try { 526 if (conditionalShow(predictedFocusOwner, time)) { 527 531 final Runnable pumpEventsForHierarchy = new Runnable () { 532 public void run() { 533 EventDispatchThread dispatchThread = 534 (EventDispatchThread )Thread.currentThread(); 535 dispatchThread.pumpEventsForHierarchy(new Conditional () { 536 public boolean evaluate() { 537 return keepBlocking && windowClosingException == null; 538 } 539 }, Dialog.this); 540 } 541 }; 542 543 modalityPushed(); 544 try { 545 if (EventQueue.isDispatchThread()) { 546 552 SequencedEvent currentSequencedEvent = KeyboardFocusManager. 553 getCurrentKeyboardFocusManager().getCurrentSequencedEvent(); 554 if (currentSequencedEvent != null) { 555 currentSequencedEvent.dispose(); 556 } 557 558 563 AccessController.doPrivileged(new PrivilegedAction () { 564 public Object run() { 565 pumpEventsForHierarchy.run(); 566 return null; 567 } 568 }); 569 570 } else { 571 synchronized (getTreeLock()) { 572 Toolkit.getEventQueue(). 573 postEvent(new PeerEvent(this, 574 pumpEventsForHierarchy, 575 PeerEvent.PRIORITY_EVENT)); 576 while (keepBlocking && windowClosingException == null) { 577 try { 578 getTreeLock().wait(); 579 } catch (InterruptedException e) { 580 break; 581 } 582 } 583 } 584 } 585 } finally { 586 modalityPopped(); 587 } 588 if (windowClosingException != null) { 589 windowClosingException.fillInStackTrace(); 590 throw windowClosingException; 591 } 592 } 593 } finally { 594 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 596 dequeueKeyEvents(time.get(), predictedFocusOwner); 597 } 598 } 599 } 600 601 final void modalityPushed() { 602 Toolkit tk = Toolkit.getDefaultToolkit(); 603 if (tk instanceof SunToolkit) { 604 SunToolkit stk = (SunToolkit)tk; 605 stk.notifyModalityPushed(this); 606 } 607 } 608 609 final void modalityPopped() { 610 Toolkit tk = Toolkit.getDefaultToolkit(); 611 if (tk instanceof SunToolkit) { 612 SunToolkit stk = (SunToolkit)tk; 613 stk.notifyModalityPopped(this); 614 } 615 } 616 617 void interruptBlocking() { 618 if (modal) { 619 disposeImpl(); 620 } else if (windowClosingException != null) { 621 windowClosingException.fillInStackTrace(); 622 windowClosingException.printStackTrace(); 623 windowClosingException = null; 624 } 625 } 626 final static class WakingRunnable implements Runnable { 627 public void run() { 628 } 629 } 630 private void hideAndDisposeHandler() { 631 if (keepBlocking) { 632 synchronized (getTreeLock()) { 633 keepBlocking = false; 634 635 if (showAppContext != null) { 636 SunToolkit.postEvent(showAppContext, 639 new PeerEvent(this, 640 new WakingRunnable(), 641 PeerEvent.PRIORITY_EVENT)); 642 showAppContext = null; 643 } 644 EventQueue.invokeLater(new WakingRunnable()); 645 getTreeLock().notifyAll(); 646 } 647 } 648 } 649 650 654 @Deprecated 655 public void hide() { 656 super.hide(); 657 hideAndDisposeHandler(); 658 } 659 660 664 void doDispose() { 665 super.doDispose(); 666 hideAndDisposeHandler(); 667 } 668 669 675 public boolean isResizable() { 676 return resizable; 677 } 678 679 685 public void setResizable(boolean resizable) { 686 boolean testvalid = false; 687 688 synchronized (this) { 689 this.resizable = resizable; 690 DialogPeer peer = (DialogPeer)this.peer; 691 if (peer != null) { 692 peer.setResizable(resizable); 693 testvalid = true; 694 } 695 } 696 697 if (testvalid && valid) { 702 invalidate(); 703 } 704 } 705 706 707 719 public void setUndecorated(boolean undecorated) { 720 721 synchronized (getTreeLock()) { 722 if (isDisplayable()) { 723 throw new IllegalComponentStateException ("The dialog is displayable."); 724 } 725 this.undecorated = undecorated; 726 } 727 } 728 729 737 public boolean isUndecorated() { 738 return undecorated; 739 } 740 741 750 protected String paramString() { 751 String str = super.paramString() + (modal ? ",modal" : ",modeless"); 752 if (title != null) { 753 str += ",title=" + title; 754 } 755 return str; 756 } 757 758 761 private static native void initIDs(); 762 763 767 768 777 public AccessibleContext getAccessibleContext() { 778 if (accessibleContext == null) { 779 accessibleContext = new AccessibleAWTDialog(); 780 } 781 return accessibleContext; 782 } 783 784 789 protected class AccessibleAWTDialog extends AccessibleAWTWindow 790 { 791 794 private static final long serialVersionUID = 4837230331833941201L; 795 796 803 public AccessibleRole getAccessibleRole() { 804 return AccessibleRole.DIALOG; 805 } 806 807 814 public AccessibleStateSet getAccessibleStateSet() { 815 AccessibleStateSet states = super.getAccessibleStateSet(); 816 if (getFocusOwner() != null) { 817 states.add(AccessibleState.ACTIVE); 818 } 819 if (isModal()) { 820 states.add(AccessibleState.MODAL); 821 } 822 if (isResizable()) { 823 states.add(AccessibleState.RESIZABLE); 824 } 825 return states; 826 } 827 828 } } 830 | Popular Tags |