KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ixenon > free > swing > HTMLViewerPane


1 /* $Id$
2  *
3  * Copyright (c) 1998 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  */

15
16 // HtmlViewerPane.java
17

18 // Description:
19
// HtmlViewerPane the JFC/Swing component
20
//
21
// Author:
22
// Peter Pilgrim
23
// Sun Jan 31 21:59:52 GMT 1999
24
//
25
// RCS HEADER
26
//
27
// $Author$
28
// $Date$
29
// $Source$
30
// $Revision$ $State$ $Locker$
31
//
32
// History
33
// ================================================================================
34
// $Log$
35

36 package ixenon.free.swing;
37
38 import java.awt.*;
39 import java.awt.event.*;
40 import java.util.*;
41 import java.io.*; // for File
42
import java.net.*; // for URL, MalformedUrlException
43

44 import javax.swing.*; // for JDialog, JFrame, JOptionPane
45
import javax.swing.event.*; // HyperlinkEvent, HyperlinkLister
46
import javax.swing.border.*; // for BevelBorder
47
import javax.swing.text.*; // for Document
48

49
50
51 /**
52  *
53  * Implementation of a HtmlViewerPane custom JFC/Swing pseudo component
54  *
55  * *INCOMPLETE*
56  *
57  * @author Peter Pilgrim Sun Jan 31 22:01:25 GMT 1999
58  * @version $Id$
59  */

