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