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.*;
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.*;
18 /** Platform class for most reasonable Java1.2+ Java2s */
19 public class Java2 extends AWT {
21 protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
23 // disable the focus manager so we can intercept the tab key
24 javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
25 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
26 public void focusPreviousComponent(Component aComponent) { }
27 public void focusNextComponent(Component aComponent) { }
31 /** this is done with reflection in case a new plugin comes out that doesn't let us pull the sun.plugin.* trick */
32 protected synchronized org.ibex.net.HTTP.Proxy _detectProxy() {
33 return (org.ibex.net.HTTP.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
34 public Object run() { try {
35 org.ibex.net.HTTP.Proxy pi = new org.ibex.net.HTTP.Proxy();
37 Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
38 Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
39 Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
41 Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
42 Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
43 Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.ibex.org") });
45 Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
47 if (((Boolean)ProxyInfo.getMethod("isSocksUsed",
48 new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
50 (String)ProxyInfo.getMethod("getSocksProxy",
51 new Class[] { }).invoke(proxyInfo, new Object[] { });
53 ((Integer)ProxyInfo.getMethod("getSocksPort",
54 new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
57 if (((Boolean)ProxyInfo.getMethod("isProxyUsed",
58 new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
60 (String)ProxyInfo.getMethod("getProxy",
61 new Class[] { }).invoke(proxyInfo, new Object[] { });
63 ((Integer)ProxyInfo.getMethod("getPort",
64 new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
67 if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
70 } catch (Throwable e) {
71 if (Log.on) Log.info(this, "No proxy information found in Java Plugin classes");
77 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Java2PixelBuffer(w, h); }
78 protected Surface __createSurface(final Box root, final boolean framed) { return new Java2Surface(root, framed); }
79 protected Surface _createSurface(final Box root, final boolean framed) {
80 return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
81 public Object run() { return __createSurface(root, framed); } }); }
84 // Inner Classes //////////////////////////////////////////////////////////////////
86 private static Cursor invisibleCursor =
87 Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
88 new Point(1, 1), "invisible");
90 public static class Java2Surface extends AWTSurface {
91 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 // this doens't seem to work on Windows
116 public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
117 Image i2 = ((AWTGlyph)source).getImage();
118 Graphics2D g2 = (Graphics2D)i.getGraphics();
119 g2.setComposite(AlphaComposite.DstOut);
120 g2.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
121 g2.drawImage(i2, dx, dy, null);
122 g2.setComposite(AlphaComposite.DstOver);
123 g2.setColor(new java.awt.Color((rgb & 0x00FF0000) >> 16, (rgb & 0x0000FF00) >> 8, (rgb & 0x000000FF)));
124 g2.fillRect(dx, dy, cx2 - dx, cy2 - dy);
125 g2.drawImage(i2, 0, 0, null);
126 g2.setClip(0, 0, i.getWidth(null), i.getHeight(null));
129 public Java2PixelBuffer(int w, int h) {
131 sm = cm.createCompatibleSampleModel(w, h);
132 int numSamples = w * h * sm.getNumDataElements();
133 if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
134 if (sbank == null || numSamples > 512 * 512 / 3) {
135 buf = new DataBufferUShort(numSamples);
137 if (numSamples > sbank.length - bank_start) {
139 sbank = new short[512 * 512];
141 buf = new DataBufferUShort(sbank, numSamples, bank_start);
142 bank_start += numSamples;
144 } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
145 if (bbank == null || numSamples > 512 * 512 / 3) {
146 buf = new DataBufferByte(numSamples);
148 if (numSamples > bbank.length - bank_start) {
150 bbank = new byte[512 * 512];
152 buf = new DataBufferByte(bbank, numSamples, bank_start);
153 bank_start += numSamples;
155 } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
156 if (ibank == null || numSamples > 512 * 512 / 3) {
157 buf = new DataBufferInt(numSamples);
159 if (numSamples > ibank.length - bank_start) {
161 ibank = new int[512 * 512];
163 buf = new DataBufferInt(ibank, numSamples, bank_start);
164 bank_start += numSamples;
167 raster = Raster.createWritableRaster(sm, buf, null);
168 i = new BufferedImage(cm, raster, false, emptyHashtable);