受http://www.oreillynet.com/onjava/blog/2007/05/javafx_first_steps_hello_onjav_1.html这篇文章的启发,加上想学习一下JavaFX,就在上面那篇文章的基础上,做了一个简易的RSS阅读器,其界面类似于Adobe Flex 2的一个demo:进入http://try.flex.org/index.cfm,找到Blog Reader的demo。

读取RSS feed并没有什么问题,使用rome:

java 代码

  1. package rssreader;   
  2. import com.sun.syndication.feed.synd.SyndEntry;   
  3. import com.sun.syndication.feed.synd.SyndFeed;   
  4. import com.sun.syndication.io.FeedException;   
  5. import com.sun.syndication.io.SyndFeedInput;   
  6. import com.sun.syndication.io.XmlReader;   
  7. import java.io.IOException;   
  8. import java.net.MalformedURLException;   
  9. import java.net.URL;   
  10. public class RssReader {   
  11. private URL url;   
  12. private Integer titleLength = 50;   
  13. public RssReader() {   
  14.     }   
  15. public SyndEntry[] load(String urlString) throws IllegalArgumentException, FeedException, IOException {   
  16. if (urlString != null && urlString != "") {   
  17. try {   
  18.                 url = new URL(urlString);   
  19.             } catch (MalformedURLException e) {   
  20.                 e.printStackTrace();   
  21. return null;   
  22.             }   
  23.         }   
  24. if (url == null) {   
  25. return null;   
  26.         }   
  27.         SyndFeedInput input = new SyndFeedInput();   
  28.         SyndFeed feed = input.build(new XmlReader(url));   
  29.         SyndEntry[] entries = (SyndEntry[]) feed.getEntries().toArray(new SyndEntry[0]);   
  30. for( SyndEntry entry : entries ) {   
  31.             entry.setTitle( entry.getTitle().length() > titleLength ? entry.getTitle().substring(0, titleLength - 3 ) + "..." : entry.getTitle() );   
  32.         }    
  33. return entries;   
  34.     }   
  35. }   

使用JavaFX构建UI:

JavaFX代码

  1. package rssreader;   
  2. import javafx.ui.*;   
  3. import javafx.ui.canvas.*;   
  4. import javax.swing.JComponent;   
  5. import com.sun.syndication.feed.synd.SyndEntry;   
  6. var reader:RssReader = READER;   
  7. class RssReaderModel {   
  8.     attribute rssUrl: String;   
  9.     attribute rssEntries: SyndEntry*;   
  10.     attribute rssContent: String?;   
  11.     attribute rssSelectedIndex: Integer;   
  12. }   
  13. var model = RssReaderModel {   
  14.     rssUrl: "http://woodstudio.javaeye.com/blog/rss_blog/alexcheng",   
  15. };   
  16. var panel = GroupPanel {   
  17. var row1 = Row {}   
  18. var row2 = Row {}   
  19. var col = Column {}   
  20.      rows: [row1, row2]   
  21.      columns: col   
  22.      content:    
  23.          [   
  24.             GroupPanel  {   
  25.                 row: row1   
  26.                 column: col   
  27. var row = Row {}   
  28. var labelCol =  new Column   
  29. var urlCol =  new Column   
  30. var butCol =  new Column   
  31.                 rows: row   
  32.                 columns: [labelCol, urlCol, butCol]   
  33.                 content:   
  34.                 [   
  35.                     SimpleLabel {   
  36.                         row: row   
  37.                         column: labelCol   
  38.                         text: "RSS URL:"
  39.                     },   
  40.                      TextField {   
  41.                         row: row   
  42.                         column: urlCol   
  43.                         columns: 60   
  44.                         value: bind model.rssUrl   
  45.                     },   
  46.                     Button {   
  47.                         row: row   
  48.                         column: butCol   
  49.                         text: "Get Entries"
  50.                         action: operation() {   
  51.                             model.rssEntries = reader.load(model.rssUrl);              
  52.                         }   
  53.                     }   
  54.                 ]   
  55.             },   
  56.             SplitPane   {   
  57.                 row: row2   
  58.                 column: col   
  59.                 orientation: HORIZONTAL   
  60.                 content:   
  61.                 [   
  62.                     SplitView  {   
  63.                         weight: 0.3   
  64.                         content:       
  65.                             Table {   
  66.                                     columns:   
  67.                                     [   
  68.                                         TableColumn {   
  69.                                             text: "Title"
  70.                                         },   
  71.                                         TableColumn {   
  72.                                             text: "Date"
  73.                                         },   
  74.                                     ]   
  75.                                     cells : bind foreach (entry in model.rssEntries)   
  76.                                             [   
  77.                                                 TableCell {   
  78.                                                     text: bind entry.title   
  79.                                                 },   
  80.                                                 TableCell {   
  81.                                                     text: bind entry.publishedDate.toLocaleString()   
  82.                                                 }   
  83.                                             ]   
  84.                                     selection: bind model.rssSelectedIndex   
  85.                                 }      
  86.                     },   
  87.                     SplitView  {   
  88.                         weight: 0.7   
  89.                         content:   
  90.                             EditorPane   {   
  91.                                 opaque: true
  92.                                 contentType: HTML   
  93.                                 editable: false
  94.                                 text: bind model.rssContent   
  95.                             }   
  96.                         }   
  97.                 ]   
  98.             }   
  99.          ]    
  100. };   
  101. trigger on (RssReaderModel.rssSelectedIndex = value) {   
  102. var desc = rssEntries[value].description.value;   
  103.     rssContent = " {desc} ";    
  104. }   
  105. MY_CONTAINER:JComponent.add(panel.getComponent());   

接着就是使用JSR223的scripting framework来执行:

java 代码

  1. package rssreader;   
  2. import javax.script.Bindings;   
  3. import javax.script.ScriptContext;   
  4. import javax.script.ScriptEngine;   
  5. import javax.script.ScriptEngineManager;   
  6. import javax.script.SimpleScriptContext;   
  7. import javax.swing.JFrame;   
  8. public class Main {   
  9. /**
  10.      * @param args
  11.      * @throws Exception
  12.      */
  13. public static void main(String[] args) throws Exception {   
  14.         RssReader reader = new RssReader();   
  15.         JFrame frame = new JFrame(   
  16. "RSS Reader implemented using JavaFX by alexcheng");   
  17.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  18.         frame.setSize(800, 600);   
  19.         ClassLoader loader = Thread.currentThread().getContextClassLoader();   
  20.         ScriptEngineManager manager = new ScriptEngineManager(loader);   
  21.         ScriptEngine engine = manager.getEngineByExtension("fx");   
  22.         Bindings bindings = engine.createBindings();   
  23.         bindings.put("READER:rssreader.RssReader", reader);   
  24.         bindings.put("MY_CONTAINER:javax.swing.JComponent", frame   
  25.                 .getContentPane());   
  26.         ScriptContext context = new SimpleScriptContext();   
  27.         context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);   
  28.         context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);   
  29.         engine.setContext(context);   
  30.         String script = "import rssreader.FxMain;";   
  31.         engine.eval(script);   
  32.         frame.setVisible(true);   
  33.     }   
  34. }   

最后的界面如下:

javafxrssreader.JPG

 描述:
 JavaFX RSS Reader

 文件大小:
 74 KB

 看过的:
 文件被下载或查看 28 次


文章来源: http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!274.entry