694a9eb50be0802ab8bba2e1cc7937ce548d3ed7
[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 import java.lang.reflect.*;
14
15
16 /** Platform class for most reasonable Java1.2+ JVMs */
17 public class Java12 extends AWT {
18
19     /** 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 */
20     protected synchronized HTTP.ProxyInfo _detectProxy() {
21         return (HTTP.ProxyInfo)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
22                 public Object run() {
23                     try {
24                         HTTP.ProxyInfo pi = new HTTP.ProxyInfo();
25                         
26                         Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
27                         Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
28                         Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
29                         
30                         Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
31                         Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
32                         Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.xwt.org") });
33                         
34                         Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
35                         
36                         if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
37                             pi.socksProxyHost =
38                                 (String)ProxyInfo.getMethod("getSocksProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
39                             pi.socksProxyPort =
40                                 ((Integer)ProxyInfo.getMethod("getSocksPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
41                         }
42                         
43                         if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
44                             pi.httpProxyHost =
45                                 (String)ProxyInfo.getMethod("getProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
46                             pi.httpProxyPort =
47                                 ((Integer)ProxyInfo.getMethod("getPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
48                         }
49                         
50                         if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
51                         else return null;
52
53                     } catch (Throwable e) {
54                         if (Log.on) Log.log(this, "exception while querying sun.plugin.protocol.PluginProxyHandler: " + e);
55                         return null;
56                     }
57                 }});
58     }
59
60     protected Socket __getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
61         return super._getSocket(host, port, ssl, negotiate);
62     }
63     protected Socket _getSocket(final String host, final int port, final boolean ssl, final boolean negotiate) throws IOException {
64         return (Socket)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
65                 public Object run() {
66                     try {
67                         return __getSocket(host, port, ssl, negotiate);
68                     } catch (Exception e) {
69                         if (Log.on) Log.log(Java12.class, "Error attempting to create socket");
70                         if (Log.on) Log.log(Java12.class, e);
71                         return null;
72                     }
73                 }
74             });
75     }
76     
77     protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java12DoubleBuffer(w, h); }
78     protected Surface _createSurface(final Box root, final boolean framed) {
79         return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
80                 public Object run() { return new Java12Surface(root, framed); }
81             });
82     }
83
84     // Inner Classes //////////////////////////////////////////////////////////////////
85
86     protected static class Java12Surface extends AWTSurface {
87         
88         public Java12Surface(Box root, boolean framed) { super(root, framed); }
89         public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
90             if (ourGraphics == null) {
91                 ourGraphics = window.getGraphics();
92
93                 // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
94                 ourGraphics.setClip(insets.left, insets.top, width + insets.left, height + insets.top);
95             }
96             _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
97                          sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
98         }
99
100         protected void _setMinimized(boolean b) {
101             if (frame == null) {
102                 if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
103                 return;
104             }
105             if (b) frame.setState(java.awt.Frame.ICONIFIED);
106             else frame.setState(java.awt.Frame.NORMAL);
107         }
108     }
109
110     protected static class Java12DoubleBuffer extends AWTDoubleBuffer {
111         private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
112         private static Hashtable emptyHashtable = new Hashtable();
113         private static short[] sbank = null;
114         private static int[] ibank = null;
115         private static byte[] bbank = null;
116         private static int bank_start = 0;
117         
118         public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
119             _doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
120         }
121         
122         public Java12DoubleBuffer(int w, int h) {
123             SampleModel sm = cm.createCompatibleSampleModel(w, h);
124             int numSamples = w * h * sm.getNumDataElements();
125             DataBuffer buf = null;
126             if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
127                 if (sbank == null || numSamples > 512 * 512 / 3) {
128                     buf = new DataBufferUShort(numSamples);
129                 } else {
130                     if (numSamples > sbank.length - bank_start) {
131                         bank_start = 0;
132                         sbank = new short[512 * 512];
133                     }
134                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
135                     bank_start += numSamples;
136                 }
137             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
138                 if (bbank == null || numSamples > 512 * 512 / 3) {
139                     buf = new DataBufferByte(numSamples);
140                 } else {
141                     if (numSamples > bbank.length - bank_start) {
142                         bank_start = 0;
143                         bbank = new byte[512 * 512];
144                     }
145                     buf = new DataBufferByte(bbank, numSamples, bank_start);
146                     bank_start += numSamples;
147                 }
148             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
149                 if (ibank == null || numSamples > 512 * 512 / 3) {
150                     buf = new DataBufferInt(numSamples);
151                 } else {
152                     if (numSamples > ibank.length - bank_start) {
153                         bank_start = 0;
154                         ibank = new int[512 * 512];
155                     }
156                     buf = new DataBufferInt(ibank, numSamples, bank_start);
157                     bank_start += numSamples;
158                 }
159             }
160             i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false,  emptyHashtable);
161             g = i.getGraphics();
162         }
163     }
164
165     /** used to avoid garbage creation with getClipBounds() */
166     private static Rectangle clipBounds = new Rectangle();
167     
168     // 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
169     // BufferedImage instances which get stored in an array deep inside Graphics2D, which the JDK does a LINEAR SCAN through. Aarrgh.
170     // This is rumored to be fixed in JDK 1.4.
171     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) {
172         
173         if (dx1 == dx2 || dy1 == dy2) return;
174
175         if (dx2 - dx1 != sx2 - sx1 || dy2 - dy1 != sy2 - sy1)
176             g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
177         else {
178             // fastpath for images that do not need to be scaled
179             if (i instanceof BufferedImage) {
180                 BufferedImage b = (BufferedImage)i;
181                 
182                 if (dx2 - dx1 != b.getWidth(null) || dy2 - dy1 != b.getHeight(null)) {
183                     b = b.getSubimage(sx1, sy1, sx2 - sx1, sy2 - sy1);
184                     sx2 = sx2 - sx1;
185                     sy2 = sy2 - sy1;
186                     sx1 = 0;
187                     sy1 = 0;
188                 }
189                 g.drawImage(b, dx1, dy1, o);
190                 
191             } else {
192
193                 // workaround for a wierd JDK bug
194                 boolean skip = false;
195                 try { g.getClipBounds(clipBounds); } catch (NullPointerException n) { skip = true; }
196
197                 g.clipRect(dx1, dy1, dx2 - dx1, dy2 - dy1);
198                 g.drawImage(i, dx1 - sx1, dy1 - sy1, o);
199
200                 if (!skip) g.setClip(clipBounds.x, clipBounds.y, clipBounds.x + clipBounds.width, clipBounds.y + clipBounds.height);
201             }
202         }
203     }
204
205     protected org.xwt.Weak _getWeak(Object o) { return new Java12Weak(o); }
206     private static class Java12Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
207         public Java12Weak(Object o) { super(o); }
208     }
209
210     private String __getClipBoard() { return super._getClipBoard(); }
211     protected String _getClipBoard() {
212         return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
213                 public Object run() { return __getClipBoard();  }
214             });
215     }
216
217     private void __setClipBoard(String s) { super._setClipBoard(s); }
218     protected void _setClipBoard(final String s) {
219         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
220                 public Object run() {
221                     __setClipBoard(s);
222                     return null;
223                 }
224             });
225     }
226
227     protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
228
229     protected void _newBrowserWindow(String url) {
230         if (Main.applet == null) {
231             if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
232             return;
233         }
234         if (Log.on) Log.log(this, "asking browser to show URL " + url);
235         try {
236             Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
237         } catch (MalformedURLException e) {
238             if (Log.on) Log.log(this, e);
239         }
240     }
241
242     /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
243     protected void _criticalAbort(String message) {
244         if (Log.on) Log.log(this, message);
245         new Semaphore().block();
246     }
247
248 }