questionable patch: merge of a lot of stuff from the svg branch
[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.awt.geom.*;
11 import java.net.*;
12 import java.util.*;
13 import org.ibex.util.*;
14 import java.lang.reflect.*;
15 import org.ibex.graphics.*;
16 import org.ibex.core.*;
17 import org.ibex.net.*;
18
19 /** Platform class for most reasonable Java1.2+ Java2s */
20 public class Java2 extends AWT {
21
22     protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
23     public Java2() {
24         // disable the focus manager so we can intercept the tab key
25         javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
26                 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
27                 public void focusPreviousComponent(Component aComponent) { }
28                 public void focusNextComponent(Component aComponent) { }
29             });
30     }
31
32     /** this is done with reflection in case a new plugin comes out that doesn't let us pull the sun.plugin.* trick */
33     protected synchronized org.ibex.net.HTTP.Proxy _detectProxy() {
34         return (org.ibex.net.HTTP.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
35                 public Object run() { try {
36                     org.ibex.net.HTTP.Proxy pi = new org.ibex.net.HTTP.Proxy();
37                     
38                     Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
39                     Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
40                     Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
41                     
42                     Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
43                     Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
44                     Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.ibex.org") });
45                     
46                     Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
47                     
48                     if (((Boolean)ProxyInfo.getMethod("isSocksUsed",
49                                                       new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
50                         pi.socksProxyHost =
51                             (String)ProxyInfo.getMethod("getSocksProxy",
52                                                         new Class[] { }).invoke(proxyInfo, new Object[] { });
53                         pi.socksProxyPort =
54                             ((Integer)ProxyInfo.getMethod("getSocksPort",
55                                                           new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
56                     }
57                     
58                     if (((Boolean)ProxyInfo.getMethod("isProxyUsed",
59                                                       new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
60                         pi.httpProxyHost =
61                             (String)ProxyInfo.getMethod("getProxy",
62                                                         new Class[] { }).invoke(proxyInfo, new Object[] { });
63                         pi.httpProxyPort =
64                             ((Integer)ProxyInfo.getMethod("getPort",
65                                                           new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
66                     }
67                     
68                     if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
69                     else return null;
70                     
71                 } catch (Throwable e) {
72                     if (Log.on) Log.info(this, "No proxy information found in Java Plugin classes");
73                     return null;
74                 }
75                 }});
76     }
77
78     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Java2PixelBuffer(w, h); }
79     protected Surface __createSurface(final Box root, final boolean framed) { return new Java2Surface(root, framed); }
80     protected Surface _createSurface(final Box root, final boolean framed) {
81         return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
82                 public Object run() { return __createSurface(root, framed); } }); }
83
84
85     // Inner Classes //////////////////////////////////////////////////////////////////
86
87     private static Cursor invisibleCursor =
88         Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
89                                                        new Point(1, 1), "invisible");
90
91     public static class Java2Surface extends AWTSurface {
92         public Java2Surface(Box root, boolean framed) { super(root, framed); }
93         public PixelBuffer getPixelBuffer() { return pb==null?(pb=new Java2PixelBuffer(this)):pb; }
94         protected void _setMinimized(boolean b) {
95             if (frame == null) Log.info(this, "JDK 1.2 can only minimize frames, not windows");
96             else if (b) frame.setState(java.awt.Frame.ICONIFIED);
97             else frame.setState(java.awt.Frame.NORMAL);
98         }
99         public void syncCursor() {
100             if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
101             else super.syncCursor();
102         }
103     }
104
105     protected static class Java2PixelBuffer extends AWTPixelBuffer {
106         private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
107         private static Hashtable emptyHashtable = new Hashtable();
108         private static short[] sbank = null;
109         private static int[] ibank = null;
110         private static byte[] bbank = null;
111         private static int bank_start = 0;
112         private WritableRaster raster = null;
113         private SampleModel sm = null;
114         private DataBuffer buf = null;
115
116         private static int[] xs = new int[65535];
117         private static int[] ys = new int[65535];
118         private static GeneralPath gp = new GeneralPath();
119
120         // this doens't seem to work on Windows
121         public void drawGlyph(org.ibex.graphics.Font.Glyph source, Affine a, Mesh h, int rgb, int bg) {
122             Image i = ((AWTGlyph)source).getImage();
123             Image i2 = ((AWTGlyph)source).i2;
124             if (((AWTGlyph)source).i2 == null) {
125                 ((AWTGlyph)source).i2 = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_ARGB);
126                 i2 = ((AWTGlyph)source).i2;
127                 i2.getGraphics().drawImage(i, 0, 0, null);
128             }
129             Graphics2D g = (Graphics2D)((AWTGlyph)source).i2.getGraphics();
130             g.setComposite(AlphaComposite.SrcIn);
131             g.setColor(new java.awt.Color((rgb & 0x00FF0000) >> 16, (rgb & 0x0000FF00) >> 8, (rgb & 0x000000FF)));
132             g.fillRect(0, 0, i2.getWidth(null), i2.getHeight(null));
133             Graphics2D g2 = (Graphics2D)getGraphics();
134             if (a!=null) g2.drawImage(i2, new java.awt.geom.AffineTransform(a.a, a.b, a.c, a.d, a.e, a.f), null);
135             else         g2.drawImage(i2, 0, 0, null);
136         }
137
138         public void fill(Mesh p, org.ibex.graphics.Paint paint) { fillStroke(p, paint, true, false); }
139         public void stroke(Mesh p, org.ibex.graphics.Paint paint) { fillStroke(p, paint, false, true); }
140         public void fillStroke(Mesh p, org.ibex.graphics.Paint paint, boolean fill, boolean stroke) {/*
141             if (g == null) g = getGraphics();
142             ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); 
143             int argb = ((org.ibex.graphics.Paint.SingleColorPaint)paint).color;
144             g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
145             gp.reset();
146             gp.setWindingRule(GeneralPath.WIND_EVEN_ODD);
147             for(int j=0; j<p.numcontours; j++) {
148                 if (p.contours[j+1] <= p.contours[j]) continue;
149                 gp.moveTo(p.x[p.contours[j]], p.y[p.contours[j]]);
150                 for(int i=p.contours[j]+1; i<p.contours[j+1]; i++) gp.lineTo(p.x[i], p.y[i]);
151                 gp.closePath();
152             }
153             if (fill) ((Graphics2D)g).fill(gp);
154             if (stroke) ((Graphics2D)g).draw(gp);
155                                                                                                      */}
156
157         public Java2PixelBuffer(Java2Surface s) { super(s); }
158         public Java2PixelBuffer(int w, int h) {
159             super(w,h);
160             sm = cm.createCompatibleSampleModel(w, h);
161             int numSamples = w * h * sm.getNumDataElements();
162             if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
163                 if (sbank == null || numSamples > 512 * 512 / 3) {
164                     buf = new DataBufferUShort(numSamples);
165                 } else {
166                     if (numSamples > sbank.length - bank_start) {
167                         bank_start = 0;
168                         sbank = new short[512 * 512];
169                     }
170                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
171                     bank_start += numSamples;
172                 }
173             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
174                 if (bbank == null || numSamples > 512 * 512 / 3) {
175                     buf = new DataBufferByte(numSamples);
176                 } else {
177                     if (numSamples > bbank.length - bank_start) {
178                         bank_start = 0;
179                         bbank = new byte[512 * 512];
180                     }
181                     buf = new DataBufferByte(bbank, numSamples, bank_start);
182                     bank_start += numSamples;
183                 }
184             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
185                 if (ibank == null || numSamples > 512 * 512 / 3) {
186                     buf = new DataBufferInt(numSamples);
187                 } else {
188                     if (numSamples > ibank.length - bank_start) {
189                         bank_start = 0;
190                         ibank = new int[512 * 512];
191                     }
192                     buf = new DataBufferInt(ibank, numSamples, bank_start);
193                     bank_start += numSamples;
194                 }
195             }
196             raster = Raster.createWritableRaster(sm, buf, null);
197             i = new BufferedImage(cm, raster, false,  emptyHashtable);
198             g = i.getGraphics();
199         }
200     }
201 }