///*
// * 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 br.com.ctecinf.swing;
//
//import java.awt.BorderLayout;
//import java.awt.Dimension;
//import java.awt.Frame;
//import java.net.Authenticator;
//import java.net.MalformedURLException;
//import java.net.PasswordAuthentication;
//import java.net.URL;
//import javafx.application.Platform;
//import javafx.beans.value.ObservableValue;
//import javafx.concurrent.Worker;
//import javafx.embed.swing.JFXPanel;
//import javafx.scene.Scene;
//import javafx.scene.web.WebEngine;
//import javafx.scene.web.WebErrorEvent;
//import javafx.scene.web.WebEvent;
//import javafx.scene.web.WebView;
//import javax.swing.BorderFactory;
//import javax.swing.JOptionPane;
//import javax.swing.JPanel;
//import javax.swing.JProgressBar;
//import javax.swing.SwingUtilities;
//
///**
// *
// * @author cassio
// */
//public abstract class BrowserPanel extends JPanel {
//
//    private JFXPanel jfxPanel = new JFXPanel();
//    private WebEngine engine;
//    private JProgressBar progressBar = new JProgressBar();
//    private WebView view;
//    private Scene scene;
//
//    protected abstract Frame getFrame();
//
//    public BrowserPanel() {
//        super(new BorderLayout());
//        initUI();
//    }
//
//    private void initUI() {
//
//        jfxPanel = new JFXPanel();
//
//        progressBar = new JProgressBar();
//        progressBar.setPreferredSize(new Dimension(150, 18));
//        progressBar.setStringPainted(true);
//
//        JPanel statusBar = new JPanel(new BorderLayout(5, 0));
//        statusBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
//        statusBar.add(progressBar, BorderLayout.EAST);
//
//        add(jfxPanel, BorderLayout.CENTER);
//        add(statusBar, BorderLayout.SOUTH);
//
//        Platform.runLater(() -> {
//            createScene();
//        });
//
//    }
//
//    private void createScene() {
//
//        view = new WebView();
//
//        engine = view.getEngine();
//        engine.setUserAgent("Mozilla/5.0 (Linux) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.37");
//        engine.setJavaScriptEnabled(true);
//
//        engine.titleProperty().addListener((ObservableValue<? extends String> observable, String oldValue, final String newValue) -> {
//            SwingUtilities.invokeLater(() -> {
//                if (getFrame() != null) {
//                    getFrame().setTitle(newValue);
//                }
//            });
//        });
//
//        engine.getLoadWorker().workDoneProperty().addListener((ObservableValue<? extends Number> observableValue, Number oldValue, final Number newValue) -> {
//            SwingUtilities.invokeLater(() -> {
//                progressBar.setValue(newValue.intValue());
//            });
//        });
//
//        engine.getLoadWorker()
//                .exceptionProperty()
//                .addListener((ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) -> {
//                    if (engine.getLoadWorker().getState() == Worker.State.FAILED) {
//                        SwingUtilities.invokeLater(() -> {
//                            JOptionPane.showMessageDialog(
//                                    this,
//                                    (value != null)
//                                            ? engine.getLocation() + "\n" + value.getMessage()
//                                            : engine.getLocation() + "\nUnexpected error.",
//                                    "Loading error...",
//                                    JOptionPane.ERROR_MESSAGE);
//                        });
//                    }
//                });
//
//        engine.setOnError((WebErrorEvent event) -> {
//            OptionPane.error(event.getException());
//        });
//
//        engine.setOnAlert((WebEvent<String> event) -> {
//            OptionPane.alert(event.getData());
//        });
//
//        engine.setConfirmHandler((String param) -> OptionPane.confirm(param));
//
//        scene = new Scene(view);
//
//        jfxPanel.setScene(scene);
//    }
//
//    public void setAuthenticator(String user, String pass) {
//        Authenticator.setDefault(new Authenticator() {
//            @Override
//            public PasswordAuthentication getPasswordAuthentication() {
//                return new PasswordAuthentication(user, pass.toCharArray());
//            }
//        });
//    }
//
//    public void loadURL(final String url) {
//
//        progressBar.setValue(0);
//        scene.setRoot(view);
//        view.setVisible(true);
//        jfxPanel.setVisible(true);
//        setVisible(true);
//
//        SwingUtilities.invokeLater(() -> {
//
//            Platform.runLater(() -> {
//                String tmp = toURL(url);
//
//                if (tmp == null) {
//                    tmp = toURL("http://" + url);
//                }
//
//                engine.load(tmp);
//            });
//        });
//    }
//
//    private static String toURL(String str) {
//        try {
//            return new URL(str).toExternalForm();
//        } catch (MalformedURLException ex) {
//            return null;
//        }
//    }
//}
