KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > tools > RSSFeeder


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package net.sf.jftp.tools;
17
18 import net.sf.jftp.config.Settings;
19 import net.sf.jftp.gui.*;
20 import net.sf.jftp.gui.base.StatusCanvas;
21 import net.sf.jftp.gui.framework.*;
22 import net.sf.jftp.system.LocalIO;
23 import net.sf.jftp.system.logging.Log;
24 import net.sf.jftp.util.*;
25
26 import java.awt.*;
27 import java.awt.event.*;
28
29 import java.net.*;
30
31 import java.util.*;
32
33 import javax.swing.*;
34
35
36 public class RSSFeeder extends JPanel implements Runnable JavaDoc, ActionListener
37 {
38     public static String JavaDoc urlstring = Settings.getRSSFeed();
39     Thread JavaDoc runner;
40     URL url;
41     RSSParser parser;
42     StatusCanvas can = new StatusCanvas();
43     HImageButton next = new HImageButton(Settings.nextRSSImage, "nextRSS",
44                                          "Display next RSS news item", this);
45     boolean header = false;
46     boolean breakHeader = false;
47     int HEADER_IVAL = 4000;
48     int LOAD_IVAL = 31 * 60000;
49
50     //"http://www.spiegel.de/schlagzeilen/rss/0,5291,,00.xml";
51
public RSSFeeder()
52     {
53         setLayout(new BorderLayout(0, 0));
54         next.setPreferredSize(new Dimension(22, 22));
55         next.setMaximumSize(new Dimension(22, 22));
56
57         //next.addActionListener(this);
58
add("West", next);
59         add("Center", can);
60         setPreferredSize(new Dimension(500, 25));
61         runner = new Thread JavaDoc(this);
62         runner.start();
63     }
64
65     public void switchTo(String JavaDoc u)
66     {
67         if(u == null)
68         {
69             return;
70         }
71
72         urlstring = u;
73
74         runner.stop();
75         runner = new Thread JavaDoc(this);
76         runner.start();
77     }
78
79     public void run()
80     {
81         long time;
82
83         LocalIO.pause(3000);
84
85         Log.out("Starting RSS Feed");
86
87         try
88         {
89             can.setInterval(10);
90             url = new URL(urlstring);
91             parser = new RSSParser(url);
92             time = System.currentTimeMillis();
93         }
94         catch(Exception JavaDoc ex)
95         {
96             Log.debug("Error: Can't load RSS feed (" + ex + ")");
97             ex.printStackTrace();
98
99             return;
100         }
101
102         while(true)
103         {
104             try
105             {
106                 Enumeration e = parser.titles.elements();
107                 Enumeration e2 = parser.descs.elements();
108
109                 while(e.hasMoreElements())
110                 {
111                     can.setText((String JavaDoc) e.nextElement());
112                     next.setEnabled(true);
113                     header = true;
114
115                     int i = 0;
116
117                     while(!breakHeader && (i < 100))
118                     {
119                         LocalIO.pause(HEADER_IVAL / 100);
120                         i++;
121                     }
122
123                     next.setEnabled(false);
124                     breakHeader = false;
125                     header = false;
126
127                     if(e2.hasMoreElements())
128                     {
129                         next.setEnabled(true);
130                         can.scrollText((String JavaDoc) e2.nextElement());
131                         next.setEnabled(false);
132                     }
133                 }
134             }
135             catch(Exception JavaDoc ex)
136             {
137                 ex.printStackTrace();
138             }
139
140             if(System.currentTimeMillis() > (LOAD_IVAL + time))
141             {
142                 parser = new RSSParser(url);
143                 time = System.currentTimeMillis();
144             }
145         }
146     }
147
148     public void actionPerformed(ActionEvent e)
149     {
150         if(e.getSource() == next)
151         {
152             if(header)
153             {
154                 breakHeader = true;
155             }
156             else
157             {
158                 can.forward();
159             }
160         }
161     }
162 }
163
Popular Tags