KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > google > search > LinkFollower


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package sample.google.search;
18
19 import javax.swing.*;
20 import javax.swing.event.HyperlinkEvent JavaDoc;
21 import javax.swing.event.HyperlinkListener JavaDoc;
22 import java.awt.*;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * class LinkFollower
27  * Listen to HyperLink actions and Open Simple web browser to open URLs
28  *
29  * @author Gayan Asanka (gayan@opensource.lk)
30  */

31 class LinkFollower implements HyperlinkListener JavaDoc, Runnable JavaDoc {
32
33
34     /**
35      * Flag to used by the thread, If set, thread opens a URL in the Simple Web Browser
36      */

37     protected static boolean showURL = false;
38
39     /**
40      * Used as the root of the Simple web Browser, only one instance is needed
41      */

42     private static JEditorPane jep;
43
44     /**
45      * Used for the Simple web Browser, only one instance is needed
46      */

47     private static JFrame f;
48     private static JPanel contentPane;
49     private static JScrollPane scrollPane;
50
51     /**
52      * Flag to prevent duplicating Building of the Simple web browser Window
53      */

54     private static boolean builded = false;
55
56     /**
57      * Keep the URL of the last Hyperlink click event
58      */

59     private static String JavaDoc currentURL;
60
61     /**
62      * Constructor
63      */

64     public LinkFollower() {
65
66     }
67
68     /**
69      * method hyperlinkUpdate
70      * The action is to show the page of the URL the user clicked on.
71      *
72      * @param evt the event. We only care when its type is ACTIVATED.
73      */

74     public void hyperlinkUpdate(HyperlinkEvent JavaDoc evt) {
75         if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
76
77             /** Window is not built yet, so build it */
78
79             if (builded == false) {
80                 buildURLWindaw();
81             }
82             try {
83                 currentURL = evt.getURL().toString();
84                 System.out.println(currentURL);
85                 //System.out.println("Going to " + currentURL);
86
showURL = true;
87
88             } catch (Exception JavaDoc e) {
89                 System.out.println("ERROR: Trouble fetching url");
90             }
91         }
92     }
93
94     /**
95      * method setPage
96      * Open the URL in the simple web browser window by replacing the previous one
97      */

98     protected void setPage() {
99         jep.setEditable(false);
100         jep.addHyperlinkListener(new LinkFollower());
101         f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
102
103         contentPane.setLayout(new BorderLayout());
104         contentPane.setPreferredSize(new Dimension(400, 100));
105         contentPane.add(scrollPane, BorderLayout.CENTER);
106
107         f.pack();
108         f.setSize(640, 360);
109         f.setVisible(true);
110         try {
111             jep.setPage(currentURL);
112             //jep.setPage("http://www.google.com/");
113
} catch (IOException JavaDoc e) {
114             System.err.println(e);
115         }
116     }
117
118     /**
119      * method buildURLWindow
120      * Build the Simple Web Broser but not displayed
121      */

122     private void buildURLWindaw() {
123         builded = true;
124         jep = new JEditorPane();
125         f = new JFrame("Simple Web Browser");
126         contentPane = (JPanel) f.getContentPane();
127         scrollPane = new JScrollPane(jep);
128     }
129
130     /**
131      * method run
132      * check the showURL flag and if set, open the url in simple Web Browser
133      */

134     public void run() {
135         while (true) {
136             if (showURL == true) {
137                 this.setPage();
138                 showURL = false;
139             }
140             try {
141                 Thread.sleep(100);
142             } catch (InterruptedException JavaDoc e) {
143                 e.printStackTrace();
144             }
145         }
146     }
147 }
148
149
Popular Tags