2002/08/07 04:34:49
[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     protected static class Java12Surface extends AWTSurface {
96         
97         public Java12Surface(Box root, boolean framed) { super(root, framed); }
98         public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
99             if (ourGraphics == null) {
100                 ourGraphics = window.getGraphics();
101
102                 // sometimes jdk1.4 doesn't set the clip properly when we're in the middle of a resize
103                 ourGraphics.setClip(insets.left, insets.top, width + insets.left, height + insets.top);
104             }
105             _doDrawImage(ourGraphics, ((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
106                          sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
107         }
108
109         protected void _setMinimized(boolean b) {
110             if (frame == null) {
111                 if (Log.on) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
112                 return;
113             }
114             if (b) frame.setState(java.awt.Frame.ICONIFIED);
115             else frame.setState(java.awt.Frame.NORMAL);
116         }
117     }
118
119     protected static class Java12DoubleBuffer extends AWTDoubleBuffer {
120         private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
121         private static Hashtable emptyHashtable = new Hashtable();
122         private static short[] sbank = null;
123         private static int[] ibank = null;
124         private static byte[] bbank = null;
125         private static int bank_start = 0;
126         
127         public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
128             _doDrawImage(g, ((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
129         }
130         
131         public Java12DoubleBuffer(int w, int h) {
132             SampleModel sm = cm.createCompatibleSampleModel(w, h);
133             int numSamples = w * h * sm.getNumDataElements();
134             DataBuffer buf = null;
135             if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
136                 if (sbank == null || numSamples > 512 * 512 / 3) {
137                     buf = new DataBufferUShort(numSamples);
138                 } else {
139                     if (numSamples > sbank.length - bank_start) {
140                         bank_start = 0;
141                         sbank = new short[512 * 512];
142                     }
143                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
144                     bank_start += numSamples;
145                 }
146             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
147                 if (bbank == null || numSamples > 512 * 512 / 3) {
148                     buf = new DataBufferByte(numSamples);
149                 } else {
150                     if (numSamples > bbank.length - bank_start) {
151                         bank_start = 0;
152                         bbank = new byte[512 * 512];
153                     }
154                     buf = new DataBufferByte(bbank, numSamples, bank_start);
155                     bank_start += numSamples;
156                 }
157             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
158                 if (ibank == null || numSamples > 512 * 512 / 3) {
159                     buf = new DataBufferInt(numSamples);
160                 } else {
161                     if (numSamples > ibank.length - bank_start) {
162                         bank_start = 0;
163                         ibank = new int[512 * 512];
164                     }
165                     buf = new DataBufferInt(ibank, numSamples, bank_start);
166                     bank_start += numSamples;
167                 }
168             }
169             i = new BufferedImage(cm, Raster.createWritableRaster(sm, buf, null), false,  emptyHashtable);
170             g = i.getGraphics();
171         }
172     }
173
174     /** used to avoid garbage creation with getClipBounds() */
175     private static Rectangle clipBounds = new Rectangle();
176     
177     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) {
178         if (dx1 == dx2 || dy1 == dy2) return;
179         g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
180     }
181
182     protected org.xwt.Weak _getWeak(Object o) { return new Java12Weak(o); }
183     private static class Java12Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
184         public Java12Weak(Object o) { super(o); }
185     }
186
187     private String __getClipBoard() { return super._getClipBoard(); }
188     protected String _getClipBoard() {
189         return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
190                 public Object run() { return __getClipBoard();  }
191             });
192     }
193
194     private void __setClipBoard(String s) { super._setClipBoard(s); }
195     protected void _setClipBoard(final String s) {
196         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
197                 public Object run() {
198                     __setClipBoard(s);
199                     return null;
200                 }
201             });
202     }
203
204     protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
205
206     protected void _newBrowserWindow(String url) {
207         if (Main.applet == null) {
208             if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
209             return;
210         }
211         if (Log.on) Log.log(this, "asking browser to show URL " + url);
212         try {
213             Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
214         } catch (MalformedURLException e) {
215             if (Log.on) Log.log(this, e);
216         }
217     }
218
219 }