KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > SwingUtilities3


1 /*
2  * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation. Sun designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Sun in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22  * CA 95054 USA or visit www.sun.com if you need additional information or
23  * have any questions.
24  */

25
26 package com.sun.java.swing;
27
28 import sun.awt.EventQueueDelegate;
29 import sun.awt.AppContext;
30 import java.util.Map JavaDoc;
31 import java.util.concurrent.Callable JavaDoc;
32 import java.awt.AWTEvent JavaDoc;
33 import java.awt.EventQueue JavaDoc;
34 import java.awt.Component JavaDoc;
35 import javax.swing.JComponent JavaDoc;
36 import javax.swing.RepaintManager JavaDoc;
37
38 /**
39  * A collection of utility methods for Swing.
40  * <p>
41  * <b>WARNING:</b> While this class is public, it should not be treated as
42  * public API and its API may change in incompatable ways between dot dot
43  * releases and even patch releases. You should not rely on this class even
44  * existing.
45  *
46  * This is a second part of sun.swing.SwingUtilities2. It is required
47  * to provide services for JavaFX applets.
48  *
49  * @version 1.2 12/17/07
50  */

51 public class SwingUtilities3 {
52     /**
53      * The {@code clientProperty} key for delegate {@code RepaintManager}
54      */

55     private static final Object JavaDoc DELEGATE_REPAINT_MANAGER_KEY =
56         new StringBuilder JavaDoc("DelegateRepaintManagerKey");
57
58     /**
59       * Registers delegate RepaintManager for {@code JComponent}.
60       */

61     public static void setDelegateRepaintManager(JComponent JavaDoc component,
62                                                 RepaintManager JavaDoc repaintManager) {
63         /* setting up flag in AppContext to speed up lookups in case
64          * there are no delegate RepaintManagers used.
65          */

66         AppContext.getAppContext().put(DELEGATE_REPAINT_MANAGER_KEY,
67                                        Boolean.TRUE);
68         
69         component.putClientProperty(DELEGATE_REPAINT_MANAGER_KEY,
70                                     repaintManager);
71     }
72
73     /**
74      * Returns delegate {@code RepaintManager} for {@code component} hierarchy.
75      */

76     public static RepaintManager JavaDoc getDelegateRepaintManager(Component JavaDoc
77                                                             component) {
78         RepaintManager JavaDoc delegate = null;
79         if (Boolean.TRUE == AppContext.getAppContext().get(
80                                                DELEGATE_REPAINT_MANAGER_KEY)) {
81             while (delegate == null && component != null) {
82                 while (component != null
83                          && ! (component instanceof JComponent JavaDoc)) {
84                     component = component.getParent();
85                 }
86                 if (component != null) {
87                     delegate = (RepaintManager JavaDoc)
88                         ((JComponent JavaDoc) component)
89                           .getClientProperty(DELEGATE_REPAINT_MANAGER_KEY);
90                     component = component.getParent();
91                 }
92
93             }
94         }
95         return delegate;
96     }
97
98     /*
99      * We use maps to avoid reflection. Hopefully it should perform better
100      * this way.
101      */

102     public static void setEventQueueDelegate(
103             Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Object JavaDoc>> map) {
104         EventQueueDelegate.setDelegate(new EventQueueDelegateFromMap(map));
105     }
106   
107     private static class EventQueueDelegateFromMap
108     implements EventQueueDelegate.Delegate {
109         private final AWTEvent JavaDoc[] afterDispatchEventArgument;
110         private final Object JavaDoc[] afterDispatchHandleArgument;
111         private final Callable JavaDoc<Void JavaDoc> afterDispatchCallable;
112         
113         private final AWTEvent JavaDoc[] beforeDispatchEventArgument;
114         private final Callable JavaDoc<Object JavaDoc> beforeDispatchCallable;
115         
116         private final EventQueue JavaDoc[] getNextEventEventQueueArgument;
117         private final Callable JavaDoc<AWTEvent JavaDoc> getNextEventCallable;
118         
119         @SuppressWarnings JavaDoc("unchecked")
120         public EventQueueDelegateFromMap(Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Object JavaDoc>> objectMap) {
121             Map JavaDoc<String JavaDoc, Object JavaDoc> methodMap = objectMap.get("afterDispatch");
122             afterDispatchEventArgument = (AWTEvent JavaDoc[]) methodMap.get("event");
123             afterDispatchHandleArgument = (Object JavaDoc[]) methodMap.get("handle");
124             afterDispatchCallable = (Callable JavaDoc<Void JavaDoc>) methodMap.get("method");
125             
126             methodMap = objectMap.get("beforeDispatch");
127             beforeDispatchEventArgument = (AWTEvent JavaDoc[]) methodMap.get("event");
128             beforeDispatchCallable = (Callable JavaDoc<Object JavaDoc>) methodMap.get("method");
129             
130             methodMap = objectMap.get("getNextEvent");
131             getNextEventEventQueueArgument =
132                 (EventQueue JavaDoc[]) methodMap.get("eventQueue");
133             getNextEventCallable = (Callable JavaDoc<AWTEvent JavaDoc>) methodMap.get("method");
134         }
135         
136         @Override JavaDoc
137         public void afterDispatch(AWTEvent JavaDoc event, Object JavaDoc handle) {
138             afterDispatchEventArgument[0] = event;
139             afterDispatchHandleArgument[0] = handle;
140             try {
141                 afterDispatchCallable.call();
142             } catch (Exception JavaDoc e) {
143                 if (e instanceof RuntimeException JavaDoc) {
144                     throw (RuntimeException JavaDoc) e;
145                 }
146             }
147         }
148
149         @Override JavaDoc
150         public Object JavaDoc beforeDispatch(AWTEvent JavaDoc event) {
151             beforeDispatchEventArgument[0] = event;
152             try {
153                 return beforeDispatchCallable.call();
154             } catch (Exception JavaDoc e) {
155                 if (e instanceof RuntimeException JavaDoc) {
156                     throw (RuntimeException JavaDoc) e;
157                 }
158             }
159             return null;
160         }
161
162         @Override JavaDoc
163         public AWTEvent JavaDoc getNextEvent(EventQueue JavaDoc eventQueue) {
164             getNextEventEventQueueArgument[0] = eventQueue;
165             try {
166                 return getNextEventCallable.call();
167             } catch (Exception JavaDoc e) {
168                 if (e instanceof RuntimeException JavaDoc) {
169                     throw (RuntimeException JavaDoc) e;
170                 }
171             }
172             return null;
173         }
174     }
175 }
176
Popular Tags