rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/FXBrwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 20 Mar 2013 08:41:24 +0100
branchfx
changeset 854 acffc26790d7
parent 853 39166e462f8d
child 856 8d6534b67252
permissions -rw-r--r--
Using FX dialog. Intercepting just the first alert('bck2brwsr') call.
     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.impl;
    19 
    20 import java.util.List;
    21 import java.util.logging.Level;
    22 import java.util.logging.Logger;
    23 import javafx.application.Application;
    24 import javafx.beans.value.ChangeListener;
    25 import javafx.beans.value.ObservableValue;
    26 import javafx.event.ActionEvent;
    27 import javafx.event.EventHandler;
    28 import javafx.geometry.HPos;
    29 import javafx.geometry.Insets;
    30 import javafx.geometry.Pos;
    31 import javafx.geometry.VPos;
    32 import javafx.scene.Node;
    33 import javafx.scene.Scene;
    34 import javafx.scene.control.Button;
    35 import javafx.scene.control.TextField;
    36 import javafx.scene.layout.ColumnConstraints;
    37 import javafx.scene.layout.GridPane;
    38 import javafx.scene.layout.Pane;
    39 import javafx.scene.layout.Priority;
    40 import javafx.scene.layout.VBox;
    41 import javafx.scene.text.Text;
    42 import javafx.scene.web.WebEngine;
    43 import javafx.scene.web.WebEvent;
    44 import javafx.scene.web.WebView;
    45 import javafx.stage.Modality;
    46 import javafx.stage.Stage;
    47 import netscape.javascript.JSObject;
    48 
    49 /**
    50  * Demonstrates a WebView object accessing a web page.
    51  *
    52  * @see javafx.scene.web.WebView
    53  * @see javafx.scene.web.WebEngine
    54  */
    55 public class FXBrwsr extends Application {
    56     private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
    57 
    58     @Override
    59     public void start(Stage primaryStage) throws Exception {
    60         Pane root = new WebViewPane(getParameters().getUnnamed());
    61         primaryStage.setScene(new Scene(root, 1024, 768));
    62         primaryStage.show();
    63     }
    64     
    65     /**
    66      * Create a resizable WebView pane
    67      */
    68     private class WebViewPane extends Pane {
    69         private final JVMBridge bridge = new JVMBridge();
    70 
    71         public WebViewPane(List<String> params) {
    72             VBox.setVgrow(this, Priority.ALWAYS);
    73             setMaxWidth(Double.MAX_VALUE);
    74             setMaxHeight(Double.MAX_VALUE);
    75             WebView view = new WebView();
    76             view.setMinSize(500, 400);
    77             view.setPrefSize(500, 400);
    78             final WebEngine eng = view.getEngine();
    79             final TextField locationField = new TextField();
    80             LOG.log(Level.FINE, "params : {0}", params);
    81             if (params.size() > 0) {
    82                 LOG.log(Level.FINE, "loading page {0}", params.get(0));
    83                 eng.load(params.get(0));
    84                 LOG.fine("done loading page ");
    85                 locationField.setText(params.get(0));
    86             }
    87             locationField.setMaxHeight(Double.MAX_VALUE);
    88             Button goButton = new Button("Go");
    89             goButton.setDefaultButton(true);
    90             EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
    91                 @Override
    92                 public void handle(ActionEvent event) {
    93                     eng.load(locationField.getText().startsWith("http://") ? locationField.getText() : "http://" + locationField.getText());
    94                 }
    95             };
    96             goButton.setOnAction(goAction);
    97             locationField.setOnAction(goAction);
    98             eng.locationProperty().addListener(new ChangeListener<String>() {
    99                 @Override
   100                 public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
   101                     locationField.setText(newValue);
   102                 }
   103             });
   104             eng.setOnAlert(new EventHandler<WebEvent<String>>() {
   105                 @Override
   106                 public void handle(WebEvent<String> t) {
   107                     if ("bck2brwsr".equals(t.getData())) { // NOI18N
   108                         if (initBck2Brwsr(eng)) {
   109                             // ignore initial message
   110                             return;
   111                         }
   112                     }
   113                     final Stage dialogStage = new Stage();
   114                     dialogStage.initModality(Modality.WINDOW_MODAL);
   115                     dialogStage.setTitle("Warning");
   116                     final Button button = new Button("Close");
   117                     final Text text = new Text(t.getData());
   118                     
   119                     VBox box = new VBox();
   120                     box.setAlignment(Pos.CENTER);
   121                     box.setSpacing(10);
   122                     box.setPadding(new Insets(10));
   123                     box.getChildren().addAll(text, button);
   124                     
   125                     dialogStage.setScene(new Scene(box));
   126                     
   127                     button.setCancelButton(true);
   128                     button.setOnAction(new EventHandler<ActionEvent>() {
   129                         @Override
   130                         public void handle(ActionEvent t) {
   131                             dialogStage.close();
   132                         }
   133                     });
   134                     
   135                     dialogStage.centerOnScreen();
   136                     dialogStage.showAndWait();
   137                 }
   138             });
   139             GridPane grid = new GridPane();
   140             grid.setVgap(5);
   141             grid.setHgap(5);
   142             GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
   143             GridPane.setConstraints(goButton, 1, 0);
   144             GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
   145             grid.getColumnConstraints().addAll(new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true), new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
   146             grid.getChildren().addAll(locationField, goButton, view);
   147             getChildren().add(grid);
   148         }
   149 
   150         boolean initBck2Brwsr(WebEngine webEngine) {
   151             JSObject jsobj = (JSObject) webEngine.executeScript("window");
   152             LOG.log(Level.FINE, "window: {0}", jsobj);
   153             Object prev = jsobj.getMember("bck2brwsr");
   154             if ("undefined".equals(prev)) {
   155                 System.getProperties().put("webEngine", webEngine);
   156                 jsobj.setMember("bck2brwsr", bridge);
   157                 return true;
   158             }
   159             return false;
   160         }
   161 
   162         @Override
   163         protected void layoutChildren() {
   164             List<Node> managed = getManagedChildren();
   165             double width = getWidth();
   166             double height = getHeight();
   167             double top = getInsets().getTop();
   168             double right = getInsets().getRight();
   169             double left = getInsets().getLeft();
   170             double bottom = getInsets().getBottom();
   171             for (int i = 0; i < managed.size(); i++) {
   172                 Node child = managed.get(i);
   173                 layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
   174             }
   175         }
   176     }
   177     
   178 }