KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > swing > HTMLViewer


1 package org.enhydra.kelp.ant.swing;
2
3
4 import javax.swing.*;
5
6 import java.awt.*; //for layout managers
7
import java.awt.event.*; //for action and window events
8

9 import java.net.URL JavaDoc;
10 import java.io.IOException JavaDoc;
11
12 import javax.swing.event.*;
13 import javax.swing.text.html.*;
14
15 //public class HTMLViewer extends JFrame
16
public class HTMLViewer extends JDialog
17                              implements HyperlinkListener {
18 // private final String newline = "\n";
19
private static JDialog dialog = null;
20     JEditorPane editorPane;
21 // JButton backButt;
22
// JButton forwardButt;
23
LifoURL historyBack = new LifoURL(100);
24     LifoURL historyForward = new LifoURL(100);
25
26     public HTMLViewer(String JavaDoc title, URL JavaDoc url) {
27         super();
28         this.setTitle(title);
29         init(url);
30     }
31     
32     public HTMLViewer(Dialog parent, String JavaDoc title, URL JavaDoc url) {
33         super(parent, title);
34         init(url);
35     }
36
37     private void init(URL JavaDoc url) {
38         GridBagLayout gridBagLayout = new GridBagLayout();
39         BorderLayout layOut = new BorderLayout();
40         JPanel editorMainPane = new JPanel();
41         editorMainPane.setLayout(layOut);
42
43 // editorMainPane.setLayout(gridBagLayout);
44

45 // backButt = new JButton("<<");
46
// backButt.addActionListener(new ActionListener() {
47
// public void actionPerformed(ActionEvent e) {
48
// backButt_ActionPerformed(e);
49
// }
50
// });
51
// backButt.setEnabled(false);
52
// setConstr(gridBagLayout, backButt, 0,0,1,1);
53
// editorMainPane.add(backButt);
54
//
55
// forwardButt = new JButton(">>");
56
// forwardButt.addActionListener(new ActionListener() {
57
// public void actionPerformed(ActionEvent e) {
58
// forwardButt_ActionPerformed(e);
59
// }
60
// });
61
// forwardButt.setEnabled(false);
62
// setConstr(gridBagLayout, forwardButt, 1,0,1,1);
63
// editorMainPane.add(forwardButt);
64

65         JLabel emptyLabel = new JLabel();
66         setConstr(gridBagLayout, emptyLabel, 2,0,1,1);
67         editorMainPane.add(emptyLabel);
68
69
70         //Create an editor pane.
71
editorPane = createEditorPane(url);
72         editorPane.addHyperlinkListener(this);
73         JScrollPane editorScrollPane = new JScrollPane(editorPane);
74         editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
75         
76         editorScrollPane.setPreferredSize(new Dimension(750, 500));
77         editorScrollPane.setMinimumSize(new Dimension(750, 500));
78 // setConstr(gridBagLayout, editorScrollPane, 0,2,3,1);
79

80         editorMainPane.setSize(new Dimension(800, 600));
81 // editorMainPane.setPreferredSize(new Dimension(800, 600));
82
editorMainPane.add(editorScrollPane);
83
84         setContentPane(editorMainPane);
85         
86
87     }
88
89     private void setConstr(GridBagLayout gbl, Component comp, int gx, int gy, int gw, int gh) {
90         gbl.setConstraints(comp, new GridBagConstraints(gx,gy,gw,gh,0,0,
91                                         GridBagConstraints.WEST, //anchor
92
GridBagConstraints.BOTH, //fill
93
new Insets(0,0,0,0),
94                                         0,0)); //ipad
95
}
96
97     public void hyperlinkUpdate(HyperlinkEvent e) {
98         if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
99             editorPane = (JEditorPane) e.getSource();
100             if (e instanceof HTMLFrameHyperlinkEvent) {
101                 HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e;
102                 HTMLDocument doc = (HTMLDocument)editorPane.getDocument();
103                 doc.processHTMLFrameHyperlinkEvent(evt);
104             } else {
105                 try {
106                     historyBack.add(editorPane.getPage());
107 // backButt.setEnabled(true);
108
editorPane.setPage(e.getURL());
109                 } catch (Throwable JavaDoc t) {
110                     t.printStackTrace();
111                 }
112             }
113         }
114     }
115
116     private JEditorPane createEditorPane(URL JavaDoc url) {
117         JEditorPane editorPane = new JEditorPane();
118         editorPane.setEditable(false);
119 // String s = null;
120
// try {
121
// s = "file:"
122
// + System.getProperty("user.dir")
123
// + System.getProperty("file.separator")
124
// + "index.htm";
125
// URL helpURL = new URL();
126
displayURL(url, editorPane);
127 // } catch (Exception e) {
128
// System.err.println("Couldn't create help URL: " + s);
129
// }
130

131         return editorPane;
132     }
133
134     private void displayURL(URL JavaDoc url, JEditorPane editorPane) {
135         try {
136             editorPane.setPage(url);
137         } catch (IOException JavaDoc e) {
138             System.err.println("Attempted to read a bad URL: " + url);
139         }
140     }
141
142 // public void forwardButt_ActionPerformed(ActionEvent e) {
143
// URL currURL = historyForward.remove();
144
// URL lastURL = editorPane.getPage();
145
// displayURL(currURL, editorPane);
146
// historyBack.add(lastURL);
147
// backButt.setEnabled(true);
148
// if (historyForward.isEmpty())
149
// forwardButt.setEnabled(false);
150
// }
151

152 // public void backButt_ActionPerformed(ActionEvent e) {
153
// URL currURL = historyBack.remove();
154
// URL lastURL = editorPane.getPage();
155
// displayURL(currURL, editorPane);
156
// historyForward.add(lastURL);
157
// forwardButt.setEnabled(true);
158
// if (historyBack.isEmpty())
159
// backButt.setEnabled(false);
160
// }
161

162     public static void createAndShow(Dialog parent, String JavaDoc title, URL JavaDoc url) {
163         if(parent == null){
164             dialog = new HTMLViewer(title, url);
165         }else{
166             dialog = new HTMLViewer(parent, title, url);
167         }
168         dialog.addWindowListener(new WindowAdapter() {
169             public void windowClosing(WindowEvent e) {
170                 dialog.dispose();
171             }
172         });
173
174         dialog.pack();
175         dialog.show();
176         //frame.setVisible(true);
177
}
178 }
179
Popular Tags