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