Source code: dtk/gui/TKHtmlWindow.java
1 //Title: D3E ToolKit
2 //Version:
3 //Copyright: See accompanying license agreement
4 //Author: John Weatherley
5 //Company:
6 //Description: D3E Toolkit
7
8 package dtk.gui;
9
10 import dtk.core.*;
11 import javax.swing.*;
12 import javax.swing.event.*;
13 import javax.swing.text.*;
14 import javax.swing.text.html.*;
15 import javax.swing.border.*;
16 import javax.swing.colorchooser.*;
17 import javax.swing.filechooser.*;
18 import javax.accessibility.*;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import java.beans.*;
23 import java.util.*;
24 import java.io.*;
25 import java.net.*;
26
27 /**
28 This class provides a window that displays an HTML Document. May be used for documents that
29 have many links to local and remote URL's and may have frames, etc.
30 */
31 abstract class TKHtmlWindow extends JFrame implements ActionListener {
32
33 String fileName;
34 JEditorPane htmlPane;
35 JScrollPane editorScrollPane;
36 JPanel panel = new JPanel();
37 URL theURL;
38 Dimension windowDimension;
39 JButton closeButton;
40
41 /**
42 Construct a document window to display HTML. If
43 no frame is passed in, a frame will also be created
44 to display the document in.
45
46 @param owner The frame that will hold this HTML document.
47 If null then a frame will be created.
48 @param title The title appearin on the frame's window.
49 @param w Window width.
50 @param h Window height.
51 @param resizable True = window can be resized, false = window not resizable.
52 @param fileDisplayed The HTML or TXT file to be displayed.
53 */
54 TKHtmlWindow(Frame owner, // Can be null
55 String title,
56 int w, int h,
57 boolean resizable,
58 String fileDisplayed)
59 {
60 super(title);
61 this.setResizable(resizable);
62 fileName = fileDisplayed;
63
64 windowDimension = new Dimension(w, h);
65
66 //Create an editor pane used to display HTML text:
67 createEditorPane();
68 }
69
70 /** Simple constructor provides no initialization.*/
71 TKHtmlWindow()
72 {
73 }
74
75 /**Designates the file to be displayed in this window*/
76 abstract public void setPage(String fName);
77
78 /**Create an uneditable EditorPane, used for Displaying HTML text.*/
79 private void createEditorPane()
80 {
81
82 htmlPane = new JEditorPane();
83 closeButton = new JButton("Close");
84 closeButton.addActionListener(this);
85
86 this.setPage(fileName);
87 panel.setLayout(new BorderLayout());
88
89 // Set up a window for the HTML pages:
90 htmlPane.setEditable(false);
91 htmlPane.addHyperlinkListener(createHyperLinkListener());
92 editorScrollPane = new JScrollPane();
93 JViewport vp = editorScrollPane.getViewport();
94 vp.add(htmlPane);
95
96
97
98 // Make a panel to hold the button:
99 JPanel buttonpanel = new JPanel();
100 buttonpanel.setOpaque(false);
101 JButton button = (JButton) buttonpanel.add(closeButton);
102 panel.add(buttonpanel, BorderLayout.SOUTH);
103
104 panel.add(editorScrollPane,BorderLayout.CENTER);
105 this.getContentPane().add(panel);
106 editorScrollPane.setPreferredSize(windowDimension);
107 TKToolkit.frame.setComponentSize(editorScrollPane);
108 this.pack();
109 }
110
111 /**
112 Open a url for display.
113
114 @param url The url to display.
115 @param JEditorPane The JEditorPane in which to display.
116 */
117 private void displayURL(URL url, JEditorPane htmlPane)
118 {
119 try
120 {
121 htmlPane.setPage(url);
122 } catch (IOException e)
123 {
124 TKLog.error("Could not read the following file: " + url);
125 }
126 }
127
128 /**
129 Button events on the "close button."
130 */
131 public void actionPerformed(ActionEvent e)
132 {
133 JButton source = (JButton)(e.getSource());
134 String buttonName = source.getText();
135
136 if( buttonName == "Close" )
137 this.setVisible(false);
138 }
139
140
141
142 /**
143 Listen for and handle hypertext events on the links found in the HTML:
144 */
145 public HyperlinkListener createHyperLinkListener()
146 {
147 return new HyperlinkListener()
148 {
149 public void hyperlinkUpdate(HyperlinkEvent linkEvent)
150 {
151
152 if (linkEvent.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
153 {
154 try
155 {
156
157 if (linkEvent instanceof HTMLFrameHyperlinkEvent)
158 {
159 ((HTMLDocument)htmlPane.getDocument()).processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent)linkEvent);
160 }
161 else
162 {
163 try
164 {
165 htmlPane.setPage(linkEvent.getURL());
166 } catch (IOException ioe)
167 {
168 TKLog.error("IOE: " + ioe);
169 }
170 }
171 } catch (Exception e)
172 {
173 TKLog.error("Problem displaying the html: " + e);
174 }
175 }
176 }
177 };
178 }
179 }
180
181
182