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+ Java2s */
17 public class Java2 extends AWT {
19 private boolean isJava14 = false;
20 protected boolean _supressDirtyOnResize() { return isJava14 ? false : true; }
23 // disable the focus manager so we can intercept the tab key
24 String versionString = System.getProperty("java.version", "");
25 int secondDecimal = versionString.substring(versionString.indexOf('.') + 1).indexOf('.');
26 if (secondDecimal != -1) versionString = versionString.substring(0, secondDecimal);
27 double version = Double.parseDouble(versionString);
31 Toolkit t = java.awt.Toolkit.getDefaultToolkit();
32 Method m = java.awt.Toolkit.class.getMethod("setDynamicLayout", new Class[] { Boolean.class });
33 m.invoke(t, new Object[] { Boolean.TRUE });
34 } catch (Exception e) {
35 Log.log(this, "Exception while trying to enable AWT Dynamic Layout");
39 javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
40 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
41 public void focusPreviousComponent(Component aComponent) { }
42 public void focusNextComponent(Component aComponent) { }
46 /** 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 */
47 protected synchronized org.xwt.Proxy _detectProxy() {
48 return (org.xwt.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
51 org.xwt.Proxy pi = new org.xwt.Proxy();
53 Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
54 Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
55 Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
57 Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
58 Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
59 Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.xwt.org") });
61 Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
63 if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
65 (String)ProxyInfo.getMethod("getSocksProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
67 ((Integer)ProxyInfo.getMethod("getSocksPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
70 if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
72 (String)ProxyInfo.getMethod("getProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
74 ((Integer)ProxyInfo.getMethod("getPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
77 if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
80 } catch (Throwable e) {
81 if (Log.on) Log.log(this, "exception while querying sun.plugin.protocol.PluginProxyHandler: " + e);
87 protected Socket __getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
88 return super._getSocket(host, port, ssl, negotiate);
90 protected Socket _getSocket(final String host, final int port, final boolean ssl, final boolean negotiate) throws IOException {
91 return (Socket)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
94 return __getSocket(host, port, ssl, negotiate);
95 } catch (Exception e) {
96 if (Log.on) Log.log(Java2.class, "Error attempting to create socket");
97 if (Log.on) Log.log(Java2.class, e);
104 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java2DoubleBuffer(w, h); }
105 protected Surface _createSurface(final Box root, final boolean framed) {
106 return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
107 public Object run() { return isJava14 ? new Java14Surface(root, framed) : new Java2Surface(root, framed); }
111 // Inner Classes //////////////////////////////////////////////////////////////////
113 private static Cursor invisibleCursor =
114 Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
115 new Point(1, 1), "invisible");
117 protected static class Java2Surface extends AWTSurface {
119 public Java2Surface(Box root, boolean framed) { super(root, framed); }
121 public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
122 if (ourGraphics == null) {
123 ourGraphics = window.getGraphics();
125 // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
126 ourGraphics.setClip(insets.left, insets.top, width + insets.left, height + insets.top);
128 _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
129 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
132 protected void _setMinimized(boolean b) {
134 if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
137 if (b) frame.setState(java.awt.Frame.ICONIFIED);
138 else frame.setState(java.awt.Frame.NORMAL);
141 public void syncCursor() {
142 if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
143 else super.syncCursor();
147 protected static class Java14Surface extends Java2Surface implements WindowStateListener {
148 public Java14Surface(Box root, boolean framed) {
150 // JDK1.4 doesn't like java.lang.Window's...
151 if (!framed) ((Frame)window).setUndecorated(true);
152 window.addWindowStateListener(this);
153 window.setVisible(true);
156 protected void makeVisible() { }
158 protected void _setMaximized(boolean m) {
160 if (Log.on) Log.log(this, "JDK 1.4 can only maximize frames, not windows");
163 frame.setExtendedState(m ? Frame.MAXIMIZED_BOTH : (minimized ? Frame.ICONIFIED : Frame.NORMAL));
165 protected void _setMinimized(boolean m) {
167 if (Log.on) Log.log(this, "JDK 1.4 can only minimize frames, not windows");
170 frame.setExtendedState(m ? Frame.ICONIFIED : (maximized ? Frame.MAXIMIZED_BOTH : Frame.NORMAL));
172 public void windowStateChanged(WindowEvent e) {
173 if (e.getOldState() != e.getNewState()) {
174 if ((e.getNewState() & Frame.MAXIMIZED_BOTH) != 0) Maximized(true);
175 else if (((e.getOldState() & Frame.MAXIMIZED_BOTH) != 0) && (e.getNewState() & Frame.MAXIMIZED_BOTH) == 0)
181 protected static class Java2DoubleBuffer extends AWTDoubleBuffer {
182 private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
183 private static Hashtable emptyHashtable = new Hashtable();
184 private static short[] sbank = null;
185 private static int[] ibank = null;
186 private static byte[] bbank = null;
187 private static int bank_start = 0;
189 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
190 _doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
193 public Java2DoubleBuffer(int w, int h) {
194 SampleModel sm = cm.createCompatibleSampleModel(w, h);
195 int numSamples = w * h * sm.getNumDataElements();
196 DataBuffer buf = null;
197 if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
198 if (sbank == null || numSamples > 512 * 512 / 3) {
199 buf = new DataBufferUShort(numSamples);
201 if (numSamples > sbank.length - bank_start) {
203 sbank = new short[512 * 512];
205 buf = new DataBufferUShort(sbank, numSamples, bank_start);
206 bank_start += numSamples;
208 } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
209 if (bbank == null || numSamples > 512 * 512 / 3) {
210 buf = new DataBufferByte(numSamples);
212 if (numSamples > bbank.length - bank_start) {
214 bbank = new byte[512 * 512];
216 buf = new DataBufferByte(bbank, numSamples, bank_start);
217 bank_start += numSamples;
219 } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
220 if (ibank == null || numSamples > 512 * 512 / 3) {
221 buf = new DataBufferInt(numSamples);
223 if (numSamples > ibank.length - bank_start) {
225 ibank = new int[512 * 512];
227 buf = new DataBufferInt(ibank, numSamples, bank_start);
228 bank_start += numSamples;
231 i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false, emptyHashtable);
236 /** used to avoid garbage creation with getClipBounds() */
237 private static Rectangle clipBounds = new Rectangle();
239 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) {
240 if (dx1 == dx2 || dy1 == dy2) return;
241 g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
244 protected org.xwt.Weak _getWeak(Object o) { return new Java2Weak(o); }
245 private static class Java2Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
246 public Java2Weak(Object o) { super(o); }
249 private String __getClipBoard() { return super._getClipBoard(); }
250 protected String _getClipBoard() {
251 return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
252 public Object run() { return __getClipBoard(); }
256 private void __setClipBoard(String s) { super._setClipBoard(s); }
257 protected void _setClipBoard(final String s) {
258 java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
259 public Object run() {
266 protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
268 protected void _newBrowserWindow(String url) {
269 if (Main.applet == null) {
270 if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
273 if (Log.on) Log.log(this, "asking browser to show URL " + url);
275 Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
276 } catch (MalformedURLException e) {
277 if (Log.on) Log.log(this, e);