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 {
20 // disable the focus manager so we can intercept the tab key
21 javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
22 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
23 public void focusPreviousComponent(Component aComponent) { }
24 public void focusNextComponent(Component aComponent) { }
28 /** 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 */
29 protected synchronized org.xwt.Proxy _detectProxy() {
30 return (org.xwt.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
33 org.xwt.Proxy pi = new org.xwt.Proxy();
35 Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
36 Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
37 Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
39 Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
40 Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
41 Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.xwt.org") });
43 Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
45 if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
47 (String)ProxyInfo.getMethod("getSocksProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
49 ((Integer)ProxyInfo.getMethod("getSocksPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
52 if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
54 (String)ProxyInfo.getMethod("getProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
56 ((Integer)ProxyInfo.getMethod("getPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
59 if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
62 } catch (Throwable e) {
63 if (Log.on) Log.log(this, "exception while querying sun.plugin.protocol.PluginProxyHandler: " + e);
69 protected Socket __getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
70 return super._getSocket(host, port, ssl, negotiate);
72 protected Socket _getSocket(final String host, final int port, final boolean ssl, final boolean negotiate) throws IOException {
73 return (Socket)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
76 return __getSocket(host, port, ssl, negotiate);
77 } catch (Exception e) {
78 if (Log.on) Log.log(Java12.class, "Error attempting to create socket");
79 if (Log.on) Log.log(Java12.class, e);
86 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java12DoubleBuffer(w, h); }
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 new Java12Surface(root, framed); }
93 // Inner Classes //////////////////////////////////////////////////////////////////
95 protected static class Java12Surface extends AWTSurface {
97 public Java12Surface(Box root, boolean framed) { super(root, framed); }
98 public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
99 if (ourGraphics == null) {
100 ourGraphics = window.getGraphics();
102 // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
103 ourGraphics.setClip(insets.left, insets.top, width + insets.left, height + insets.top);
105 _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
106 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
109 protected void _setMinimized(boolean b) {
111 if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
114 if (b) frame.setState(java.awt.Frame.ICONIFIED);
115 else frame.setState(java.awt.Frame.NORMAL);
119 protected static class Java12DoubleBuffer extends AWTDoubleBuffer {
120 private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
121 private static Hashtable emptyHashtable = new Hashtable();
122 private static short[] sbank = null;
123 private static int[] ibank = null;
124 private static byte[] bbank = null;
125 private static int bank_start = 0;
127 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
128 _doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
131 public Java12DoubleBuffer(int w, int h) {
132 SampleModel sm = cm.createCompatibleSampleModel(w, h);
133 int numSamples = w * h * sm.getNumDataElements();
134 DataBuffer buf = null;
135 if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
136 if (sbank == null || numSamples > 512 * 512 / 3) {
137 buf = new DataBufferUShort(numSamples);
139 if (numSamples > sbank.length - bank_start) {
141 sbank = new short[512 * 512];
143 buf = new DataBufferUShort(sbank, numSamples, bank_start);
144 bank_start += numSamples;
146 } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
147 if (bbank == null || numSamples > 512 * 512 / 3) {
148 buf = new DataBufferByte(numSamples);
150 if (numSamples > bbank.length - bank_start) {
152 bbank = new byte[512 * 512];
154 buf = new DataBufferByte(bbank, numSamples, bank_start);
155 bank_start += numSamples;
157 } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
158 if (ibank == null || numSamples > 512 * 512 / 3) {
159 buf = new DataBufferInt(numSamples);
161 if (numSamples > ibank.length - bank_start) {
163 ibank = new int[512 * 512];
165 buf = new DataBufferInt(ibank, numSamples, bank_start);
166 bank_start += numSamples;
169 i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false, emptyHashtable);
174 /** used to avoid garbage creation with getClipBounds() */
175 private static Rectangle clipBounds = new Rectangle();
177 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) {
178 if (dx1 == dx2 || dy1 == dy2) return;
179 g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
182 protected org.xwt.Weak _getWeak(Object o) { return new Java12Weak(o); }
183 private static class Java12Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
184 public Java12Weak(Object o) { super(o); }
187 private String __getClipBoard() { return super._getClipBoard(); }
188 protected String _getClipBoard() {
189 return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
190 public Object run() { return __getClipBoard(); }
194 private void __setClipBoard(String s) { super._setClipBoard(s); }
195 protected void _setClipBoard(final String s) {
196 java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
197 public Object run() {
204 protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
206 protected void _newBrowserWindow(String url) {
207 if (Main.applet == null) {
208 if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
211 if (Log.on) Log.log(this, "asking browser to show URL " + url);
213 Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
214 } catch (MalformedURLException e) {
215 if (Log.on) Log.log(this, e);