Can play minesweeper on iPad ibrwsr
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 10 Mar 2014 12:56:48 +0100
branchibrwsr
changeset 1015ef40158eb9c
parent 100 b083d786eeec
child 102 09a95867af5e
Can play minesweeper on iPad
minesweeper/nbactions.xml
minesweeper/pom.xml
minesweeper/src/main/java/org/apidesign/demo/minesweeper/Foo.java
minesweeper/src/main/java/org/apidesign/demo/minesweeper/IBrwsrPrsntr.java
minesweeper/src/main/java/org/apidesign/demo/minesweeper/Main.java
minesweeper/src/main/java/org/apidesign/demo/minesweeper/RoboVMJFXLauncher.java
     1.1 --- a/minesweeper/nbactions.xml	Mon Mar 10 10:59:38 2014 +0100
     1.2 +++ b/minesweeper/nbactions.xml	Mon Mar 10 12:56:48 2014 +0100
     1.3 @@ -23,8 +23,7 @@
     1.4      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     1.5      THE SOFTWARE.
     1.6  
     1.7 --->
     1.8 -<actions>
     1.9 +--><actions>
    1.10      <action>
    1.11          <actionName>run</actionName>
    1.12          <goals>
    1.13 @@ -42,4 +41,11 @@
    1.14              <jpda.listen>maven</jpda.listen>
    1.15          </properties>
    1.16      </action>
    1.17 +    <action>
    1.18 +            <actionName>CUSTOM-iPad Sim</actionName>
    1.19 +            <displayName>iPad Sim</displayName>
    1.20 +            <goals>
    1.21 +                <goal>robovm:ipad-sim</goal>
    1.22 +            </goals>
    1.23 +        </action>
    1.24  </actions>
     2.1 --- a/minesweeper/pom.xml	Mon Mar 10 10:59:38 2014 +0100
     2.2 +++ b/minesweeper/pom.xml	Mon Mar 10 12:56:48 2014 +0100
     2.3 @@ -23,6 +23,23 @@
     2.4    <build>
     2.5        <plugins>
     2.6            <plugin>
     2.7 +                <groupId>org.robovm</groupId>
     2.8 +                <artifactId>robovm-maven-plugin</artifactId>
     2.9 +                <version>0.0.8.1</version>
    2.10 +                <configuration>
    2.11 +                    <config>
    2.12 +                        <mainClass>org.apidesign.demo.minesweeper.RoboVMJFXLauncher</mainClass>
    2.13 +                        <forceLinkClasses>
    2.14 +                            <pattern>java.util.logging.ConsoleHandler</pattern>
    2.15 +                            <pattern>java.util.logging.SimpleFormatter</pattern>
    2.16 +                            <pattern>org.netbeans.html.ko4j.KO4J</pattern>
    2.17 +                            <pattern>org.netbeans.html.sound.impl.BrowserAudioEnv</pattern>
    2.18 +                        </forceLinkClasses>                        
    2.19 +                    </config>
    2.20 +                    <includeJFX>false</includeJFX>
    2.21 +                </configuration>
    2.22 +          </plugin>
    2.23 +          <plugin>
    2.24                <groupId>org.apache.maven.plugins</groupId>
    2.25                <artifactId>maven-compiler-plugin</artifactId>
    2.26                <version>2.3.2</version>
    2.27 @@ -88,6 +105,21 @@
    2.28        <version>6.7</version>
    2.29        <scope>test</scope>
    2.30      </dependency>
    2.31 +    <dependency>
    2.32 +      <groupId>org.apidesign.html</groupId>
    2.33 +      <artifactId>xtrnlbrwsr</artifactId>
    2.34 +      <version>0.7-SNAPSHOT</version>
    2.35 +    </dependency>
    2.36 +    <dependency>
    2.37 +        <groupId>org.robovm</groupId>
    2.38 +        <artifactId>robovm-rt</artifactId>
    2.39 +        <version>0.0.8</version>
    2.40 +    </dependency>
    2.41 +    <dependency>
    2.42 +        <groupId>org.robovm</groupId>
    2.43 +        <artifactId>robovm-cocoatouch</artifactId>
    2.44 +        <version>0.0.8</version>
    2.45 +    </dependency>    
    2.46    </dependencies>
    2.47    <profiles>
    2.48        <profile>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/minesweeper/src/main/java/org/apidesign/demo/minesweeper/Foo.java	Mon Mar 10 12:56:48 2014 +0100
     3.3 @@ -0,0 +1,30 @@
     3.4 +/*
     3.5 + * To change this license header, choose License Headers in Project Properties.
     3.6 + * To change this template file, choose Tools | Templates
     3.7 + * and open the template in the editor.
     3.8 + */
     3.9 +package org.apidesign.demo.minesweeper;
    3.10 +
    3.11 +import org.robovm.cocoatouch.foundation.NSURL;
    3.12 +import org.robovm.cocoatouch.foundation.NSURLRequest;
    3.13 +import org.robovm.objc.ObjCClass;
    3.14 +import org.robovm.objc.Selector;
    3.15 +import org.robovm.rt.bro.Bro;
    3.16 +import org.robovm.rt.bro.annotation.Bridge;
    3.17 +import org.robovm.rt.bro.annotation.Library;
    3.18 +
    3.19 +@Library("Foundation")
    3.20 +public class Foo {
    3.21 +    static {
    3.22 +        Bro.bind();
    3.23 +    }
    3.24 +
    3.25 +    private static final Selector requestWithURL$ = Selector.register("requestWithURL:");
    3.26 +    @Bridge(symbol = "objc_msgSend")
    3.27 +    private native static NSURLRequest objc_requestWithURL(ObjCClass __self__, Selector __cmd__, NSURL URL);
    3.28 +    public static NSURLRequest requestWithURL(NSURL url) {
    3.29 +        return objc_requestWithURL(ObjCClass.getByType(NSURLRequest.class), requestWithURL$, url);
    3.30 +    }
    3.31 +    
    3.32 +    
    3.33 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/minesweeper/src/main/java/org/apidesign/demo/minesweeper/IBrwsrPrsntr.java	Mon Mar 10 12:56:48 2014 +0100
     4.3 @@ -0,0 +1,59 @@
     4.4 +package org.apidesign.demo.minesweeper;
     4.5 +
     4.6 +import java.io.IOException;
     4.7 +import java.net.URI;
     4.8 +import java.util.logging.Logger;
     4.9 +import org.apidesign.html.brwsr.XtrnlBrwsrPrsntr;
    4.10 +import org.robovm.cocoatouch.foundation.NSURL;
    4.11 +import org.robovm.cocoatouch.uikit.UIWebView;
    4.12 +import org.robovm.objc.ObjCBlock;
    4.13 +import org.robovm.objc.block.VoidBlock;
    4.14 +import org.robovm.rt.bro.Bro;
    4.15 +import org.robovm.rt.bro.NativeObject;
    4.16 +import org.robovm.rt.bro.annotation.Bridge;
    4.17 +import org.robovm.rt.bro.annotation.Library;
    4.18 +
    4.19 +/**
    4.20 + *
    4.21 + * @author jtulach
    4.22 + */
    4.23 +@Library("dispatch")    
    4.24 +final class IBrwsrPrsntr extends XtrnlBrwsrPrsntr {
    4.25 +    private final UIWebView wv;
    4.26 +    
    4.27 +    static {
    4.28 +        Bro.bind();
    4.29 +    }
    4.30 +
    4.31 +    public static class Queue extends NativeObject {
    4.32 +    }
    4.33 +
    4.34 +    @Bridge(symbol = "dispatch_get_current_queue")
    4.35 +    public static native Queue getMainQueue();
    4.36 +
    4.37 +    @Bridge(symbol = "dispatch_async")
    4.38 +    public static native void async(Queue queue, ObjCBlock block);
    4.39 +
    4.40 +    public IBrwsrPrsntr(UIWebView wv) throws Exception {
    4.41 +        this.wv = wv;
    4.42 +    }
    4.43 +
    4.44 +    @Override
    4.45 +    public void runSafe(final Runnable r) {
    4.46 +        VoidBlock vb = new VoidBlock() {
    4.47 +            @Override
    4.48 +            public void invoke() {
    4.49 +                r.run();
    4.50 +            }
    4.51 +        };
    4.52 +        ObjCBlock ocb = VoidBlock.Marshaler.toObjCBlock(vb);
    4.53 +        async(getMainQueue(), ocb);
    4.54 +    }
    4.55 +    
    4.56 +    @Override
    4.57 +    protected void showBrwsr(URI page) throws IOException {
    4.58 +        wv.loadRequest(Foo.requestWithURL(new NSURL(page.toString())));
    4.59 +    }
    4.60 +    private static final Logger LOG = Logger.getLogger(IBrwsrPrsntr.class.getName());
    4.61 +    
    4.62 +}
     5.1 --- a/minesweeper/src/main/java/org/apidesign/demo/minesweeper/Main.java	Mon Mar 10 10:59:38 2014 +0100
     5.2 +++ b/minesweeper/src/main/java/org/apidesign/demo/minesweeper/Main.java	Mon Mar 10 12:56:48 2014 +0100
     5.3 @@ -30,7 +30,7 @@
     5.4  import java.util.logging.Level;
     5.5  import java.util.logging.Logger;
     5.6  import net.java.html.boot.BrowserBuilder;
     5.7 -import org.apidesign.html.dlvkbrwsr.XtrnlBrwsrPrsntr;
     5.8 +import org.apidesign.html.brwsr.XtrnlBrwsrPrsntr;
     5.9  
    5.10  
    5.11  /** Bootstrap and initialization. */
    5.12 @@ -38,38 +38,11 @@
    5.13      private static final Logger LOG = Logger.getLogger(Main.class.getName());
    5.14      private Main() {
    5.15      }
    5.16 -
    5.17 -    static Object[] showBrwsr(URI uri, String cmd) throws IOException {
    5.18 -        LOG.log(Level.INFO, "Showing {0}", uri);
    5.19 -        if (cmd == null) {
    5.20 -            try {
    5.21 -                LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[]{
    5.22 -                    System.getProperty("java.vm.name"),
    5.23 -                    System.getProperty("java.vm.vendor"),
    5.24 -                    System.getProperty("java.vm.version"),});
    5.25 -                java.awt.Desktop.getDesktop().browse(uri);
    5.26 -                LOG.log(Level.INFO, "Desktop.browse successfully finished");
    5.27 -                return null;
    5.28 -            } catch (UnsupportedOperationException ex) {
    5.29 -                LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
    5.30 -                LOG.log(Level.FINE, null, ex);
    5.31 -            }
    5.32 -        }
    5.33 -        {
    5.34 -            String cmdName = cmd == null ? "xdg-open" : cmd;
    5.35 -            String[] cmdArr = {
    5.36 -                cmdName, uri.toString()
    5.37 -            };
    5.38 -            LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
    5.39 -            final Process process = Runtime.getRuntime().exec(cmdArr);
    5.40 -            return new Object[]{process, null};
    5.41 -        }
    5.42 -    }
    5.43      
    5.44 -    /** Launches the browser */
    5.45 -    public static void main(String... args) throws Exception {
    5.46 +    /** Called when page is ready */
    5.47 +    public static void onPageLoad(String... args) throws Exception {
    5.48          /*
    5.49 -        Logger l = Logger.getLogger("org.apidesign.html.dlvkbrwsr");
    5.50 +        Logger l = Logger.getLogger("org.apidesign.html.brwsr");
    5.51          l.setLevel(Level.ALL);
    5.52          l.setUseParentHandlers(false);
    5.53          ConsoleHandler ch = new ConsoleHandler();
    5.54 @@ -77,23 +50,6 @@
    5.55          l.addHandler(ch);
    5.56          */
    5.57          
    5.58 -        
    5.59 -        BrowserBuilder.newBrowser(new XtrnlBrwsrPrsntr() {
    5.60 -            @Override
    5.61 -            protected void showBrwsr(URI page) throws IOException {
    5.62 -                Main.showBrwsr(page, null);
    5.63 -            }
    5.64 -        }).
    5.65 -            loadPage("pages/index.html").
    5.66 -            loadClass(Main.class).
    5.67 -            invoke("onPageLoad", args).
    5.68 -            showAndWait();
    5.69 -        System.in.read();
    5.70 -        System.exit(0);
    5.71 -    }
    5.72 -    
    5.73 -    /** Called when page is ready */
    5.74 -    public static void onPageLoad(String... args) throws Exception {
    5.75          Mines m = new Mines();
    5.76          m.applyBindings();
    5.77      }
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/minesweeper/src/main/java/org/apidesign/demo/minesweeper/RoboVMJFXLauncher.java	Mon Mar 10 12:56:48 2014 +0100
     6.3 @@ -0,0 +1,57 @@
     6.4 +package org.apidesign.demo.minesweeper;
     6.5 +
     6.6 +import java.net.URL;
     6.7 +import java.util.logging.Level;
     6.8 +import java.util.logging.Logger;
     6.9 +import org.robovm.cocoatouch.coregraphics.CGRect;
    6.10 +import org.robovm.cocoatouch.foundation.NSAutoreleasePool;
    6.11 +import org.robovm.cocoatouch.foundation.NSDictionary;
    6.12 +import org.robovm.cocoatouch.uikit.UIApplication;
    6.13 +import org.robovm.cocoatouch.uikit.UIApplicationDelegate;
    6.14 +import org.robovm.cocoatouch.uikit.UIColor;
    6.15 +import org.robovm.cocoatouch.uikit.UIScreen;
    6.16 +import org.robovm.cocoatouch.uikit.UIWebView;
    6.17 +import org.robovm.cocoatouch.uikit.UIWindow;
    6.18 +
    6.19 +public class RoboVMJFXLauncher extends UIApplicationDelegate.Adapter {
    6.20 +    int clickCount;
    6.21 +    private UIWindow window;
    6.22 +    private UIWebView wv;
    6.23 +    
    6.24 +    @Override
    6.25 +    public boolean didFinishLaunching(UIApplication application, NSDictionary launchOptions) {
    6.26 +        wv = new UIWebView();
    6.27 +        wv.setFrame(new CGRect(0.0f, 0.0f, 800.0f, 1080.0f));
    6.28 +        
    6.29 +        window = new UIWindow(UIScreen.getMainScreen().getBounds());
    6.30 +        window.setBackgroundColor(UIColor.lightGrayColor());
    6.31 +        window.addSubview(wv);
    6.32 +        window.makeKeyAndVisible();
    6.33 +
    6.34 +        try {
    6.35 +            IBrwsrPrsntr p = new IBrwsrPrsntr(wv);
    6.36 +            URL u = RoboVMJFXLauncher.class.getResource("pages/index.html");
    6.37 +            p.displayPage(u, new Runnable() {
    6.38 +                @Override
    6.39 +                public void run() {
    6.40 +                    try {
    6.41 +                        Main.onPageLoad();
    6.42 +                    } catch (Exception ex) {
    6.43 +                        Logger.getLogger(RoboVMJFXLauncher.class.getName()).log(Level.SEVERE, null, ex);
    6.44 +                    }
    6.45 +                }
    6.46 +            });
    6.47 +        } catch (Throwable t) {
    6.48 +            t.printStackTrace();
    6.49 +        }
    6.50 +        
    6.51 +        
    6.52 +        return true;
    6.53 +    }
    6.54 +
    6.55 +    public static void main(String[] args) throws Exception {
    6.56 +        NSAutoreleasePool pool = new NSAutoreleasePool();
    6.57 +        UIApplication.main(args, null, RoboVMJFXLauncher.class);
    6.58 +        pool.drain();
    6.59 +    }
    6.60 +}