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