2002/04/30 21:15:33
[org.ibex.core.git] / src / org / xwt / plat / Java12.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.plat;
3
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.awt.image.*;
7 import java.awt.datatransfer.*;
8 import java.net.*;
9 import java.io.*;
10 import java.util.*;
11 import org.xwt.util.*;
12 import org.xwt.*;
13
14 // FEATURE: Java 1.4 allows undecorated frames and can maximize windows
15
16 /** Platform class for most reasonable Java1.2+ JVMs */
17 public class Java12 extends AWT {
18
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() {
22                 public Object run() {
23                     try {
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);
28                         return null;
29                     }
30                 }
31             });
32     }
33     
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); }
38             });
39     }
40
41     // Inner Classes //////////////////////////////////////////////////////////////////
42
43     protected static class Java12Surface extends AWTSurface {
44         
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();
49
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);
52             }
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);
55         }
56
57         protected void _setMinimized(boolean b) {
58             if (frame == null) {
59                 if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
60                 return;
61             }
62             if (b) frame.setState(java.awt.Frame.ICONIFIED);
63             else frame.setState(java.awt.Frame.NORMAL);
64         }
65     }
66
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;
74         
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);
77         }
78         
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);
86                 } else {
87                     if (numSamples > sbank.length - bank_start) {
88                         bank_start = 0;
89                         sbank = new short[512 * 512];
90                     }
91                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
92                     bank_start += numSamples;
93                 }
94             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
95                 if (bbank == null || numSamples > 512 * 512 / 3) {
96                     buf = new DataBufferByte(numSamples);
97                 } else {
98                     if (numSamples > bbank.length - bank_start) {
99                         bank_start = 0;
100                         bbank = new byte[512 * 512];
101                     }
102                     buf = new DataBufferByte(bbank, numSamples, bank_start);
103                     bank_start += numSamples;
104                 }
105             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
106                 if (ibank == null || numSamples > 512 * 512 / 3) {
107                     buf = new DataBufferInt(numSamples);
108                 } else {
109                     if (numSamples > ibank.length - bank_start) {
110                         bank_start = 0;
111                         ibank = new int[512 * 512];
112                     }
113                     buf = new DataBufferInt(ibank, numSamples, bank_start);
114                     bank_start += numSamples;
115                 }
116             }
117             i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false,  emptyHashtable);
118             g = i.getGraphics();
119         }
120     }
121
122     /** used to avoid garbage creation with getClipBounds() */
123     private static Rectangle clipBounds = new Rectangle();
124     
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) {
129         
130         if (dx1 == dx2 || dy1 == dy2) return;
131
132         if (dx2 - dx1 != sx2 - sx1 || dy2 - dy1 != sy2 - sy1)
133             g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
134         else {
135             // fastpath for images that do not need to be scaled
136             if (i instanceof BufferedImage) {
137                 BufferedImage b = (BufferedImage)i;
138                 
139                 if (dx2 - dx1 != b.getWidth(null) || dy2 - dy1 != b.getHeight(null)) {
140                     b = b.getSubimage(sx1, sy1, sx2 - sx1, sy2 - sy1);
141                     sx2 = sx2 - sx1;
142                     sy2 = sy2 - sy1;
143                     sx1 = 0;
144                     sy1 = 0;
145                 }
146                 g.drawImage(b, dx1, dy1, o);
147                 
148             } else {
149
150                 // workaround for a wierd JDK bug
151                 boolean skip = false;
152                 try { g.getClipBounds(clipBounds); } catch (NullPointerException n) { skip = true; }
153
154                 g.clipRect(dx1, dy1, dx2 - dx1, dy2 - dy1);
155                 g.drawImage(i, dx1 - sx1, dy1 - sy1, o);
156
157                 if (!skip) g.setClip(clipBounds.x, clipBounds.y, clipBounds.x + clipBounds.width, clipBounds.y + clipBounds.height);
158             }
159         }
160     }
161
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); }
165     }
166
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();  }
171             });
172     }
173
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() {
178                     __setClipBoard(s);
179                     return null;
180                 }
181             });
182     }
183
184     protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
185
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()");
189             return;
190         }
191         if (Log.on) Log.log(this, "asking browser to show URL " + url);
192         try {
193             Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
194         } catch (MalformedURLException e) {
195             if (Log.on) Log.log(this, e);
196         }
197     }
198
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();
203     }
204
205 }