c277efa45c9a42876d7cae1189229329d10c269e
[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     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) {
169         if (dx1 == dx2 || dy1 == dy2) return;
170         g.drawImage(i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, o);
171     }
172
173     protected org.xwt.Weak _getWeak(Object o) { return new Java12Weak(o); }
174     private static class Java12Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
175         public Java12Weak(Object o) { super(o); }
176     }
177
178     private String __getClipBoard() { return super._getClipBoard(); }
179     protected String _getClipBoard() {
180         return (String)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
181                 public Object run() { return __getClipBoard();  }
182             });
183     }
184
185     private void __setClipBoard(String s) { super._setClipBoard(s); }
186     protected void _setClipBoard(final String s) {
187         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
188                 public Object run() {
189                     __setClipBoard(s);
190                     return null;
191                 }
192             });
193     }
194
195     protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
196
197     protected void _newBrowserWindow(String url) {
198         if (Main.applet == null) {
199             if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
200             return;
201         }
202         if (Log.on) Log.log(this, "asking browser to show URL " + url);
203         try {
204             Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
205         } catch (MalformedURLException e) {
206             if (Log.on) Log.log(this, e);
207         }
208     }
209
210 }