f478b5872956032e8d4460587396b98188b78fae
[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, "No proxy information found in Java Plugin classes");
87                         return null;
88                     }
89                 }});
90     }
91
92     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Java2PixelBuffer(w, h); }
93     protected Surface _createSurface(final Box root, final boolean framed) {
94         return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
95                 public Object run() {
96                     if (isJava14) {
97                         try {
98                              // weaken the binding here to avoid link errors on 1.3.x
99                              Class java14SurfaceClass = Class.forName(Java2.class.getName() + "$Java14Surface");
100                              Constructor ctor = java14SurfaceClass.getConstructor(new Class[] { Box.class, Boolean.TYPE });
101                              return (Surface)ctor.newInstance(new Object[] { root, new Boolean(framed) });
102                         } catch (Exception e) {
103                             Log.log(this, e);
104                             throw new LinkageError("error: " + e);
105                         }
106                     } else {
107                         return new Java2Surface(root, framed);
108                     }
109                 }
110             });
111     }
112
113     // Inner Classes //////////////////////////////////////////////////////////////////
114
115     private static Cursor invisibleCursor =
116         Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
117                                                        new Point(1, 1), "invisible");
118
119     protected static class Java2Surface extends AWTSurface {
120         
121         public Java2Surface(Box root, boolean framed) { super(root, framed); }
122
123         protected void _setMinimized(boolean b) {
124             if (frame == null) Log.log(this, "JDK 1.2 can only minimize frames, not windows");
125             else if (b) frame.setState(java.awt.Frame.ICONIFIED);
126             else frame.setState(java.awt.Frame.NORMAL);
127         }
128
129         public void syncCursor() {
130             if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
131             else super.syncCursor();
132         }
133     }
134
135     protected static class Java14Surface extends Java2Surface implements WindowStateListener {
136         public Java14Surface(Box root, boolean framed) {
137             super(root, true);
138             // JDK1.4 doesn't like java.lang.Window's...
139             if (!framed) ((Frame)window).setUndecorated(true);
140             window.addWindowStateListener(this);
141             window.setVisible(true);
142         }
143
144         protected void makeVisible() { }
145         
146         protected void _setMaximized(boolean m) {
147             if (frame == null) {
148                 if (Log.on) Log.log(this, "JDK 1.4 can only maximize frames, not windows");
149                 return;
150             }
151             frame.setExtendedState(m ? Frame.MAXIMIZED_BOTH : (minimized ? Frame.ICONIFIED : Frame.NORMAL));
152         }
153         protected void _setMinimized(boolean m) {
154             if (frame == null) {
155                 if (Log.on) Log.log(this, "JDK 1.4 can only minimize frames, not windows");
156                 return;
157             }
158             frame.setExtendedState(m ? Frame.ICONIFIED : (maximized ? Frame.MAXIMIZED_BOTH : Frame.NORMAL));
159         }
160         public void windowStateChanged(WindowEvent e) {
161             if (e.getOldState() != e.getNewState()) {
162                 if ((e.getNewState() & Frame.MAXIMIZED_BOTH) != 0) Maximized(true);
163                 else if (((e.getOldState() & Frame.MAXIMIZED_BOTH) != 0) && (e.getNewState() & Frame.MAXIMIZED_BOTH) == 0)
164                     Maximized(false);
165             }
166         }
167     }
168
169     protected static class Java2PixelBuffer extends AWTPixelBuffer {
170         private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
171         private static Hashtable emptyHashtable = new Hashtable();
172         private static short[] sbank = null;
173         private static int[] ibank = null;
174         private static byte[] bbank = null;
175         private static int bank_start = 0;
176         private WritableRaster raster = null;
177         private SampleModel sm = null;
178         private DataBuffer buf = null;
179
180         public void drawPictureAlphaOnly(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
181             AWTPicture src = (AWTPicture)source;
182             Graphics2D g2 = (Graphics2D)i.getGraphics();
183             g2.setComposite(AlphaComposite.DstOut);
184             g2.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
185             g2.drawImage(src.i, dx, dy, null);
186             g2.setComposite(AlphaComposite.DstOver);
187             g2.setColor(new Color((rgb & 0x00FF0000) >> 16, (rgb & 0x0000FF00) >> 8, (rgb & 0x000000FF)));
188             g2.fillRect(dx, dy, cx2 - dx, cy2 - dy);
189             g2.drawImage(i, 0, 0, null);
190             g2.setClip(0, 0, i.getWidth(null), i.getHeight(null));
191         }
192
193         public Java2PixelBuffer(int w, int h) {
194             sm = cm.createCompatibleSampleModel(w, h);
195             int numSamples = w * h * sm.getNumDataElements();
196             if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
197                 if (sbank == null || numSamples > 512 * 512 / 3) {
198                     buf = new DataBufferUShort(numSamples);
199                 } else {
200                     if (numSamples > sbank.length - bank_start) {
201                         bank_start = 0;
202                         sbank = new short[512 * 512];
203                     }
204                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
205                     bank_start += numSamples;
206                 }
207             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
208                 if (bbank == null || numSamples > 512 * 512 / 3) {
209                     buf = new DataBufferByte(numSamples);
210                 } else {
211                     if (numSamples > bbank.length - bank_start) {
212                         bank_start = 0;
213                         bbank = new byte[512 * 512];
214                     }
215                     buf = new DataBufferByte(bbank, numSamples, bank_start);
216                     bank_start += numSamples;
217                 }
218             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
219                 if (ibank == null || numSamples > 512 * 512 / 3) {
220                     buf = new DataBufferInt(numSamples);
221                 } else {
222                     if (numSamples > ibank.length - bank_start) {
223                         bank_start = 0;
224                         ibank = new int[512 * 512];
225                     }
226                     buf = new DataBufferInt(ibank, numSamples, bank_start);
227                     bank_start += numSamples;
228                 }
229             }
230             raster = Raster.createWritableRaster(sm, buf, null);
231             i = new BufferedImage(cm, raster, false,  emptyHashtable);
232             g = i.getGraphics();
233         }
234     }
235
236     protected String getDescriptiveName() { return isJava14 ? "Java 1.4+ JVM" : "Java 1.2+ JVM"; }
237
238 }