-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
package org.xwt.plat;
import java.awt.*;
import java.util.*;
import org.xwt.util.*;
import org.xwt.*;
+import java.lang.reflect.*;
-// FEATURE: Java 1.4 allows undecorated frames and can maximize windows
-/** Platform class for most reasonable Java1.2+ JVMs */
+/** Platform class for most reasonable Java1.2+ Java2s */
public class Java2 extends AWT {
- protected Socket __getSocket(String host, int port, boolean ssl) throws IOException { return super._getSocket(host, port, ssl); }
- protected Socket _getSocket(final String host, final int port, final boolean ssl) throws IOException {
+ private boolean isJava14 = false;
+ protected boolean _supressDirtyOnResize() {
+ return false;
+ //return (isJava14 && !System.getProperty("os.name", "").equals("Mac OS X"))? false : true;
+ }
+
+ public Java2() {
+ // disable the focus manager so we can intercept the tab key
+ String versionString = System.getProperty("java.version", "");
+ int secondDecimal = versionString.substring(versionString.indexOf('.') + 1).indexOf('.');
+ if (secondDecimal != -1) versionString = versionString.substring(0, secondDecimal);
+ /*
+ double version = Double.parseDouble(versionString);
+ if (version >= 1.4) {
+ isJava14 = true;
+ try {
+ Toolkit t = java.awt.Toolkit.getDefaultToolkit();
+ Method m = java.awt.Toolkit.class.getMethod("setDynamicLayout", new Class[] { Boolean.class });
+ m.invoke(t, new Object[] { Boolean.TRUE });
+ } catch (Exception e) {
+ Log.log(this, "Exception while trying to enable AWT Dynamic Layout");
+ Log.log(this, e);
+ }
+ }
+ */
+ javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
+ public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
+ public void focusPreviousComponent(Component aComponent) { }
+ public void focusNextComponent(Component aComponent) { }
+ });
+ }
+
+ /** 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 */
+ protected synchronized org.xwt.Proxy _detectProxy() {
+ return (org.xwt.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
+ public Object run() {
+ try {
+ org.xwt.Proxy pi = new org.xwt.Proxy();
+
+ Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
+ Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
+ Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
+
+ Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
+ Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
+ Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.xwt.org") });
+
+ Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
+
+ if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
+ pi.socksProxyHost =
+ (String)ProxyInfo.getMethod("getSocksProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
+ pi.socksProxyPort =
+ ((Integer)ProxyInfo.getMethod("getSocksPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
+ }
+
+ if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
+ pi.httpProxyHost =
+ (String)ProxyInfo.getMethod("getProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
+ pi.httpProxyPort =
+ ((Integer)ProxyInfo.getMethod("getPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
+ }
+
+ if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
+ else return null;
+
+ } catch (Throwable e) {
+ if (Log.on) Log.log(this, "exception while querying sun.plugin.protocol.PluginProxyHandler: " + e);
+ return null;
+ }
+ }});
+ }
+
+ protected Socket __getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
+ return super._getSocket(host, port, ssl, negotiate);
+ }
+ protected Socket _getSocket(final String host, final int port, final boolean ssl, final boolean negotiate) throws IOException {
return (Socket)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
public Object run() {
try {
- return __getSocket(host, port, ssl);
+ return __getSocket(host, port, ssl, negotiate);
} catch (Exception e) {
if (Log.on) Log.log(Java2.class, "Error attempting to create socket");
if (Log.on) Log.log(Java2.class, e);
});
}
- protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java2DoubleBuffer(w, h); }
+ protected PixelBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java2DoubleBuffer(w, h); }
protected Surface _createSurface(final Box root, final boolean framed) {
return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
- public Object run() { return new Java2Surface(root, framed); }
+ public Object run() {
+ if (isJava14) {
+ try {
+ // weaken the binding here to avoid link errors on 1.3.x
+ Class java14SurfaceClass = Class.forName(Java2.class.getName() + "$Java14Surface");
+ Constructor ctor = java14SurfaceClass.getConstructor(new Class[] { Box.class, Boolean.TYPE });
+ return (Surface)ctor.newInstance(new Object[] { root, new Boolean(framed) });
+ } catch (Exception e) {
+ Log.log(this, e);
+ throw new LinkageError("error: " + e);
+ }
+ } else {
+ return new Java2Surface(root, framed);
+ }
+ }
});
}
// Inner Classes //////////////////////////////////////////////////////////////////
+ private static Cursor invisibleCursor =
+ Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
+ new Point(1, 1), "invisible");
+
protected static class Java2Surface extends AWTSurface {
public Java2Surface(Box root, boolean framed) { super(root, framed); }
- public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
- if (ourGraphics == null) ourGraphics = window.getGraphics();
- _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
+
+ public void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
+ if (ourGraphics == null) {
+ ourGraphics = window.getGraphics();
+
+ // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
+ ourGraphics.setClip(insets.left, insets.top, root.width + insets.left, root.height + insets.top);
+ }
+ _doDrawImage(window.getGraphics(), ((AWTPixelBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
}
if (b) frame.setState(java.awt.Frame.ICONIFIED);
else frame.setState(java.awt.Frame.NORMAL);
}
+
+ public void syncCursor() {
+ if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
+ else super.syncCursor();
+ }
+ }
+
+ protected static class Java14Surface extends Java2Surface implements WindowStateListener {
+ public Java14Surface(Box root, boolean framed) {
+ super(root, true);
+ // JDK1.4 doesn't like java.lang.Window's...
+ if (!framed) ((Frame)window).setUndecorated(true);
+ window.addWindowStateListener(this);
+ window.setVisible(true);
+ }
+
+ protected void makeVisible() { }
+
+ protected void _setMaximized(boolean m) {
+ if (frame == null) {
+ if (Log.on) Log.log(this, "JDK 1.4 can only maximize frames, not windows");
+ return;
+ }
+ frame.setExtendedState(m ? Frame.MAXIMIZED_BOTH : (minimized ? Frame.ICONIFIED : Frame.NORMAL));
+ }
+ protected void _setMinimized(boolean m) {
+ if (frame == null) {
+ if (Log.on) Log.log(this, "JDK 1.4 can only minimize frames, not windows");
+ return;
+ }
+ frame.setExtendedState(m ? Frame.ICONIFIED : (maximized ? Frame.MAXIMIZED_BOTH : Frame.NORMAL));
+ }
+ public void windowStateChanged(WindowEvent e) {
+ if (e.getOldState() != e.getNewState()) {
+ if ((e.getNewState() & Frame.MAXIMIZED_BOTH) != 0) Maximized(true);
+ else if (((e.getOldState() & Frame.MAXIMIZED_BOTH) != 0) && (e.getNewState() & Frame.MAXIMIZED_BOTH) == 0)
+ Maximized(false);
+ }
+ }
}
- protected static class Java2DoubleBuffer extends AWTDoubleBuffer {
+ protected static class Java2PixelBuffer extends AWTDoubleBuffer {
private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
private static Hashtable emptyHashtable = new Hashtable();
private static short[] sbank = null;
_doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
}
- public Java2DoubleBuffer(int w, int h) {
- super(w, h);
+ public Java2PixelBuffer(int w, int h) {
SampleModel sm = cm.createCompatibleSampleModel(w, h);
+ int numSamples = w * h * sm.getNumDataElements();
DataBuffer buf = null;
if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
- if (sbank == null || w * h > 512 * 512 / 3) {
- buf = new DataBufferUShort(w * h);
+ if (sbank == null || numSamples > 512 * 512 / 3) {
+ buf = new DataBufferUShort(numSamples);
} else {
- if (w * h > sbank.length - bank_start) {
+ if (numSamples > sbank.length - bank_start) {
bank_start = 0;
sbank = new short[512 * 512];
}
- buf = new DataBufferUShort(sbank, w * h, bank_start);
- bank_start += w * h;
+ buf = new DataBufferUShort(sbank, numSamples, bank_start);
+ bank_start += numSamples;
}
} else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
- if (bbank == null || w * h > 512 * 512 / 3) {
- buf = new DataBufferByte(w * h);
+ if (bbank == null || numSamples > 512 * 512 / 3) {
+ buf = new DataBufferByte(numSamples);
} else {
- if (w * h > bbank.length - bank_start) {
+ if (numSamples > bbank.length - bank_start) {
bank_start = 0;
bbank = new byte[512 * 512];
}
- buf = new DataBufferByte(bbank, w * h, bank_start);
- bank_start += w * h;
+ buf = new DataBufferByte(bbank, numSamples, bank_start);
+ bank_start += numSamples;
}
} else if (sm.getDataType() == DataBuffer.TYPE_INT) {
- if (ibank == null || w * h > 512 * 512 / 3) {
- buf = new DataBufferInt(w * h);
+ if (ibank == null || numSamples > 512 * 512 / 3) {
+ buf = new DataBufferInt(numSamples);
} else {
- if (w * h > ibank.length - bank_start) {
+ if (numSamples > ibank.length - bank_start) {
bank_start = 0;
ibank = new int[512 * 512];
}
- buf = new DataBufferInt(ibank, w * h, bank_start);
- bank_start += w * h;
+ buf = new DataBufferInt(ibank, numSamples, bank_start);
+ bank_start += numSamples;
}
}
i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false, emptyHashtable);
/** used to avoid garbage creation with getClipBounds() */
private static Rectangle clipBounds = new Rectangle();
- // 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
- // BufferedImage instances which get stored in an array deep inside Graphics2D, which the JDK does a LINEAR SCAN through. Aarrgh.
- // This is rumored to be fixed in JDK 1.4.
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) {
-
if (dx1 == dx2 || dy1 == dy2) return;
-
- if (dx2 - dx1 != sx2 - sx1 || dy2 - dy1 != sy2 - sy1)
- g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
- else {
- // fastpath for images that do not need to be scaled
- if (i instanceof BufferedImage) {
- BufferedImage b = (BufferedImage)i;
-
- if (dx2 - dx1 != b.getWidth(null) || dy2 - dy1 != b.getHeight(null)) {
- b = b.getSubimage(sx1, sy1, sx2 - sx1, sy2 - sy1);
- sx2 = sx2 - sx1;
- sy2 = sy2 - sy1;
- sx1 = 0;
- sy1 = 0;
- }
- g.drawImage(b, dx1, dy1, o);
-
- } else {
-
- // workaround for a wierd JDK bug
- boolean skip = false;
- try { g.getClipBounds(clipBounds); } catch (NullPointerException n) { skip = true; }
-
- g.clipRect(dx1, dy1, dx2 - dx1, dy2 - dy1);
- g.drawImage(i, dx1 - sx1, dy1 - sy1, o);
-
- if (!skip) g.setClip(clipBounds.x, clipBounds.y, clipBounds.x + clipBounds.width, clipBounds.y + clipBounds.height);
- }
- }
+ g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
}
protected org.xwt.Weak _getWeak(Object o) { return new Java2Weak(o); }
});
}
- protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
+ protected String getDescriptiveName() { return isJava14 ? "Java 1.4+ JVM" : "Java 1.2+ JVM"; }
+
+ protected void _newBrowserWindow(String url) {
+ /*
+ FIXME
+ if (Main.applet == null) {
+ if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
+ return;
+ }
+ if (Log.on) Log.log(this, "asking browser to show URL " + url);
+ try {
+ Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
+ } catch (MalformedURLException e) {
+ if (Log.on) Log.log(this, e);
+ }
+ */
+ }
+
}