001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.tools;
017:
018: import net.sf.jftp.config.Settings;
019: import net.sf.jftp.gui.*;
020: import net.sf.jftp.gui.base.StatusCanvas;
021: import net.sf.jftp.gui.framework.*;
022: import net.sf.jftp.system.LocalIO;
023: import net.sf.jftp.system.logging.Log;
024: import net.sf.jftp.util.*;
025:
026: import java.awt.*;
027: import java.awt.event.*;
028:
029: import java.net.*;
030:
031: import java.util.*;
032:
033: import javax.swing.*;
034:
035: public class RSSFeeder extends JPanel implements Runnable,
036: ActionListener {
037: public static String urlstring = Settings.getRSSFeed();
038: Thread runner;
039: URL url;
040: RSSParser parser;
041: StatusCanvas can = new StatusCanvas();
042: HImageButton next = new HImageButton(Settings.nextRSSImage,
043: "nextRSS", "Display next RSS news item", this );
044: boolean header = false;
045: boolean breakHeader = false;
046: int HEADER_IVAL = 4000;
047: int LOAD_IVAL = 31 * 60000;
048:
049: //"http://www.spiegel.de/schlagzeilen/rss/0,5291,,00.xml";
050: public RSSFeeder() {
051: setLayout(new BorderLayout(0, 0));
052: next.setPreferredSize(new Dimension(22, 22));
053: next.setMaximumSize(new Dimension(22, 22));
054:
055: //next.addActionListener(this);
056: add("West", next);
057: add("Center", can);
058: setPreferredSize(new Dimension(500, 25));
059: runner = new Thread(this );
060: runner.start();
061: }
062:
063: public void switchTo(String u) {
064: if (u == null) {
065: return;
066: }
067:
068: urlstring = u;
069:
070: runner.stop();
071: runner = new Thread(this );
072: runner.start();
073: }
074:
075: public void run() {
076: long time;
077:
078: LocalIO.pause(3000);
079:
080: Log.out("Starting RSS Feed");
081:
082: try {
083: can.setInterval(10);
084: url = new URL(urlstring);
085: parser = new RSSParser(url);
086: time = System.currentTimeMillis();
087: } catch (Exception ex) {
088: Log.debug("Error: Can't load RSS feed (" + ex + ")");
089: ex.printStackTrace();
090:
091: return;
092: }
093:
094: while (true) {
095: try {
096: Enumeration e = parser.titles.elements();
097: Enumeration e2 = parser.descs.elements();
098:
099: while (e.hasMoreElements()) {
100: can.setText((String) e.nextElement());
101: next.setEnabled(true);
102: header = true;
103:
104: int i = 0;
105:
106: while (!breakHeader && (i < 100)) {
107: LocalIO.pause(HEADER_IVAL / 100);
108: i++;
109: }
110:
111: next.setEnabled(false);
112: breakHeader = false;
113: header = false;
114:
115: if (e2.hasMoreElements()) {
116: next.setEnabled(true);
117: can.scrollText((String) e2.nextElement());
118: next.setEnabled(false);
119: }
120: }
121: } catch (Exception ex) {
122: ex.printStackTrace();
123: }
124:
125: if (System.currentTimeMillis() > (LOAD_IVAL + time)) {
126: parser = new RSSParser(url);
127: time = System.currentTimeMillis();
128: }
129: }
130: }
131:
132: public void actionPerformed(ActionEvent e) {
133: if (e.getSource() == next) {
134: if (header) {
135: breakHeader = true;
136: } else {
137: can.forward();
138: }
139: }
140: }
141: }
|