60 public class HTMLViewerPane extends JPanel
61 implements ActionListener, HyperlinkListener
62 {
63     public final static boolean debug=false;
64
65     /** the default back button text */
66     public final static String JavaDoc DEFAULT_BACK_TEXT="Back";
67     /** the default back button tooltip text */
68     public final static String JavaDoc DEFAULT_BACK_TOOLTIP_TEXT="Go back";
69     /** the default next button text */
70     public final static String JavaDoc DEFAULT_NEXT_TEXT="Next";
71     /** the default next button tooltip text */
72     public final static String JavaDoc DEFAULT_NEXT_TOOLTIP_TEXT="Go next";
73     /** the default reload button text */
74     public final static String JavaDoc DEFAULT_RELOAD_TEXT="Reload";
75     /** the default reload button tooltip text */
76     public final static String JavaDoc DEFAULT_RELOAD_TOOLTIP_TEXT="Reload";
77     
78     /** combo box */
79     protected JComboBox comboBox;
80     /** back button */
81     protected JButton bt_back;
82     /** next button */
83     protected JButton bt_next;
84     /** reload button */
85     protected JButton bt_reload;
86     
87     /** editor pane */
88     protected JEditorPane editorPane;
89
90     /** the status label */
91     protected JLabel lb_status;
92
93     /** true if the viewer pane is browsable or not */
94     protected boolean browsable = true;
95
96     /** URL stack index counter */
97     protected int urlStackIndex=-1;
98
99     /** stack vector of URLs (stored in external form) */
100     protected Stack urlStack = new Stack();
101
102     // Java hex dump byte data from file:../images/go-back.gif
103
private final static byte GO_BACK_IMAGE_DATA[] = {
104     (byte)0x47, (byte)0x49, (byte)0x46, (byte)0x38, (byte)0x39, (byte)0x61, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0,
105     (byte)0xa1, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x92, (byte)0xc4, (byte)0xdb, (byte)0x4f,
106     (byte)0x86, (byte)0xdd, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x21, (byte)0xfe, (byte)0xe, (byte)0x4d, (byte)0x61,
107     (byte)0x64, (byte)0x65, (byte)0x20, (byte)0x77, (byte)0x69, (byte)0x74, (byte)0x68, (byte)0x20, (byte)0x47, (byte)0x49,
108     (byte)0x4d, (byte)0x50, (byte)0x0, (byte)0x21, (byte)0xf9, (byte)0x4, (byte)0x1, (byte)0xa, (byte)0x0, (byte)0x3,
109     (byte)0x0, (byte)0x2c, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0,
110     (byte)0x0, (byte)0x2, (byte)0x50, (byte)0x9c, (byte)0x8f, (byte)0x6, (byte)0xc9, (byte)0x8d, (byte)0x10, (byte)0x96,
111     (byte)0x9b, (byte)0x2a, (byte)0x44, (byte)0x3a, (byte)0xa1, (byte)0xbd, (byte)0x38, (byte)0xe9, (byte)0x2d, (byte)0x75,
112     (byte)0xd5, (byte)0x6, (byte)0x8a, (byte)0xc3, (byte)0x47, (byte)0x72, (byte)0x18, (byte)0x9a, (byte)0x46, (byte)0xc0,
113     (byte)0x1b, (byte)0x3e, (byte)0xed, (byte)0x5c, (byte)0xca, (byte)0xf4, (byte)0x1d, (byte)0xb2, (byte)0x77, (byte)0x2a,
114     (byte)0x1, (byte)0xc2, (byte)0xf, (byte)0xc, (byte)0xa, (byte)0x87, (byte)0xbd, (byte)0xa1, (byte)0x91, (byte)0xf8,
115     (byte)0x38, (byte)0x2a, (byte)0x5, (byte)0xb1, (byte)0xd3, (byte)0x12, (byte)0xc9, (byte)0xf0, (byte)0x3d, (byte)0x81,
116     (byte)0xcd, (byte)0x83, (byte)0x94, (byte)0x8, (byte)0x7b, (byte)0x51, (byte)0xae, (byte)0x54, (byte)0x93, (byte)0x55,
117     (byte)0x58, (byte)0xed, (byte)0x5c, (byte)0xc3, (byte)0x22, (byte)0x29, (byte)0xb9, (byte)0x7c, (byte)0x36, (byte)0xa5,
118     (byte)0xbd, (byte)0x94, (byte)0x2, (byte)0x0, (byte)0x3b,
119     };
120
121     // Java hex dump byte data from file:../images/go-forward.gif
122
private final byte GO_FORWARD_IMAGE_DATA[] = {
123     (byte)0x47, (byte)0x49, (byte)0x46, (byte)0x38, (byte)0x39, (byte)0x61, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0,
124     (byte)0xa1, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x92, (byte)0xc4, (byte)0xdb, (byte)0x4f,
125     (byte)0x86, (byte)0xdd, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x21, (byte)0xfe, (byte)0xe, (byte)0x4d, (byte)0x61,
126     (byte)0x64, (byte)0x65, (byte)0x20, (byte)0x77, (byte)0x69, (byte)0x74, (byte)0x68, (byte)0x20, (byte)0x47, (byte)0x49,
127     (byte)0x4d, (byte)0x50, (byte)0x0, (byte)0x21, (byte)0xf9, (byte)0x4, (byte)0x1, (byte)0xa, (byte)0x0, (byte)0x3,
128     (byte)0x0, (byte)0x2c, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0,
129     (byte)0x0, (byte)0x2, (byte)0x51, (byte)0x9c, (byte)0x8f, (byte)0x3, (byte)0xc9, (byte)0x8d, (byte)0x10, (byte)0x96,
130     (byte)0x9b, (byte)0x7, (byte)0x46, (byte)0x3a, (byte)0x6d, (byte)0xb8, (byte)0x98, (byte)0xe9, (byte)0x2d, (byte)0x75,
131     (byte)0xc6, (byte)0xb7, (byte)0x71, (byte)0x22, (byte)0x59, (byte)0x86, (byte)0x1e, (byte)0xc0, (byte)0xa2, (byte)0xa5,
132     (byte)0xf9, (byte)0xbc, (byte)0xf2, (byte)0xc, (byte)0x2b, (byte)0xf4, (byte)0x9d, (byte)0xc6, (byte)0x38, (byte)0x2e,
133     (byte)0x1, (byte)0xc2, (byte)0xf, (byte)0xc, (byte)0xa, (byte)0x87, (byte)0xbd, (byte)0xa1, (byte)0x91, (byte)0x58,
134     (byte)0x39, (byte)0x2a, (byte)0x5, (byte)0x2a, (byte)0xc5, (byte)0x12, (byte)0x99, (byte)0xf0, (byte)0x3d, (byte)0x81,
135     (byte)0xcd, (byte)0xa, (byte)0xe0, (byte)0x7a, (byte)0x3d, (byte)0x56, (byte)0x33, (byte)0x50, (byte)0x91, (byte)0x33,
136     (byte)0xb8, (byte)0xc5, (byte)0x48, (byte)0x99, (byte)0xde, (byte)0xe8, (byte)0x2f, (byte)0x7c, (byte)0x42, (byte)0x9f,
137     (byte)0xca, (byte)0xec, (byte)0x44, (byte)0x1, (byte)0x0, (byte)0x3b,
138     };
139
140     // Java hex dump byte data from file:../images/reload.gif
141
private final static byte RELOAD_IMAGE_DATA[] = {
142     (byte)0x47, (byte)0x49, (byte)0x46, (byte)0x38, (byte)0x39, (byte)0x61, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0,
143     (byte)0xc2, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x92, (byte)0xc4, (byte)0xdb, (byte)0x4f,
144     (byte)0x86, (byte)0xdd, (byte)0x34, (byte)0x59, (byte)0x91, (byte)0x6f, (byte)0x95, (byte)0xa5, (byte)0xff, (byte)0xff,
145     (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x21, (byte)0xfe, (byte)0xe,
146     (byte)0x4d, (byte)0x61, (byte)0x64, (byte)0x65, (byte)0x20, (byte)0x77, (byte)0x69, (byte)0x74, (byte)0x68, (byte)0x20,
147     (byte)0x47, (byte)0x49, (byte)0x4d, (byte)0x50, (byte)0x0, (byte)0x21, (byte)0xf9, (byte)0x4, (byte)0x1, (byte)0xa,
148     (byte)0x0, (byte)0x7, (byte)0x0, (byte)0x2c, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x18, (byte)0x0,
149     (byte)0x18, (byte)0x0, (byte)0x0, (byte)0x3, (byte)0x77, (byte)0x78, (byte)0xba, (byte)0xac, (byte)0xf0, (byte)0xb0,
150     (byte)0xc9, (byte)0xf6, (byte)0x82, (byte)0xbd, (byte)0x6f, (byte)0x52, (byte)0x70, (byte)0xaf, (byte)0xf8, (byte)0x2,
151     (byte)0xa0, (byte)0x1d, (byte)0x55, (byte)0x7, (byte)0x9e, (byte)0xe1, (byte)0xc4, (byte)0x99, (byte)0x28, (byte)0x98,
152     (byte)0x51, (byte)0x2c, (byte)0xa, (byte)0x1, (byte)0x83, (byte)0x28, (byte)0x71, (byte)0x2d, (byte)0x44, (byte)0xec,
153     (byte)0x43, (byte)0xad, (byte)0xca, (byte)0x36, (byte)0xc0, (byte)0x8e, (byte)0xd0, (byte)0xb3, (byte)0x31, (byte)0x44,
154     (byte)0x80, (byte)0xcf, (byte)0xcb, (byte)0xc1, (byte)0x2b, (byte)0xc2, (byte)0x90, (byte)0x46, (byte)0x66, (byte)0xd3,
155     (byte)0x77, (byte)0xc, (byte)0x44, (byte)0x1d, (byte)0xc2, (byte)0xe9, (byte)0x95, (byte)0x64, (byte)0x31, (byte)0x3e,
156     (byte)0x86, (byte)0x3d, (byte)0x27, (byte)0xac, (byte)0x4b, (byte)0xca, (byte)0x6a, (byte)0xb7, (byte)0x5c, (byte)0xcc,
157     (byte)0x90, (byte)0x18, (byte)0x46, (byte)0x3b, (byte)0x3a, (byte)0x81, (byte)0xa9, (byte)0x58, (byte)0xb3, (byte)0xea,
158     (byte)0x84, (byte)0x6b, (byte)0xee, (byte)0x45, (byte)0xdd, (byte)0xf2, (byte)0xc1, (byte)0x8f, (byte)0xaa, (byte)0x1e,
159     (byte)0x2e, (byte)0x79, (byte)0x7a, (byte)0x81, (byte)0x39, (byte)0x83, (byte)0x2b, (byte)0x2d, (byte)0x32, (byte)0x23,
160     (byte)0x38, (byte)0x86, (byte)0x11, (byte)0x8b, (byte)0x27, (byte)0x33, (byte)0x7f, (byte)0x1b, (byte)0x8e, (byte)0x92,
161     (byte)0x95, (byte)0x96, (byte)0xc, (byte)0x9, (byte)0x0, (byte)0x3b,
162     };
163     
164     /**
165      * Create a HTMLViewerPane with default configuration
166      */

167     public HTMLViewerPane()
168     {
169     initialise( DEFAULT_BACK_TEXT, DEFAULT_NEXT_TEXT, DEFAULT_RELOAD_TEXT,
170             DEFAULT_BACK_TOOLTIP_TEXT, DEFAULT_NEXT_TOOLTIP_TEXT,
171             DEFAULT_RELOAD_TOOLTIP_TEXT );
172     }
173     
174     /**
175      * Create a HTMLViewerPane with default configuration
176      * @param url the URL to be visited
177      */

178     public HTMLViewerPane( String JavaDoc url )
179     throws IOException
180     {
181     this( url, DEFAULT_BACK_TEXT, DEFAULT_NEXT_TEXT, DEFAULT_RELOAD_TEXT );
182     }
183     
184     /**
185      * Create a HTMLViewerPane
186      * @param url the URL to visit
187      * @param backText the text for the back button
188      * @param nextText the text for the next button
189      */

190     public HTMLViewerPane( String JavaDoc url,
191                String JavaDoc backText,
192                String JavaDoc nextText,
193                String JavaDoc reloadText)
194     throws IOException
195     {
196     this( url, backText, nextText, reloadText,
197           DEFAULT_BACK_TOOLTIP_TEXT, DEFAULT_NEXT_TOOLTIP_TEXT,
198           DEFAULT_RELOAD_TOOLTIP_TEXT );
199     }
200     
201     /**
202      * Create a HTMLViewerPane
203      * @param url the URL to visit
204      * @param backText the text for the back button
205      * @param nextText the text for the next button
206      * @param backToolTip the tool tip text for the back button
207      * @param nextToolTip the tool tip text for the next button
208      */

209     public HTMLViewerPane( String JavaDoc url,
210                String JavaDoc backText,
211                String JavaDoc nextText,
212                String JavaDoc reloadText,
213                String JavaDoc backToolTip,
214                String JavaDoc nextToolTip,
215                String JavaDoc reloadToolTip )
216     throws IOException
217     {
218     initialise( backText, nextText, reloadText,
219             backToolTip, nextToolTip, reloadToolTip );
220     setPage(url);
221     // Why is this here? Because Swing Model View Controller
222
// design, causes the initial URL to be added twice!!
223
comboBox.addActionListener(this);
224     }
225     
226     /** An implementation shared method that creates initial GUI */
227     protected void initialise( String JavaDoc backText,
228                 String JavaDoc nextText,
229                 String JavaDoc reloadText,
230                 String JavaDoc backToolTip,
231                 String JavaDoc nextToolTip,
232                 String JavaDoc reloadToolTip )
233     {
234     setLayout( new BorderLayout( 2,2 ));
235
236     // ======================================================================
237
// Create the button box panel
238
// ======================================================================
239
JPanel np = new JPanel();
240     np.setLayout( new BorderLayout() );
241
242     Font labelFont = new Font( "Courier", Font.BOLD, 10 );
243     Font entryFont = new Font( "Courier", Font.PLAIN, 10 );
244
245     JPanel anyPanel = new JPanel();
246     anyPanel.setLayout( new FlowLayout( FlowLayout.LEFT ));
247     bt_back = new JButton( backText );
248     bt_back.setToolTipText( backToolTip );
249     ImageIcon img1 =
250         new ImageIcon(
251         Toolkit.getDefaultToolkit().createImage(GO_BACK_IMAGE_DATA) );
252     bt_back.setIcon( img1 );
253     bt_back.setHorizontalTextPosition( SwingConstants.CENTER );
254     bt_back.setVerticalTextPosition( SwingConstants.BOTTOM );
255     bt_back.setFont( labelFont );
256     bt_back.addActionListener(this);
257     anyPanel.add(bt_back);
258
259     bt_next = new JButton( nextText );
260     bt_next.setToolTipText( nextToolTip );
261     ImageIcon img2 =
262         new ImageIcon(
263         Toolkit.getDefaultToolkit().createImage(GO_FORWARD_IMAGE_DATA) );
264     bt_next.setIcon( img2 );
265     bt_next.setHorizontalTextPosition( SwingConstants.CENTER );
266     bt_next.setVerticalTextPosition( SwingConstants.BOTTOM );
267     bt_next.setFont( labelFont );
268     bt_next.addActionListener(this);
269     anyPanel.add(bt_next);
270
271     bt_reload = new JButton( reloadText );
272     bt_reload.setToolTipText( reloadToolTip );
273     ImageIcon img3 =
274         new ImageIcon(
275         Toolkit.getDefaultToolkit().createImage(RELOAD_IMAGE_DATA) );
276     bt_reload.setIcon( img3 );
277     bt_reload.setHorizontalTextPosition( SwingConstants.CENTER );
278     bt_reload.setVerticalTextPosition( SwingConstants.BOTTOM );
279     bt_reload.setFont( labelFont );
280     bt_reload.addActionListener(this);
281     anyPanel.add(bt_reload);
282     np.add( anyPanel, BorderLayout.CENTER );
283     
284     comboBox = new JComboBox();
285     comboBox.setFont( entryFont );
286     // Why was this next line removed? Because Swing Model View
287
// Controller design, causes the initial URL to be added
288
// twice!! comboBox.addActionListener(this);
289
np.add( comboBox, BorderLayout.SOUTH );
290     
291     add( np, BorderLayout.NORTH );
292
293     // ======================================================================
294
// Create the southern button box panel
295
// ======================================================================
296
JPanel sp = new JPanel();
297     sp.setLayout( new BorderLayout() );
298
299     // Room for a progress bar to the west
300

301     lb_status = new JLabel( "-*- Status -*-" );
302     lb_status.setFont( labelFont );
303     sp.add( lb_status, BorderLayout.CENTER );
304
305     add( sp, BorderLayout.SOUTH );
306     
307     // ======================================================================
308
// Create the central HTMLViewerPane work area panel
309
// ======================================================================
310
editorPane = new JEditorPane();
311     editorPane.setFont( new Font( "Courier", Font.PLAIN, 12 ) );
312     editorPane.setEditable(false);
313     editorPane.addHyperlinkListener(this);
314     
315     JScrollPane ta_scrollable_text = new JScrollPane( editorPane ) {
316         public Dimension getPreferredSize() {
317         Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
318         Dimension bestSize;
319         if (screen.width > 950)
320             bestSize = new Dimension( 735, 765 );
321         else
322             bestSize = new Dimension( 500, 415 );
323         Dimension size = getSize();
324         if (size.width < bestSize.width )
325             size.width = bestSize.width;
326         if (size.height < bestSize.height )
327         size.height = bestSize.height;
328         return (size);
329         }
330     };
331     add("Center", ta_scrollable_text );
332     }
333     
334     /** Update the component */
335     protected void updateComponent()
336     {
337     if (browsable) {
338         bt_back.setEnabled( urlStackIndex > 0 );
339         bt_next.setEnabled( urlStackIndex < urlStack.size()-1 );
340     }
341     else {
342         bt_back.setEnabled(false);
343         bt_next.setEnabled(false);
344     }
345     
346     repaint();
347     }
348
349     /** Set the visibility of the navigation buttons
350      * and the URL chooser combo box.
351      * @param visible true if the buttons are visible
352      */

353     public void setVisibleNavigation( boolean visible )
354     {
355     bt_back.setVisible(visible);
356     bt_next.setVisible(visible);
357     bt_reload.setVisible(visible);
358     comboBox.setVisible(visible);
359     editorPane.getParent().invalidate();
360     editorPane.getParent().repaint();
361     }
362     
363     /** Go back to previous URL */
364     public void goBack()
365     {
366     if ( urlStackIndex > 0 ) {
367         int newIndex = urlStackIndex - 1;
368         String JavaDoc url = (String JavaDoc)urlStack.elementAt(newIndex);
369         try {
370         editorPane.setPage( url );
371         urlStackIndex = newIndex;
372         // System.out.println("goBack() urlStackIndex: "+urlStackIndex+" url: "+url );
373
lb_status.setText( url );
374         updateComponent();
375         }
376         catch (IOException e) {
377         System.err.println( getClass().getName()+" going forward error url: " +url );
378         }
379     }
380     }
381     
382     /** Go to next URL */
383     public void goNext()
384     {
385     if ( urlStackIndex < urlStack.size()-1 ) {
386         int newIndex = urlStackIndex + 1;
387         String JavaDoc url = (String JavaDoc)urlStack.elementAt(newIndex);
388         try {
389         editorPane.setPage( url );
390         urlStackIndex = newIndex;
391         // System.out.println("goNext() urlStackIndex: "+urlStackIndex+" url: "+url );
392
lb_status.setText( url );
393         updateComponent();
394         }
395         catch (IOException e) {
396         System.err.println( getClass().getName()+" going forward error url: " +url );
397         }
398     }
399     }
400
401     protected void pushNewURLonStack( String JavaDoc url )
402     {
403     if (debug) {
404         System.out.println( "pushNewURLonStack(`"+url+"')" );
405         System.out.println( "*V* urlStackIndex = "+urlStackIndex );
406         System.out.println( "size = "+urlStack.size() );
407     }
408     
409     if ( urlStack.size() < 1 )
410         urlStackIndex = 0;
411     if ( urlStackIndex < urlStack.size() - 1 ) {
412         // Clear off upper elements
413
for ( int j = urlStackIndex+1; j<urlStack.size(); ++j ) {
414         try {
415             urlStack.pop();
416         }
417         catch (EmptyStackException e) { ; }
418         }
419     }
420
421     urlStack.push(url);
422     urlStackIndex = urlStack.size() - 1;
423
424     if (debug) {
425         System.out.println( "*N* urlStackIndex = "+urlStackIndex );
426         System.out.println( "size = "+urlStack.size() );
427     }
428     
429     updateComponent();
430     }
431
432     /** add a URL to the combo box */
433     public void addURLToComboBox( String JavaDoc url )
434     {
435     for (int j=0; j<comboBox.getItemCount(); ++j ) {
436         String JavaDoc refUrl = (String JavaDoc)comboBox.getItemAt(j);
437         if ( url.equals(refUrl) ) {
438         // This URL already exists.
439
return;
440         }
441     }
442     
443     comboBox.addItem(url);
444     int index = comboBox.getItemCount();
445     comboBox.setSelectedIndex( index - 1 );
446     }
447     
448     /** Gets the HTMLViewerPane's page URL */
449     public URL getPage()
450     {
451     return editorPane.getPage();
452     }
453
454     /** Sets the HTMLViewerPane's page URL
455      * @param url the url to set the pane to
456      */

457     public void setPage( URL url )
458     throws IOException
459     {
460     setPage( true, url );
461     }
462     
463     /** Private implemantation method to set the HTMLViewerPane's page URL
464      * @param addToStack if true then add the url to the internal url stack.
465      * @param url the url to set the viewer pane to
466      */

467     protected void setPage( boolean addToStack, URL url )
468     throws IOException
469     {
470     try {
471         String JavaDoc extURL = url.toExternalForm();
472         // System.out.println("Before loading page:");
473
// showInfo( editorPane );
474
editorPane.setPage( url );
475         // System.out.println("After loading page:");
476
// showInfo( editorPane );
477
lb_status.setText( extURL );
478         addURLToComboBox( extURL );
479         if ( addToStack )
480         pushNewURLonStack( extURL ); // !!!
481
updateComponent();
482     }
483     catch (IOException ie) {
484         throw ie; // Rethrow the exception
485
}
486     }
487
488     /** Sets the HTMLViewerPane's page URL
489      * @param url the url to set the viewer pane to
490      */

491     public void setPage( String JavaDoc url )
492     throws IOException
493     {
494     try {
495         setPage( new URL(url) );
496     }
497     catch (MalformedURLException ue ) {
498         System.err.println("Malformed URL Exception: "+ue );
499     }
500     }
501
502     /** Return a boolean flag if the viewer is browsable */
503     public boolean isBrowsable()
504     {
505     return browsable;
506     }
507     
508     /** Sets the boolean flag if the viewer is browsable
509      * @param flag true if the viewer is browsable.
510      */

511     public void setBrowsable( boolean flag )
512     {
513     browsable=flag;
514     updateComponent();
515     }
516
517     /** Gets the HTMLViewerPane's content type */
518     public String JavaDoc getContentType()
519     {
520     return editorPane.getContentType();
521     }
522     
523     /** Sets the HTMLViewerPane's content type
524      * @param contentPane sets the viewer pane's content type
525      */

526     public void setContentType( String JavaDoc contentType )
527     throws IOException
528     {
529     editorPane.setContentType( contentType );
530     }
531
532
533     /** get reference back button object*/
534     public JButton getBackButton()
535     {
536     return bt_back;
537     }
538
539     /** convenience method to get back button text */
540     public String JavaDoc getBackButtonText()
541     {
542     return bt_back.getText();
543     }
544
545     /** convenience method to set back button text */
546     public void setBackButtonText( String JavaDoc text )
547     {
548     bt_back.setText(text);
549     }
550
551     /** convenience method to get back button tool tip text */
552     public String JavaDoc getBackButtonToolTipText()
553     {
554     return bt_back.getToolTipText();
555     }
556
557     /** convenience method to set back button tool tip text */
558     public void setBackButtonToolTipText( String JavaDoc tooltip_text )
559     {
560     bt_back.setToolTipText(tooltip_text);
561     }
562     
563     /** convenience method to get back button icon */
564     public Icon getBackButtonIcon()
565     {
566     return bt_back.getIcon();
567     }
568
569     /** convenience method to set back button icon */
570     public void setBackButtonIcon( Icon icon )
571     {
572     bt_back.setIcon(icon);
573     }
574
575     
576     /** get reference next button object*/
577     public JButton getNextButton()
578     {
579     return bt_next;
580     }
581
582     /** convenience method to get next button text */
583     public String JavaDoc getNextButtonText()
584     {
585     return bt_next.getText();
586     }
587
588     /** convenience method to set next button text */
589     public void setNextButtonText( String JavaDoc text )
590     {
591     bt_next.setText(text);
592     }
593
594     /** convenience method to get next button tool tip text */
595     public String JavaDoc getNextButtonToolTipText()
596     {
597     return bt_next.getToolTipText();
598     }
599
600     /** convenience method to set next button tool tip text */
601     public void setNextButtonToolTipText( String JavaDoc tooltip_text )
602     {
603     bt_next.setToolTipText(tooltip_text);
604     }
605     
606     /** convenience method to get next button icon */
607     public Icon getNextButtonIcon()
608     {
609     return bt_next.getIcon();
610     }
611
612     /** convenience method to set next button icon */
613     public void setNextButtonIcon( Icon icon )
614     {
615     bt_next.setIcon(icon);
616     }
617
618     
619     /** get reference reload button object*/
620     public JButton getReloadButton()
621     {
622     return bt_reload;
623     }
624
625     /** convenience method to get reload button text */
626     public String JavaDoc getReloadButtonText()
627     {
628     return bt_reload.getText();
629     }
630
631     /** convenience method to set reload button text */
632     public void setReloadButtonText( String JavaDoc text )
633     {
634     bt_reload.setText(text);
635     }
636
637     /** convenience method to get reload button tool tip text */
638     public String JavaDoc getReloadButtonToolTipText()
639     {
640     return bt_reload.getToolTipText();
641     }
642
643     /** convenience method to set reload button tool tip text */
644     public void setReloadButtonToolTipText( String JavaDoc tooltip_text )
645     {
646     bt_reload.setToolTipText(tooltip_text);
647     }
648     
649     /** convenience method to get reload button icon */
650     public Icon getReloadButtonIcon()
651     {
652     return bt_reload.getIcon();
653     }
654
655     /** convenience method to set reload button icon */
656     public void setReloadButtonIcon( Icon icon )
657     {
658     bt_reload.setIcon(icon);
659     }
660
661     protected void enableBrowsing( boolean flag )
662     {
663     comboBox.setEnabled(flag);
664     bt_reload.setEnabled(flag);
665     bt_back.setEnabled(flag);
666     bt_next.setEnabled(flag);
667     if (flag)
668         updateComponent();
669     }
670
671     // ======================================================================
672
// EVENT HANDLING
673
// ======================================================================
674

675     public void actionPerformed( ActionEvent e )
676     {
677     Object JavaDoc src = e.getSource();
678     if ( src == bt_back ) {
679         goBack();
680     }
681     else if ( src == bt_next ) {
682         goNext();
683     }
684     else if ( src == bt_reload ) {
685         Cursor c = editorPane.getCursor();
686         Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
687         editorPane.setCursor(waitCursor);
688         enableBrowsing(false);
689         try { editorPane.setPage(""); } catch (IOException xyz) { ; }
690         
691         SwingUtilities.invokeLater(
692         new PageLoader( editorPane.getPage(), c, false));
693     }
694     else if ( src == comboBox ) {
695         int index = comboBox.getSelectedIndex();
696         if (index >= 0 ) {
697         String JavaDoc urlExt = (String JavaDoc)comboBox.getItemAt(index);
698         if (browsable) {
699             try {
700             // editorPane.setPage( urlExt );
701
Cursor c = editorPane.getCursor();
702             Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
703             editorPane.setCursor(waitCursor);
704             enableBrowsing(false);
705             SwingUtilities.invokeLater(
706                 new PageLoader( new URL(urlExt), c, true));
707             }
708             catch ( IOException ioe) { ; }
709         }
710         else
711             lb_status.setText( urlExt );
712         }
713     }
714     }
715
716     public void hyperlinkUpdate( HyperlinkEvent evt )
717     {
718     if ( evt.getEventType() == HyperlinkEvent.EventType.ENTERED )
719         lb_status.setText( evt.getURL().toString() );
720     else if ( evt.getEventType() == HyperlinkEvent.EventType.EXITED )
721         lb_status.setText( "-*- Status -*-" );
722     else if ( evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED ) {
723
724         if (browsable) {
725         Cursor c = editorPane.getCursor();
726         Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
727         editorPane.setCursor(waitCursor);
728         enableBrowsing(false);
729         SwingUtilities.invokeLater(
730             new PageLoader( evt.getURL(), c, true));
731         }
732         else
733         lb_status.setText( evt.getURL().toString() );
734     }
735     }
736     
737     public static void showInfo( JEditorPane ep )
738     {
739     System.out.println("Document type: " + ep.getContentType() );
740     System.out.println("Editor Kit: " + ep.getEditorKit().getClass().getName() );
741     System.out.println("Document Type: " + ep.getDocument().getClass().getName() );
742     System.out.println("----------------------------------------");
743     }
744     
745     // ======================================================================
746
// INNER CLASSES
747
// ======================================================================
748

749     /**
750      * Borrowed from the SwingSet Metalworks collection
751      * temporary class that loads synchronously (although
752      * later than the request so that a cursor change
753      * can be done).
754      */

755     class PageLoader implements Runnable JavaDoc {
756
757         private URL url;
758         private Cursor cursor;
759     private boolean addToStack;
760     
761         PageLoader(URL u, Cursor c, boolean a ) {
762             url = u;
763             cursor = c;
764         addToStack = a;
765         }
766
767         public void run() {
768             if (url == null) {
769                 // restore the original cursor
770
editorPane.setCursor(cursor);
771
772                 // PENDING(prinz) remove this hack when
773
// automatic validation is activated.
774
Container parent = editorPane.getParent();
775                 parent.repaint();
776             }
777         else {
778         String JavaDoc oldURL = lb_status.getText();
779                 Document doc = editorPane.getDocument(); // Save old document
780
try {
781             lb_status.setText( url.toExternalForm() );
782             setPage(addToStack, url );
783                 }
784         catch (IOException ioe) {
785             lb_status.setText("Failed to load URL: "+url.toExternalForm() );
786                     editorPane.setDocument(doc);
787                     getToolkit().beep();
788                 }
789         finally {
790                     // schedule the cursor to revert after
791
// the paint has happended.
792
url = null;
793             enableBrowsing(true);
794                     SwingUtilities.invokeLater(this);
795                 }
796             }
797         }
798
799     }
800
801
802
803     public static void main( String JavaDoc [] args )
804     {
805     JFrame frame = new JFrame("HTMLViewerPane Demonstration");
806     frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
807     if (args.length != 1) {
808         System.out.println("USAGE: java HTMLViewerPane <URL>");
809         System.exit(69);
810     }
811     
812     try {
813         String JavaDoc url = args[0];
814         File file = new File(url);
815         if ( file.exists() )
816         url = "file:"+file.getAbsolutePath();
817         System.out.println("starting URL: `"+url+"'");
818         HTMLViewerPane pane = new HTMLViewerPane( url );
819         frame.getContentPane().add( pane );
820         // pane.setBackButtonIcon( new ImageIcon( "images/my-back.gif" ));
821
// pane.setNextButtonIcon( new ImageIcon( "images/my-next.gif" ));
822
frame.pack();
823         frame.setVisible(true);
824     }
825     catch (IOException e) {
826         System.out.println(e);
827         System.exit(69);
828     }
829     }
830 }
831
832 // fini
833
Popular Tags