001: // MiniBrowser.java
002: // $Id: MiniBrowser.java,v 1.4 2000/08/16 21:37:30 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.editors;
007:
008: import java.awt.Cursor;
009: import java.awt.GridLayout;
010: import java.awt.BorderLayout;
011: import java.awt.Container;
012: import java.awt.event.WindowAdapter;
013: import java.awt.event.WindowEvent;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016:
017: import javax.swing.JPanel;
018: import javax.swing.JLabel;
019: import javax.swing.JButton;
020: import javax.swing.JScrollPane;
021: import javax.swing.JEditorPane;
022: import javax.swing.BorderFactory;
023: import javax.swing.JTextField;
024: import javax.swing.event.HyperlinkEvent;
025: import javax.swing.event.HyperlinkListener;
026:
027: import java.net.URL;
028: import java.net.MalformedURLException;
029:
030: import java.util.Vector;
031: import java.util.NoSuchElementException;
032:
033: import java.io.IOException;
034:
035: import org.w3c.jigadmin.widgets.ClosableFrame;
036:
037: import org.w3c.tools.widgets.Utilities;
038:
039: /**
040: * A mini HTML browser.
041: * @version $Revision: 1.4 $
042: * @author Benoît Mahé (bmahe@w3.org)
043: */
044: public class MiniBrowser extends ClosableFrame {
045:
046: private static MiniBrowser mbrowser = null;
047:
048: private Vector prevurls = null;
049: private Vector nexturls = null;
050:
051: protected final static String NEXT_AC = "next";
052: protected final static String PREV_AC = "prev";
053: protected final static String TEXT_AC = "text";
054:
055: protected JLabel statusBar = null;
056: protected JTextField urlField = null;
057: protected JEditorPane editor = null;
058: protected JButton next_B = null;
059: protected JButton prev_B = null;
060:
061: protected History history = null;
062: protected URL currentURL = null;
063:
064: /**
065: * The browser history.
066: */
067: class History {
068: private Vector prevurls = null;
069: private Vector nexturls = null;
070:
071: protected synchronized void add(Object obj) {
072: prevurls.addElement(obj);
073: nexturls.clear();
074: prev_B.setEnabled(true);
075: next_B.setEnabled(false);
076: }
077:
078: protected synchronized Object getPrev(Object current)
079: throws NoSuchElementException {
080: Object prev = prevurls.lastElement();
081: prevurls.removeElementAt(prevurls.size() - 1);
082: nexturls.addElement(current);
083: if (prevurls.size() > 0)
084: prev_B.setEnabled(true);
085: else
086: prev_B.setEnabled(false);
087: next_B.setEnabled(true);
088: return prev;
089: }
090:
091: protected synchronized Object getNext(Object current)
092: throws NoSuchElementException {
093: Object next = nexturls.lastElement();
094: nexturls.removeElementAt(nexturls.size() - 1);
095: prevurls.addElement(current);
096: prev_B.setEnabled(true);
097: if (nexturls.size() > 0)
098: next_B.setEnabled(true);
099: else
100: next_B.setEnabled(false);
101: return next;
102: }
103:
104: History() {
105: prevurls = new Vector(10);
106: nexturls = new Vector(10);
107: }
108: }
109:
110: /**
111: * Our internal HyperLinkListener.
112: */
113: HyperlinkListener hll = new HyperlinkListener() {
114: public void hyperlinkUpdate(HyperlinkEvent he) {
115: HyperlinkEvent.EventType type = he.getEventType();
116:
117: if (type == HyperlinkEvent.EventType.ENTERED) {
118: editor.setCursor(Cursor
119: .getPredefinedCursor(Cursor.HAND_CURSOR));
120: statusBar.setText(he.getURL().toString());
121: } else if (type == HyperlinkEvent.EventType.EXITED) {
122: editor.setCursor(Cursor.getDefaultCursor());
123: statusBar.setText(" ");
124: } else {
125: try {
126: if (currentURL != null)
127: history.add(currentURL);
128: setPage(he.getURL());
129: if (urlField != null) {
130: urlField.setText(he.getURL().toString());
131: }
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135: }
136: }
137: };
138:
139: /**
140: * Our internal ActionListener.
141: */
142: ActionListener al = new ActionListener() {
143: public void actionPerformed(ActionEvent ae) {
144: String command = ae.getActionCommand();
145: if (command.equals(PREV_AC)) {
146: try {
147: setPage((URL) history.getPrev(currentURL));
148: } catch (NoSuchElementException ex) {
149: } catch (IOException io) {
150: io.printStackTrace();
151: }
152: } else if (command.equals(NEXT_AC)) {
153: try {
154: setPage((URL) history.getNext(currentURL));
155: } catch (NoSuchElementException ex) {
156: } catch (IOException io) {
157: io.printStackTrace();
158: }
159: } else if (command.equals(TEXT_AC)) {
160:
161: history.add(currentURL);
162: try {
163: setPage(urlField.getText());
164: } catch (Exception ex) {
165: ex.printStackTrace();
166: }
167: }
168: }
169: };
170:
171: protected void build(String url) {
172: Container cont = getContentPane();
173: cont.setLayout(new BorderLayout());
174:
175: prev_B = new JButton("Prev");
176: prev_B.setActionCommand(PREV_AC);
177: prev_B.addActionListener(al);
178: prev_B.setEnabled(false);
179: next_B = new JButton("Next");
180: next_B.setActionCommand(NEXT_AC);
181: next_B.addActionListener(al);
182: next_B.setEnabled(false);
183: JPanel pb = new JPanel(new GridLayout(1, 2));
184: pb.add(prev_B);
185: pb.add(next_B);
186:
187: urlField = new JTextField(40);
188: urlField.setActionCommand(TEXT_AC);
189: urlField.addActionListener(al);
190: urlField.setBorder(BorderFactory.createLoweredBevelBorder());
191: JLabel location = new JLabel(" Location:");
192: JPanel urlp = new JPanel(new BorderLayout());
193: urlp.add(location, BorderLayout.WEST);
194: urlp.add(urlField, BorderLayout.CENTER);
195:
196: JPanel bar = new JPanel(new BorderLayout());
197: bar.add(pb, BorderLayout.WEST);
198: bar.add(urlp, BorderLayout.CENTER);
199: bar.setBorder(BorderFactory.createRaisedBevelBorder());
200:
201: statusBar = new JLabel(url);
202: statusBar.setFont(Utilities.smallFont);
203: statusBar.setBorder(BorderFactory.createRaisedBevelBorder());
204:
205: editor = new JEditorPane();
206: editor.setEditable(false);
207: editor.setEditorKitForContentType("text/html",
208: new MyHTMLEditorKit());
209: editor.addHyperlinkListener(hll);
210:
211: cont.add(bar, BorderLayout.NORTH);
212: cont.add(new JScrollPane(editor), BorderLayout.CENTER);
213: cont.add(statusBar, BorderLayout.SOUTH);
214: }
215:
216: /**
217: * Constructor.
218: * @param url The url to browse
219: * @param title the frame Title
220: * @exception MalformedURLException if the URL is an invalid URL.
221: * @exception IOException if an IO error occurs.
222: */
223: public MiniBrowser(String url, String title)
224: throws MalformedURLException, IOException {
225: super (title);
226: history = new History();
227: build(url);
228: setPage(url);
229: }
230:
231: /**
232: * Constructor.
233: * @param title the frame Title
234: * @exception MalformedURLException if the URL is an invalid URL.
235: * @exception IOException if an IO error occurs.
236: */
237: public MiniBrowser(String title) throws MalformedURLException,
238: IOException {
239: super (title);
240: history = new History();
241: build("");
242: }
243:
244: /**
245: * The frame has been closed.
246: */
247: protected void close() {
248: mbrowser = null;
249: }
250:
251: /**
252: * Set the current URL.
253: * @param url the new current url.
254: * @exception MalformedURLException if the URL is an invalid URL.
255: * @exception IOException if an IO error occurs.
256: */
257: public void setPage(String url) throws MalformedURLException,
258: IOException {
259: currentURL = new URL(url);
260: editor.setPage(currentURL);
261: urlField.setText(url);
262: }
263:
264: /**
265: * Set the current URL.
266: * @param url the new current url.
267: * @exception IOException if an IO error occurs.
268: */
269: public void setPage(URL url) throws IOException {
270: currentURL = url;
271: editor.setPage(url);
272: urlField.setText(url.toExternalForm());
273: }
274:
275: /**
276: * Show the current MiniBrowser for the given URL.
277: * @param url the new current url.
278: * @param title the frame title.
279: * @exception MalformedURLException if the URL is an invalid URL.
280: * @exception IOException if an IO error occurs.
281: */
282: public static void showDocumentationURL(String url, String title)
283: throws MalformedURLException, IOException {
284: if (mbrowser == null) {
285: mbrowser = new MiniBrowser(url, title);
286: mbrowser.setSize(600, 600);
287: mbrowser.setVisible(true);
288: } else {
289: mbrowser.history.add(mbrowser.currentURL);
290: mbrowser.setPage(url);
291: }
292: }
293:
294: public static void main(String args[]) {
295: try {
296:
297: MiniBrowser browser = null;
298: if (args.length > 0)
299: browser = new MiniBrowser(args[0], "Mini Browser");
300: else
301: browser = new MiniBrowser("Mini Browser");
302:
303: browser.addWindowListener(new WindowAdapter() {
304: public void windowClosing(WindowEvent e) {
305: System.exit(0);
306: }
307: });
308:
309: browser.setSize(600, 600);
310: browser.setVisible(true);
311: } catch (Exception ex) {
312: ex.printStackTrace();
313: }
314: }
315:
316: }
|