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 // disable the focus manager so we can intercept the tab key
25 javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
26 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
27 public void focusPreviousComponent(Component aComponent) { }
28 public void focusNextComponent(Component aComponent) { }
32 /** this is done with reflection in case a new plugin comes out that doesn't let us pull the sun.plugin.* trick */
33 protected synchronized org.ibex.net.HTTP.Proxy _detectProxy() {
34 return (org.ibex.net.HTTP.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
35 public Object run() { try {
36 org.ibex.net.HTTP.Proxy pi = new org.ibex.net.HTTP.Proxy();
38 Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
39 Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
40 Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
42 Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
43 Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
44 Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.ibex.org") });
46 Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
48 if (((Boolean)ProxyInfo.getMethod("isSocksUsed",
49 new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
51 (String)ProxyInfo.getMethod("getSocksProxy",
52 new Class[] { }).invoke(proxyInfo, new Object[] { });
54 ((Integer)ProxyInfo.getMethod("getSocksPort",
55 new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
58 if (((Boolean)ProxyInfo.getMethod("isProxyUsed",
59 new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
61 (String)ProxyInfo.getMethod("getProxy",
62 new Class[] { }).invoke(proxyInfo, new Object[] { });
64 ((Integer)ProxyInfo.getMethod("getPort",
65 new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
68 if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
71 } catch (Throwable e) {
72 if (Log.on) Log.info(this, "No proxy information found in Java Plugin classes");
78 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Java2PixelBuffer(w, h); }
79 protected Surface __createSurface(final Box root, final boolean framed) { return new Java2Surface(root, framed); }
80 protected Surface _createSurface(final Box root, final boolean framed) {
81 return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
82 public Object run() { return __createSurface(root, framed); } }); }
85 // Inner Classes //////////////////////////////////////////////////////////////////
87 private static Cursor invisibleCursor =
88 Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
89 new Point(1, 1), "invisible");
91 public static class Java2Surface extends AWTSurface {
92 public Java2Surface(Box root, boolean framed) { super(root, framed); }
93 protected void _setMinimized(boolean b) {
94 if (frame == null) Log.info(this, "JDK 1.2 can only minimize frames, not windows");
95 else if (b) frame.setState(java.awt.Frame.ICONIFIED);
96 else frame.setState(java.awt.Frame.NORMAL);
98 public void syncCursor() {
99 if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
100 else super.syncCursor();
104 protected static class Java2PixelBuffer extends AWTPixelBuffer {
105 private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
106 private static Hashtable emptyHashtable = new Hashtable();
107 private static short[] sbank = null;
108 private static int[] ibank = null;
109 private static byte[] bbank = null;
110 private static int bank_start = 0;
111 private WritableRaster raster = null;
112 private SampleModel sm = null;
113 private DataBuffer buf = null;
115 private static int[] xs = new int[65535];
116 private static int[] ys = new int[65535];
117 private static GeneralPath gp = new GeneralPath();
119 public void fill(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint) { fillStroke(p, paint, true, false); }
120 public void stroke(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint) { fillStroke(p, paint, false, true); }
121 public void fillStroke(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint, boolean fill, boolean stroke) {
122 if (g == null) g = getGraphics();
123 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
124 int argb = ((org.ibex.graphics.Paint.SingleColorPaint)paint).color;
125 g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
127 gp.setWindingRule(GeneralPath.WIND_EVEN_ODD);
128 for(int j=0; j<p.numcontours; j++) {
129 if (p.contours[j+1] <= p.contours[j]) continue;
130 gp.moveTo(p.x[p.contours[j]], p.y[p.contours[j]]);
131 for(int i=p.contours[j]+1; i<p.contours[j+1]; i++) gp.lineTo(p.x[i], p.y[i]);
134 if (fill) ((Graphics2D)g).fill(gp);
135 if (stroke) ((Graphics2D)g).draw(gp);
138 public Java2PixelBuffer(int w, int h) {
140 sm = cm.createCompatibleSampleModel(w, h);
141 int numSamples = w * h * sm.getNumDataElements();
142 if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
143 if (sbank == null || numSamples > 512 * 512 / 3) {
144 buf = new DataBufferUShort(numSamples);
146 if (numSamples > sbank.length - bank_start) {
148 sbank = new short[512 * 512];
150 buf = new DataBufferUShort(sbank, numSamples, bank_start);
151 bank_start += numSamples;
153 } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
154 if (bbank == null || numSamples > 512 * 512 / 3) {
155 buf = new DataBufferByte(numSamples);
157 if (numSamples > bbank.length - bank_start) {
159 bbank = new byte[512 * 512];
161 buf = new DataBufferByte(bbank, numSamples, bank_start);
162 bank_start += numSamples;
164 } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
165 if (ibank == null || numSamples > 512 * 512 / 3) {
166 buf = new DataBufferInt(numSamples);
168 if (numSamples > ibank.length - bank_start) {
170 ibank = new int[512 * 512];
172 buf = new DataBufferInt(ibank, numSamples, bank_start);
173 bank_start += numSamples;
176 raster = Raster.createWritableRaster(sm, buf, null);
177 i = new BufferedImage(cm, raster, false, emptyHashtable);