Revision: 2300
          http://vexi.svn.sourceforge.net/vexi/?rev=2300&view=rev
Author:   clrg
Date:     2007-09-22 10:53:06 -0700 (Sat, 22 Sep 2007)

Log Message:
-----------
Attempt at merging the Java2 and Java2VolatileImage files

Added Paths:
-----------
    branches/VolatileImage

Copied: branches/VolatileImage (from rev 2295, 
trunk/core/org.vexi.core/src/org/vexi/plat/Java2.java)
===================================================================
--- branches/VolatileImage                              (rev 0)
+++ branches/VolatileImage      2007-09-22 17:53:06 UTC (rev 2300)
@@ -0,0 +1,328 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the GNU General Public License version 2 ("the License").
+// You may not use this file except in compliance with the License.
+
+package org.vexi.plat;
+
+import java.awt.AlphaComposite;
+import java.awt.Canvas;
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Cursor;
+import java.awt.Frame;
+import java.awt.Graphics2D;
+import java.awt.Image;
+import java.awt.MediaTracker;
+import java.awt.Point;
+import java.awt.Toolkit;
+import java.awt.Transparency;
+import java.awt.Window;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseWheelEvent;
+import java.awt.event.MouseWheelListener;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowStateListener;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.awt.image.DataBuffer;
+import java.awt.image.DataBufferByte;
+import java.awt.image.DataBufferInt;
+import java.awt.image.DataBufferUShort;
+import java.awt.image.DirectColorModel;
+import java.awt.image.MemoryImageSource;
+import java.awt.image.Raster;
+import java.awt.image.SampleModel;
+import java.awt.image.WritableRaster;
+import java.awt.image.VolatileImage;
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.util.Hashtable;
+
+import org.ibex.js.Fountain;
+import org.ibex.js.JS;
+import org.ibex.js.JSExn;
+import org.ibex.js.JSU;
+import org.ibex.util.Log;
+import org.vexi.core.Box;
+import org.vexi.core.Surface;
+import org.vexi.graphics.Font;
+import org.vexi.graphics.Picture;
+import org.vexi.graphics.PixelBuffer;
+
+/** Platform class for most reasonable Java1.2+ Java2s */
+public class Java2 extends AWT {
+       {
+               Platform.systemfonts = new JS.Immutable() {
+                       File dir = null;
+                       {
+                               String osname = System.getProperty("os.name");
+                               System.out.println(osname);
+                               if(osname.contains("Windows")){
+                                       dir = new File("c:/Windows/Fonts");
+                               }
+                       }
+                       public JS get(JS key) throws JSExn {
+                               if(dir!=null){
+                                       String s = JSU.toString(key);
+                                       File f = new File(dir, s + ".ttf");
+                                       if(f.exists()){
+                                               return new 
Fountain.File(f.getPath());
+                                       }
+                                       f = new File(dir, s + ".ttc");
+                                       if(f.exists()){
+                                               return new 
Fountain.File(f.getPath());
+                                       }
+                               }
+                               return super.get(key);
+                       }
+               };
+       }
+
+    private boolean isJava14 = false;
+
+    public Java2() {
+        // disable the focus manager so we can intercept the tab key
+        String versionString = System.getProperty("java.version", "");
+        int secondDecimal = versionString.substring(versionString.indexOf('.') 
+ 1).indexOf('.');
+        if (secondDecimal != -1) versionString = versionString.substring(0, 
versionString.indexOf('.') + 1 + secondDecimal);
+        double version = Double.parseDouble(versionString);
+        if (version >= 1.4) {
+            isJava14 = true;
+            try {
+                Toolkit t = java.awt.Toolkit.getDefaultToolkit();
+                Method m = 
java.awt.Toolkit.class.getMethod("setDynamicLayout", new Class[] { Boolean.TYPE 
});
+                m.invoke(t, new Object[] { Boolean.TRUE });
+            } catch (Exception e) {
+                Log.warn(this, "Exception while trying to enable AWT Dynamic 
Layout");
+                Log.warn(this, e);
+            }
+        }
+        javax.swing.FocusManager.setCurrentManager(new 
javax.swing.FocusManager() {
+                public void processKeyEvent(Component focusedComponent, 
KeyEvent anEvent) { }
+                public void focusPreviousComponent(Component aComponent) { }
+                public void focusNextComponent(Component aComponent) { }
+            });
+    }
+
+    /** 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 */
+    protected synchronized org.vexi.net.HTTP.Proxy _detectProxy() {
+        return 
(org.vexi.net.HTTP.Proxy)java.security.AccessController.doPrivileged(new 
java.security.PrivilegedAction() {
+                public Object run() {
+                    try {
+                        org.vexi.net.HTTP.Proxy pi = new 
org.vexi.net.HTTP.Proxy();
+                        
+                        Class PluginProxyHandler = 
Class.forName("sun.plugin.protocol.PluginProxyHandler");
+                        Method getDefaultProxyHandler = 
PluginProxyHandler.getMethod("getDefaultProxyHandler", new Class[] { });
+                        Object proxyHandler = 
getDefaultProxyHandler.invoke(null, new Object[] { });
+                        
+                        Class ProxyHandler = 
Class.forName("sun.plugin.protocol.ProxyHandler");
+                        Method getProxyInfo = 
ProxyHandler.getMethod("getProxyInfo", new Class[] { URL.class });
+                        Object proxyInfo = getProxyInfo.invoke(proxyHandler, 
new Object[] { new URL("http://www.ibex.org";) });
+                        
+                        Class ProxyInfo = 
Class.forName("sun.plugin.protocol.ProxyInfo");
+                        
+                        if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new 
Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
+                            
pi.setSocksProxyHost((String)ProxyInfo.getMethod("getSocksProxy", new Class[] { 
}).invoke(proxyInfo, new Object[] { }));
+                            
pi.setSocksProxyPort(((Integer)ProxyInfo.getMethod("getSocksPort", new Class[] 
{ }).invoke(proxyInfo, new Object[] { })).intValue());
+                        }
+                        
+                        if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new 
Class[] { }).invoke(proxyInfo, new Object[] { })).booleanValue()) {
+                            
pi.setHttpProxyHost((String)ProxyInfo.getMethod("getProxy", new Class[] { 
}).invoke(proxyInfo, new Object[] { }));
+                            
pi.setHttpProxyPort(((Integer)ProxyInfo.getMethod("getPort", new Class[] { 
}).invoke(proxyInfo, new Object[] { })).intValue());
+                        }
+                        
+                        if (pi.getHttpProxyHost() != null || 
pi.getSocksProxyHost() != null) return pi;
+                        // else
+                        return null;
+
+                    } catch (Throwable e) {
+                        Log.warn(this, "No proxy information found in Java 
Plugin classes");
+                        return null;
+                    }
+                }});
+    }
+
+    protected Picture _createPicture(JS r) {
+       return new Java2Picture(r);
+    }
+
+    protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { 
return new Java2PixelBuffer(w, h, (Java2Surface)owner); }
+    protected Surface _createSurface(final Box root, final boolean framed) {
+        return (Surface)java.security.AccessController.doPrivileged(new 
java.security.PrivilegedAction() {
+                public Object run() {
+                    if (isJava14) {
+                        try {
+                             // weaken the binding here to avoid link errors 
on 1.3.x
+                             Class java14SurfaceClass = 
Class.forName(Java2.class.getName() + "$Java14Surface");
+                             Constructor ctor = 
java14SurfaceClass.getConstructor(new Class[] { Box.class, Boolean.TYPE });
+                               return (Surface)ctor.newInstance(new Object[] { 
root, Boolean.valueOf(framed) });
+                        } catch (Exception e) {
+                            Log.info(this, e);
+                            throw new LinkageError("error: " + e);
+                        }
+                    }
+                    // else
+                    return new Java2Surface(root, framed);
+                }
+            });
+    }
+
+    // Inner Classes 
//////////////////////////////////////////////////////////////////
+
+    private static Cursor invisibleCursor =
+        Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(2, 2, 
BufferedImage.TYPE_INT_ARGB),
+                                                       new Point(1, 1), 
"invisible");
+
+    protected static class Java2Surface extends AWTSurface {
+
+        public Java2Surface(Box root, boolean framed) { super(root, framed); }
+
+        protected void _setMinimized(boolean b) {
+            if (frame == null) Log.info(this, "JDK 1.2 can only minimize 
frames, not windows");
+            else if (b) frame.setState(java.awt.Frame.ICONIFIED);
+            else frame.setState(java.awt.Frame.NORMAL);
+        }
+
+        public void syncCursor() {
+            if (cursor.equals("invisible")) window.setCursor(invisibleCursor);
+            else super.syncCursor();
+        }
+    }
+
+    protected static class Java14Surface extends Java2Surface implements 
WindowStateListener, MouseWheelListener {
+        public Java14Surface(Box root, boolean framed) {
+            super(root, true);
+            // JDK1.4 doesn't like java.lang.Window's...
+            if (!framed) ((Frame)window).setUndecorated(true);
+            window.addWindowStateListener(this);
+            window.addMouseWheelListener(this);
+            window.setVisible(true);
+        }
+
+        protected void makeVisible() { }
+        
+        protected void _setMaximized(boolean m) {
+            if (frame == null) {
+                Log.warn(this, "JDK 1.4 can only maximize frames, not 
windows");
+                return;
+            }
+            frame.setExtendedState(m ? Frame.MAXIMIZED_BOTH : (minimized ? 
Frame.ICONIFIED : Frame.NORMAL));
+        }
+        protected void _setMinimized(boolean m) {
+            if (frame == null) {
+                Log.warn(this, "JDK 1.4 can only minimize frames, not 
windows");
+                return;
+            }
+            frame.setExtendedState(m ? Frame.ICONIFIED : (maximized ? 
Frame.MAXIMIZED_BOTH : Frame.NORMAL));
+        }
+        public void windowStateChanged(WindowEvent e) {
+            if (e.getOldState() != e.getNewState()) {
+                if ((e.getNewState() & Frame.MAXIMIZED_BOTH) != 0) 
Maximized(true);
+                else if (((e.getOldState() & Frame.MAXIMIZED_BOTH) != 0) && 
(e.getNewState() & Frame.MAXIMIZED_BOTH) == 0)
+                    Maximized(false);
+            }
+        }
+
+        public void mouseWheelMoved(MouseWheelEvent m) {
+            if (m.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
+                VScroll(m.getUnitsToScroll());
+        }
+        
+        public synchronized void render() {
+            AWTPixelBuffer pb = (AWTPixelBuffer) backbuffer;
+            if(!(pb.i instanceof VolatileImage)) throw new Error("should never 
happen");
+            VolatileImage vi = (VolatileImage) pb.i;
+            for(;;) {
+                super.render();
+                if(!vi.contentsLost()) break;
+                dirty(0,0,vi.getWidth(),vi.getHeight());
+                Log.debug(this,"Contents lost!");
+            }
+        }
+    }
+    
+    /** Allow for caching the buffered images to optimise repeated creations */
+    protected Font.Glyph _createGlyph(Font f, char c) { return new 
Java2Glyph(f, c); }
+    protected static class Java2Glyph extends AWTGlyph {
+        private BufferedImage i2 = null;
+        public Java2Glyph(Font f, char c) { super(f, c); }
+        BufferedImage getBufferedImage() {
+            if (i2 == null) {
+                i2 = new BufferedImage(width, height, 
BufferedImage.TYPE_INT_ARGB);
+            }
+            return i2;
+        }
+    }
+
+    protected static class Java2PixelBuffer extends AWTPixelBuffer {
+       
+        public void drawGlyph(org.vexi.graphics.Font.Glyph glyph, int dx, int 
dy, int cx1, int cy1, int cx2, int cy2, int argb) {
+            if (glyph.width == 0 || glyph.height == 0) return;
+            int w = cx2 - cx1;
+            int h = cy2 - cy1;
+            BufferedImage g = ((Java2Glyph)glyph).getBufferedImage();
+            Graphics2D dst = (Graphics2D)i.getGraphics();
+            Graphics2D src = (Graphics2D)g.getGraphics();
+            src.setClip(0, 0, glyph.width, glyph.height);
+            src.setComposite(AlphaComposite.Src);
+            src.setColor(new Color((argb & 0x00ff0000) >> 16, (argb & 
0x0000ff00) >> 8, argb & 0x000000ff, (argb >> 24) & 0x000000ff));
+            src.drawImage(((AWTGlyph)glyph).getImage(), 0, 0, null);
+            src.setComposite(AlphaComposite.SrcIn);
+            src.fillRect(0, 0, glyph.width, glyph.height);
+            dst.setClip(cx1, cy1, w, h);
+            dst.setComposite(AlphaComposite.SrcOver);
+            dst.drawImage(g, dx, dy, null);
+            dst.setClip(0, 0, i.getWidth(null), i.getHeight(null));
+        }
+        
+        /** draw an unscaled image */
+        public void drawPicture(Picture source, int dx, int dy, int cx1, int 
cy1, int cx2, int cy2) {
+            ((Java2Picture)source).init(surface.window);
+            g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
+            g.drawImage(((AWTPicture)source).i, dx, dy, null);
+            g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
+        }
+
+        public Java2PixelBuffer(int w, int h, Java2Surface s) {
+            super((AWTSurface)s);
+            i = s.window.getGraphicsConfiguration().createCompatibleImage(w, 
h, Transparency.TRANSLUCENT);
+            g = i.getGraphics();
+        }
+    }
+
+    static class Java2Picture extends AWTPicture{
+       private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 
0x0000FF00, 0x000000FF, 0xFF000000);
+        
+               public Java2Picture(JS r) {
+                       super(r);
+               } 
+               
+        public void init(Window window) {
+            if (initialized) return;
+            initialized = true;
+            MemoryImageSource mis = new MemoryImageSource(width, height, 
cmodel, data, 0, width);
+            mis.setAnimated(true);
+            Image tmp = Toolkit.getDefaultToolkit().createImage(mis);
+            MediaTracker mediatracker = new MediaTracker(new Canvas());
+            mediatracker.addImage(tmp, 1);
+            try { mediatracker.waitForAll(); } catch (InterruptedException e) 
{ }
+            mediatracker.removeImage(tmp);
+            synchronized(AWTPixelBuffer.class) { 
+                if (AWTPixelBuffer.component == null) {
+                    AWTPixelBuffer.component = new Frame();
+                    AWTPixelBuffer.component.setVisible(false);
+                    AWTPixelBuffer.component.addNotify();
+                }
+            }
+            System.out.println("Init managed image: " +stream);
+            i = 
window.getGraphicsConfiguration().createCompatibleImage(tmp.getWidth(null), 
tmp.getHeight(null), Transparency.TRANSLUCENT);
+            i.getGraphics().drawImage(tmp, 0, 0, null);
+        }
+       
+    }
+    
+    protected String getDescriptiveName() { return isJava14 ? "Java 1.4+ JVM" 
: "Java 1.2+ JVM"; }
+
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to