KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > amazon > 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.amazon.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  * @auther Gayan Asanka (gayan@opensource.lk)
30  */

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

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

41     private static JFrame f;
42
43     /**
44      * Used for the Simple web Browser, only one instance is needed
45      */

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

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

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

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

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

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

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

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