1 package com.sun.syndication.samples;
2
3 import com.sun.syndication.feed.synd.SyndFeed;
4 import com.sun.syndication.io.SyndFeedInput;
5
6 import java.applet.Applet;
7 import java.awt.*;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.io.InputStreamReader;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13
14
15
16
17
18
19
20
21
22
23 /***
24 * @author Ken Kast
25 *
26 */
27 public class RomeApplet extends Applet implements ActionListener {
28
29 private static final int fieldWidth = 80;
30 private URL home = null;
31 private Label feedLabel;
32 private TextArea feedText;
33 private Button fetchButton;
34 private Checkbox fileButton;
35 private CheckboxGroup fileURLButtons;
36 private TextField urlField;
37 private Checkbox urlButton;
38 private GridBagLayout layout = new GridBagLayout();
39 private GridBagConstraints constraints = new GridBagConstraints();
40
41 public void init() {
42
43
44
45 home = getCodeBase();
46
47
48
49
50
51
52
53 setSize(10 * fieldWidth, 450);
54 setBackground(Color.lightGray);
55 setLayout(layout);
56 fileURLButtons = new CheckboxGroup();
57 urlButton = new Checkbox("URL", fileURLButtons, true);
58 add(urlButton);
59 fileButton = new Checkbox("File", fileURLButtons, false);
60 add(fileButton);
61 urlField = new TextField("", fieldWidth);
62 urlField.setEditable(true);
63 add(urlField);
64 feedLabel = new Label("Feed");
65 add(feedLabel);
66 feedText = new TextArea(20, fieldWidth);
67 add(feedText);
68 fetchButton = new Button("Fetch");
69 fetchButton.addActionListener(this);
70 add(fetchButton);
71
72 locateWidget(urlButton, 0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER);
73 locateWidget(fileButton, 0, 1, 1, 1, 2, 0, GridBagConstraints.WEST);
74 locateWidget(urlField, 1, 0, 1, 2, 2, 0, GridBagConstraints.WEST);
75 locateWidget(feedLabel, 0, 2, 1, 1, 10, 10,
76 GridBagConstraints.NORTHEAST);
77 locateWidget(feedText, 1, 2, 1, 1, 10, 10, GridBagConstraints.NORTHWEST);
78 locateWidget(fetchButton, 1, 3, 1, 1, 10, 10, GridBagConstraints.CENTER);
79 validate();
80 urlField.requestFocus();
81 }
82
83 private void locateWidget(Component widget, int gridx, int gridy,
84 int gridwidth, int gridheight, int top, int bottom, int anchor) {
85 constraints.gridx = gridx;
86 constraints.gridy = gridy;
87 constraints.gridwidth = gridwidth;
88 constraints.gridheight = gridheight;
89 constraints.insets.top = top;
90 constraints.insets.bottom = bottom;
91 constraints.anchor = anchor;
92 layout.setConstraints(widget, constraints);
93 }
94
95
96
97
98 public void actionPerformed(ActionEvent e) {
99 Checkbox selChkbx = fileURLButtons.getSelectedCheckbox();
100 URL url;
101 if (e.getSource() == fetchButton) {
102 boolean isSourceURL = (selChkbx == urlButton);
103 try {
104 if (isSourceURL)
105 url = new URL(urlField.getText().toString());
106 else
107 url = new URL(home, urlField.getText().toString());
108 feedText.setText(getRSSFeed(url).toString());
109 }
110 catch (MalformedURLException ex) {
111 System.err.println("Malformed URL " + ex.getMessage());
112 }
113 catch (Exception ex) {
114 ex.printStackTrace();
115 System.err.println("ERROR: " + ex.getMessage());
116 }
117 }
118 }
119
120 private SyndFeed getRSSFeed(URL url) throws Exception {
121 SyndFeed feed;
122 SyndFeedInput input = new SyndFeedInput();
123 feed = input.build(new InputStreamReader(url.openStream()));
124 return feed;
125 }
126 }