Source code: com/yaftp/utils/SwingHtmlLinkListener.java
1 /**
2 *
3 * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.yaftp.utils;
21
22 import java.awt.*;
23 import javax.swing.*;
24 import javax.swing.event.*;
25
26 /**
27 SwingHtmlLinkListener.java
28 A hyperlink listener for use with JEditorPane. This
29 listener will change the cursor over hotspots based on enter/exit
30 events and also load a new page when a valid hyperlink is clicked.
31
32 @Author Jean-Yves MENGANT
33 CopyRights Jean-Yves MENGANT 1998,1999,2000
34
35 */
36
37 public class SwingHtmlLinkListener implements HyperlinkListener {
38
39 private JEditorPane _pane; // The pane we're using to display HTML
40
41 private JTextField _urlField; // An optional textfield for showing
42 // the current URL being displayed
43
44 private JLabel _statusBar; // An option label for showing where
45 // a link would take you
46
47 public SwingHtmlLinkListener(JEditorPane jep, JTextField jtf, JLabel jl) {
48 _pane = jep;
49 _urlField = jtf;
50 _statusBar = jl;
51 }
52
53 public SwingHtmlLinkListener(JEditorPane jep) {
54 this(jep, null, null);
55 }
56
57 public void hyperlinkUpdate(HyperlinkEvent he) {
58 // We'll keep some basic debuggin information in here so you can
59 // verify our new editor kit is working.
60 System.out.print("Hyperlink event started...");
61
62 HyperlinkEvent.EventType type = he.getEventType();
63 // Ok. Decide which event we got...
64 if (type == HyperlinkEvent.EventType.ENTERED) {
65 // Enter event. Go the the "hand" cursor and fill in the status bar
66 System.out.println("entered");
67 _pane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
68 _statusBar.setText(he.getURL().toString());
69 }
70 else if (type == HyperlinkEvent.EventType.EXITED) {
71 // Exit event. Go back to the default cursor and clear the status bar
72 System.out.println("exited");
73 _pane.setCursor(Cursor.getDefaultCursor());
74 _statusBar.setText(" ");
75 }
76 else {
77 // Jump event. Get the url, and if it's not null, switch to that
78 // page in the main editor pane and update the "site url" label.
79 System.out.println("activated");
80 try {
81 _pane.setPage(he.getURL());
82 if ( _urlField != null) {
83 _urlField.setText(he.getURL().toString());
84 }
85 }
86 catch (Exception e) {
87 e.printStackTrace();
88 }
89 }
90 }
91 }