1 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 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 54 || comp instanceof javax.swing.RootPaneContainer ) 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 [] {ContainerPeer.class, PanelPeer.class}, new FakePanelPeer((Panel) comp)); 65 else if (comp instanceof TextField) 66 peer = getFakePeer(new Class [] {TextFieldPeer.class, TextComponentPeer.class}, new FakeTextFieldPeer((TextField) comp)); 67 else if (comp instanceof TextArea) 68 peer = getFakePeer(new Class [] {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 [] {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 fakePeerInterfaces, FakeComponentPeer compPeer) { 91 return getFakePeer(new Class [] {fakePeerInterfaces}, compPeer); 92 } 93 94 private static FakePeer getFakePeer(Class [] fakePeerInterfaces, FakeComponentPeer compPeer) { 95 96 Class [] interfaces = new Class [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 proxyClass = Proxy.getProxyClass(compPeer.getClass().getClassLoader(), interfaces); 104 FakePeerInvocationHandler handler = new FakePeerInvocationHandler(compPeer); 105 try { 106 return (FakePeer) proxyClass.getConstructor(new Class [] { InvocationHandler.class }).newInstance(new Object [] { handler }); 107 } catch (Exception 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"); f.setAccessible(true); 117 f.set(comp, peer); 118 } 119 catch (Exception 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 try { 139 Field f = Container.class.getDeclaredField("component"); f.setAccessible(true); 141 return (Component[]) f.get(container); 142 } 143 catch (Exception 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"); f.setAccessible(true); 154 Object peer = (ComponentPeer) f.get(comp); 155 if (peer instanceof FakePeer) { 156 f.set(comp, null); 157 return (FakePeer) peer; 158 } 159 } 160 catch (Exception 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); } 174 return defaultFont; 175 } 176 177 private static Font defaultFont; 178 } 179 | Popular Tags |