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.
8 import java.awt.event.*;
9 import java.awt.image.*;
10 import java.awt.geom.*;
13 import org.ibex.util.*;
14 import java.lang.reflect.*;
15 import org.ibex.graphics.*;
16 import org.ibex.core.*;
17 import org.ibex.net.*;
19 /** Platform class for most reasonable Java1.2+ Java2s */
20 public class Java2 extends AWT {
22 protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
24 // Properties for Apple JDK 1.3
25 System.setProperty("apple.awt.showGrowBox", "false");
26 System.setProperty("com.apple.hwaccel", "true");
27 System.setProperty("com.apple.forcehwaccel", "true");
28 System.setProperty("apple.awt.window.position.forceSafeUserPositioning", "true");
29 System.setProperty("apple.awt.window.position.forceSafeCreation", "true");
31 // disable the focus manager so we can intercept the tab key
32 javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
33 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
34 public void focusPreviousComponent(Component aComponent) { }
35 public void focusNextComponent(Component aComponent) { }
39 /** this is done with reflection in case a new plugin comes out that doesn't let us pull the sun.plugin.* trick */
40 protected synchronized org.ibex.net.HTTP.Proxy _detectProxy() {
41 return (org.ibex.net.HTTP.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
42 public Object run() { try {
43 org.ibex.net.HTTP.Proxy pi = new org.ibex.net.HTTP.Proxy();
45 Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
46 Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
47 Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
49 Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
50 Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
51 Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.ibex.org") });
53 Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
55 if (((Boolean)ProxyInfo.getMethod("isSocksUsed",
56 new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
58 (String)ProxyInfo.getMethod("getSocksProxy",
59 new Class[] { }).invoke(proxyInfo, new Object[] { });
61 ((Integer)ProxyInfo.getMethod("getSocksPort",
62 new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
65 if (((Boolean)ProxyInfo.getMethod("isProxyUsed",
66 new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
68 (String)ProxyInfo.getMethod("getProxy",
69 new Class[] { }).invoke(proxyInfo, new Object[] { });
71 ((Integer)ProxyInfo.getMethod("getPort",
72 new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
75 if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
78 } catch (Throwable e) {
79 if (Log.on) Log.info(this, "No proxy information found in Java Plugin classes");
85 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Java2PixelBuffer(w, h); }
86 protected Surface __createSurface(final Box root, final boolean framed) { return new Java2Surface(root, framed); }
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() { return __createSurface(root, framed); } }); }
92 // Inner Classes //////////////////////////////////////////////////////////////////
94 private static Cursor invisibleCursor =
95 Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
96 new Point(1, 1), "invisible");
98 public static class Java2Surface extends AWTSurface {
99 public Java2Surface(Box root, boolean framed) { super(root, framed); }
100 protected void _setMinimized(boolean b) {
101 if (frame == null) Log.info(this, "JDK 1.2 can only minimize frames, not windows");
102 else if (b) frame.setState(java.awt.Frame.ICONIFIED);
103 else frame.setState(java.awt.Frame.NORMAL);
105 public void syncCursor() {
106 if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
107 else super.syncCursor();
111 protected static class Java2PixelBuffer extends AWTPixelBuffer {
112 private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
113 private static Hashtable emptyHashtable = new Hashtable();
114 private static short[] sbank = null;
115 private static int[] ibank = null;
116 private static byte[] bbank = null;
117 private static int bank_start = 0;
118 private WritableRaster raster = null;
119 private SampleModel sm = null;
120 private DataBuffer buf = null;
122 private static int[] xs = new int[65535];
123 private static int[] ys = new int[65535];
124 private static GeneralPath gp = new GeneralPath();
126 public void fill(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint) { fillStroke(p, paint, true, false); }
127 public void stroke(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint) { fillStroke(p, paint, false, true); }
128 public void fillStroke(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint, boolean fill, boolean stroke) {
129 if (g == null) g = getGraphics();
130 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
131 int argb = ((org.ibex.graphics.Paint.SingleColorPaint)paint).color;
132 g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
134 gp.setWindingRule(GeneralPath.WIND_EVEN_ODD);
135 for(int j=0; j<p.numcontours; j++) {
136 if (p.contours[j+1] <= p.contours[j]) continue;
137 gp.moveTo(p.x[p.contours[j]], p.y[p.contours[j]]);
138 for(int i=p.contours[j]+1; i<p.contours[j+1]; i++) gp.lineTo(p.x[i], p.y[i]);
141 if (fill) ((Graphics2D)g).fill(gp);
142 if (stroke) ((Graphics2D)g).draw(gp);
145 public Java2PixelBuffer(int w, int h) {
147 sm = cm.createCompatibleSampleModel(w, h);
148 int numSamples = w * h * sm.getNumDataElements();
149 if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
150 if (sbank == null || numSamples > 512 * 512 / 3) {
151 buf = new DataBufferUShort(numSamples);
153 if (numSamples > sbank.length - bank_start) {
155 sbank = new short[512 * 512];
157 buf = new DataBufferUShort(sbank, numSamples, bank_start);
158 bank_start += numSamples;
160 } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
161 if (bbank == null || numSamples > 512 * 512 / 3) {
162 buf = new DataBufferByte(numSamples);
164 if (numSamples > bbank.length - bank_start) {
166 bbank = new byte[512 * 512];
168 buf = new DataBufferByte(bbank, numSamples, bank_start);
169 bank_start += numSamples;
171 } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
172 if (ibank == null || numSamples > 512 * 512 / 3) {
173 buf = new DataBufferInt(numSamples);
175 if (numSamples > ibank.length - bank_start) {
177 ibank = new int[512 * 512];
179 buf = new DataBufferInt(ibank, numSamples, bank_start);
180 bank_start += numSamples;
183 raster = Raster.createWritableRaster(sm, buf, null);
184 i = new BufferedImage(cm, raster, false, emptyHashtable);