001 /* PAVLOV -- Multiple Choice Study System
002 * Copyright (C) 2000 - 2004 T.J. Willis
003 *
004 * This program is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU General Public License
006 * as published by the Free Software Foundation; either version 2
007 * of the License, or (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, write to the Free Software
016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017 *
018 * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/swing/HTMLInternalFrame.java,v 1.11 2004/07/01 05:50:21 tj_willis Exp $
019 */
020 package net.sourceforge.pavlov.swing;
021
022
023 import javax.swing.*;
024 import javax.swing.event.*;
025 import javax.swing.text.html.*;
026
027 import java.awt.*;
028 import java.awt.event.*;
029 import java.io.*;
030 import java.net.*;
031 import org.apache.log4j.*;
032 import java.util.*;
033
034 public class HTMLInternalFrame
035 extends JDialog
036 implements ActionListener, HyperlinkListener
037 {
038
039 JEditorPane html;
040 boolean reload = false;
041 URL myURL;
042 Vector<URL> urls;
043 JComboBox urlBox;
044
045 public HTMLInternalFrame(String title, String url) {
046 super((JFrame)null,title);//,true,true,true,true);
047 URL u = null;
048 try {
049 u = new URL( url);
050 } catch (Exception e) {
051 reload = true;
052 }
053 myURL = u;
054 init(u);
055
056 }
057
058 /**
059 * Create a HTMLInternalFrame with the given title and initial URL.
060 */
061 public HTMLInternalFrame(String title, URL url) {
062 super((JFrame)null,title);//,true,true,true,true);
063 myURL = url;
064 init(url);
065 }
066
067 private void init(URL url)
068 {
069
070 html = new JEditorPane();
071 html.setContentType("text/html");
072 html.setEditable(false);
073 html.addHyperlinkListener(this);//createHyperLinkListener());
074 urls = new Vector<URL>();
075 JLabel urlLab = new JLabel("URL:");
076 Box b = new Box(BoxLayout.X_AXIS);
077 urlBox = new JComboBox();
078 urlBox.setEditable(false);
079 urlBox.addActionListener(this);
080 b.add(urlLab);
081 b.add(urlBox);
082 setURL( url);
083 getContentPane().add(b,BorderLayout.NORTH);
084 setDefaultCloseOperation(HIDE_ON_CLOSE);
085 JScrollPane scroller = new JScrollPane();
086 JViewport vp = scroller.getViewport();
087 vp.add(html);
088 getContentPane().add(scroller, BorderLayout.CENTER);
089 /*
090 //this.setBounds
091 this.setVisible(true);
092 invalidate();
093 pack();
094 setVisible(false);
095 invalidate();
096 validate();
097 setVisible(true);
098 invalidate();
099 validate();
100 */
101 pack(); //setSize(getPreferredSize());
102 HTMLEditorKit kit = (HTMLEditorKit)html.getEditorKit();
103 kit.setAutoFormSubmission(true);
104 //html.setEditorKit(kit);
105 }
106
107 public void setVisible(boolean x)
108 {
109 if(x==true && reload == true){
110 setURL(myURL);
111 }
112 super.setVisible(x);
113 }
114
115
116
117
118 public void setURL(URL url)
119 {
120 try {
121 if(url != null) {
122 urls.remove(url);
123 urls.add(0,url);
124 urlBox.removeItem(url);
125 urlBox.addItem(url);
126 urlBox.setSelectedItem(url);
127 html.setPage(url);
128 } else {
129 html.setText(niceError("Null URL",url));
130 }
131 pack();
132 } catch (MalformedURLException e) {
133 html.setText(niceError("Malformed URL: " + e,url));
134 reload = true;
135 } catch (IOException e) {
136 html.setText(niceError("IOException: " + e,url));
137 reload = true;
138 } catch (Exception e) {
139 html.setText(niceError("Exception: " + e,url));
140 reload = true;
141 }
142
143 }
144
145 protected String niceError(String msg, URL u)
146 {
147 String buf = new String();
148 buf +="<HTML><HEAD><TITLE>ERROR RETRIEVING PAGE</TITLE></HEAD>";
149 buf +="<BODY><H2>ERROR RETRIEVING PAGE</H2>";
150 buf +="<HR>";
151 buf +="Attempting to load page: " + u +"<BR>";
152 buf +="Error is: " + msg;
153 buf +="</BODY></HTML>";
154 return buf.toString();
155
156 }
157
158 // public HyperlinkListener createHyperLinkListener() {
159 // return new HyperlinkListener() {
160 public void hyperlinkUpdate(HyperlinkEvent e) {
161 //System.out.println("event type="+e.getEventType());
162 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
163 if( e instanceof FormSubmitEvent ){
164 System.out.println("Caught FormSubmit");
165 return;
166 } else if (e instanceof HTMLFrameHyperlinkEvent) {
167 HTMLDocument doc = (HTMLDocument)html.getDocument();
168 doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent)e);
169 } else {
170 myURL = e.getURL();
171 setURL(myURL);
172 }
173 }
174 }
175 // };
176 // }
177
178 public java.awt.Dimension getPreferredSize()
179 {
180 java.awt.Dimension d = html.getPreferredSize();
181 d.width+=20;
182 if(d.width<510) d.width=510;
183 d.height+=30;
184 if(d.height<510) d.height=510;
185 return d;
186 }
187
188 public java.awt.Dimension getMinimumSize()
189 {
190 java.awt.Dimension d = html.getMinimumSize();
191 d.width+=20;
192 d.height+=30;
193 return d;
194 }
195
196
197 protected void closeDialog()
198 {
199 setVisible(false);
200 }
201
202 public void actionPerformed(ActionEvent e){
203 if(e.getSource().equals(urlBox)){
204 //System.out.println("ActionCommand = " + e.getActionCommand());
205 Object sel = urlBox.getSelectedItem();
206 //System.out.println("New Selection = " + sel);
207 if(sel instanceof URL){
208 setURL((URL)sel);
209 }
210 }
211
212 }
213 }