/*
* 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 java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
//import javafx.collections.ListChangeListener;
//import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.scene.image.WritableImage;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.ImageCursor;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.web.WebEngine;
//import javafx.scene.web.WebHistory;
//import javafx.scene.web.WebHistory.Entry;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
/**
*
* @author ruhuayan
*/
public class NewsBrowser extends Application {
private Scene scene;
private Browser browser;
private WebEngine webEngine;
//private WebHistory webHistory;
private Canvas canvas;
private GraphicsContext gc;
private boolean canvasOn = false;
private double mouseX=5.0, mouseY=5.0;
private double mouseStartY = 0, mouseStartX = 0;
private TextField tempText;
private HBox textBox;
private double webpageHeight=600.0;
private double canvasYPos = 0;
private double webpageYPos = 0;
private HBox writeBox;
private boolean eraserDisable = false;
private boolean handDisable = false;
private int draggedCount = 0;
//private ObservableList extends Entry> urlList;
@Override
public void start(Stage stage) {
browser = new Browser();
webEngine = browser.getWebEngine();
ProgressBar pb = browser.getProgressBar();
pb.progressProperty().bind(webEngine.getLoadWorker().progressProperty());
TextField urlTextField =browser.getURLTextField();
//handle web page load Listener
webEngine.getLoadWorker().stateProperty().addListener((ObservableValue extends Worker.State> ov, Worker.State oldState, Worker.State newState) -> {
String title = webEngine.getTitle();
if(!(title == null)) stage.setTitle(title);
pb.setVisible(true);
if(newState == Worker.State.FAILED){
webEngine.loadContent("
Page Loading FailedPage Loading Failed");
}
if (newState == Worker.State.SUCCEEDED) {
pb.setVisible(false);
String urlString = (String)webEngine.getLocation();
if(urlString!=null)
urlTextField.setText(webEngine.getLocation());
webpageHeight = executeJavascript("document.height");
// int cIndex = getHistoryIndex(webEngine.getLocation().toString());
// System.out.println("cIndex: "+cIndex+" size: "+urlList.size());
// if (cIndex==0){ //to disable next and previous button
// }
// if (cIndex == urlList.size()-1){
// }
}
});
//to work out the web history index
// webHistory = webEngine.getHistory();
// webHistory.getEntries().addListener(new ListChangeListener() {
// @Override
// public void onChanged(ListChangeListener.Change extends Entry> c) {
// urlList = c.getList();
// for (Entry e : c.getList()) {
// System.out.println(e.getUrl());
// }
// }
// });
//handle URL textField actionListener
urlTextField.setOnAction(new EventHandler() {
@Override
public void handle(Event event) {
String url = urlTextField.getText();
webEngine.load(correctURL(url));
}
});
urlTextField.setOnMouseClicked((MouseEvent me) -> {
if(me.getClickCount()==1) urlTextField.requestFocus();
else if(me.getClickCount()==2)
urlTextField.selectAll();
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
});
//Handle "Save As Image" Button(hyperlink) ActionLister
Hyperlink saveAsButton = browser.getSaveAsButton();
saveAsButton.setOnAction((ActionEvent event) -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save as an Image:");
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter( "Image Files", "*.png", "*.jpg", "*.gif");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showSaveDialog(stage);
if(file!=null){
WritableImage image = scene.snapshot(null);
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (IOException ex) {
System.out.println(ex.toString());
}
}
});
//refresh button action
Hyperlink refreshButton = browser.getRefreshButton();
refreshButton.setOnAction((ActionEvent ae)->{
String url = urlTextField.getText();
//if(url.equals("")) webEngine.load("http://www.google.com");
//else
webEngine.load(correctURL(url));
});
//next button action
Hyperlink nextButton = browser.getNextButton();
Hyperlink prevButton = browser.getPreviousButton();
nextButton.setOnAction((ActionEvent ae)->{
webEngine.executeScript("history.forward()");
});
//previous button action
prevButton.setOnAction((ActionEvent ae)->{
webEngine.executeScript("history.back()");
});
Hyperlink writeButton = browser.getWriteButton();
writeButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent e) {
if(!canvasOn){
writeButton.setText("Cancel");
webpageHeight = (webpageHeight<600)? 600:webpageHeight;
canvas = new Canvas(850,webpageHeight);
gc = canvas.getGraphicsContext2D();
browser.addNode(canvas,0,0,850,webpageHeight+100);
moveBrowser(0.0);
//canvas.maxHeight(webpageHeight);
browser.setOpacity(0.8);
canvas.toFront();
canvasOn=true;
HBox bottomBar = browser.getBottomBar();
bottomBar.toFront();
if(writeBox==null)
writeBox = browser.createWriteBox();
else
writeBox.setVisible(true);
//System.out.println("canvas width: "+canvas.getWidth()+" canvas Height: "+ canvas.getHeight());
//canvas Action Handler
canvas.setOnScroll((ScrollEvent se) -> {
if(webpageHeight>600){
moveBrowser(se.getDeltaY());
//
if(textBox != null){
removeTextBox();
}
}
//System.out.println("canvasYPos: "+ canvasYPos +" webPageYPos:"+ webpageYPos);
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
});
canvas.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler(){
@Override
public void handle(KeyEvent event) {
gc.fillText(event.getText(), mouseX, mouseY);
mouseX +=12;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
canvas.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler() {
@Override
public void handle(MouseEvent event) {
int stroke = browser.getDrawStrok();
double moveToY = event.getY();
double moveToX = event.getX();
if(gc!=null){
if(eraserDisable){
gc.clearRect(event.getX(), event.getY(), 24, 24);
}else if(handDisable){
if(moveToY>0 && moveToY=5){
if(mouseStartX>=0&&mouseStartX<=800&&mouseStartY>=0&&mouseStartY<=webpageHeight){
gc.setStroke(browser.getDrawColor());
gc.setLineWidth(stroke);
gc.strokeLine(mouseStartX, mouseStartY, moveToX, moveToY);
}
mouseStartX = moveToX;
mouseStartY = moveToY;
draggedCount = 0;
}
// gc.setFill(browser.getDrawColor());
// gc.fillOval(event.getX(), event.getY(), stroke, stroke);
}
}
}
});
canvas.addEventFilter(MouseEvent.MOUSE_RELEASED,new EventHandler(){
@Override
public void handle(MouseEvent event) {
canvas.requestFocus();
mouseX = event.getX();
mouseY = event.getY();
if(event.getButton()==MouseButton.SECONDARY){
if(tempText==null){
tempText = new TextField();
tempText.setMinWidth(200);
textBox = new HBox();
textBox.getChildren().add(tempText);
webpageYPos = executeJavascript("window.scrollY");
browser.addNode(textBox, mouseX, mouseY-webpageYPos-20,100.0,20.0);
tempText.requestFocus();
tempText.setOnAction((ActionEvent ae) -> {
gc.setFill(browser.getDrawColor());
gc.setFont(Font.font(browser.getFontName(),browser.getFontSize()));
gc.fillText(tempText.getText(),mouseX,mouseY);
removeTextBox();
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
});
}//end if
}//end if
else{
if(!eraserDisable && !handDisable&&mouseStartX>0&&mouseStartY>0){
//System.out.println("Release X: " +mouseStartX+" "+mouseStartY);
gc.strokeLine(mouseStartX, mouseStartY, event.getX(), event.getY());
mouseStartX = 0;
mouseStartY = 0;
draggedCount = 0;
}
}
}
});
Hyperlink eraser = browser.getEraser();
Hyperlink hand = browser.getHandButton();
hand.setStyle("-fx-background-color: rgba(255, 255, 255, 1);");
eraser.setStyle("-fx-background-color: rgba(255, 255, 255, 1);");
// eraserDisable = false;
// eraserDisable = false;
eraser.setOnAction((ActionEvent ae)->{
if(textBox!=null) removeTextBox();
if(!eraserDisable){
eraser.setStyle("-fx-background-color: rgba(0, 100, 100, 0.5);");
hand.setStyle("-fx-background-color: rgba(255, 255, 255, 1);");
scene.setCursor(new ImageCursor(new Image("icons/eraser.png")));
//scene.setCursor(new ImageCursor(tempImage,tempImage.getWidth()/2,tempImage.getHeight()/2));
eraserDisable = true;
handDisable = false;
}else {
eraserDisable = false;
scene.setCursor(Cursor.DEFAULT);
eraser.setStyle("-fx-background-color: rgba(255, 255, 255, 1);");
}
});
hand.setOnAction((ActionEvent ae)->{
if(textBox!=null) removeTextBox();
if(!handDisable){
scene.setCursor(Cursor.OPEN_HAND);
hand.setStyle("-fx-background-color: rgba(0, 100, 100, 0.5);");
eraser.setStyle("-fx-background-color: rgba(255, 255, 255, 1);");
handDisable = true;
eraserDisable = false;
}else{
handDisable = false;
scene.setCursor(Cursor.DEFAULT);
hand.setStyle("-fx-background-color: rgba(255, 255, 255, 1);");
}
});
//End of canvas action Handler
}//end if
else{
writeButton.setText("Write");
handDisable = false;
eraserDisable = false;
browser.removeNode(canvas);
canvasOn = false;
canvas = null;
gc = null;
if(textBox != null){
removeTextBox();
}
browser.setOpacity(1);
writeBox.setVisible(false);
scene.setCursor(Cursor.DEFAULT);
}
}
});
//
//stage.setMaximized(true);
scene = new Scene(browser);
stage.setScene(scene);
stage.setResizable(false);
stage.getIcons().add(new Image("icons/paint.png"));
// apply CSS style
scene.getStylesheets().add(NewsBrowser.class.getResource("BrowserToolbar.css").toExternalForm());
stage.show();
}
// private int getHistoryIndex(String url){
// int urlNumbers = urlList.size();
// if(urlNumbers>0){
// for(int i=0; i0 && canvasYPos< 0) {
if(webpageYPos>distance)
canvasYPos += distance;
else canvasYPos=0;
}
if(distance <0 && canvasYPos > (browser.getHeight()-webpageHeight)){
if((webpageHeight-webpageYPos)>Math.abs(distance))
canvasYPos += distance;
else canvasYPos = browser.getHeight()-webpageHeight;
}
canvas.setTranslateY(canvasYPos);
webEngine.executeScript("window.scrollTo(0,"+ Math.abs(canvasYPos)+ ");");
}
// private double getWebpageHeight(){
// return webpageHeight;
// }
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}