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);
55 if (Log.on) Log.log(this, e);
61 protected Socket __getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
62 return super._getSocket(host, port, ssl, negotiate);
64 protected Socket _getSocket(final String host, final int port, final boolean ssl, final boolean negotiate) throws IOException {
65 return (Socket)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
68 return __getSocket(host, port, ssl, negotiate);
69 } catch (Exception e) {
70 if (Log.on) Log.log(Java12.class, "Error attempting to create socket");
71 if (Log.on) Log.log(Java12.class, e);
78 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java12DoubleBuffer(w, h); }
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 new Java12Surface(root, framed); }
85 // Inner Classes //////////////////////////////////////////////////////////////////
87 protected static class Java12Surface extends AWTSurface {
89 public Java12Surface(Box root, boolean framed) { super(root, framed); }
90 public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
91 if (ourGraphics == null) {
92 ourGraphics = window.getGraphics();
94 // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
95 ourGraphics.setClip(insets.left, insets.top, width + insets.left, height + insets.top);
97 _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
98 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
101 protected void _setMinimized(boolean b) {
103 if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
106 if (b) frame.setState(java.awt.Frame.ICONIFIED);
107 else frame.setState(java.awt.Frame.NORMAL);
111 protected static class Java12DoubleBuffer extends AWTDoubleBuffer {
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;
119 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
120 _doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
123 public Java12DoubleBuffer(int w, int h) {
124 SampleModel sm = cm.createCompatibleSampleModel(w, h);
125 int numSamples = w * h * sm.getNumDataElements();
126 DataBuffer buf = null;
127 if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
128 if (sbank == null || numSamples > 512 * 512 / 3) {
129 buf = new DataBufferUShort(numSamples);
131 if (numSamples > sbank.length - bank_start) {
133 sbank = new short[512 * 512];
135 buf = new DataBufferUShort(sbank, numSamples, bank_start);
136 bank_start += numSamples;
138 } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
139 if (bbank == null || numSamples > 512 * 512 / 3) {
140 buf = new DataBufferByte(numSamples);
142 if (numSamples > bbank.length - bank_start) {
144 bbank = new byte[512 * 512];
146 buf = new DataBufferByte(bbank, numSamples, bank_start);
147 bank_start += numSamples;
149 } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
150 if (ibank == null || numSamples > 512 * 512 / 3) {
151 buf = new DataBufferInt(numSamples);
153 if (numSamples > ibank.length - bank_start) {
155 ibank = new int[512 * 512];
157 buf = new DataBufferInt(ibank, numSamples, bank_start);
158 bank_start += numSamples;
161 i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false, emptyHashtable);
166 /** used to avoid garbage creation with getClipBounds() */
167 private static Rectangle clipBounds = new Rectangle();
169 // FEATURE: performance hits here are a result of getSubimage() -- if we don't call it, Graphics2D will. It creates tons of int[]s, as well as
170 // BufferedImage instances which get stored in an array deep inside Graphics2D, which the JDK does a LINEAR SCAN through. Aarrgh.
171 // This is rumored to be fixed in JDK 1.4.
172 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) {
174 if (dx1 == dx2 || dy1 == dy2) return;
176 if (dx2 - dx1 != sx2 - sx1 || dy2 - dy1 != sy2 - sy1)
177 g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
179 // fastpath for images that do not need to be scaled
180 if (i instanceof BufferedImage) {
181 BufferedImage b = (BufferedImage)i;
183 if (dx2 - dx1 != b.getWidth(null) || dy2 - dy1 != b.getHeight(null)) {
184 b = b.getSubimage(sx1, sy1, sx2 - sx1, sy2 - sy1);
190 g.drawImage(b, dx1, dy1, o);
194 // workaround for a wierd JDK bug
195 boolean skip = false;
196 try { g.getClipBounds(clipBounds); } catch (NullPointerException n) { skip = true; }
198 g.clipRect(dx1, dy1, dx2 - dx1, dy2 - dy1);
199 g.drawImage(i, dx1 - sx1, dy1 - sy1, o);
201 if (!skip) g.setClip(clipBounds.x, clipBounds.y, clipBounds.x + clipBounds.width, clipBounds.y + clipBounds.height);
206 protected org.xwt.Weak _getWeak(Object o) { return new Java12Weak(o); }
207 private static class Java12Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
208 public Java12Weak(Object o) { super(o); }
211 private String __getClipBoard() { return super._getClipBoard(); }
212 protected String _getClipBoard() {
213 return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
214 public Object run() { return __getClipBoard(); }
218 private void __setClipBoard(String s) { super._setClipBoard(s); }
219 protected void _setClipBoard(final String s) {
220 java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
221 public Object run() {
228 protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
230 protected void _newBrowserWindow(String url) {
231 if (Main.applet == null) {
232 if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
235 if (Log.on) Log.log(this, "asking browser to show URL " + url);
237 Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
238 } catch (MalformedURLException e) {
239 if (Log.on) Log.log(this, e);
243 /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
244 protected void _criticalAbort(String message) {
245 if (Log.on) Log.log(this, message);
246 new Semaphore().block();