ec48893821e5a0363ebe9fbbcbb3275266536453
[org.ibex.core.git] / src / org / ibex / plat / Java2.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex.plat;
3
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.awt.image.*;
7 import java.net.*;
8 import java.util.*;
9 import org.ibex.util.*;
10 import org.ibex.*;
11 import java.lang.reflect.*;
12
13
14 /** Platform class for most reasonable Java1.2+ Java2s */
15 public class Java2 extends AWT {
16
17     private boolean isJava14 = false;
18
19     public Java2() {
20         // disable the focus manager so we can intercept the tab key
21         String versionString = System.getProperty("java.version", "");
22         int secondDecimal = versionString.substring(versionString.indexOf('.') + 1).indexOf('.');
23         if (secondDecimal != -1) versionString = versionString.substring(0, versionString.indexOf('.') + 1 + secondDecimal);
24         double version = Double.parseDouble(versionString);
25         if (version >= 1.4) {
26             isJava14 = true;
27             try {
28                 Toolkit t = java.awt.Toolkit.getDefaultToolkit();
29                 Method m = java.awt.Toolkit.class.getMethod("setDynamicLayout", new Class[] { Boolean.TYPE });
30                 m.invoke(t, new Object[] { Boolean.TRUE });
31             } catch (Exception e) {
32                 Log.info(this, "Exception while trying to enable AWT Dynamic Layout");
33                 Log.info(this, e);
34             }
35         }
36         javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
37                 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
38                 public void focusPreviousComponent(Component aComponent) { }
39                 public void focusNextComponent(Component aComponent) { }
40             });
41     }
42
43     /** this is done with reflection in case a new version of the plugin comes out that doesn't let us pull the sun.plugin.* trick */
44     protected synchronized org.ibex.HTTP.Proxy _detectProxy() {
45         return (org.ibex.HTTP.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
46                 public Object run() {
47                     try {
48                         org.ibex.HTTP.Proxy pi = new org.ibex.HTTP.Proxy();
49                         
50                         Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
51                         Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
52                         Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
53                         
54                         Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
55                         Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
56                         Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.ibex.org") });
57                         
58                         Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
59                         
60                         if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
61                             pi.socksProxyHost =
62                                 (String)ProxyInfo.getMethod("getSocksProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
63                             pi.socksProxyPort =
64                                 ((Integer)ProxyInfo.getMethod("getSocksPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
65                         }
66                         
67                         if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
68                             pi.httpProxyHost =
69                                 (String)ProxyInfo.getMethod("getProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
70                             pi.httpProxyPort =
71                                 ((Integer)ProxyInfo.getMethod("getPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
72                         }
73                         
74                         if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
75                         else return null;
76
77                     } catch (Throwable e) {
78                         if (Log.on) Log.info(this, "No proxy information found in Java Plugin classes");
79                         return null;
80                     }
81                 }});
82     }
83
84     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Java2PixelBuffer(w, h); }
85     protected Surface _createSurface(final Box root, final boolean framed) {
86         return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
87                 public Object run() {
88                     if (isJava14) {
89                         try {
90                              // weaken the binding here to avoid link errors on 1.3.x
91                              Class java14SurfaceClass = Class.forName(Java2.class.getName() + "$Java14Surface");
92                              Constructor ctor = java14SurfaceClass.getConstructor(new Class[] { Box.class, Boolean.TYPE });
93                                return (Surface)ctor.newInstance(new Object[] { root, Boolean.valueOf(framed) });
94                         } catch (Exception e) {
95                             Log.info(this, e);
96                             throw new LinkageError("error: " + e);
97                         }
98                     } else {
99                         return new Java2Surface(root, framed);
100                     }
101                 }
102             });
103     }
104
105     // Inner Classes //////////////////////////////////////////////////////////////////
106
107     private static Cursor invisibleCursor =
108         Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
109                                                        new Point(1, 1), "invisible");
110
111     protected static class Java2Surface extends AWTSurface {
112
113         public Java2Surface(Box root, boolean framed) { super(root, framed); }
114
115         protected void _setMinimized(boolean b) {
116             if (frame == null) Log.info(this, "JDK 1.2 can only minimize frames, not windows");
117             else if (b) frame.setState(java.awt.Frame.ICONIFIED);
118             else frame.setState(java.awt.Frame.NORMAL);
119         }
120
121         public void syncCursor() {
122             if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
123             else super.syncCursor();
124         }
125     }
126
127     protected static class Java14Surface extends Java2Surface implements WindowStateListener, MouseWheelListener {
128         public Java14Surface(Box root, boolean framed) {
129             super(root, true);
130             // JDK1.4 doesn't like java.lang.Window's...
131             if (!framed) ((Frame)window).setUndecorated(true);
132             window.addWindowStateListener(this);
133             window.addMouseWheelListener(this);
134             window.setVisible(true);
135         }
136
137         protected void makeVisible() { }
138         
139         protected void _setMaximized(boolean m) {
140             if (frame == null) {
141                 if (Log.on) Log.info(this, "JDK 1.4 can only maximize frames, not windows");
142                 return;
143             }
144             frame.setExtendedState(m ? Frame.MAXIMIZED_BOTH : (minimized ? Frame.ICONIFIED : Frame.NORMAL));
145         }
146         protected void _setMinimized(boolean m) {
147             if (frame == null) {
148                 if (Log.on) Log.info(this, "JDK 1.4 can only minimize frames, not windows");
149                 return;
150             }
151             frame.setExtendedState(m ? Frame.ICONIFIED : (maximized ? Frame.MAXIMIZED_BOTH : Frame.NORMAL));
152         }
153         public void windowStateChanged(WindowEvent e) {
154             if (e.getOldState() != e.getNewState()) {
155                 if ((e.getNewState() & Frame.MAXIMIZED_BOTH) != 0) Maximized(true);
156                 else if (((e.getOldState() & Frame.MAXIMIZED_BOTH) != 0) && (e.getNewState() & Frame.MAXIMIZED_BOTH) == 0)
157                     Maximized(false);
158             }
159         }
160
161         public void mouseWheelMoved(MouseWheelEvent m) {
162             if (m.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
163                 VScroll(m.getUnitsToScroll());
164         }
165     }
166
167     protected static class Java2PixelBuffer extends AWTPixelBuffer {
168         private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
169         private static Hashtable emptyHashtable = new Hashtable();
170         private static short[] sbank = null;
171         private static int[] ibank = null;
172         private static byte[] bbank = null;
173         private static int bank_start = 0;
174         private WritableRaster raster = null;
175         private SampleModel sm = null;
176         private DataBuffer buf = null;
177
178         // this doens't seem to work on Windows
179         public void drawGlyph(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
180             AWTPicture src = (AWTPicture)source;
181             src.init();
182             Graphics2D g2 = (Graphics2D)i.getGraphics();
183             g2.setComposite(AlphaComposite.DstOut);
184             g2.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
185             g2.drawImage(src.i, dx, dy, null);
186             g2.setComposite(AlphaComposite.DstOver);
187             g2.setColor(new Color((rgb & 0x00FF0000) >> 16, (rgb & 0x0000FF00) >> 8, (rgb & 0x000000FF)));
188             g2.fillRect(dx, dy, cx2 - dx, cy2 - dy);
189             g2.drawImage(i, 0, 0, null);
190             g2.setClip(0, 0, i.getWidth(null), i.getHeight(null));
191         }
192
193         public Java2PixelBuffer(int w, int h) {
194             sm = cm.createCompatibleSampleModel(w, h);
195             int numSamples = w * h * sm.getNumDataElements();
196             if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
197                 if (sbank == null || numSamples > 512 * 512 / 3) {
198                     buf = new DataBufferUShort(numSamples);
199                 } else {
200                     if (numSamples > sbank.length - bank_start) {
201                         bank_start = 0;
202                         sbank = new short[512 * 512];
203                     }
204                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
205                     bank_start += numSamples;
206                 }
207             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
208                 if (bbank == null || numSamples > 512 * 512 / 3) {
209                     buf = new DataBufferByte(numSamples);
210                 } else {
211                     if (numSamples > bbank.length - bank_start) {
212                         bank_start = 0;
213                         bbank = new byte[512 * 512];
214                     }
215                     buf = new DataBufferByte(bbank, numSamples, bank_start);
216                     bank_start += numSamples;
217                 }
218             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
219                 if (ibank == null || numSamples > 512 * 512 / 3) {
220                     buf = new DataBufferInt(numSamples);
221                 } else {
222                     if (numSamples > ibank.length - bank_start) {
223                         bank_start = 0;
224                         ibank = new int[512 * 512];
225                     }
226                     buf = new DataBufferInt(ibank, numSamples, bank_start);
227                     bank_start += numSamples;
228                 }
229             }
230             raster = Raster.createWritableRaster(sm, buf, null);
231             i = new BufferedImage(cm, raster, false,  emptyHashtable);
232             g = i.getGraphics();
233         }
234     }
235
236     protected String getDescriptiveName() { return isJava14 ? "Java 1.4+ JVM" : "Java 1.2+ JVM"; }
237
238 }