HTMLViewer.java :  » J2EE » enhydra-IDE-plugin » org » enhydra » kelp » ant » swing » Java Open Source

Java Open Source » J2EE » enhydra IDE plugin 
enhydra IDE plugin » org » enhydra » kelp » ant » swing » HTMLViewer.java
package org.enhydra.kelp.ant.swing;


import javax.swing.*;

import java.awt.*;              //for layout managers
import java.awt.event.*;        //for action and window events

import java.net.URL;
import java.io.IOException;

import javax.swing.event.*;
import javax.swing.text.html.*;

//public class HTMLViewer extends JFrame
public class HTMLViewer extends JDialog
                             implements HyperlinkListener {
//    private final String newline = "\n";
    private static JDialog dialog = null;
  JEditorPane editorPane;
//  JButton backButt;
//  JButton forwardButt;
  LifoURL historyBack = new LifoURL(100);
  LifoURL historyForward = new LifoURL(100);

  public HTMLViewer(String title, URL url) {
    super();
    this.setTitle(title);
    init(url);
  }
  
    public HTMLViewer(Dialog parent, String title, URL url) {
        super(parent, title);
    init(url);
    }

  private void init(URL url) {
        GridBagLayout gridBagLayout = new GridBagLayout();
        BorderLayout layOut = new BorderLayout();
        JPanel editorMainPane = new JPanel();
        editorMainPane.setLayout(layOut);

//        editorMainPane.setLayout(gridBagLayout);

//        backButt = new JButton("<<");
//        backButt.addActionListener(new ActionListener() {
//          public void actionPerformed(ActionEvent e) {
//            backButt_ActionPerformed(e);
//          }
//        });
//        backButt.setEnabled(false);
//        setConstr(gridBagLayout, backButt, 0,0,1,1);
//        editorMainPane.add(backButt);
//
//        forwardButt = new JButton(">>");
//        forwardButt.addActionListener(new ActionListener() {
//          public void actionPerformed(ActionEvent e) {
//            forwardButt_ActionPerformed(e);
//          }
//        });
//        forwardButt.setEnabled(false);
//        setConstr(gridBagLayout, forwardButt, 1,0,1,1);
//        editorMainPane.add(forwardButt);

        JLabel emptyLabel = new JLabel();
        setConstr(gridBagLayout, emptyLabel, 2,0,1,1);
        editorMainPane.add(emptyLabel);


        //Create an editor pane.
        editorPane = createEditorPane(url);
        editorPane.addHyperlinkListener(this);
        JScrollPane editorScrollPane = new JScrollPane(editorPane);
        editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
     
        editorScrollPane.setPreferredSize(new Dimension(750, 500));
        editorScrollPane.setMinimumSize(new Dimension(750, 500));
//        setConstr(gridBagLayout, editorScrollPane, 0,2,3,1);

    editorMainPane.setSize(new Dimension(800, 600));
//    editorMainPane.setPreferredSize(new Dimension(800, 600));
        editorMainPane.add(editorScrollPane);

        setContentPane(editorMainPane);
        

  }

    private void setConstr(GridBagLayout gbl, Component comp, int gx, int gy, int gw, int gh) {
      gbl.setConstraints(comp, new GridBagConstraints(gx,gy,gw,gh,0,0,
                      GridBagConstraints.WEST, //anchor
                    GridBagConstraints.BOTH, //fill
                    new Insets(0,0,0,0),
                    0,0)); //ipad
    }

    public void hyperlinkUpdate(HyperlinkEvent e) {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      editorPane = (JEditorPane) e.getSource();
      if (e instanceof HTMLFrameHyperlinkEvent) {
        HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
        HTMLDocument doc = (HTMLDocument)editorPane.getDocument();
        doc.processHTMLFrameHyperlinkEvent(evt);
      } else {
        try {
          historyBack.add(editorPane.getPage());
//          backButt.setEnabled(true);
          editorPane.setPage(e.getURL());
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
    }
    }

    private JEditorPane createEditorPane(URL url) {
        JEditorPane editorPane = new JEditorPane();
        editorPane.setEditable(false);
//        String s = null;
//        try {
//            s = "file:"
//                + System.getProperty("user.dir")
//                + System.getProperty("file.separator")
//                + "index.htm";
//            URL helpURL = new URL();
            displayURL(url, editorPane);
//        } catch (Exception e) {
//            System.err.println("Couldn't create help URL: " + s);
//        }

        return editorPane;
    }

    private void displayURL(URL url, JEditorPane editorPane) {
        try {
            editorPane.setPage(url);
        } catch (IOException e) {
            System.err.println("Attempted to read a bad URL: " + url);
        }
    }

//    public void forwardButt_ActionPerformed(ActionEvent e) {
//        URL currURL = historyForward.remove();
//        URL lastURL = editorPane.getPage();
//        displayURL(currURL, editorPane);
//        historyBack.add(lastURL);
//        backButt.setEnabled(true);
//        if (historyForward.isEmpty())
//          forwardButt.setEnabled(false);
//    }

//    public void backButt_ActionPerformed(ActionEvent e) {
//        URL currURL = historyBack.remove();
//        URL lastURL = editorPane.getPage();
//        displayURL(currURL, editorPane);
//        historyForward.add(lastURL);
//        forwardButt.setEnabled(true);
//        if (historyBack.isEmpty())
//          backButt.setEnabled(false);
//    }

    public static void createAndShow(Dialog parent, String title, URL url) {
        if(parent == null){
          dialog = new HTMLViewer(title, url);
        }else{
          dialog = new HTMLViewer(parent, title, url);
        }
        dialog.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dialog.dispose();
            }
        });

        dialog.pack();
        dialog.show();
        //frame.setVisible(true);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.