001: package org.enhydra.kelp.ant.swing;
002:
003: import javax.swing.*;
004:
005: import java.awt.*; //for layout managers
006: import java.awt.event.*; //for action and window events
007:
008: import java.net.URL;
009: import java.io.IOException;
010:
011: import javax.swing.event.*;
012: import javax.swing.text.html.*;
013:
014: //public class HTMLViewer extends JFrame
015: public class HTMLViewer extends JDialog implements HyperlinkListener {
016: // private final String newline = "\n";
017: private static JDialog dialog = null;
018: JEditorPane editorPane;
019: // JButton backButt;
020: // JButton forwardButt;
021: LifoURL historyBack = new LifoURL(100);
022: LifoURL historyForward = new LifoURL(100);
023:
024: public HTMLViewer(String title, URL url) {
025: super ();
026: this .setTitle(title);
027: init(url);
028: }
029:
030: public HTMLViewer(Dialog parent, String title, URL url) {
031: super (parent, title);
032: init(url);
033: }
034:
035: private void init(URL url) {
036: GridBagLayout gridBagLayout = new GridBagLayout();
037: BorderLayout layOut = new BorderLayout();
038: JPanel editorMainPane = new JPanel();
039: editorMainPane.setLayout(layOut);
040:
041: // editorMainPane.setLayout(gridBagLayout);
042:
043: // backButt = new JButton("<<");
044: // backButt.addActionListener(new ActionListener() {
045: // public void actionPerformed(ActionEvent e) {
046: // backButt_ActionPerformed(e);
047: // }
048: // });
049: // backButt.setEnabled(false);
050: // setConstr(gridBagLayout, backButt, 0,0,1,1);
051: // editorMainPane.add(backButt);
052: //
053: // forwardButt = new JButton(">>");
054: // forwardButt.addActionListener(new ActionListener() {
055: // public void actionPerformed(ActionEvent e) {
056: // forwardButt_ActionPerformed(e);
057: // }
058: // });
059: // forwardButt.setEnabled(false);
060: // setConstr(gridBagLayout, forwardButt, 1,0,1,1);
061: // editorMainPane.add(forwardButt);
062:
063: JLabel emptyLabel = new JLabel();
064: setConstr(gridBagLayout, emptyLabel, 2, 0, 1, 1);
065: editorMainPane.add(emptyLabel);
066:
067: //Create an editor pane.
068: editorPane = createEditorPane(url);
069: editorPane.addHyperlinkListener(this );
070: JScrollPane editorScrollPane = new JScrollPane(editorPane);
071: editorScrollPane
072: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
073:
074: editorScrollPane.setPreferredSize(new Dimension(750, 500));
075: editorScrollPane.setMinimumSize(new Dimension(750, 500));
076: // setConstr(gridBagLayout, editorScrollPane, 0,2,3,1);
077:
078: editorMainPane.setSize(new Dimension(800, 600));
079: // editorMainPane.setPreferredSize(new Dimension(800, 600));
080: editorMainPane.add(editorScrollPane);
081:
082: setContentPane(editorMainPane);
083:
084: }
085:
086: private void setConstr(GridBagLayout gbl, Component comp, int gx,
087: int gy, int gw, int gh) {
088: gbl.setConstraints(comp, new GridBagConstraints(gx, gy, gw, gh,
089: 0, 0, GridBagConstraints.WEST, //anchor
090: GridBagConstraints.BOTH, //fill
091: new Insets(0, 0, 0, 0), 0, 0)); //ipad
092: }
093:
094: public void hyperlinkUpdate(HyperlinkEvent e) {
095: if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
096: editorPane = (JEditorPane) e.getSource();
097: if (e instanceof HTMLFrameHyperlinkEvent) {
098: HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
099: HTMLDocument doc = (HTMLDocument) editorPane
100: .getDocument();
101: doc.processHTMLFrameHyperlinkEvent(evt);
102: } else {
103: try {
104: historyBack.add(editorPane.getPage());
105: // backButt.setEnabled(true);
106: editorPane.setPage(e.getURL());
107: } catch (Throwable t) {
108: t.printStackTrace();
109: }
110: }
111: }
112: }
113:
114: private JEditorPane createEditorPane(URL url) {
115: JEditorPane editorPane = new JEditorPane();
116: editorPane.setEditable(false);
117: // String s = null;
118: // try {
119: // s = "file:"
120: // + System.getProperty("user.dir")
121: // + System.getProperty("file.separator")
122: // + "index.htm";
123: // URL helpURL = new URL();
124: displayURL(url, editorPane);
125: // } catch (Exception e) {
126: // System.err.println("Couldn't create help URL: " + s);
127: // }
128:
129: return editorPane;
130: }
131:
132: private void displayURL(URL url, JEditorPane editorPane) {
133: try {
134: editorPane.setPage(url);
135: } catch (IOException e) {
136: System.err.println("Attempted to read a bad URL: " + url);
137: }
138: }
139:
140: // public void forwardButt_ActionPerformed(ActionEvent e) {
141: // URL currURL = historyForward.remove();
142: // URL lastURL = editorPane.getPage();
143: // displayURL(currURL, editorPane);
144: // historyBack.add(lastURL);
145: // backButt.setEnabled(true);
146: // if (historyForward.isEmpty())
147: // forwardButt.setEnabled(false);
148: // }
149:
150: // public void backButt_ActionPerformed(ActionEvent e) {
151: // URL currURL = historyBack.remove();
152: // URL lastURL = editorPane.getPage();
153: // displayURL(currURL, editorPane);
154: // historyForward.add(lastURL);
155: // forwardButt.setEnabled(true);
156: // if (historyBack.isEmpty())
157: // backButt.setEnabled(false);
158: // }
159:
160: public static void createAndShow(Dialog parent, String title,
161: URL url) {
162: if (parent == null) {
163: dialog = new HTMLViewer(title, url);
164: } else {
165: dialog = new HTMLViewer(parent, title, url);
166: }
167: dialog.addWindowListener(new WindowAdapter() {
168: public void windowClosing(WindowEvent e) {
169: dialog.dispose();
170: }
171: });
172:
173: dialog.pack();
174: dialog.show();
175: //frame.setVisible(true);
176: }
177: }
|