added some Apple-JVM specific properties
[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         // Properties for Apple JDK 1.3
24         System.setProperty("apple.awt.showGrowBox", "false");
25         System.setProperty("com.apple.hwaccel", "true");
26         System.setProperty("com.apple.forcehwaccel", "true");
27         System.setProperty("apple.awt.window.position.forceSafeUserPositioning", "true");
28         System.setProperty("apple.awt.window.position.forceSafeCreation", "true");
29
30         // disable the focus manager so we can intercept the tab key
31         javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
32                 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
33                 public void focusPreviousComponent(Component aComponent) { }
34                 public void focusNextComponent(Component aComponent) { }
35             });
36     }
37
38     /** this is done with reflection in case a new plugin comes out that doesn't let us pull the sun.plugin.* trick */
39     protected synchronized org.ibex.net.HTTP.Proxy _detectProxy() {
40         return (org.ibex.net.HTTP.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
41                 public Object run() { try {
42                     org.ibex.net.HTTP.Proxy pi = new org.ibex.net.HTTP.Proxy();
43                     
44                     Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
45                     Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
46                     Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
47                     
48                     Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
49                     Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
50                     Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.ibex.org") });
51                     
52                     Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
53                     
54                     if (((Boolean)ProxyInfo.getMethod("isSocksUsed",
55                                                       new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
56                         pi.socksProxyHost =
57                             (String)ProxyInfo.getMethod("getSocksProxy",
58                                                         new Class[] { }).invoke(proxyInfo, new Object[] { });
59                         pi.socksProxyPort =
60                             ((Integer)ProxyInfo.getMethod("getSocksPort",
61                                                           new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
62                     }
63                     
64                     if (((Boolean)ProxyInfo.getMethod("isProxyUsed",
65                                                       new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
66                         pi.httpProxyHost =
67                             (String)ProxyInfo.getMethod("getProxy",
68                                                         new Class[] { }).invoke(proxyInfo, new Object[] { });
69                         pi.httpProxyPort =
70                             ((Integer)ProxyInfo.getMethod("getPort",
71                                                           new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
72                     }
73                     
74                     if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
75                     else return null;
76                     
77                 } catch (Throwable e) {
78                     if (Log.on) Log.info(this, "No proxy information found in Java Plugin classes");
79                     return null;
80                 }
81                 }});
82     }
83
84     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Java2PixelBuffer(w, h); }
85     protected Surface __createSurface(final Box root, final boolean framed) { return new Java2Surface(root, framed); }
86     protected Surface _createSurface(final Box root, final boolean framed) {
87         return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
88                 public Object run() { return __createSurface(root, framed); } }); }
89
90
91     // Inner Classes //////////////////////////////////////////////////////////////////
92
93     private static Cursor invisibleCursor =
94         Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
95                                                        new Point(1, 1), "invisible");
96
97     public static class Java2Surface extends AWTSurface {
98         public Java2Surface(Box root, boolean framed) { super(root, framed); }
99
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         // this doens't seem to work on Windows
123         public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
124             Image i2 = ((AWTGlyph)source).getImage();
125             Graphics2D g2 = (Graphics2D)i.getGraphics();
126             g2.setComposite(AlphaComposite.DstOut);
127             g2.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
128             g2.drawImage(i2, dx, dy, null);
129             g2.setComposite(AlphaComposite.DstOver);
130             g2.setColor(new java.awt.Color((rgb & 0x00FF0000) >> 16, (rgb & 0x0000FF00) >> 8, (rgb & 0x000000FF)));
131             g2.fillRect(dx, dy, cx2 - dx, cy2 - dy);
132             g2.drawImage(i2, 0, 0, null);
133             g2.setClip(0, 0, i.getWidth(null), i.getHeight(null));
134         }
135
136         public Java2PixelBuffer(int w, int h) {
137             super(w,h);
138             sm = cm.createCompatibleSampleModel(w, h);
139             int numSamples = w * h * sm.getNumDataElements();
140             if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
141                 if (sbank == null || numSamples > 512 * 512 / 3) {
142                     buf = new DataBufferUShort(numSamples);
143                 } else {
144                     if (numSamples > sbank.length - bank_start) {
145                         bank_start = 0;
146                         sbank = new short[512 * 512];
147                     }
148                     buf = new DataBufferUShort(sbank, numSamples, bank_start);
149                     bank_start += numSamples;
150                 }
151             } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
152                 if (bbank == null || numSamples > 512 * 512 / 3) {
153                     buf = new DataBufferByte(numSamples);
154                 } else {
155                     if (numSamples > bbank.length - bank_start) {
156                         bank_start = 0;
157                         bbank = new byte[512 * 512];
158                     }
159                     buf = new DataBufferByte(bbank, numSamples, bank_start);
160                     bank_start += numSamples;
161                 }
162             } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
163                 if (ibank == null || numSamples > 512 * 512 / 3) {
164                     buf = new DataBufferInt(numSamples);
165                 } else {
166                     if (numSamples > ibank.length - bank_start) {
167                         bank_start = 0;
168                         ibank = new int[512 * 512];
169                     }
170                     buf = new DataBufferInt(ibank, numSamples, bank_start);
171                     bank_start += numSamples;
172                 }
173             }
174             raster = Raster.createWritableRaster(sm, buf, null);
175             i = new BufferedImage(cm, raster, false,  emptyHashtable);
176             g = i.getGraphics();
177         }
178     }
179 }