News
Source
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" xmlns:pm="http://primefaces.org/mobile"> <f:view renderKitId="PRIMEFACES_MOBILE" /> <h:head> <link type="text/css" rel="stylesheet" href="#{request.contextPath}/css/syntaxhighlighter/syntaxhighlighter.css" /> </h:head> <h:body> <pm:page id="main"> <pm:header title="News" swatch="b"></pm:header> <pm:content> <h:form id="mainForm"> <p:growl id="messages" showDetail="true" /> <p:dataList var="group" value="#{newsView.groups}"> <p:commandLink value="#{group.title}" action="pm:group?transition=slide" update=":group:groupTitle :group:groupForm:entries"> <f:setPropertyActionListener value="#{group}" target="#{newsView.selectedGroup}" /> </p:commandLink> </p:dataList> </h:form> </pm:content> <pm:footer title="Feeds from Reuters" /> </pm:page> <pm:page id="group"> <pm:header swatch="b"> <p:button styleClass="ui-toolbar-header-button-left ui-button-inline" value="Back" icon="ui-icon-arrow-l" outcome="pm:main?transition=slide&reverse=true" /> <h1><h:outputText id="groupTitle" value="#{newsView.selectedGroup.title}" /></h1> </pm:header> <pm:content> <h:form id="groupForm"> <p:dataList id="entries" var="newsEntry" value="#{newsView.selectedGroup.entries}"> <p:commandLink value="#{newsEntry.title}" action="pm:entry?transition=slide" update=":entry:entryDetail"> <f:setPropertyActionListener value="#{newsEntry}" target="#{newsView.selectedEntry}" /> </p:commandLink> </p:dataList> </h:form> </pm:content> </pm:page> <pm:page id="entry"> <pm:header title="Detail" swatch="b"> <p:button styleClass="ui-toolbar-header-button-left ui-button-inline" value="Back" icon="ui-icon-arrow-l" outcome="pm:group?transition=slide&reverse=true" /> </pm:header> <pm:content> <h:outputText id="entryDetail" value="#{newsView.selectedEntry.content}" escape="false"/> </pm:content> </pm:page> <script language="javascript" src="#{request.contextPath}/syntaxhighlighter/scripts/sh.js"></script> <script language="javascript"> SyntaxHighlighter.all(); </script> </h:body> </html>
package org.primefaces.showcase.mobile; import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.io.SyndFeedInput; import com.sun.syndication.io.XmlReader; import java.io.Serializable; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import javax.faces.application.FacesMessage; import javax.faces.bean.ApplicationScoped; import javax.faces.bean.ManagedBean; import javax.faces.context.FacesContext; import org.primefaces.showcase.domain.NewsEntry; import org.primefaces.showcase.domain.NewsGroup; @ManagedBean @ApplicationScoped public class NewsView implements Serializable { private List<NewsGroup> groups; private NewsEntry selectedEntry; private NewsGroup selectedGroup; private Map<String,String> feeds; @PostConstruct public void init() { feeds = new HashMap<String, String>(); feeds.put("Top Stories", "http://feeds.reuters.com/reuters/topNews"); feeds.put("World", "http://feeds.reuters.com/Reuters/worldNews"); feeds.put("Technology", "http://feeds.reuters.com/reuters/technologyNews"); feeds.put("Sports", "http://feeds.reuters.com/reuters/sportsNews"); feeds.put("Entertainment", " http://feeds.reuters.com/reuters/entertainment"); feeds.put("Business", "http://feeds.reuters.com/reuters/businessNews"); feeds.put("Politics", "http://feeds.reuters.com/Reuters/PoliticsNews"); groups = fetchNews(); } public List<NewsGroup> getGroups() { return groups; } public NewsEntry getSelectedEntry() { return selectedEntry; } public void setSelectedEntry(NewsEntry selectedEntry) { this.selectedEntry = selectedEntry; } public NewsGroup getSelectedGroup() { return selectedGroup; } public void setSelectedGroup(NewsGroup selectedGroup) { this.selectedGroup = selectedGroup; } public List<NewsGroup> fetchNews() { List<NewsGroup> news = new ArrayList<NewsGroup>(); try { for(String key : feeds.keySet()) { URL feedSource = new URL(feeds.get(key)); SyndFeedInput input = new SyndFeedInput(); SyndFeed feed = input.build(new XmlReader(feedSource)); List<NewsEntry> entries = new ArrayList<NewsEntry>(); int i = 0; for(Object f : feed.getEntries()) { SyndEntry entry = (SyndEntry) f; String title = entry.getTitle(); title = title.length() <= 25 ? title : title.substring(0, 25); entries.add(new NewsEntry(i, title + "...", entry.getDescription().getValue())); i++; } news.add(new NewsGroup(key, entries)); } } catch(Exception exception) { FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Please try again"); FacesContext.getCurrentInstance().addMessage(null, msg); } return news; } }
package org.primefaces.examples.domain; import java.io.Serializable; import java.util.List; public class NewsGroup implements Serializable { private String title; private List<NewsEntry> entries; public NewsGroup() { } public NewsGroup(String title, List<NewsEntry> entries) { this.title = title; this.entries = entries; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<NewsEntry> getEntries() { return entries; } public void setEntries(List<NewsEntry> entries) { this.entries = entries; } }
package org.primefaces.examples.domain; import java.io.Serializable; public class NewsEntry implements Serializable { private String title; private String content; private int index; public NewsEntry() { } public NewsEntry(int index, String title, String content) { this.index = index; this.content = content; this.title = title; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } }