package com.macfaq; import java.beans.*; import java.awt.*; import java.net.*; import java.io.*; import java.awt.event.*; import java.awt.image.*; public class URLWatcher extends Panel implements Runnable { protected URL url = null; protected int interval = 0; protected TextField theURL = new TextField(); protected TextArea thePage = new TextArea(20, 60); protected Button reloadButton = new Button("Reload"); protected ImageCanvas theImage = new ImageCanvas(); protected Label URLLabel = new Label("URL: "); // either thePage or theImage, depending on whether // or not we're looking at an image or a text page private Component loaded = thePage; public void setURL(URL url) { try { this.url = new URL(url.toString()); theURL.setText(url.toString()); reload(); } catch (MalformedURLException e) { } } public URL getURL() { return url; } public int getRefreshInterval() { return interval; } // possible customizer for instructions public void setRefreshInterval(int r) { interval = r; } public String getText() { return theURL.getText(); } public Image getImage() { return theImage.getImage(); } public Font getLabelFont() { return URLLabel.getFont(); } public void setLabelFont(Font f) { URLLabel.setFont(f); } public Font getTheURLFont() { return theURL.getFont(); } public void setTheURLFont(Font f) { theURL.setFont(f); } public Font getThePageFont() { return thePage.getFont(); } public void setThePageFont(Font f) { thePage.setFont(f); } public boolean getUserCanEditURL() { return theURL.isEditable(); } public void setUserCanEditURL(boolean allowEdits) { theURL.setEditable(allowEdits); } protected boolean showReloadButton = true; protected boolean showTheURL = true; protected Panel north = new Panel(); protected Panel south = new Panel(); public synchronized void setShowReloadButton(boolean b) { if (showReloadButton != b) { // change required if (!b) remove(south); else add("South", south); showReloadButton = b; } } public boolean getShowReloadButton() { return showReloadButton; } public synchronized void setShowTheURL(boolean b) { if (showTheURL != b) { // change required if (!b) remove(north); else add("North", north); showTheURL = b; } } public boolean getShowTheURL() { return showTheURL; } protected void init() { setLayout(new BorderLayout()); north.setLayout(new BorderLayout()); north.add("West", URLLabel); north.add("Center", theURL); reloadButton.addActionListener(new Reloader()); theURL.addActionListener(new Reloader()); south.add(reloadButton); Panel theData = new Panel(); thePage.setEditable(false); theData.add("Center", thePage); if (showReloadButton) add("South", south); add("Center", thePage); if (showTheURL) add("North", north); Thread t = new Thread(this); t.setDaemon(true); t.start(); } public URLWatcher() throws MalformedURLException { this(new URL("http://sunsite.unc.edu/nywc/"), 0); } public URLWatcher(URL u) { this(u, 0); } public URLWatcher(int refreshInterval) throws MalformedURLException { this(new URL("http://sunsite.unc.edu/nywc/"), refreshInterval); } public URLWatcher(String url) throws MalformedURLException { this(new URL(url), 0); } public URLWatcher(String url, int refreshInterval) throws MalformedURLException { this(new URL(url), refreshInterval); } public URLWatcher(URL u, int refreshInterval) { url = u; interval = refreshInterval; init(); } public synchronized void reload() { if (url != null) { try { Object o = url.getContent(); if (o instanceof InputStream) { if (loaded != thePage) { remove(loaded); add(thePage); loaded = thePage; } InputStream is = (InputStream) o; String text = readStream(is); thePage.setText(text); } else if (o instanceof ImageProducer) { if (loaded != theImage) { remove(loaded); add(theImage); loaded = theImage; } // draw the image ImageProducer ip = (ImageProducer) o; Image image = getToolkit().createImage(ip); theImage.setImage(image); theImage.repaint(); } else { if (loaded != thePage) { remove(loaded); add(thePage); loaded = thePage; } thePage.setText("Unknown Content Type: " + o.getClass()); } } catch (IOException e) { } } else { System.err.println("the null URL"); } } protected String readStream(InputStream is) { StringBuffer sb = new StringBuffer(); try { InputStreamReader isr = new InputStreamReader(is); int c; while ((c = isr.read()) != -1) { sb.append((char) c); } } catch (IOException e) { } return sb.toString(); } public void run() { while (true) { try { if (interval > 0) { Thread.sleep(interval * 60000L); reload(); } else { Thread.sleep(60000L); } } catch(InterruptedException e) { } } } public void addTextListener(TextListener l) { thePage.addTextListener(l); } public void removeTextListener(TextListener l) { thePage.removeTextListener(l); } public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { URL u = new URL(args[i]); URLWatcher u1 = new URLWatcher(); Frame f = new Frame("URLWatcher " + i); u1.setURL(u); f.add("Center", u1); f.setSize(300, 200); f.setLocation(50 + i*15, 50+i*15); f.setVisible(true); } catch (MalformedURLException e) { } } } class Reloader implements ActionListener { public void actionPerformed(ActionEvent evt) { try { setURL(new URL(theURL.getText())); reload(); } catch (MalformedURLException ex) { } } } }