/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package newsbrowser; import javafx.event.ActionEvent; import javafx.geometry.HPos; import javafx.geometry.Pos; import javafx.geometry.VPos; import javafx.scene.Node; import javafx.scene.control.ColorPicker; import javafx.scene.control.ComboBox; import javafx.scene.control.Hyperlink; import javafx.scene.control.ProgressBar; import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.Region; import javafx.scene.paint.Color; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; /** * * @author ruhuayan */ class Browser extends Region { private final HBox searchBar; private final HBox pbBar; private final HBox bottomBar; private final WebView webview; private final WebEngine webEngine; private final TextField urlTextField; private final Hyperlink next; private final Hyperlink previous; private final Hyperlink refresh; private final Hyperlink writeButton; private final Hyperlink saveAsButton; //private final HBox writeBox; private final Hyperlink hand; private final Hyperlink eraser; private final ProgressBar pb; private final double browserW =800,browserH=600; private String fontName; private int fontSize; private Color drawColor; private int drawStroke; // private String browserTitle; public Browser() { //apply the styles getStyleClass().add("browser"); webview = new WebView(); webEngine = webview.getEngine(); searchBar = new HBox(); searchBar.setAlignment(Pos.BOTTOM_LEFT); searchBar.getStyleClass().add("browser-searchBar"); next = new Hyperlink("", new ImageView(new Image("icons/go-next.png"))); previous = new Hyperlink("", new ImageView(new Image("icons/go-previous.png"))); refresh = new Hyperlink("",new ImageView(new Image("icons/refresh.png"))); urlTextField = new TextField(); urlTextField.prefWidthProperty().bind(webview.prefWidthProperty().subtract(previous.getWidth()*3)); searchBar.getChildren().addAll(previous,next,refresh,urlTextField); pb = new ProgressBar(0.2); pbBar = new HBox(); pbBar.setAlignment(Pos.BOTTOM_LEFT); pb.prefWidthProperty().bind(webview.prefWidthProperty()); //pbBar.getStyleClass().add("pbBar"); pbBar.getChildren().add(pb); writeButton = new Hyperlink("Write",new ImageView(new Image("icons/write.png"))); saveAsButton = new Hyperlink("Save As Image",new ImageView(new Image("icons/saveAs.png"))); // create the bottombar bottomBar = new HBox(); bottomBar.setAlignment(Pos.BOTTOM_LEFT); //writeBox = new HBox(); fontName = "Calibri"; fontSize = 12; drawColor = Color.BLACK; drawStroke = 5; eraser = new Hyperlink("",new ImageView(new Image("icons/eraser.png"))); hand = new Hyperlink("", new ImageView(new Image("icons/Hand.png"))); //createWriteBox(); bottomBar.getStyleClass().add("browser-bottombar"); bottomBar.getChildren().addAll(saveAsButton,writeButton); //bottomBar.getChildren().add(writeBox); //writeBox.setVisible(false); // load the home page webEngine.load("http://www.google.com"); //add components getChildren().add(searchBar); getChildren().add(webview); getChildren().add(bottomBar); getChildren().add(pbBar); } public WebEngine getWebEngine(){ return webEngine; } public ProgressBar getProgressBar(){ return pb; } // JavaScript interface object public TextField getURLTextField(){ return urlTextField; } public HBox createWriteBox(){ HBox writeBox = new HBox(); writeBox.getStyleClass().add("writeBox"); ComboBox fontComboBox = new ComboBox<>(); fontComboBox.getItems().addAll("Arial Black","Calibri","Segoe Script","Verdana"); fontComboBox.setValue("Calibri"); fontComboBox.setOnAction((ActionEvent ae)->{ setFontName(fontComboBox.getValue()); }); fontComboBox.getStyleClass().add("combobox"); ComboBox fsComboBox = new ComboBox<>(); fsComboBox.getItems().addAll("10","12","14","16","18","20"); fsComboBox.setValue("12"); fsComboBox.setOnAction((ActionEvent ae)->{ setFontSize(Integer.parseInt(fsComboBox.getValue())); }); fsComboBox.getStyleClass().add("combobox"); ColorPicker colorPicker = new ColorPicker(); colorPicker.setValue(Color.BLACK); colorPicker.setOnAction((ActionEvent ae)->{ setDrawColor(colorPicker.getValue()); }); colorPicker.getStyleClass().add("combobox"); ComboBox strokeComboBox = new ComboBox<>(); strokeComboBox.getItems().addAll("Stroke:","3","5","7","9","11"); strokeComboBox.setOnAction((ActionEvent ae)->{ if(!"Stroke".equals(strokeComboBox.getValue())) setDrawStroke(Integer.parseInt(strokeComboBox.getValue())); }); strokeComboBox.setValue("5"); strokeComboBox.getStyleClass().add("comboBox"); writeBox.getChildren().addAll(fontComboBox,fsComboBox,colorPicker,strokeComboBox,eraser,hand); //HBox.setHgrow(writeBox, Priority.ALWAYS); bottomBar.getChildren().add(writeBox); return writeBox; } @Override protected void layoutChildren() { double w = getWidth(); double h = getHeight(); double tbHeight = bottomBar.prefHeight(w); double sbHeight = searchBar.prefHeight(w); layoutInArea(searchBar,0,0,w,sbHeight,0,HPos.CENTER,VPos.CENTER); layoutInArea(webview,0,sbHeight,w,h-sbHeight,0,HPos.CENTER, VPos.CENTER); layoutInArea(pbBar,0,sbHeight,w,3,0,HPos.CENTER,VPos.CENTER); layoutInArea(bottomBar,0,h-tbHeight,w,tbHeight,0,HPos.CENTER,VPos.CENTER); } @Override protected double computePrefWidth(double height) { return browserW; } @Override protected double computePrefHeight(double width) { return browserH; } public Hyperlink getSaveAsButton(){ return saveAsButton; } public Hyperlink getRefreshButton(){ return refresh; } public Hyperlink getNextButton(){ return next; } public Hyperlink getPreviousButton(){ return previous; } public Hyperlink getWriteButton(){ return writeButton; } public void addNode(Node n,double xPos,double yPos, double w, double h){ Browser.this.getChildren().add(n); Browser.this.layoutInArea(n, xPos, yPos, w, h, 0, HPos.LEFT, VPos.TOP); } public void removeNode(Node n){ Browser.this.getChildren().remove(n); } public HBox getBottomBar(){ return bottomBar; } public Hyperlink getHandButton(){ return hand; } public Hyperlink getEraser(){ return eraser; } public void setFontName(String fn){ fontName = fn; } public String getFontName(){ return fontName; } public void setFontSize(int fs){ fontSize =fs; } public int getFontSize(){ return fontSize; } public void setDrawColor(Color dc){ drawColor = dc; } public Color getDrawColor(){ return drawColor; } public void setDrawStroke(int ds){ drawStroke = ds; } public int getDrawStrok(){ return drawStroke; } }