Mesh replaces Polygon
[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         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         private static int[] xs = new int[65535];
116         private static int[] ys = new int[65535];
117         private static GeneralPath gp = new GeneralPath();
118
119         // this doens't seem to work on Windows
120         public void drawGlyph(org.ibex.graphics.Font.Glyph source, Affine a, Mesh h, int rgb, int bg) {
121             Image i = ((AWTGlyph)source).getImage();
122             Image i2 = ((AWTGlyph)source).i2;
123             if (((AWTGlyph)source).i2 == null) {
124                 ((AWTGlyph)source).i2 = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_ARGB);
125                 i2 = ((AWTGlyph)source).i2;
126                 i2.getGraphics().drawImage(i, 0, 0, null);
127             }
128             Graphics2D g = (Graphics2D)((AWTGlyph)source).i2.getGraphics();
129             g.setComposite(AlphaComposite.SrcIn);
130             g.setColor(new java.awt.Color((rgb & 0x00FF0000) >> 16, (rgb & 0x0000FF00) >> 8, (rgb & 0x000000FF)));
131             g.fillRect(0, 0, i2.getWidth(null), i2.getHeight(null));
132             Graphics2D g2 = (Graphics2D)this.i.getGraphics().create();
133             g2.transform(new java.awt.geom.AffineTransform(a.a, a.b, a.c, a.d, a.e, a.f));
134             g2.drawImage(i2, 0, 0, null);
135         }
136
137         public void fill(Mesh p, org.ibex.graphics.Paint paint) { fillStroke(p, paint, true, false); }
138         public void stroke(Mesh p, org.ibex.graphics.Paint paint) { fillStroke(p, paint, false, true); }
139         public void fillStroke(Mesh p, org.ibex.graphics.Paint paint, boolean fill, boolean stroke) {/*
140             if (g == null) g = getGraphics();
141             ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); 
142             int argb = ((org.ibex.graphics.Paint.SingleColorPaint)paint).color;
143             g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
144             gp.reset();
145             gp.setWindingRule(GeneralPath.WIND_EVEN_ODD);
146             for(int j=0; j<p.numcontours; j++) {
147                 if (p.contours[j+1] <= p.contours[j]) continue;
148                 gp.moveTo(p.x[p.contours[j]], p.y[p.contours[j]]);
149                 for(int i=p.contours[j]+1; i<p.contours[j+1]; i++) gp.lineTo(p.x[i], p.y[i]);
150                 gp.closePath();
151             }
152             if (fill) ((Graphics2D)g).fill(gp);
153             if (stroke) ((Graphics2D)g).draw(gp);
154                                                                                                      */}
155
156         public Java2PixelBuffer(int w, int h) {
157             super(w,h);
158             sm = cm.createCompatibleSampleModel(w, h);
159             int numSamples = w * h * sm.getNumDataElements();
160             if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
161                 if (sbank == null || numSamples > 512 * 512 / 3) {
162                     buf = new DataBufferUShort(numSamples);
163                 } else {
164                     if (numSamples > sbank.length - bank_start) {
165                         bank_start = 0;
166                         sbank = new short[512 * 512];
167                     }
168                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
169                     bank_start += numSamples;
170                 }
171             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
172                 if (bbank == null || numSamples > 512 * 512 / 3) {
173                     buf = new DataBufferByte(numSamples);
174                 } else {
175                     if (numSamples > bbank.length - bank_start) {
176                         bank_start = 0;
177                         bbank = new byte[512 * 512];
178                     }
179                     buf = new DataBufferByte(bbank, numSamples, bank_start);
180                     bank_start += numSamples;
181                 }
182             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
183                 if (ibank == null || numSamples > 512 * 512 / 3) {
184                     buf = new DataBufferInt(numSamples);
185                 } else {
186                     if (numSamples > ibank.length - bank_start) {
187                         bank_start = 0;
188                         ibank = new int[512 * 512];
189                     }
190                     buf = new DataBufferInt(ibank, numSamples, bank_start);
191                     bank_start += numSamples;
192                 }
193             }
194             raster = Raster.createWritableRaster(sm, buf, null);
195             i = new BufferedImage(cm, raster, false,  emptyHashtable);
196             g = i.getGraphics();
197         }
198     }
199 }