1 7 package java.awt; 8 9 import java.awt.event.*; 10 11 import sun.awt.AppContext; 12 13 abstract class ModalEventFilter implements EventFilter { 14 15 protected Dialog modalDialog; 16 protected boolean disabled; 17 18 protected ModalEventFilter(Dialog modalDialog) { 19 this.modalDialog = modalDialog; 20 disabled = false; 21 } 22 23 Dialog getModalDialog() { 24 return modalDialog; 25 } 26 27 public FilterAction acceptEvent(AWTEvent event) { 28 if (disabled || !modalDialog.isVisible()) { 29 return FilterAction.ACCEPT; 30 } 31 int eventID = event.getID(); 32 if ((eventID >= MouseEvent.MOUSE_FIRST && 33 eventID <= MouseEvent.MOUSE_LAST) || 34 (eventID >= ActionEvent.ACTION_FIRST && 35 eventID <= ActionEvent.ACTION_LAST) || 36 eventID == WindowEvent.WINDOW_CLOSING) 37 { 38 Object o = event.getSource(); 39 if (o instanceof sun.awt.ModalExclude) { 40 } else if (o instanceof Component ) { 43 Component c = (Component )o; 44 while ((c != null) && !(c instanceof Window )) { 45 c = c.getParent_NoClientCode(); 46 } 47 if (c != null) { 48 return acceptWindow((Window )c); 49 } 50 } 51 } 52 return FilterAction.ACCEPT; 53 } 54 55 protected abstract FilterAction acceptWindow(Window w); 56 57 void disable() { 63 disabled = true; 64 } 65 66 int compareTo(ModalEventFilter another) { 67 Dialog anotherDialog = another.getModalDialog(); 68 Component c = modalDialog; 71 while (c != null) { 72 if (c == anotherDialog) { 73 return 1; 74 } 75 c = c.getParent_NoClientCode(); 76 } 77 c = anotherDialog; 78 while (c != null) { 79 if (c == modalDialog) { 80 return -1; 81 } 82 c = c.getParent_NoClientCode(); 83 } 84 Dialog blocker = modalDialog.getModalBlocker(); 86 while (blocker != null) { 87 if (blocker == anotherDialog) { 88 return -1; 89 } 90 blocker = blocker.getModalBlocker(); 91 } 92 blocker = anotherDialog.getModalBlocker(); 93 while (blocker != null) { 94 if (blocker == modalDialog) { 95 return 1; 96 } 97 blocker = blocker.getModalBlocker(); 98 } 99 return modalDialog.getModalityType().compareTo(anotherDialog.getModalityType()); 101 } 102 103 static ModalEventFilter createFilterForDialog(Dialog modalDialog) { 104 switch (modalDialog.getModalityType()) { 105 case DOCUMENT_MODAL: return new DocumentModalEventFilter(modalDialog); 106 case APPLICATION_MODAL: return new ApplicationModalEventFilter(modalDialog); 107 case TOOLKIT_MODAL: return new ToolkitModalEventFilter(modalDialog); 108 } 109 return null; 110 } 111 112 private static class ToolkitModalEventFilter extends ModalEventFilter { 113 114 private AppContext appContext; 115 116 ToolkitModalEventFilter(Dialog modalDialog) { 117 super(modalDialog); 118 appContext = modalDialog.appContext; 119 } 120 121 protected FilterAction acceptWindow(Window w) { 122 if (w.isModalExcluded(Dialog.ModalExclusionType.TOOLKIT_EXCLUDE)) { 123 return FilterAction.ACCEPT; 124 } 125 if (w.appContext != appContext) { 126 return FilterAction.REJECT; 127 } 128 while (w != null) { 129 if (w == modalDialog) { 130 return FilterAction.ACCEPT_IMMEDIATELY; 131 } 132 w = w.getOwner(); 133 } 134 return FilterAction.REJECT; 135 } 136 } 137 138 private static class ApplicationModalEventFilter extends ModalEventFilter { 139 140 private AppContext appContext; 141 142 ApplicationModalEventFilter(Dialog modalDialog) { 143 super(modalDialog); 144 appContext = modalDialog.appContext; 145 } 146 147 protected FilterAction acceptWindow(Window w) { 148 if (w.isModalExcluded(Dialog.ModalExclusionType.APPLICATION_EXCLUDE)) { 149 return FilterAction.ACCEPT; 150 } 151 if (w.appContext == appContext) { 152 while (w != null) { 153 if (w == modalDialog) { 154 return FilterAction.ACCEPT_IMMEDIATELY; 155 } 156 w = w.getOwner(); 157 } 158 return FilterAction.REJECT; 159 } 160 return FilterAction.ACCEPT; 161 } 162 } 163 164 private static class DocumentModalEventFilter extends ModalEventFilter { 165 166 private Window documentRoot; 167 168 DocumentModalEventFilter(Dialog modalDialog) { 169 super(modalDialog); 170 documentRoot = modalDialog.getDocumentRoot(); 171 } 172 173 protected FilterAction acceptWindow(Window w) { 174 if (w.isModalExcluded(Dialog.ModalExclusionType.APPLICATION_EXCLUDE)) { 177 Window w1 = modalDialog.getOwner(); 178 while (w1 != null) { 179 if (w1 == w) { 180 return FilterAction.REJECT; 181 } 182 w1 = w1.getOwner(); 183 } 184 return FilterAction.ACCEPT; 185 } 186 while (w != null) { 187 if (w == modalDialog) { 188 return FilterAction.ACCEPT_IMMEDIATELY; 189 } 190 if (w == documentRoot) { 191 return FilterAction.REJECT; 192 } 193 w = w.getOwner(); 194 } 195 return FilterAction.ACCEPT; 196 } 197 } 198 } 199 | Popular Tags |