1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
5 import java.awt.event.*;
6 import java.awt.image.*;
7 import java.awt.datatransfer.*;
11 import org.xwt.util.*;
13 import java.lang.reflect.*;
16 /** Platform class for most reasonable Java1.2+ JVMs */
17 public class Java12 extends AWT {
19 /** 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 */
20 protected synchronized HTTP.ProxyInfo _detectProxy() {
21 return (HTTP.ProxyInfo)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
24 HTTP.ProxyInfo pi = new HTTP.ProxyInfo();
26 Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
27 Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
28 Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
30 Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
31 Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
32 Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.xwt.org") });
34 Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
36 if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
38 (String)ProxyInfo.getMethod("getSocksProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
40 ((Integer)ProxyInfo.getMethod("getSocksPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
43 if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
45 (String)ProxyInfo.getMethod("getProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
47 ((Integer)ProxyInfo.getMethod("getPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
50 if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
53 } catch (Throwable e) {
54 if (Log.on) Log.log(this, "exception while querying sun.plugin.protocol.PluginProxyHandler: " + e);
60 protected Socket __getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
61 return super._getSocket(host, port, ssl, negotiate);
63 protected Socket _getSocket(final String host, final int port, final boolean ssl, final boolean negotiate) throws IOException {
64 return (Socket)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
67 return __getSocket(host, port, ssl, negotiate);
68 } catch (Exception e) {
69 if (Log.on) Log.log(Java12.class, "Error attempting to create socket");
70 if (Log.on) Log.log(Java12.class, e);
77 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java12DoubleBuffer(w, h); }
78 protected Surface _createSurface(final Box root, final boolean framed) {
79 return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
80 public Object run() { return new Java12Surface(root, framed); }
84 // Inner Classes //////////////////////////////////////////////////////////////////
86 protected static class Java12Surface extends AWTSurface {
88 public Java12Surface(Box root, boolean framed) { super(root, framed); }
89 public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
90 if (ourGraphics == null) {
91 ourGraphics = window.getGraphics();
93 // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
94 ourGraphics.setClip(insets.left, insets.top, width + insets.left, height + insets.top);
96 _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
97 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
100 protected void _setMinimized(boolean b) {
102 if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
105 if (b) frame.setState(java.awt.Frame.ICONIFIED);
106 else frame.setState(java.awt.Frame.NORMAL);
110 protected static class Java12DoubleBuffer extends AWTDoubleBuffer {
111 private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
112 private static Hashtable emptyHashtable = new Hashtable();
113 private static short[] sbank = null;
114 private static int[] ibank = null;
115 private static byte[] bbank = null;
116 private static int bank_start = 0;
118 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
119 _doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
122 public Java12DoubleBuffer(int w, int h) {
123 SampleModel sm = cm.createCompatibleSampleModel(w, h);
124 int numSamples = w * h * sm.getNumDataElements();
125 DataBuffer buf = null;
126 if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
127 if (sbank == null || numSamples > 512 * 512 / 3) {
128 buf = new DataBufferUShort(numSamples);
130 if (numSamples > sbank.length - bank_start) {
132 sbank = new short[512 * 512];
134 buf = new DataBufferUShort(sbank, numSamples, bank_start);
135 bank_start += numSamples;
137 } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
138 if (bbank == null || numSamples > 512 * 512 / 3) {
139 buf = new DataBufferByte(numSamples);
141 if (numSamples > bbank.length - bank_start) {
143 bbank = new byte[512 * 512];
145 buf = new DataBufferByte(bbank, numSamples, bank_start);
146 bank_start += numSamples;
148 } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
149 if (ibank == null || numSamples > 512 * 512 / 3) {
150 buf = new DataBufferInt(numSamples);
152 if (numSamples > ibank.length - bank_start) {
154 ibank = new int[512 * 512];
156 buf = new DataBufferInt(ibank, numSamples, bank_start);
157 bank_start += numSamples;
160 i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false, emptyHashtable);
165 /** used to avoid garbage creation with getClipBounds() */
166 private static Rectangle clipBounds = new Rectangle();
168 protected static void _doDrawImage(Graphics g, Image i, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver o) {
169 if (dx1 == dx2 || dy1 == dy2) return;
170 g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
173 protected org.xwt.Weak _getWeak(Object o) { return new Java12Weak(o); }
174 private static class Java12Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
175 public Java12Weak(Object o) { super(o); }
178 private String __getClipBoard() { return super._getClipBoard(); }
179 protected String _getClipBoard() {
180 return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
181 public Object run() { return __getClipBoard(); }
185 private void __setClipBoard(String s) { super._setClipBoard(s); }
186 protected void _setClipBoard(final String s) {
187 java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
188 public Object run() {
195 protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
197 protected void _newBrowserWindow(String url) {
198 if (Main.applet == null) {
199 if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
202 if (Log.on) Log.log(this, "asking browser to show URL " + url);
204 Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
205 } catch (MalformedURLException e) {
206 if (Log.on) Log.log(this, e);
210 /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
211 protected void _criticalAbort(String message) {
212 if (Log.on) Log.log(this, message);
213 new Semaphore().block();