2002/08/16 23:39:03
[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     public Java12() {
20         // disable the focus manager so we can intercept the tab key
21         javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
22                 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
23                 public void focusPreviousComponent(Component aComponent) { }
24                 public void focusNextComponent(Component aComponent) { }
25             });
26     }
27
28     /** 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 */
29     protected synchronized org.xwt.Proxy _detectProxy() {
30         return (org.xwt.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
31                 public Object run() {
32                     try {
33                         org.xwt.Proxy pi = new org.xwt.Proxy();
34                         
35                         Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
36                         Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
37                         Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
38                         
39                         Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
40                         Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
41                         Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.xwt.org") });
42                         
43                         Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
44                         
45                         if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
46                             pi.socksProxyHost =
47                                 (String)ProxyInfo.getMethod("getSocksProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
48                             pi.socksProxyPort =
49                                 ((Integer)ProxyInfo.getMethod("getSocksPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
50                         }
51                         
52                         if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
53                             pi.httpProxyHost =
54                                 (String)ProxyInfo.getMethod("getProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
55                             pi.httpProxyPort =
56                                 ((Integer)ProxyInfo.getMethod("getPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
57                         }
58                         
59                         if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
60                         else return null;
61
62                     } catch (Throwable e) {
63                         if (Log.on) Log.log(this, "exception while querying sun.plugin.protocol.PluginProxyHandler: " + e);
64                         return null;
65                     }
66                 }});
67     }
68
69     protected Socket __getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
70         return super._getSocket(host, port, ssl, negotiate);
71     }
72     protected Socket _getSocket(final String host, final int port, final boolean ssl, final boolean negotiate) throws IOException {
73         return (Socket)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
74                 public Object run() {
75                     try {
76                         return __getSocket(host, port, ssl, negotiate);
77                     } catch (Exception e) {
78                         if (Log.on) Log.log(Java12.class, "Error attempting to create socket");
79                         if (Log.on) Log.log(Java12.class, e);
80                         return null;
81                     }
82                 }
83             });
84     }
85     
86     protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Java12DoubleBuffer(w, h); }
87     protected Surface _createSurface(final Box root, final boolean framed) {
88         return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
89                 public Object run() { return new Java12Surface(root, framed); }
90             });
91     }
92
93     // Inner Classes //////////////////////////////////////////////////////////////////
94
95     private static Cursor invisibleCursor =
96         Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
97                                                        new Point(1, 1), "invisible");
98
99     protected static class Java12Surface extends AWTSurface {
100         
101         public Java12Surface(Box root, boolean framed) { super(root, framed); }
102         public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
103             if (ourGraphics == null) {
104                 ourGraphics = window.getGraphics();
105
106                 // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
107                 ourGraphics.setClip(insets.left, insets.top, width + insets.left, height + insets.top);
108             }
109             _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
110                          sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
111         }
112
113         protected void _setMinimized(boolean b) {
114             if (frame == null) {
115                 if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
116                 return;
117             }
118             if (b) frame.setState(java.awt.Frame.ICONIFIED);
119             else frame.setState(java.awt.Frame.NORMAL);
120         }
121
122         public void syncCursor() {
123             if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
124             else super.syncCursor();
125         }
126     }
127
128     protected static class Java12DoubleBuffer extends AWTDoubleBuffer {
129         private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
130         private static Hashtable emptyHashtable = new Hashtable();
131         private static short[] sbank = null;
132         private static int[] ibank = null;
133         private static byte[] bbank = null;
134         private static int bank_start = 0;
135         
136         public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
137             _doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
138         }
139         
140         public Java12DoubleBuffer(int w, int h) {
141             SampleModel sm = cm.createCompatibleSampleModel(w, h);
142             int numSamples = w * h * sm.getNumDataElements();
143             DataBuffer buf = null;
144             if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
145                 if (sbank == null || numSamples > 512 * 512 / 3) {
146                     buf = new DataBufferUShort(numSamples);
147                 } else {
148                     if (numSamples > sbank.length - bank_start) {
149                         bank_start = 0;
150                         sbank = new short[512 * 512];
151                     }
152                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
153                     bank_start += numSamples;
154                 }
155             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
156                 if (bbank == null || numSamples > 512 * 512 / 3) {
157                     buf = new DataBufferByte(numSamples);
158                 } else {
159                     if (numSamples > bbank.length - bank_start) {
160                         bank_start = 0;
161                         bbank = new byte[512 * 512];
162                     }
163                     buf = new DataBufferByte(bbank, numSamples, bank_start);
164                     bank_start += numSamples;
165                 }
166             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
167                 if (ibank == null || numSamples > 512 * 512 / 3) {
168                     buf = new DataBufferInt(numSamples);
169                 } else {
170                     if (numSamples > ibank.length - bank_start) {
171                         bank_start = 0;
172                         ibank = new int[512 * 512];
173                     }
174                     buf = new DataBufferInt(ibank, numSamples, bank_start);
175                     bank_start += numSamples;
176                 }
177             }
178             i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false,  emptyHashtable);
179             g = i.getGraphics();
180         }
181     }
182
183     /** used to avoid garbage creation with getClipBounds() */
184     private static Rectangle clipBounds = new Rectangle();
185     
186     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) {
187         if (dx1 == dx2 || dy1 == dy2) return;
188         g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
189     }
190
191     protected org.xwt.Weak _getWeak(Object o) { return new Java12Weak(o); }
192     private static class Java12Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
193         public Java12Weak(Object o) { super(o); }
194     }
195
196     private String __getClipBoard() { return super._getClipBoard(); }
197     protected String _getClipBoard() {
198         return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
199                 public Object run() { return __getClipBoard();  }
200             });
201     }
202
203     private void __setClipBoard(String s) { super._setClipBoard(s); }
204     protected void _setClipBoard(final String s) {
205         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
206                 public Object run() {
207                     __setClipBoard(s);
208                     return null;
209                 }
210             });
211     }
212
213     protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
214
215     protected void _newBrowserWindow(String url) {
216         if (Main.applet == null) {
217             if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
218             return;
219         }
220         if (Log.on) Log.log(this, "asking browser to show URL " + url);
221         try {
222             Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
223         } catch (MalformedURLException e) {
224             if (Log.on) Log.log(this, e);
225         }
226     }
227
228 }