split Java2 into Java2/Java4
[org.ibex.core.git] / src / org / ibex / plat / Java2.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.plat;
6
7 import java.awt.*;
8 import java.awt.event.*;
9 import java.awt.image.*;
10 import java.net.*;
11 import java.util.*;
12 import org.ibex.util.*;
13 import java.lang.reflect.*;
14 import org.ibex.graphics.*;
15 import org.ibex.core.*;
16 import org.ibex.net.*;
17
18 /** Platform class for most reasonable Java1.2+ Java2s */
19 public class Java2 extends AWT {
20
21     protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
22     public Java2() {
23         // disable the focus manager so we can intercept the tab key
24         javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
25                 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
26                 public void focusPreviousComponent(Component aComponent) { }
27                 public void focusNextComponent(Component aComponent) { }
28             });
29     }
30
31     /** this is done with reflection in case a new plugin comes out that doesn't let us pull the sun.plugin.* trick */
32     protected synchronized org.ibex.net.HTTP.Proxy _detectProxy() {
33         return (org.ibex.net.HTTP.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
34                 public Object run() { try {
35                     org.ibex.net.HTTP.Proxy pi = new org.ibex.net.HTTP.Proxy();
36                     
37                     Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
38                     Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
39                     Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
40                     
41                     Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
42                     Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
43                     Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.ibex.org") });
44                     
45                     Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
46                     
47                     if (((Boolean)ProxyInfo.getMethod("isSocksUsed",
48                                                       new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
49                         pi.socksProxyHost =
50                             (String)ProxyInfo.getMethod("getSocksProxy",
51                                                         new Class[] { }).invoke(proxyInfo, new Object[] { });
52                         pi.socksProxyPort =
53                             ((Integer)ProxyInfo.getMethod("getSocksPort",
54                                                           new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
55                     }
56                     
57                     if (((Boolean)ProxyInfo.getMethod("isProxyUsed",
58                                                       new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
59                         pi.httpProxyHost =
60                             (String)ProxyInfo.getMethod("getProxy",
61                                                         new Class[] { }).invoke(proxyInfo, new Object[] { });
62                         pi.httpProxyPort =
63                             ((Integer)ProxyInfo.getMethod("getPort",
64                                                           new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
65                     }
66                     
67                     if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
68                     else return null;
69                     
70                 } catch (Throwable e) {
71                     if (Log.on) Log.info(this, "No proxy information found in Java Plugin classes");
72                     return null;
73                 }
74                 }});
75     }
76
77     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Java2PixelBuffer(w, h); }
78     protected Surface __createSurface(final Box root, final boolean framed) { return new Java2Surface(root, framed); }
79     protected Surface _createSurface(final Box root, final boolean framed) {
80         return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
81                 public Object run() { return __createSurface(root, framed); } }); }
82
83
84     // Inner Classes //////////////////////////////////////////////////////////////////
85
86     private static Cursor invisibleCursor =
87         Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
88                                                        new Point(1, 1), "invisible");
89
90     public static class Java2Surface extends AWTSurface {
91         public Java2Surface(Box root, boolean framed) { super(root, framed); }
92
93         protected void _setMinimized(boolean b) {
94             if (frame == null) Log.info(this, "JDK 1.2 can only minimize frames, not windows");
95             else if (b) frame.setState(java.awt.Frame.ICONIFIED);
96             else frame.setState(java.awt.Frame.NORMAL);
97         }
98         public void syncCursor() {
99             if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
100             else super.syncCursor();
101         }
102     }
103
104     protected static class Java2PixelBuffer extends AWTPixelBuffer {
105         private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
106         private static Hashtable emptyHashtable = new Hashtable();
107         private static short[] sbank = null;
108         private static int[] ibank = null;
109         private static byte[] bbank = null;
110         private static int bank_start = 0;
111         private WritableRaster raster = null;
112         private SampleModel sm = null;
113         private DataBuffer buf = null;
114
115         // this doens't seem to work on Windows
116         public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
117             Image i2 = ((AWTGlyph)source).getImage();
118             Graphics2D g2 = (Graphics2D)i.getGraphics();
119             g2.setComposite(AlphaComposite.DstOut);
120             g2.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
121             g2.drawImage(i2, dx, dy, null);
122             g2.setComposite(AlphaComposite.DstOver);
123             g2.setColor(new java.awt.Color((rgb & 0x00FF0000) >> 16, (rgb & 0x0000FF00) >> 8, (rgb & 0x000000FF)));
124             g2.fillRect(dx, dy, cx2 - dx, cy2 - dy);
125             g2.drawImage(i2, 0, 0, null);
126             g2.setClip(0, 0, i.getWidth(null), i.getHeight(null));
127         }
128
129         public Java2PixelBuffer(int w, int h) {
130             super(w,h);
131             sm = cm.createCompatibleSampleModel(w, h);
132             int numSamples = w * h * sm.getNumDataElements();
133             if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
134                 if (sbank == null || numSamples > 512 * 512 / 3) {
135                     buf = new DataBufferUShort(numSamples);
136                 } else {
137                     if (numSamples > sbank.length - bank_start) {
138                         bank_start = 0;
139                         sbank = new short[512 * 512];
140                     }
141                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
142                     bank_start += numSamples;
143                 }
144             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
145                 if (bbank == null || numSamples > 512 * 512 / 3) {
146                     buf = new DataBufferByte(numSamples);
147                 } else {
148                     if (numSamples > bbank.length - bank_start) {
149                         bank_start = 0;
150                         bbank = new byte[512 * 512];
151                     }
152                     buf = new DataBufferByte(bbank, numSamples, bank_start);
153                     bank_start += numSamples;
154                 }
155             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
156                 if (ibank == null || numSamples > 512 * 512 / 3) {
157                     buf = new DataBufferInt(numSamples);
158                 } else {
159                     if (numSamples > ibank.length - bank_start) {
160                         bank_start = 0;
161                         ibank = new int[512 * 512];
162                     }
163                     buf = new DataBufferInt(ibank, numSamples, bank_start);
164                     bank_start += numSamples;
165                 }
166             }
167             raster = Raster.createWritableRaster(sm, buf, null);
168             i = new BufferedImage(cm, raster, false,  emptyHashtable);
169             g = i.getGraphics();
170         }
171     }
172 }