+// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.xwt.plat;
+
+import java.awt.*;
+import java.awt.event.*;
+import java.awt.image.*;
+import java.awt.datatransfer.*;
+import java.net.*;
+import java.io.*;
+import java.util.*;
+import org.xwt.util.*;
+import org.xwt.*;
+
+// FEATURE: Java 1.4 allows undecorated frames and can maximize windows
+
+/** Platform class for most reasonable Java1.2+ JVMs */
+public class Java12 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 {
+ return (Socket)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
+ public Object run() {
+ try {
+ return __getSocket(host, port, ssl);
+ } catch (Exception e) {
+ if (Log.on) Log.log(Java12.class, "Error attempting to create socket");
+ if (Log.on) Log.log(Java12.class, e);
+ return null;
+ }
+ }
+ });
+ }
+
+ protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java12DoubleBuffer(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 Java12Surface(root, framed); }
+ });
+ }
+
+ // Inner Classes //////////////////////////////////////////////////////////////////
+
+ protected static class Java12Surface extends AWTSurface {
+
+ public Java12Surface(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();
+
+ // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
+ ourGraphics.setClip(insets.left, insets.top, width + insets.left, height + insets.top);
+ }
+ _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
+ sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
+ }
+
+ protected void _setMinimized(boolean b) {
+ if (frame == null) {
+ if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
+ return;
+ }
+ if (b) frame.setState(java.awt.Frame.ICONIFIED);
+ else frame.setState(java.awt.Frame.NORMAL);
+ }
+ }
+
+ protected static class Java12DoubleBuffer extends AWTDoubleBuffer {
+ private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
+ private static Hashtable emptyHashtable = new Hashtable();
+ private static short[] sbank = null;
+ private static int[] ibank = null;
+ private static byte[] bbank = null;
+ private static int bank_start = 0;
+
+ public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
+ _doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
+ }
+
+ public Java12DoubleBuffer(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 || numSamples > 512 * 512 / 3) {
+ buf = new DataBufferUShort(numSamples);
+ } else {
+ if (numSamples > sbank.length - bank_start) {
+ bank_start = 0;
+ sbank = new short[512 * 512];
+ }
+ buf = new DataBufferUShort(sbank, numSamples, bank_start);
+ bank_start += numSamples;
+ }
+ } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
+ if (bbank == null || numSamples > 512 * 512 / 3) {
+ buf = new DataBufferByte(numSamples);
+ } else {
+ if (numSamples > bbank.length - bank_start) {
+ bank_start = 0;
+ bbank = new byte[512 * 512];
+ }
+ buf = new DataBufferByte(bbank, numSamples, bank_start);
+ bank_start += numSamples;
+ }
+ } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
+ if (ibank == null || numSamples > 512 * 512 / 3) {
+ buf = new DataBufferInt(numSamples);
+ } else {
+ if (numSamples > ibank.length - bank_start) {
+ bank_start = 0;
+ ibank = new int[512 * 512];
+ }
+ buf = new DataBufferInt(ibank, numSamples, bank_start);
+ bank_start += numSamples;
+ }
+ }
+ i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false, emptyHashtable);
+ g = i.getGraphics();
+ }
+ }
+
+ /** 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);
+ }
+ }
+ }
+
+ protected org.xwt.Weak _getWeak(Object o) { return new Java12Weak(o); }
+ private static class Java12Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
+ public Java12Weak(Object o) { super(o); }
+ }
+
+ private String __getClipBoard() { return super._getClipBoard(); }
+ protected String _getClipBoard() {
+ return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
+ public Object run() { return __getClipBoard(); }
+ });
+ }
+
+ private void __setClipBoard(String s) { super._setClipBoard(s); }
+ protected void _setClipBoard(final String s) {
+ java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
+ public Object run() {
+ __setClipBoard(s);
+ return null;
+ }
+ });
+ }
+
+ protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
+
+ protected void _newBrowserWindow(String url) {
+ 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);
+ }
+ }
+
+ /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
+ protected void _criticalAbort(String message) {
+ if (Log.on) Log.log(this, message);
+ new Semaphore().block();
+ }
+
+}