jdk/src/share/classes/java/awt/Dialog.java

Print this page




 728      * Construct a name for this component.  Called by getName() when the
 729      * name is null.
 730      */
 731     String constructComponentName() {
 732         synchronized (Dialog.class) {
 733             return base + nameCounter++;
 734         }
 735     }
 736 
 737     /**
 738      * Makes this Dialog displayable by connecting it to
 739      * a native screen resource.  Making a dialog displayable will
 740      * cause any of its children to be made displayable.
 741      * This method is called internally by the toolkit and should
 742      * not be called directly by programs.
 743      * @see Component#isDisplayable
 744      * @see #removeNotify
 745      */
 746     public void addNotify() {
 747         synchronized (getTreeLock()) {
 748             if (parent != null && parent.getPeer() == null) {
 749                 parent.addNotify();
 750             }
 751 
 752             if (peer == null) {
 753                 peer = getToolkit().createDialog(this);
 754             }
 755             super.addNotify();
 756         }
 757     }
 758 
 759     /**
 760      * Indicates whether the dialog is modal.
 761      * <p>
 762      * This method is obsolete and is kept for backwards compatiblity only.
 763      * Use {@link #getModalityType getModalityType()} instead.
 764      *
 765      * @return    <code>true</code> if this dialog window is modal;
 766      *            <code>false</code> otherwise
 767      *
 768      * @see       java.awt.Dialog#DEFAULT_MODALITY_TYPE


 925                     // keep the KeyEvents from being dispatched
 926                     // until the focus has been transfered
 927                     time.set(Toolkit.getEventQueue().getMostRecentKeyEventTime());
 928                     KeyboardFocusManager.getCurrentKeyboardFocusManager().
 929                         enqueueKeyEvents(time.get(), toFocus);
 930                 }
 931 
 932                 // This call is required as the show() method of the Dialog class
 933                 // does not invoke the super.show(). So wried... :(
 934                 mixOnShowing();
 935 
 936                 peer.setVisible(true); // now guaranteed never to block
 937                 if (isModalBlocked()) {
 938                     modalBlocker.toFront();
 939                 }
 940 
 941                 setLocationByPlatform(false);
 942                 for (int i = 0; i < ownedWindowList.size(); i++) {
 943                     Window child = ownedWindowList.elementAt(i).get();
 944                     if ((child != null) && child.showWithParent) {
 945                         child.show();
 946                         child.showWithParent = false;
 947                     }       // endif
 948                 }   // endfor
 949                 Window.updateChildFocusableWindowState(this);
 950 
 951                 createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
 952                                       this, parent,
 953                                       HierarchyEvent.SHOWING_CHANGED,
 954                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
 955                 if (componentListener != null ||
 956                         (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
 957                         Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
 958                     ComponentEvent e =
 959                         new ComponentEvent(this, ComponentEvent.COMPONENT_SHOWN);
 960                     Toolkit.getEventQueue().postEvent(e);
 961                 }
 962             }
 963         }
 964 
 965         if (retval && (state & OPENED) == 0) {


1030             conditionalShow(null, null);
1031         } else {
1032             AppContext showAppContext = AppContext.getAppContext();
1033 
1034             AtomicLong time = new AtomicLong();
1035             Component predictedFocusOwner = null;
1036             try {
1037                 predictedFocusOwner = getMostRecentFocusOwner();
1038                 if (conditionalShow(predictedFocusOwner, time)) {
1039                     modalFilter = ModalEventFilter.createFilterForDialog(this);
1040                     final Conditional cond = new Conditional() {
1041                         @Override
1042                         public boolean evaluate() {
1043                             return windowClosingException == null;
1044                         }
1045                     };
1046 
1047                     // if this dialog is toolkit-modal, the filter should be added
1048                     // to all EDTs (for all AppContexts)
1049                     if (modalityType == ModalityType.TOOLKIT_MODAL) {
1050                         Iterator it = AppContext.getAppContexts().iterator();
1051                         while (it.hasNext()) {
1052                             AppContext appContext = (AppContext)it.next();
1053                             if (appContext == showAppContext) {
1054                                 continue;
1055                             }
1056                             EventQueue eventQueue = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
1057                             // it may occur that EDT for appContext hasn't been started yet, so
1058                             // we post an empty invocation event to trigger EDT initialization
1059                             Runnable createEDT = new Runnable() {
1060                                 public void run() {};
1061                             };
1062                             eventQueue.postEvent(new InvocationEvent(this, createEDT));
1063                             EventDispatchThread edt = eventQueue.getDispatchThread();
1064                             edt.addEventFilter(modalFilter);
1065                         }
1066                     }
1067 
1068                     modalityPushed();
1069                     try {
1070                         final EventQueue eventQueue = AccessController.doPrivileged(
1071                             new PrivilegedAction<EventQueue>() {
1072                                 public EventQueue run() {
1073                                     return Toolkit.getDefaultToolkit().getSystemEventQueue();
1074                                 }
1075                         });
1076                         secondaryLoop = eventQueue.createSecondaryLoop(cond, modalFilter, 0);
1077                         if (!secondaryLoop.enter()) {
1078                             secondaryLoop = null;
1079                         }
1080                     } finally {
1081                         modalityPopped();
1082                     }
1083 
1084                     // if this dialog is toolkit-modal, its filter must be removed
1085                     // from all EDTs (for all AppContexts)
1086                     if (modalityType == ModalityType.TOOLKIT_MODAL) {
1087                         Iterator it = AppContext.getAppContexts().iterator();
1088                         while (it.hasNext()) {
1089                             AppContext appContext = (AppContext)it.next();
1090                             if (appContext == showAppContext) {
1091                                 continue;
1092                             }
1093                             EventQueue eventQueue = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
1094                             EventDispatchThread edt = eventQueue.getDispatchThread();
1095                             edt.removeEventFilter(modalFilter);
1096                         }
1097                     }
1098 
1099                     if (windowClosingException != null) {
1100                         windowClosingException.fillInStackTrace();
1101                         throw windowClosingException;
1102                     }
1103                 }
1104             } finally {
1105                 if (predictedFocusOwner != null) {
1106                     // Restore normal key event dispatching
1107                     KeyboardFocusManager.getCurrentKeyboardFocusManager().
1108                         dequeueKeyEvents(time.get(), predictedFocusOwner);
1109                 }


1379      * --- Modality support ---
1380      *
1381      */
1382 
1383     /*
1384      * This method is called only for modal dialogs.
1385      *
1386      * Goes through the list of all visible top-level windows and
1387      * divide them into three distinct groups: blockers of this dialog,
1388      * blocked by this dialog and all others. Then blocks this dialog
1389      * by first met dialog from the first group (if any) and blocks all
1390      * the windows from the second group.
1391      */
1392     void modalShow() {
1393         // find all the dialogs that block this one
1394         IdentityArrayList<Dialog> blockers = new IdentityArrayList<Dialog>();
1395         for (Dialog d : modalDialogs) {
1396             if (d.shouldBlock(this)) {
1397                 Window w = d;
1398                 while ((w != null) && (w != this)) {
1399                     w = (Window)(w.getOwner_NoClientCode());
1400                 }
1401                 if ((w == this) || !shouldBlock(d) || (modalityType.compareTo(d.getModalityType()) < 0)) {
1402                     blockers.add(d);
1403                 }
1404             }
1405         }
1406 
1407         // add all blockers' blockers to blockers :)
1408         for (int i = 0; i < blockers.size(); i++) {
1409             Dialog blocker = blockers.get(i);
1410             if (blocker.isModalBlocked()) {
1411                 Dialog blockerBlocker = blocker.getModalBlocker();
1412                 if (!blockers.contains(blockerBlocker)) {
1413                     blockers.add(i + 1, blockerBlocker);
1414                 }
1415             }
1416         }
1417 
1418         if (blockers.size() > 0) {
1419             blockers.get(0).blockWindow(this);


1594                 Dialog modalDialog = modalDialogs.get(i);
1595                 if (modalDialog.shouldBlock(w)) {
1596                     modalDialog.blockWindow(w);
1597                     break;
1598                 }
1599             }
1600         }
1601     }
1602 
1603     private void readObject(ObjectInputStream s)
1604         throws ClassNotFoundException, IOException, HeadlessException
1605     {
1606         GraphicsEnvironment.checkHeadless();
1607         s.defaultReadObject();
1608 
1609         // in 1.5 or earlier modalityType was absent, so use "modal" instead
1610         if (modalityType == null) {
1611             setModal(modal);
1612         }
1613 
1614         blockedWindows = new IdentityArrayList();
1615     }
1616 
1617     /*
1618      * --- Accessibility Support ---
1619      *
1620      */
1621 
1622     /**
1623      * Gets the AccessibleContext associated with this Dialog.
1624      * For dialogs, the AccessibleContext takes the form of an
1625      * AccessibleAWTDialog.
1626      * A new AccessibleAWTDialog instance is created if necessary.
1627      *
1628      * @return an AccessibleAWTDialog that serves as the
1629      *         AccessibleContext of this Dialog
1630      * @since 1.3
1631      */
1632     public AccessibleContext getAccessibleContext() {
1633         if (accessibleContext == null) {
1634             accessibleContext = new AccessibleAWTDialog();




 728      * Construct a name for this component.  Called by getName() when the
 729      * name is null.
 730      */
 731     String constructComponentName() {
 732         synchronized (Dialog.class) {
 733             return base + nameCounter++;
 734         }
 735     }
 736 
 737     /**
 738      * Makes this Dialog displayable by connecting it to
 739      * a native screen resource.  Making a dialog displayable will
 740      * cause any of its children to be made displayable.
 741      * This method is called internally by the toolkit and should
 742      * not be called directly by programs.
 743      * @see Component#isDisplayable
 744      * @see #removeNotify
 745      */
 746     public void addNotify() {
 747         synchronized (getTreeLock()) {
 748             if (parent != null && parent.peer == null) {
 749                 parent.addNotify();
 750             }
 751 
 752             if (peer == null) {
 753                 peer = getToolkit().createDialog(this);
 754             }
 755             super.addNotify();
 756         }
 757     }
 758 
 759     /**
 760      * Indicates whether the dialog is modal.
 761      * <p>
 762      * This method is obsolete and is kept for backwards compatiblity only.
 763      * Use {@link #getModalityType getModalityType()} instead.
 764      *
 765      * @return    <code>true</code> if this dialog window is modal;
 766      *            <code>false</code> otherwise
 767      *
 768      * @see       java.awt.Dialog#DEFAULT_MODALITY_TYPE


 925                     // keep the KeyEvents from being dispatched
 926                     // until the focus has been transfered
 927                     time.set(Toolkit.getEventQueue().getMostRecentKeyEventTime());
 928                     KeyboardFocusManager.getCurrentKeyboardFocusManager().
 929                         enqueueKeyEvents(time.get(), toFocus);
 930                 }
 931 
 932                 // This call is required as the show() method of the Dialog class
 933                 // does not invoke the super.show(). So wried... :(
 934                 mixOnShowing();
 935 
 936                 peer.setVisible(true); // now guaranteed never to block
 937                 if (isModalBlocked()) {
 938                     modalBlocker.toFront();
 939                 }
 940 
 941                 setLocationByPlatform(false);
 942                 for (int i = 0; i < ownedWindowList.size(); i++) {
 943                     Window child = ownedWindowList.elementAt(i).get();
 944                     if ((child != null) && child.showWithParent) {
 945                         child.setVisible(true);
 946                         child.showWithParent = false;
 947                     }       // endif
 948                 }   // endfor
 949                 Window.updateChildFocusableWindowState(this);
 950 
 951                 createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
 952                                       this, parent,
 953                                       HierarchyEvent.SHOWING_CHANGED,
 954                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
 955                 if (componentListener != null ||
 956                         (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
 957                         Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
 958                     ComponentEvent e =
 959                         new ComponentEvent(this, ComponentEvent.COMPONENT_SHOWN);
 960                     Toolkit.getEventQueue().postEvent(e);
 961                 }
 962             }
 963         }
 964 
 965         if (retval && (state & OPENED) == 0) {


1030             conditionalShow(null, null);
1031         } else {
1032             AppContext showAppContext = AppContext.getAppContext();
1033 
1034             AtomicLong time = new AtomicLong();
1035             Component predictedFocusOwner = null;
1036             try {
1037                 predictedFocusOwner = getMostRecentFocusOwner();
1038                 if (conditionalShow(predictedFocusOwner, time)) {
1039                     modalFilter = ModalEventFilter.createFilterForDialog(this);
1040                     final Conditional cond = new Conditional() {
1041                         @Override
1042                         public boolean evaluate() {
1043                             return windowClosingException == null;
1044                         }
1045                     };
1046 
1047                     // if this dialog is toolkit-modal, the filter should be added
1048                     // to all EDTs (for all AppContexts)
1049                     if (modalityType == ModalityType.TOOLKIT_MODAL) {
1050                         Iterator<AppContext> it = AppContext.getAppContexts().iterator();
1051                         while (it.hasNext()) {
1052                             AppContext appContext = it.next();
1053                             if (appContext == showAppContext) {
1054                                 continue;
1055                             }
1056                             EventQueue eventQueue = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
1057                             // it may occur that EDT for appContext hasn't been started yet, so
1058                             // we post an empty invocation event to trigger EDT initialization
1059                             Runnable createEDT = new Runnable() {
1060                                 public void run() {};
1061                             };
1062                             eventQueue.postEvent(new InvocationEvent(this, createEDT));
1063                             EventDispatchThread edt = eventQueue.getDispatchThread();
1064                             edt.addEventFilter(modalFilter);
1065                         }
1066                     }
1067 
1068                     modalityPushed();
1069                     try {
1070                         final EventQueue eventQueue = AccessController.doPrivileged(
1071                             new PrivilegedAction<EventQueue>() {
1072                                 public EventQueue run() {
1073                                     return Toolkit.getDefaultToolkit().getSystemEventQueue();
1074                                 }
1075                         });
1076                         secondaryLoop = eventQueue.createSecondaryLoop(cond, modalFilter, 0);
1077                         if (!secondaryLoop.enter()) {
1078                             secondaryLoop = null;
1079                         }
1080                     } finally {
1081                         modalityPopped();
1082                     }
1083 
1084                     // if this dialog is toolkit-modal, its filter must be removed
1085                     // from all EDTs (for all AppContexts)
1086                     if (modalityType == ModalityType.TOOLKIT_MODAL) {
1087                         Iterator<AppContext> it = AppContext.getAppContexts().iterator();
1088                         while (it.hasNext()) {
1089                             AppContext appContext = it.next();
1090                             if (appContext == showAppContext) {
1091                                 continue;
1092                             }
1093                             EventQueue eventQueue = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
1094                             EventDispatchThread edt = eventQueue.getDispatchThread();
1095                             edt.removeEventFilter(modalFilter);
1096                         }
1097                     }
1098 
1099                     if (windowClosingException != null) {
1100                         windowClosingException.fillInStackTrace();
1101                         throw windowClosingException;
1102                     }
1103                 }
1104             } finally {
1105                 if (predictedFocusOwner != null) {
1106                     // Restore normal key event dispatching
1107                     KeyboardFocusManager.getCurrentKeyboardFocusManager().
1108                         dequeueKeyEvents(time.get(), predictedFocusOwner);
1109                 }


1379      * --- Modality support ---
1380      *
1381      */
1382 
1383     /*
1384      * This method is called only for modal dialogs.
1385      *
1386      * Goes through the list of all visible top-level windows and
1387      * divide them into three distinct groups: blockers of this dialog,
1388      * blocked by this dialog and all others. Then blocks this dialog
1389      * by first met dialog from the first group (if any) and blocks all
1390      * the windows from the second group.
1391      */
1392     void modalShow() {
1393         // find all the dialogs that block this one
1394         IdentityArrayList<Dialog> blockers = new IdentityArrayList<Dialog>();
1395         for (Dialog d : modalDialogs) {
1396             if (d.shouldBlock(this)) {
1397                 Window w = d;
1398                 while ((w != null) && (w != this)) {
1399                     w = w.getOwner_NoClientCode();
1400                 }
1401                 if ((w == this) || !shouldBlock(d) || (modalityType.compareTo(d.getModalityType()) < 0)) {
1402                     blockers.add(d);
1403                 }
1404             }
1405         }
1406 
1407         // add all blockers' blockers to blockers :)
1408         for (int i = 0; i < blockers.size(); i++) {
1409             Dialog blocker = blockers.get(i);
1410             if (blocker.isModalBlocked()) {
1411                 Dialog blockerBlocker = blocker.getModalBlocker();
1412                 if (!blockers.contains(blockerBlocker)) {
1413                     blockers.add(i + 1, blockerBlocker);
1414                 }
1415             }
1416         }
1417 
1418         if (blockers.size() > 0) {
1419             blockers.get(0).blockWindow(this);


1594                 Dialog modalDialog = modalDialogs.get(i);
1595                 if (modalDialog.shouldBlock(w)) {
1596                     modalDialog.blockWindow(w);
1597                     break;
1598                 }
1599             }
1600         }
1601     }
1602 
1603     private void readObject(ObjectInputStream s)
1604         throws ClassNotFoundException, IOException, HeadlessException
1605     {
1606         GraphicsEnvironment.checkHeadless();
1607         s.defaultReadObject();
1608 
1609         // in 1.5 or earlier modalityType was absent, so use "modal" instead
1610         if (modalityType == null) {
1611             setModal(modal);
1612         }
1613 
1614         blockedWindows = new IdentityArrayList<>();
1615     }
1616 
1617     /*
1618      * --- Accessibility Support ---
1619      *
1620      */
1621 
1622     /**
1623      * Gets the AccessibleContext associated with this Dialog.
1624      * For dialogs, the AccessibleContext takes the form of an
1625      * AccessibleAWTDialog.
1626      * A new AccessibleAWTDialog instance is created if necessary.
1627      *
1628      * @return an AccessibleAWTDialog that serves as the
1629      *         AccessibleContext of this Dialog
1630      * @since 1.3
1631      */
1632     public AccessibleContext getAccessibleContext() {
1633         if (accessibleContext == null) {
1634             accessibleContext = new AccessibleAWTDialog();