import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndFeed; import java.awt.Cursor; import java.awt.Graphics; import java.util.List; import javax.swing.AbstractListModel; import javax.swing.JOptionPane; import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; import org.jdesktop.swingx.decorator.AlternateRowHighlighter; import org.jdesktop.swingx.decorator.Highlighter; import org.jdesktop.swingx.decorator.HighlighterPipeline; /* * BeanTester.java * * Created on June 30, 2006, 1:49 PM */ /** * * @author richardallenbair */ public class YahooNewsDemo extends javax.swing.JFrame { /** Creates new form BeanTester */ public YahooNewsDemo() { initComponents(); results.setHighlighters(new HighlighterPipeline(new Highlighter[] {AlternateRowHighlighter.beige})); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // //GEN-BEGIN:initComponents private void initComponents() { javax.swing.JLabel jLabel1; javax.swing.JScrollPane jScrollPane1; yahooNews = new org.jdesktop.swingx.ws.yahoo.rss.YahooNews(); worker = new org.jdesktop.swingx.BackgroundWorker(); executeButton = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); feedNameBox = new javax.swing.JComboBox(); jScrollPane1 = new javax.swing.JScrollPane(); results = new org.jdesktop.swingx.JXList(); worker.addBackgroundListener(new org.jdesktop.swingx.event.BackgroundListener() { public void process(org.jdesktop.swingx.event.BackgroundEvent evt) { workerProcess(evt); } public void started(org.jdesktop.swingx.event.BackgroundEvent evt) { workerStarted(evt); } public void done(org.jdesktop.swingx.event.BackgroundEvent evt) { workerDone(evt); } public void doInBackground(org.jdesktop.swingx.event.BackgroundEvent evt) { workerDoInBackground(evt); } }); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); executeButton.setText("Refresh"); executeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { executeButtonActionPerformed(evt); } }); jLabel1.setText("Search"); feedNameBox.setEditable(true); feedNameBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "movies", "music", "topstories", "us", "world" })); jScrollPane1.setViewportView(results); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(feedNameBox, 0, 492, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(executeButton))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(executeButton) .addComponent(feedNameBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// //GEN-END:initComponents private void workerStarted(org.jdesktop.swingx.event.BackgroundEvent evt) {//GEN-FIRST:event_workerStarted executeButton.setEnabled(false); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); }//GEN-LAST:event_workerStarted private void workerProcess(org.jdesktop.swingx.event.BackgroundEvent evt) {//GEN-FIRST:event_workerProcess results.setModel(new RssListModel((SyndFeed)evt.getData()[0])); }//GEN-LAST:event_workerProcess private void workerDone(org.jdesktop.swingx.event.BackgroundEvent evt) {//GEN-FIRST:event_workerDone executeButton.setEnabled(true); setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); }//GEN-LAST:event_workerDone private void workerDoInBackground(org.jdesktop.swingx.event.BackgroundEvent evt) {//GEN-FIRST:event_workerDoInBackground try { evt.getWorker().publish(yahooNews.readFeed()); } catch (Exception e) { JOptionPane.showInternalMessageDialog(this, e, "Failed to show feed", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_workerDoInBackground private void executeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_executeButtonActionPerformed yahooNews.setFeedName("" + feedNameBox.getSelectedItem()); worker.execute(); }//GEN-LAST:event_executeButtonActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new YahooNewsDemo().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton executeButton; private javax.swing.JComboBox feedNameBox; private org.jdesktop.swingx.JXList results; private org.jdesktop.swingx.BackgroundWorker worker; private org.jdesktop.swingx.ws.yahoo.rss.YahooNews yahooNews; // End of variables declaration//GEN-END:variables private static final class RssListModel extends AbstractListModel { private SyndFeed feed; private List entries; public RssListModel(SyndFeed feed) { this.feed = feed; entries = feed.getEntries(); } public int getSize() { return entries.size(); } public Object getElementAt(int i) { SyndEntry entry = (SyndEntry)entries.get(i); return entry.getTitle() + " (" + entry.getPublishedDate() + ")"; } } }