/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * SimpleBrowser.java * * Created on Apr 11, 2012, 2:00:21 PM */ package org.pepsoft.worldpainter.browser; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JOptionPane; import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkEvent.EventType; import javax.swing.event.HyperlinkListener; import javax.swing.text.html.HTMLEditorKit; import org.pepsoft.util.IconUtils; /** * * @author pepijn */ public class SimpleBrowser extends javax.swing.JDialog implements HyperlinkListener { /** Creates new form SimpleBrowser */ public SimpleBrowser(java.awt.Frame parent, boolean modal, String title, String initialUrl) { super(parent, modal); initComponents(); setTitle(title); HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); jEditorPane1.setEditorKit(htmlEditorKit); try { jEditorPane1.setPage(initialUrl); } catch (IOException e) { throw new RuntimeException("I/O error loading page", e); } jEditorPane1.addHyperlinkListener(this); history.add(initialUrl); historySize = 1; historyPointer = 0; setLocationRelativeTo(parent); } public Action getBackAction() { return backAction; } public Action getForwardAction() { return forwardAction; } public ActionListener getActionListener() { return actionListener; } public void setActionListener(ActionListener actionListener) { this.actionListener = actionListener; } // HyperlinkListener @Override public void hyperlinkUpdate(HyperlinkEvent e) { if ((e.getEventType() == EventType.ACTIVATED) && (e.getURL().getProtocol().equals("action")) && (actionListener != null)) { ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, e.getURL().getFile()); actionListener.actionPerformed(event); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jEditorPane1 = new javax.swing.JEditorPane(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Simple Browser"); jEditorPane1.setEditable(false); jEditorPane1.setFont(new java.awt.Font("Dialog", 0, 15)); // NOI18N jEditorPane1.addHyperlinkListener(this::jEditorPane1HyperlinkUpdate); jScrollPane1.setViewportView(jEditorPane1); getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600); }// </editor-fold>//GEN-END:initComponents private void jEditorPane1HyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_jEditorPane1HyperlinkUpdate if (evt.getEventType() == EventType.ACTIVATED) { URL url = evt.getURL(); try { jEditorPane1.setPage(url); if (historyPointer == (historySize - 1)) { // At the end of the history history.add(url.toExternalForm()); historyPointer++; historySize++; } else { // Not at the end of the history; erase the tail end historyPointer++; history.set(historyPointer, url.toExternalForm()); historySize = historyPointer + 1; } backAction.setEnabled(true); forwardAction.setEnabled(false); } catch (IOException e) { JOptionPane.showMessageDialog(this, "I/O error loading page " + url, "Error Loading Page", JOptionPane.ERROR_MESSAGE); } } }//GEN-LAST:event_jEditorPane1HyperlinkUpdate private final Action backAction = new AbstractAction("back", IconUtils.loadScaledIcon("org/pepsoft/worldpainter/icons/resultset_previous.png")) { { setEnabled(false); } @Override public void actionPerformed(ActionEvent event) { if (historyPointer > 0) { historyPointer--; String url = history.get(historyPointer); try { jEditorPane1.setPage(url); if (historyPointer == 0) { setEnabled(false); } forwardAction.setEnabled(true); } catch (IOException e) { JOptionPane.showMessageDialog(SimpleBrowser.this, "I/O error loading page " + url, "Error Loading Page", JOptionPane.ERROR_MESSAGE); } } } private static final long serialVersionUID = 1L; }; private final Action forwardAction = new AbstractAction("forward", IconUtils.loadScaledIcon("org/pepsoft/worldpainter/icons/resultset_next.png")) { { setEnabled(false); } @Override public void actionPerformed(ActionEvent event) { if (historyPointer < (historySize - 1)) { historyPointer++; String url = history.get(historyPointer); try { jEditorPane1.setPage(url); if (historyPointer == (historySize - 1)) { setEnabled(false); } backAction.setEnabled(true); } catch (IOException e) { JOptionPane.showMessageDialog(SimpleBrowser.this, "I/O error loading page " + url, "Error Loading Page", JOptionPane.ERROR_MESSAGE); } } } private static final long serialVersionUID = 1L; }; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JEditorPane jEditorPane1; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables private final List<String> history = new ArrayList<>(); private int historySize, historyPointer; private ActionListener actionListener; private static final long serialVersionUID = 1L; }