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