1 /* 2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package hello; 27 28 import javafx.application.Application; 29 import javafx.concurrent.Worker; 30 import javafx.geometry.Insets; 31 import javafx.scene.Scene; 32 import javafx.scene.control.Button; 33 import javafx.scene.control.TextField; 34 import javafx.scene.effect.ColorAdjust; 35 import javafx.scene.image.Image; 36 import javafx.scene.image.ImageView; 37 import javafx.scene.layout.BorderPane; 38 import javafx.scene.layout.HBox; 39 import javafx.scene.layout.Priority; 40 import javafx.scene.shape.Circle; 41 import javafx.scene.text.Text; 42 import javafx.scene.web.WebEngine; 43 import javafx.scene.web.WebView; 44 import javafx.stage.Stage; 45 46 import java.util.List; 47 48 /** 49 * Simple WebView application. 50 */ 51 public class HelloWebView extends Application { 52 private static final String DEFAULT_URL = "http://www.oracle.com/java/"; 53 private static final double urlBoxMinDimension = 32.0; 54 private static final double rotateFactor = 180.0; 55 private static final ColorAdjust black = new ColorAdjust(); 56 57 final ImageView backImage = new ImageView(); 58 final ImageView forwardImage = new ImageView(); 59 final ImageView reloadImage = new ImageView(); 60 61 @Override 62 public void start(Stage stage) { 63 List<String> args = getParameters().getRaw(); 64 final String initialURL = args.size() > 0 ? args.get(0) : DEFAULT_URL; 65 66 final WebView webView = new WebView(); 67 final WebEngine webEngine = webView.getEngine(); 68 69 final TextField urlBox = new TextField(); 70 urlBox.setMinHeight(urlBoxMinDimension); 71 urlBox.setText(initialURL); 72 HBox.setHgrow(urlBox, Priority.ALWAYS); 73 urlBox.setOnAction(e -> webEngine.load(urlBox.getText())); 74 75 final Text bottomTitle = new Text(); 76 bottomTitle.textProperty().bind(urlBox.textProperty()); 77 78 final Button goStopButton = new Button("Go"); 79 goStopButton.setPrefSize(1.5 * urlBoxMinDimension, urlBoxMinDimension); 80 goStopButton.setOnAction(e -> webEngine.load(urlBox.getText())); 81 82 final Button backButton = new Button(); 83 backButton.setShape(new Circle(1)); 84 backButton.setDisable(true); 85 backButton.setPadding(new Insets(7.5,8.5,8.5,7.5)); 86 backButton.setOnAction(e -> webEngine.getHistory().go(-1)); 87 88 final Button forwardButton = new Button(); 89 forwardButton.setShape(new Circle(1)); 90 forwardButton.setDisable(true); 91 forwardButton.setPadding(new Insets(8.5,7.5,7.5,8.5)); 92 forwardButton.setOnAction(e -> webEngine.getHistory().go(+1)); 93 94 final Button reloadButton = new Button(); 95 reloadButton.setPadding(new Insets(8,8,8,8)); 96 reloadButton.setOnAction(e -> webEngine.reload()); 97 98 black.setBrightness(-1); 99 100 if (isJigsawMode()) { 101 try { 102 backImage.setImage(new Image("jrt:/javafx.web/com/sun/" + 103 "javafx/webkit/prism/resources/mediaPlayDisabled.png")); 104 backImage.setRotate(rotateFactor); 105 backImage.setEffect(black); 106 backButton.setGraphic(backImage); 107 } catch (NullPointerException e) { 108 backButton.setText("Back"); 109 } 110 111 try { 112 forwardImage.setImage(new Image("jrt:/javafx.web/com/sun/" + 113 "javafx/webkit/prism/resources/mediaPlayDisabled.png")); 114 forwardImage.setEffect(black); 115 forwardButton.setGraphic(forwardImage); 116 } catch (NullPointerException e) { 117 forwardButton.setText("Forward"); 118 } 119 120 try { 121 reloadImage.setImage(new Image("jrt:/javafx.web/javafx/scene/web/Redo_16x16_JFX.png")); 122 reloadImage.setEffect(black); 123 reloadButton.setGraphic(reloadImage); 124 } catch (NullPointerException e) { 125 reloadButton.setText("Reload"); 126 } 127 128 } 129 130 else { 131 try { 132 backImage.setImage(new Image(WebEngine.class.getResourceAsStream("/com/sun/" + 133 "javafx/webkit/prism/resources/mediaPlayDisabled.png"))); 134 backImage.setRotate(rotateFactor); 135 backImage.setEffect(black); 136 backButton.setGraphic(backImage); 137 } catch (NullPointerException e) { 138 backButton.setText("Back"); 139 } 140 141 try { 142 forwardImage.setImage(new Image(WebEngine.class.getResourceAsStream("/com/sun/" + 143 "javafx/webkit/prism/resources/mediaPlayDisabled.png"))); 144 forwardImage.setEffect(black); 145 forwardButton.setGraphic(forwardImage); 146 } catch (NullPointerException e) { 147 forwardButton.setText("Forward"); 148 } 149 150 try { 151 reloadImage.setImage(new Image(WebEngine.class.getResourceAsStream("/com/sun/" + 152 "javafx/scene/web/skin/Redo_16x16_JFX.png"))); 153 reloadImage.setEffect(black); 154 reloadButton.setGraphic(reloadImage); 155 } catch (NullPointerException e) { 156 reloadButton.setText("Reload"); 157 } 158 159 160 } 161 162 final HBox naviBar = new HBox(); 163 naviBar.getChildren().addAll(backButton, forwardButton, urlBox, reloadButton, goStopButton); 164 naviBar.setPadding(new Insets(2)); 165 166 final BorderPane root = new BorderPane(); 167 root.setTop(naviBar); 168 root.setCenter(webView); 169 root.setBottom(bottomTitle); 170 171 webEngine.locationProperty().addListener((observable, oldValue, newValue) -> urlBox.setText(newValue)); 172 173 webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> { 174 if (newValue.equals(Worker.State.READY) || newValue.equals(Worker.State.SCHEDULED) || newValue.equals(Worker.State.RUNNING)) { 175 bottomTitle.setVisible(true); 176 goStopButton.setText("Stop"); 177 goStopButton.setOnAction(e -> webEngine.getLoadWorker().cancel()); 178 } 179 180 else { 181 bottomTitle.setVisible(false); 182 goStopButton.setText("Go"); 183 goStopButton.setOnAction(e -> webEngine.load(urlBox.getText())); 184 } 185 }); 186 187 webEngine.getHistory().currentIndexProperty().addListener((observable, oldValue, newValue) -> { 188 int length = webEngine.getHistory().getEntries().size(); 189 190 backButton.setDisable((int)newValue == 0); 191 forwardButton.setDisable((int)newValue >= length - 1); 192 }); 193 194 webEngine.load(initialURL); 195 196 Scene scene = new Scene(root); 197 stage.setScene(scene); 198 199 stage.setTitle("HelloWebView: java version " + System.getProperty("java.version")); 200 stage.show(); 201 stage.widthProperty().addListener (w -> urlBox.setMaxWidth(stage.getWidth() - backButton.getWidth() 202 - forwardButton.getWidth() - reloadButton.getWidth() - goStopButton.getWidth())); 203 } 204 205 public boolean isJigsawMode() { 206 Class clazz = null; 207 try { 208 clazz = Class.forName("java.lang.reflect.Module", false, HelloWebView.class.getClassLoader()); 209 } catch (Exception e) { } 210 211 return clazz != null; 212 213 } 214 215 public static void main(String[] args) { 216 Application.launch(args); 217 } 218 }