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

Print this page




  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.awt.peer.DialogPeer;
  28 import java.awt.event.*;
  29 import java.io.ObjectInputStream;
  30 import java.io.IOException;
  31 import java.util.Iterator;
  32 import java.util.concurrent.atomic.AtomicLong;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import javax.accessibility.*;
  36 import sun.awt.AppContext;
  37 import sun.awt.AWTPermissions;
  38 import sun.awt.SunToolkit;
  39 import sun.awt.PeerEvent;
  40 import sun.awt.util.IdentityArrayList;
  41 import sun.awt.util.IdentityLinkedList;
  42 import java.security.AccessControlException;

  43 
  44 /**
  45  * A Dialog is a top-level window with a title and a border
  46  * that is typically used to take some form of input from the user.
  47  *
  48  * The size of the dialog includes any area designated for the
  49  * border.  The dimensions of the border area can be obtained
  50  * using the <code>getInsets</code> method, however, since
  51  * these dimensions are platform-dependent, a valid insets
  52  * value cannot be obtained until the dialog is made displayable
  53  * by either calling <code>pack</code> or <code>show</code>.
  54  * Since the border area is included in the overall size of the
  55  * dialog, the border effectively obscures a portion of the dialog,
  56  * constraining the area available for rendering and/or displaying
  57  * subcomponents to the rectangle which has an upper-left corner
  58  * location of <code>(insets.left, insets.top)</code>, and has a size of
  59  * <code>width - (insets.left + insets.right)</code> by
  60  * <code>height - (insets.top + insets.bottom)</code>.
  61  * <p>
  62  * The default layout for a dialog is <code>BorderLayout</code>.


1027      */
1028     @Deprecated
1029     public void show() {
1030         if (!initialized) {
1031             throw new IllegalStateException("The dialog component " +
1032                 "has not been initialized properly");
1033         }
1034 
1035         beforeFirstShow = false;
1036         if (!isModal()) {
1037             conditionalShow(null, null);
1038         } else {
1039             AppContext showAppContext = AppContext.getAppContext();
1040 
1041             AtomicLong time = new AtomicLong();
1042             Component predictedFocusOwner = null;
1043             try {
1044                 predictedFocusOwner = getMostRecentFocusOwner();
1045                 if (conditionalShow(predictedFocusOwner, time)) {
1046                     modalFilter = ModalEventFilter.createFilterForDialog(this);
1047                     final Conditional cond = new Conditional() {
1048                         @Override
1049                         public boolean evaluate() {
1050                             return windowClosingException == null;
1051                         }
1052                     };
1053 
1054                     // if this dialog is toolkit-modal, the filter should be added
1055                     // to all EDTs (for all AppContexts)
1056                     if (modalityType == ModalityType.TOOLKIT_MODAL) {
1057                         Iterator<AppContext> it = AppContext.getAppContexts().iterator();
1058                         while (it.hasNext()) {
1059                             AppContext appContext = it.next();
1060                             if (appContext == showAppContext) {
1061                                 continue;
1062                             }
1063                             EventQueue eventQueue = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
1064                             // it may occur that EDT for appContext hasn't been started yet, so
1065                             // we post an empty invocation event to trigger EDT initialization
1066                             Runnable createEDT = new Runnable() {
1067                                 public void run() {};
1068                             };
1069                             eventQueue.postEvent(new InvocationEvent(this, createEDT));
1070                             EventDispatchThread edt = eventQueue.getDispatchThread();
1071                             edt.addEventFilter(modalFilter);
1072                         }
1073                     }
1074 
1075                     modalityPushed();
1076                     try {
1077                         final EventQueue eventQueue = AccessController.doPrivileged(
1078                             new PrivilegedAction<EventQueue>() {
1079                                 public EventQueue run() {
1080                                     return Toolkit.getDefaultToolkit().getSystemEventQueue();
1081                                 }
1082                         });
1083                         secondaryLoop = eventQueue.createSecondaryLoop(cond, modalFilter, 0);
1084                         if (!secondaryLoop.enter()) {
1085                             secondaryLoop = null;
1086                         }
1087                     } finally {
1088                         modalityPopped();
1089                     }
1090 
1091                     // if this dialog is toolkit-modal, its filter must be removed
1092                     // from all EDTs (for all AppContexts)
1093                     if (modalityType == ModalityType.TOOLKIT_MODAL) {
1094                         Iterator<AppContext> it = AppContext.getAppContexts().iterator();
1095                         while (it.hasNext()) {
1096                             AppContext appContext = it.next();
1097                             if (appContext == showAppContext) {
1098                                 continue;
1099                             }
1100                             EventQueue eventQueue = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
1101                             EventDispatchThread edt = eventQueue.getDispatchThread();
1102                             edt.removeEventFilter(modalFilter);
1103                         }
1104                     }
1105 
1106                     if (windowClosingException != null) {
1107                         windowClosingException.fillInStackTrace();
1108                         throw windowClosingException;
1109                     }
1110                 }
1111             } finally {
1112                 if (predictedFocusOwner != null) {
1113                     // Restore normal key event dispatching
1114                     KeyboardFocusManager.getCurrentKeyboardFocusManager().
1115                         dequeueKeyEvents(time.get(), predictedFocusOwner);
1116                 }
1117             }
1118         }
1119     }
1120 
1121     final void modalityPushed() {
1122         Toolkit tk = Toolkit.getDefaultToolkit();
1123         if (tk instanceof SunToolkit) {
1124             SunToolkit stk = (SunToolkit)tk;
1125             stk.notifyModalityPushed(this);
1126         }
1127     }
1128 
1129     final void modalityPopped() {
1130         Toolkit tk = Toolkit.getDefaultToolkit();
1131         if (tk instanceof SunToolkit) {
1132             SunToolkit stk = (SunToolkit)tk;
1133             stk.notifyModalityPopped(this);
1134         }
1135     }
1136 
1137     void interruptBlocking() {
1138         if (isModal()) {
1139             disposeImpl();
1140         } else if (windowClosingException != null) {
1141             windowClosingException.fillInStackTrace();
1142             windowClosingException.printStackTrace();
1143             windowClosingException = null;
1144         }
1145     }
1146 
1147     private void hideAndDisposePreHandler() {
1148         isInHide = true;
1149         synchronized (getTreeLock()) {
1150             if (secondaryLoop != null) {
1151                 modalHide();
1152                 // dialog can be shown and then disposed before its
1153                 // modal filter is created
1154                 if (modalFilter != null) {
1155                     modalFilter.disable();
1156                 }
1157                 modalDialogs.remove(this);
1158             }
1159         }
1160     }
1161     private void hideAndDisposeHandler() {
1162         if (secondaryLoop != null) {
1163             secondaryLoop.exit();
1164             secondaryLoop = null;
1165         }
1166         isInHide = false;




  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.awt.peer.DialogPeer;
  28 import java.awt.event.*;
  29 import java.io.ObjectInputStream;
  30 import java.io.IOException;
  31 import java.util.Iterator;
  32 import java.util.concurrent.atomic.AtomicLong;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import javax.accessibility.*;
  36 import sun.awt.AppContext;
  37 import sun.awt.AWTPermissions;
  38 import sun.awt.SunToolkit;
  39 import sun.awt.PeerEvent;
  40 import sun.awt.util.IdentityArrayList;
  41 import sun.awt.util.IdentityLinkedList;
  42 import java.security.AccessControlException;
  43 import java.util.function.BooleanSupplier;
  44 
  45 /**
  46  * A Dialog is a top-level window with a title and a border
  47  * that is typically used to take some form of input from the user.
  48  *
  49  * The size of the dialog includes any area designated for the
  50  * border.  The dimensions of the border area can be obtained
  51  * using the <code>getInsets</code> method, however, since
  52  * these dimensions are platform-dependent, a valid insets
  53  * value cannot be obtained until the dialog is made displayable
  54  * by either calling <code>pack</code> or <code>show</code>.
  55  * Since the border area is included in the overall size of the
  56  * dialog, the border effectively obscures a portion of the dialog,
  57  * constraining the area available for rendering and/or displaying
  58  * subcomponents to the rectangle which has an upper-left corner
  59  * location of <code>(insets.left, insets.top)</code>, and has a size of
  60  * <code>width - (insets.left + insets.right)</code> by
  61  * <code>height - (insets.top + insets.bottom)</code>.
  62  * <p>
  63  * The default layout for a dialog is <code>BorderLayout</code>.


1028      */
1029     @Deprecated
1030     public void show() {
1031         if (!initialized) {
1032             throw new IllegalStateException("The dialog component " +
1033                 "has not been initialized properly");
1034         }
1035 
1036         beforeFirstShow = false;
1037         if (!isModal()) {
1038             conditionalShow(null, null);
1039         } else {
1040             AppContext showAppContext = AppContext.getAppContext();
1041 
1042             AtomicLong time = new AtomicLong();
1043             Component predictedFocusOwner = null;
1044             try {
1045                 predictedFocusOwner = getMostRecentFocusOwner();
1046                 if (conditionalShow(predictedFocusOwner, time)) {
1047                     modalFilter = ModalEventFilter.createFilterForDialog(this);







1048                     // if this dialog is toolkit-modal, the filter should be added
1049                     // to all EDTs (for all AppContexts)
1050                     if (modalityType == ModalityType.TOOLKIT_MODAL) {
1051                         Iterator<AppContext> it = AppContext.getAppContexts().iterator();
1052                         while (it.hasNext()) {
1053                             AppContext appContext = it.next();
1054                             if (appContext == showAppContext) {
1055                                 continue;
1056                             }
1057                             EventQueue eventQueue = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
1058                             // it may occur that EDT for appContext hasn't been started yet, so
1059                             // we post an empty invocation event to trigger EDT initialization
1060                             eventQueue.postEvent(new InvocationEvent(this, () -> {}));



1061                             EventDispatchThread edt = eventQueue.getDispatchThread();
1062                             edt.addEventFilter(modalFilter);
1063                         }
1064                     }
1065 
1066                     modalityPushed();
1067                     try {
1068                         final EventQueue eventQueue = AccessController.doPrivileged(
1069                                 (PrivilegedAction<EventQueue>) Toolkit.getDefaultToolkit()::getSystemEventQueue);
1070                         secondaryLoop = eventQueue.createSecondaryLoop(() -> true, modalFilter, 0);




1071                         if (!secondaryLoop.enter()) {
1072                             secondaryLoop = null;
1073                         }
1074                     } finally {
1075                         modalityPopped();
1076                     }
1077 
1078                     // if this dialog is toolkit-modal, its filter must be removed
1079                     // from all EDTs (for all AppContexts)
1080                     if (modalityType == ModalityType.TOOLKIT_MODAL) {
1081                         Iterator<AppContext> it = AppContext.getAppContexts().iterator();
1082                         while (it.hasNext()) {
1083                             AppContext appContext = it.next();
1084                             if (appContext == showAppContext) {
1085                                 continue;
1086                             }
1087                             EventQueue eventQueue = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
1088                             EventDispatchThread edt = eventQueue.getDispatchThread();
1089                             edt.removeEventFilter(modalFilter);
1090                         }
1091                     }





1092                 }
1093             } finally {
1094                 if (predictedFocusOwner != null) {
1095                     // Restore normal key event dispatching
1096                     KeyboardFocusManager.getCurrentKeyboardFocusManager().
1097                         dequeueKeyEvents(time.get(), predictedFocusOwner);
1098                 }
1099             }
1100         }
1101     }
1102 
1103     final void modalityPushed() {
1104         Toolkit tk = Toolkit.getDefaultToolkit();
1105         if (tk instanceof SunToolkit) {
1106             SunToolkit stk = (SunToolkit)tk;
1107             stk.notifyModalityPushed(this);
1108         }
1109     }
1110 
1111     final void modalityPopped() {
1112         Toolkit tk = Toolkit.getDefaultToolkit();
1113         if (tk instanceof SunToolkit) {
1114             SunToolkit stk = (SunToolkit)tk;
1115             stk.notifyModalityPopped(this);
1116         }
1117     }
1118 










1119     private void hideAndDisposePreHandler() {
1120         isInHide = true;
1121         synchronized (getTreeLock()) {
1122             if (secondaryLoop != null) {
1123                 modalHide();
1124                 // dialog can be shown and then disposed before its
1125                 // modal filter is created
1126                 if (modalFilter != null) {
1127                     modalFilter.disable();
1128                 }
1129                 modalDialogs.remove(this);
1130             }
1131         }
1132     }
1133     private void hideAndDisposeHandler() {
1134         if (secondaryLoop != null) {
1135             secondaryLoop.exit();
1136             secondaryLoop = null;
1137         }
1138         isInHide = false;