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