KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > ModalEventFilter


1 /*
2  * @(#)ModalEventFilter.java 1.3 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.awt.event.*;
10
11 import sun.awt.AppContext;
12
13 abstract class ModalEventFilter implements EventFilter JavaDoc {
14
15     protected Dialog JavaDoc modalDialog;
16     protected boolean disabled;
17
18     protected ModalEventFilter(Dialog JavaDoc modalDialog) {
19         this.modalDialog = modalDialog;
20         disabled = false;
21     }
22
23     Dialog JavaDoc getModalDialog() {
24         return modalDialog;
25     }
26
27     public FilterAction acceptEvent(AWTEvent JavaDoc 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 JavaDoc o = event.getSource();
39             if (o instanceof sun.awt.ModalExclude) {
40                 // Exclude this object from modality and
41
// continue to pump it's events.
42
} else if (o instanceof Component JavaDoc) {
43                 Component JavaDoc c = (Component JavaDoc)o;
44                 while ((c != null) && !(c instanceof Window JavaDoc)) {
45                     c = c.getParent_NoClientCode();
46                 }
47                 if (c != null) {
48                     return acceptWindow((Window JavaDoc)c);
49                 }
50             }
51         }
52         return FilterAction.ACCEPT;
53     }
54
55     protected abstract FilterAction acceptWindow(Window JavaDoc w);
56
57     // When a modal dialog is hidden its modal filter may not be deleted from
58
// EventDispatchThread event filters immediately, so we need to mark the filter
59
// as disabled to prevent it from working. Simple checking for visibility of
60
// the modalDialog is not enough, as it can be hidden and then shown again
61
// with a new event pump and a new filter
62
void disable() {
63         disabled = true;
64     }
65
66     int compareTo(ModalEventFilter JavaDoc another) {
67         Dialog JavaDoc anotherDialog = another.getModalDialog();
68         // check if modalDialog is from anotherDialog's hierarchy
69
// or vice versa
70
Component JavaDoc 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         // check if one dialog blocks (directly or indirectly) another
85
Dialog JavaDoc 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         // compare modality types
100
return modalDialog.getModalityType().compareTo(anotherDialog.getModalityType());
101     }
102
103     static ModalEventFilter JavaDoc createFilterForDialog(Dialog JavaDoc 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 JavaDoc {
113
114         private AppContext appContext;
115
116         ToolkitModalEventFilter(Dialog JavaDoc modalDialog) {
117             super(modalDialog);
118             appContext = modalDialog.appContext;
119         }
120
121         protected FilterAction acceptWindow(Window JavaDoc 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 JavaDoc {
139
140         private AppContext appContext;
141
142         ApplicationModalEventFilter(Dialog JavaDoc modalDialog) {
143             super(modalDialog);
144             appContext = modalDialog.appContext;
145         }
146
147         protected FilterAction acceptWindow(Window JavaDoc 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 JavaDoc {
165
166         private Window JavaDoc documentRoot;
167
168         DocumentModalEventFilter(Dialog JavaDoc modalDialog) {
169             super(modalDialog);
170             documentRoot = modalDialog.getDocumentRoot();
171         }
172
173         protected FilterAction acceptWindow(Window JavaDoc w) {
174             // application- and toolkit-excluded windows are blocked by
175
// document-modal dialogs from their child hierarchy
176
if (w.isModalExcluded(Dialog.ModalExclusionType.APPLICATION_EXCLUDE)) {
177                 Window JavaDoc 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