KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > fakepeer > FakePeerSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.form.fakepeer;
22
23 import java.awt.*;
24 import java.awt.peer.ButtonPeer;
25 import java.awt.peer.CanvasPeer;
26 import java.awt.peer.CheckboxPeer;
27 import java.awt.peer.ChoicePeer;
28 import java.awt.peer.ComponentPeer;
29 import java.awt.peer.ContainerPeer;
30 import java.awt.peer.LabelPeer;
31 import java.awt.peer.ListPeer;
32 import java.awt.peer.PanelPeer;
33 import java.awt.peer.ScrollPanePeer;
34 import java.awt.peer.ScrollbarPeer;
35 import java.awt.peer.TextAreaPeer;
36 import java.awt.peer.TextComponentPeer;
37 import java.awt.peer.TextFieldPeer;
38 import java.lang.reflect.*;
39
40
41 /**
42  *
43  * @author Tran Duc Trung
44  */

45
46 public class FakePeerSupport
47 {
48     private FakePeerSupport() {
49     }
50     
51     public static boolean attachFakePeer(Component comp) {
52         if (comp == null || comp.isDisplayable()
53               || comp instanceof javax.swing.JComponent JavaDoc
54               || comp instanceof javax.swing.RootPaneContainer JavaDoc)
55             return false;
56
57         FakePeer peer = null;
58
59         if (comp instanceof Label)
60             peer = getFakePeer(LabelPeer.class, new FakeLabelPeer((Label) comp));
61         else if (comp instanceof Button)
62             peer = getFakePeer(ButtonPeer.class, new FakeButtonPeer((Button) comp));
63         else if (comp instanceof Panel)
64             peer = getFakePeer(new Class JavaDoc[] {ContainerPeer.class, PanelPeer.class}, new FakePanelPeer((Panel) comp));
65         else if (comp instanceof TextField)
66             peer = getFakePeer(new Class JavaDoc[] {TextFieldPeer.class, TextComponentPeer.class}, new FakeTextFieldPeer((TextField) comp));
67         else if (comp instanceof TextArea)
68             peer = getFakePeer(new Class JavaDoc[] {TextAreaPeer.class, TextComponentPeer.class}, new FakeTextAreaPeer((TextArea) comp));
69         else if (comp instanceof TextComponent)
70             peer = getFakePeer(TextComponentPeer.class, new FakeTextComponentPeer((TextComponent) comp));
71         else if (comp instanceof Checkbox)
72             peer = getFakePeer(CheckboxPeer.class, new FakeCheckboxPeer((Checkbox) comp));
73         else if (comp instanceof Choice)
74             peer = getFakePeer(ChoicePeer.class, new FakeChoicePeer((Choice) comp));
75         else if (comp instanceof List)
76             peer = getFakePeer(ListPeer.class, new FakeListPeer((List) comp));
77         else if (comp instanceof Scrollbar)
78             peer = getFakePeer(ScrollbarPeer.class, new FakeScrollbarPeer((Scrollbar) comp));
79         else if (comp instanceof ScrollPane)
80             peer = getFakePeer(new Class JavaDoc[] {ContainerPeer.class, ScrollPanePeer.class}, new FakeScrollPanePeer((ScrollPane) comp));
81         else if (comp instanceof Canvas)
82             peer = getFakePeer(CanvasPeer.class, new FakeCanvasPeer((Canvas) comp));
83         else
84             return false;
85
86         attachFakePeer(comp, peer);
87         return true;
88     }
89     
90     private static FakePeer getFakePeer(Class JavaDoc fakePeerInterfaces, FakeComponentPeer compPeer) {
91         return getFakePeer(new Class JavaDoc[] {fakePeerInterfaces}, compPeer);
92     }
93     
94     private static FakePeer getFakePeer(Class JavaDoc[] fakePeerInterfaces, FakeComponentPeer compPeer) {
95         
96         // FakePeer.class and java.awt.peer.LightweightPeer.class interfaces
97
// should be implemented for each FakeComponentPeer
98
Class JavaDoc[] interfaces = new Class JavaDoc[fakePeerInterfaces.length + 2];
99         System.arraycopy(fakePeerInterfaces, 0, interfaces, 0, fakePeerInterfaces.length);
100         interfaces[fakePeerInterfaces.length] = FakePeer.class;
101         interfaces[fakePeerInterfaces.length+1] = java.awt.peer.LightweightPeer.class;
102         
103         Class JavaDoc proxyClass = Proxy.getProxyClass(compPeer.getClass().getClassLoader(), interfaces);
104         FakePeerInvocationHandler handler = new FakePeerInvocationHandler(compPeer);
105         try {
106            return (FakePeer) proxyClass.getConstructor(new Class JavaDoc[] { InvocationHandler.class }).newInstance(new Object JavaDoc[] { handler });
107         } catch (Exception JavaDoc e) {
108             org.openide.ErrorManager.getDefault().notify(e);
109         }
110         return null;
111     }
112     
113     public static void attachFakePeer(Component comp, ComponentPeer peer) {
114         try {
115             Field f = Component.class.getDeclaredField("peer"); // NOI18N
116
f.setAccessible(true);
117             f.set(comp, peer);
118         }
119         catch (Exception JavaDoc ex) {
120             org.openide.ErrorManager.getDefault().notify(
121                 org.openide.ErrorManager.INFORMATIONAL, ex);
122         }
123     }
124
125     public static void attachFakePeerRecursively(Container container) {
126         Component components[] = getComponents(container);
127         for (int i=0; i < components.length; i++) {
128             Component comp = components[i];
129             attachFakePeer(comp);
130             if (comp instanceof Container)
131                 attachFakePeerRecursively((Container) comp);
132         }
133     }
134
135     static Component[] getComponents(Container container) {
136         // hack for the case some "smart" containers delegate getComponents()
137
// to some subcontainer (which becomes inaccessible then)
138
try {
139             Field f = Container.class.getDeclaredField("component"); // NOI18N
140
f.setAccessible(true);
141             return (Component[]) f.get(container);
142         }
143         catch (Exception JavaDoc ex) {
144             org.openide.ErrorManager.getDefault().notify(
145                 org.openide.ErrorManager.INFORMATIONAL, ex);
146         }
147         return container.getComponents();
148     }
149
150     public static ComponentPeer detachFakePeer(Component comp) {
151         try {
152             Field f = Component.class.getDeclaredField("peer"); // NOI18N
153
f.setAccessible(true);
154             Object JavaDoc peer = (ComponentPeer) f.get(comp);
155             if (peer instanceof FakePeer) {
156                 f.set(comp, null);
157                 return (FakePeer) peer;
158             }
159         }
160         catch (Exception JavaDoc ex) {
161             org.openide.ErrorManager.getDefault().notify(
162                 org.openide.ErrorManager.INFORMATIONAL, ex);
163         }
164         return null;
165     }
166     
167     public static Font getDefaultAWTFont() {
168         if (defaultFont == null) {
169             defaultFont = org.openide.windows.WindowManager.getDefault()
170                                                .getMainWindow().getFont();
171             if (defaultFont == null)
172                 defaultFont = new Font("Dialog", Font.PLAIN, 12); // NOI18N
173
}
174         return defaultFont;
175     }
176
177     private static Font defaultFont;
178 }
179
Popular Tags