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.*;
14 // FEATURE: Java 1.4 allows undecorated frames and can maximize windows
16 /** Platform class for most reasonable Java1.2+ JVMs */
17 public class Java12 extends AWT {
19 protected Socket __getSocket(String host, int port, boolean ssl) throws IOException { return super._getSocket(host, port, ssl); }
20 protected Socket _getSocket(final String host, final int port, final boolean ssl) throws IOException {
21 return (Socket)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
24 return __getSocket(host, port, ssl);
25 } catch (Exception e) {
26 if (Log.on) Log.log(Java12.class, "Error attempting to create socket");
27 if (Log.on) Log.log(Java12.class, e);
34 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java12DoubleBuffer(w, h); }
35 protected Surface _createSurface(final Box root, final boolean framed) {
36 return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
37 public Object run() { return new Java12Surface(root, framed); }
41 // Inner Classes //////////////////////////////////////////////////////////////////
43 protected static class Java12Surface extends AWTSurface {
45 public Java12Surface(Box root, boolean framed) { super(root, framed); }
46 public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
47 if (ourGraphics == null) {
48 ourGraphics = window.getGraphics();
50 // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
51 ourGraphics.setClip(insets.left, insets.top, width + insets.left, height + insets.top);
53 _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
54 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
57 protected void _setMinimized(boolean b) {
59 if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
62 if (b) frame.setState(java.awt.Frame.ICONIFIED);
63 else frame.setState(java.awt.Frame.NORMAL);
67 protected static class Java12DoubleBuffer extends AWTDoubleBuffer {
68 private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
69 private static Hashtable emptyHashtable = new Hashtable();
70 private static short[] sbank = null;
71 private static int[] ibank = null;
72 private static byte[] bbank = null;
73 private static int bank_start = 0;
75 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
76 _doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
79 public Java12DoubleBuffer(int w, int h) {
80 SampleModel sm = cm.createCompatibleSampleModel(w, h);
81 int numSamples = w * h * sm.getNumDataElements();
82 DataBuffer buf = null;
83 if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
84 if (sbank == null || numSamples > 512 * 512 / 3) {
85 buf = new DataBufferUShort(numSamples);
87 if (numSamples > sbank.length - bank_start) {
89 sbank = new short[512 * 512];
91 buf = new DataBufferUShort(sbank, numSamples, bank_start);
92 bank_start += numSamples;
94 } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
95 if (bbank == null || numSamples > 512 * 512 / 3) {
96 buf = new DataBufferByte(numSamples);
98 if (numSamples > bbank.length - bank_start) {
100 bbank = new byte[512 * 512];
102 buf = new DataBufferByte(bbank, numSamples, bank_start);
103 bank_start += numSamples;
105 } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
106 if (ibank == null || numSamples > 512 * 512 / 3) {
107 buf = new DataBufferInt(numSamples);
109 if (numSamples > ibank.length - bank_start) {
111 ibank = new int[512 * 512];
113 buf = new DataBufferInt(ibank, numSamples, bank_start);
114 bank_start += numSamples;
117 i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false, emptyHashtable);
122 /** used to avoid garbage creation with getClipBounds() */
123 private static Rectangle clipBounds = new Rectangle();
125 // 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
126 // BufferedImage instances which get stored in an array deep inside Graphics2D, which the JDK does a LINEAR SCAN through. Aarrgh.
127 // This is rumored to be fixed in JDK 1.4.
128 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) {
130 if (dx1 == dx2 || dy1 == dy2) return;
132 if (dx2 - dx1 != sx2 - sx1 || dy2 - dy1 != sy2 - sy1)
133 g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
135 // fastpath for images that do not need to be scaled
136 if (i instanceof BufferedImage) {
137 BufferedImage b = (BufferedImage)i;
139 if (dx2 - dx1 != b.getWidth(null) || dy2 - dy1 != b.getHeight(null)) {
140 b = b.getSubimage(sx1, sy1, sx2 - sx1, sy2 - sy1);
146 g.drawImage(b, dx1, dy1, o);
150 // workaround for a wierd JDK bug
151 boolean skip = false;
152 try { g.getClipBounds(clipBounds); } catch (NullPointerException n) { skip = true; }
154 g.clipRect(dx1, dy1, dx2 - dx1, dy2 - dy1);
155 g.drawImage(i, dx1 - sx1, dy1 - sy1, o);
157 if (!skip) g.setClip(clipBounds.x, clipBounds.y, clipBounds.x + clipBounds.width, clipBounds.y + clipBounds.height);
162 protected org.xwt.Weak _getWeak(Object o) { return new Java12Weak(o); }
163 private static class Java12Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
164 public Java12Weak(Object o) { super(o); }
167 private String __getClipBoard() { return super._getClipBoard(); }
168 protected String _getClipBoard() {
169 return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
170 public Object run() { return __getClipBoard(); }
174 private void __setClipBoard(String s) { super._setClipBoard(s); }
175 protected void _setClipBoard(final String s) {
176 java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
177 public Object run() {
184 protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
186 protected void _newBrowserWindow(String url) {
187 if (Main.applet == null) {
188 if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
191 if (Log.on) Log.log(this, "asking browser to show URL " + url);
193 Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
194 } catch (MalformedURLException e) {
195 if (Log.on) Log.log(this, e);
199 /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
200 protected void _criticalAbort(String message) {
201 if (Log.on) Log.log(this, message);
202 new Semaphore().block();