1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
5 import java.awt.event.*;
6 import java.awt.image.*;
9 import org.ibex.util.*;
10 import java.lang.reflect.*;
11 import org.ibex.graphics.*;
12 import org.ibex.core.*;
13 import org.ibex.net.*;
15 /** Platform class for most reasonable Java1.2+ Java2s */
16 public class Java2 extends AWT {
18 private boolean isJava14 = false;
21 // disable the focus manager so we can intercept the tab key
22 String versionString = System.getProperty("java.version", "");
23 int secondDecimal = versionString.substring(versionString.indexOf('.') + 1).indexOf('.');
24 if (secondDecimal != -1) versionString = versionString.substring(0, versionString.indexOf('.') + 1 + secondDecimal);
25 double version = Double.parseDouble(versionString);
29 Toolkit t = java.awt.Toolkit.getDefaultToolkit();
30 Method m = java.awt.Toolkit.class.getMethod("setDynamicLayout", new Class[] { Boolean.TYPE });
31 m.invoke(t, new Object[] { Boolean.TRUE });
32 } catch (Exception e) {
33 Log.info(this, "Exception while trying to enable AWT Dynamic Layout");
37 javax.swing.FocusManager.setCurrentManager(new javax.swing.FocusManager() {
38 public void processKeyEvent(Component focusedComponent, KeyEvent anEvent) { }
39 public void focusPreviousComponent(Component aComponent) { }
40 public void focusNextComponent(Component aComponent) { }
44 /** this is done with reflection in case a new version of the plugin comes out that doesn't let us pull the sun.plugin.* trick */
45 protected synchronized org.ibex.net.HTTP.Proxy _detectProxy() {
46 return (org.ibex.net.HTTP.Proxy)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
49 org.ibex.net.HTTP.Proxy pi = new org.ibex.net.HTTP.Proxy();
51 Class PluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
52 Method getDefaultProxyHandler = PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
53 Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[] { });
55 Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
56 Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
57 Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new URL("http://www.ibex.org") });
59 Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
61 if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
63 (String)ProxyInfo.getMethod("getSocksProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
65 ((Integer)ProxyInfo.getMethod("getSocksPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
68 if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
70 (String)ProxyInfo.getMethod("getProxy", new Class[] { }).invoke(proxyInfo, new Object[] { });
72 ((Integer)ProxyInfo.getMethod("getPort", new Class[] { }).invoke(proxyInfo, new Object[] { })).intValue();
75 if (pi.httpProxyHost != null || pi.socksProxyHost != null) return pi;
78 } catch (Throwable e) {
79 if (Log.on) Log.info(this, "No proxy information found in Java Plugin classes");
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) {
87 return (Surface)java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
91 // weaken the binding here to avoid link errors on 1.3.x
92 Class java14SurfaceClass = Class.forName(Java2.class.getName() + "$Java14Surface");
93 Constructor ctor = java14SurfaceClass.getConstructor(new Class[] { Box.class, Boolean.TYPE });
94 return (Surface)ctor.newInstance(new Object[] { root, Boolean.valueOf(framed) });
95 } catch (Exception e) {
97 throw new LinkageError("error: " + e);
100 return new Java2Surface(root, framed);
106 // Inner Classes //////////////////////////////////////////////////////////////////
108 private static Cursor invisibleCursor =
109 Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB),
110 new Point(1, 1), "invisible");
112 protected static class Java2Surface extends AWTSurface {
114 public Java2Surface(Box root, boolean framed) { super(root, framed); }
116 protected void _setMinimized(boolean b) {
117 if (frame == null) Log.info(this, "JDK 1.2 can only minimize frames, not windows");
118 else if (b) frame.setState(java.awt.Frame.ICONIFIED);
119 else frame.setState(java.awt.Frame.NORMAL);
122 public void syncCursor() {
123 if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
124 else super.syncCursor();
128 protected static class Java14Surface extends Java2Surface implements WindowStateListener, MouseWheelListener {
129 public Java14Surface(Box root, boolean framed) {
131 // JDK1.4 doesn't like java.lang.Window's...
132 if (!framed) ((Frame)window).setUndecorated(true);
133 window.addWindowStateListener(this);
134 window.addMouseWheelListener(this);
135 window.setVisible(true);
138 protected void makeVisible() { }
140 protected void _setMaximized(boolean m) {
142 if (Log.on) Log.info(this, "JDK 1.4 can only maximize frames, not windows");
145 frame.setExtendedState(m ? Frame.MAXIMIZED_BOTH : (minimized ? Frame.ICONIFIED : Frame.NORMAL));
147 protected void _setMinimized(boolean m) {
149 if (Log.on) Log.info(this, "JDK 1.4 can only minimize frames, not windows");
152 frame.setExtendedState(m ? Frame.ICONIFIED : (maximized ? Frame.MAXIMIZED_BOTH : Frame.NORMAL));
154 public void windowStateChanged(WindowEvent e) {
155 if (e.getOldState() != e.getNewState()) {
156 if ((e.getNewState() & Frame.MAXIMIZED_BOTH) != 0) Maximized(true);
157 else if (((e.getOldState() & Frame.MAXIMIZED_BOTH) != 0) && (e.getNewState() & Frame.MAXIMIZED_BOTH) == 0)
162 public void mouseWheelMoved(MouseWheelEvent m) {
163 if (m.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
164 VScroll(m.getUnitsToScroll());
168 protected static class Java2PixelBuffer extends AWTPixelBuffer {
169 private static ColorModel cm = Toolkit.getDefaultToolkit().getColorModel();
170 private static Hashtable emptyHashtable = new Hashtable();
171 private static short[] sbank = null;
172 private static int[] ibank = null;
173 private static byte[] bbank = null;
174 private static int bank_start = 0;
175 private WritableRaster raster = null;
176 private SampleModel sm = null;
177 private DataBuffer buf = null;
179 // this doens't seem to work on Windows
180 public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
181 Image i2 = ((AWTGlyph)source).getImage();
182 Graphics2D g2 = (Graphics2D)i.getGraphics();
183 g2.setComposite(AlphaComposite.DstOut);
184 g2.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
185 g2.drawImage(i2, dx, dy, null);
186 g2.setComposite(AlphaComposite.DstOver);
187 g2.setColor(new java.awt.Color((rgb & 0x00FF0000) >> 16, (rgb & 0x0000FF00) >> 8, (rgb & 0x000000FF)));
188 g2.fillRect(dx, dy, cx2 - dx, cy2 - dy);
189 g2.drawImage(i2, 0, 0, null);
190 g2.setClip(0, 0, i.getWidth(null), i.getHeight(null));
193 public Java2PixelBuffer(int w, int h) {
194 sm = cm.createCompatibleSampleModel(w, h);
195 int numSamples = w * h * sm.getNumDataElements();
196 if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
197 if (sbank == null || numSamples > 512 * 512 / 3) {
198 buf = new DataBufferUShort(numSamples);
200 if (numSamples > sbank.length - bank_start) {
202 sbank = new short[512 * 512];
204 buf = new DataBufferUShort(sbank, numSamples, bank_start);
205 bank_start += numSamples;
207 } else if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
208 if (bbank == null || numSamples > 512 * 512 / 3) {
209 buf = new DataBufferByte(numSamples);
211 if (numSamples > bbank.length - bank_start) {
213 bbank = new byte[512 * 512];
215 buf = new DataBufferByte(bbank, numSamples, bank_start);
216 bank_start += numSamples;
218 } else if (sm.getDataType() == DataBuffer.TYPE_INT) {
219 if (ibank == null || numSamples > 512 * 512 / 3) {
220 buf = new DataBufferInt(numSamples);
222 if (numSamples > ibank.length - bank_start) {
224 ibank = new int[512 * 512];
226 buf = new DataBufferInt(ibank, numSamples, bank_start);
227 bank_start += numSamples;
230 raster = Raster.createWritableRaster(sm, buf, null);
231 i = new BufferedImage(cm, raster, false, emptyHashtable);
236 protected String getDescriptiveName() { return isJava14 ? "Java 1.4+ JVM" : "Java 1.2+ JVM"; }