launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/FXBrwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Nov 2013 14:20:49 +0100
changeset 1415 9718335431b7
parent 1283 1d0e583ac981
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Expand the center web view, when frame is expanded
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.launcher.fximpl;
    19 
    20 import java.util.List;
    21 import java.util.TooManyListenersException;
    22 import java.util.logging.Level;
    23 import java.util.logging.Logger;
    24 import javafx.application.Application;
    25 import javafx.application.Platform;
    26 import javafx.beans.value.ChangeListener;
    27 import javafx.beans.value.ObservableValue;
    28 import javafx.event.ActionEvent;
    29 import javafx.event.EventHandler;
    30 import javafx.geometry.Insets;
    31 import javafx.geometry.Pos;
    32 import javafx.scene.Scene;
    33 import javafx.scene.control.Button;
    34 import javafx.scene.control.ToolBar;
    35 import javafx.scene.layout.BorderPane;
    36 import javafx.scene.layout.HBox;
    37 import javafx.scene.layout.Priority;
    38 import javafx.scene.layout.VBox;
    39 import javafx.scene.text.Text;
    40 import javafx.scene.web.WebEngine;
    41 import javafx.scene.web.WebEvent;
    42 import javafx.scene.web.WebView;
    43 import javafx.stage.Modality;
    44 import javafx.stage.Stage;
    45 import netscape.javascript.JSObject;
    46 
    47 /**
    48  * Demonstrates a WebView object accessing a web page.
    49  *
    50  * @see javafx.scene.web.WebView
    51  * @see javafx.scene.web.WebEngine
    52  */
    53 public class FXBrwsr extends Application {
    54     private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
    55     
    56     @Override
    57     public void start(Stage primaryStage) throws Exception {
    58         WebView view = new WebView();
    59         WebController wc = new WebController(view, getParameters().getUnnamed());
    60         
    61         FXInspect.initialize(view.getEngine());
    62 
    63         final VBox vbox = new VBox();
    64         vbox.setAlignment( Pos.CENTER );
    65         vbox.setStyle( "-fx-background-color: #808080;");
    66 
    67 
    68         HBox hbox = new HBox();
    69         hbox.setStyle( "-fx-background-color: #808080;");
    70         hbox.setAlignment(Pos.CENTER);
    71         hbox.getChildren().add(vbox);
    72         HBox.setHgrow(vbox, Priority.ALWAYS);
    73         vbox.getChildren().add(view);
    74         VBox.setVgrow(view, Priority.ALWAYS);
    75 
    76         BorderPane root = new BorderPane();
    77         final boolean showToolbar = "true".equals(this.getParameters().getNamed().get("toolbar")); // NOI18N
    78         final boolean useFirebug = "true".equals(this.getParameters().getNamed().get("firebug")); // NOI18N
    79         if (showToolbar) {
    80             final ToolBar toolbar = new BrowserToolbar(view, vbox, useFirebug);
    81             root.setTop( toolbar );
    82         }
    83         root.setCenter(hbox);
    84 
    85         Scene scene = new Scene(root, 800, 600);
    86 
    87         primaryStage.setTitle( "Device Emulator" );
    88         primaryStage.setScene( scene );
    89         primaryStage.show();
    90     }
    91     
    92     /**
    93      * Create a resizable WebView pane
    94      */
    95     private static class WebController {
    96         private final JVMBridge bridge;
    97 
    98         public WebController(WebView view, List<String> params) {
    99             this.bridge = new JVMBridge(view.getEngine());
   100             LOG.log(Level.INFO, "Initializing WebView with {0}", params);
   101             final WebEngine eng = view.getEngine();
   102             try {
   103                 JVMBridge.addBck2BrwsrLoad(new InitBck2Brwsr(eng));
   104             } catch (TooManyListenersException ex) {
   105                 LOG.log(Level.SEVERE, null, ex);
   106             }
   107             
   108             if (params.size() > 0) {
   109                 LOG.log(Level.INFO, "loading page {0}", params.get(0));
   110                 eng.load(params.get(0));
   111                 LOG.fine("back from load");
   112             }
   113             eng.setOnAlert(new EventHandler<WebEvent<String>>() {
   114                 @Override
   115                 public void handle(WebEvent<String> t) {
   116                     final Stage dialogStage = new Stage();
   117                     dialogStage.initModality(Modality.WINDOW_MODAL);
   118                     dialogStage.setTitle("Warning");
   119                     final Button button = new Button("Close");
   120                     final Text text = new Text(t.getData());
   121                     
   122                     VBox box = new VBox();
   123                     box.setAlignment(Pos.CENTER);
   124                     box.setSpacing(10);
   125                     box.setPadding(new Insets(10));
   126                     box.getChildren().addAll(text, button);
   127                     
   128                     dialogStage.setScene(new Scene(box));
   129                     
   130                     button.setCancelButton(true);
   131                     button.setOnAction(new EventHandler<ActionEvent>() {
   132                         @Override
   133                         public void handle(ActionEvent t) {
   134                             dialogStage.close();
   135                         }
   136                     });
   137                     
   138                     dialogStage.centerOnScreen();
   139                     dialogStage.showAndWait();
   140                 }
   141             });
   142         }
   143 
   144         boolean initBck2Brwsr(WebEngine webEngine) {
   145             JSObject jsobj = (JSObject) webEngine.executeScript("window");
   146             LOG.log(Level.FINE, "window: {0}", jsobj);
   147             Object prev = jsobj.getMember("bck2brwsr");
   148             if ("undefined".equals(prev)) {
   149                 jsobj.setMember("bck2brwsr", bridge);
   150                 return true;
   151             }
   152             return false;
   153         }
   154 
   155         private class InitBck2Brwsr implements ChangeListener<Void>, Runnable {
   156             private final WebEngine eng;
   157 
   158             public InitBck2Brwsr(WebEngine eng) {
   159                 this.eng = eng;
   160             }
   161 
   162             @Override
   163             public synchronized void changed(ObservableValue<? extends Void> ov, Void t, Void t1) {
   164                 Platform.runLater(this);
   165                 try {
   166                     wait();
   167                 } catch (InterruptedException ex) {
   168                     LOG.log(Level.SEVERE, null, ex);
   169                 }
   170             }
   171 
   172             @Override
   173             public synchronized void run() {
   174                 initBck2Brwsr(eng);
   175                 notifyAll();
   176             }
   177         }
   178     }
   179     
   180 }