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