JavaFX 获取Rss
转载:点击打开链接http://mxcyk.com/?post=208
本文使用的javafx1.2在netbeansIDE下开发
/*转载请注明:mxcyk.com
*JavaFX 1.2
* Main.fx
*/
package feedread;
import java.lang.Exception;
import javafx.data.feed.atom.AtomTask;
import javafx.data.feed.atom.Entry;
import javafx.data.feed.atom.Feed;
import javafx.data.feed.rss.Channel;
import javafx.data.feed.rss.Item;
import javafx.data.feed.rss.RssTask;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextBox;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Flow;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Alert;
import javafx.stage.Stage;
class FeedItem
{
var title: String;
var link: String
}
var items: FeedItem [];
var index: Integer = 0;
var feedImageURL: String;
var buttonGoRef: Button;
var textBoxURLRef: TextBox;
function go (url: String): Void
{
delete items;
index = 0;
buttonGoRef.disable = true;
buttonGoRef.focusTraversable = false;
RssTask
{
interval: 60s
location: url
onChannel: function (c: Channel)
{
feedImageURL = c.image.url
}
onItem: function (i: Item)
{
insert FeedItem
{
title: i.title
link: i.link
}
into items
}
onException: function (e: Exception)
{
def msg = e.getMessage ();
if (msg == "must use AtomTask for Atom feeds")
goAtom (url)
else
Alert.inform ("Error", e.getMessage ());
buttonGoRef.disable = false;
buttonGoRef.focusTraversable = true
}
onDone: function ()
{
buttonGoRef.disable = false;
buttonGoRef.focusTraversable = true;
}
}.update ()
}
function goAtom (url: String): Void
{
AtomTask
{
interval: 60s
location: url
onFeed: function (f: Feed)
{
feedImageURL = f.icon.uri
}
onEntry: function (e: Entry)
{
var title = e.title.text;
if (title.length () > 100)
title = "{title.substring (0, 100)}...";
insert FeedItem
{
title: title
link: e.links [0].href
}
into items
}
onException: function (e: Exception)
{
Alert.inform ("Error", e.getMessage ());
buttonGoRef.disable = false;
buttonGoRef.focusTraversable = true
}
onDone: function ()
{
buttonGoRef.disable = false;
buttonGoRef.focusTraversable = true
}
}.update ()
}
Stage
{
title: "mxcyk.com Rss解析"
var sceneRef: Scene
scene: sceneRef = Scene
{
width: 550
height: 350
fill: LinearGradient
{
startX: 0.0
startY: 0.0
endX: 0.0
endY: 1.0
stops:
[
Stop { offset: 0.0 color: Color.NAVY },
Stop { offset: 0.5 color: Color.BLUEVIOLET },
Stop { offset: 1.0 color: Color.NAVY }
]
}
var flowRef: Flow
content: flowRef = Flow
{
vertical: true
vgap: 20
layoutX: bind (sceneRef.width-flowRef.layoutBounds.width)/2-
flowRef.layoutBounds.minX
layoutY: bind (sceneRef.height-flowRef.layoutBounds.height)/2-
flowRef.layoutBounds.minY
content:
[
ImageView
{
image: Image
{
url: "{__DIR__}res/feedread.png"
}
}
Flow
{
hgap: 20
var textBoxURLRef: TextBox
content:
[
Label
{
graphic: Text
{
content: "URL"
fill: Color.GOLD
font: Font
{
name: "Arial BOLD"
size: 14
}
effect: DropShadow
{
spread: 0.5
}
}
}
textBoxURLRef = TextBox
{
columns: 40
}
buttonGoRef = Button
{
text: "Go"
action: function (): Void
{
go (textBoxURLRef.text)
}
}
]
}
Group
{
content: FeedItemPanel
{
itemTitle: bind items [index].title
itemLink: bind items [index].link
feedImageURL: bind feedImageURL
effect: Reflection { fraction: 0.6 }
}
}
Flow
{
hgap: 20
content:
[
Button
{
text: "|<"
disable: bind if ((sizeof items == 0) or
(index == 0))
then true else false
focusTraversable: bind if ((sizeof items == 0) or
(index == 0))
then false else true
action: function (): Void
{
index = 0
}
}
Button
{
text: "<"
disable: bind if ((sizeof items == 0) or
(index == 0))
then true else false
focusTraversable: bind if ((sizeof items == 0) or
(index == 0))
then false else true
action: function (): Void
{
if (index != 0)
index--
}
}
Button
{
text: ">"
disable: bind if ((sizeof items == 0) or
(index == sizeof items-1))
then true else false
focusTraversable: bind if ((sizeof items == 0) or
(index == sizeof items-1))
then false else true
action: function (): Void
{
if (index != sizeof items-1)
index++
}
}
Button
{
text: ">|"
disable: bind if ((sizeof items == 0) or
(index == sizeof items-1))
then true else false
focusTraversable: bind if ((sizeof items == 0) or
(index == sizeof items-1))
then false else true
action: function (): Void
{
index = sizeof items-1
}
}
]
}
]
}
}
}
//--------------------------------------------------------------------------------------
/*转载请注明:mxcyk.com
* FeedItemPanel.fx
*/
package feedread;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Panel;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;
import javafx.stage.AppletStageExtension;
public class FeedItemPanel extends CustomNode
{
public var itemTitle: String;
public var itemLink: String;
public var feedImageURL: String on replace
{
if (feedImageURL == null or feedImageURL == "")
imageRef = Image { url: "{__DIR__}res/feedicon.png" }
else
imageRef = Image { url: feedImageURL }
}
var imageRef: Image;
override function create (): Node
{
Group
{
var hyperlinkRef: Hyperlink
var imageViewRef: ImageView
var rectangleRef: Rectangle
var titleTextRef: Text
content:
[
rectangleRef = Rectangle
{
width: 400
height: 80
arcWidth: 15
arcHeight: 15
stroke: Color.GOLD
strokeWidth: 3.0
fill: Color.WHITE
}
imageViewRef = ImageView
{
layoutX: bind (rectangleRef.layoutBounds.width-
imageViewRef.layoutBounds.width)/2-
imageViewRef.layoutBounds.minX
layoutY: bind (rectangleRef.layoutBounds.height-
imageViewRef.layoutBounds.height)/2-
imageViewRef.layoutBounds.minY
opacity: 0.3
fitWidth: bind if (imageRef.width > 380) then 380
else imageRef.width
fitHeight: bind if (imageRef.height > 60) then 60
else imageRef.height
preserveRatio: true
image: bind imageRef
}
titleTextRef = Text
{
layoutX: bind (rectangleRef.layoutBounds.width-
titleTextRef.layoutBounds.width)/2-
titleTextRef.layoutBounds.minX
layoutY: bind if (itemLink != null)
10-titleTextRef.layoutBounds.minY
else
(rectangleRef.layoutBounds.height-
titleTextRef.layoutBounds.height)/2-
titleTextRef.layoutBounds.minY
content: bind itemTitle
wrappingWidth: bind rectangleRef.width-10
textOrigin: TextOrigin.TOP
font: Font
{
name: "Arial"
size: 16
}
}
Panel
{
content: hyperlinkRef = Hyperlink
{
layoutX: bind (rectangleRef.layoutBounds.width-
hyperlinkRef.layoutBounds.width)/2-
hyperlinkRef.layoutBounds.minX
layoutY: bind (rectangleRef.layoutBounds.height-
hyperlinkRef.layoutBounds.height-10)-
hyperlinkRef.layoutBounds.minY
text: bind if (itemLink == null) then ""
else ">>> Check it out! <<<"
focusTraversable: false
action: function (): Void
{
AppletStageExtension.showDocument (itemLink,
"_blank")
}
}
}
]
}
}
}