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