/*
* PanelRSS.java
*
* Created on 30 Сентябрь 2008 г., 16:18
*/
package clips.rss;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.main.ClipsException;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.utils.DefaultBrowser;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.table_config_states.StateSaver;
import cli_fmw.utils.table_config_states.TableState;
import com.sun.syndication.io.FeedException;
import java.awt.Point;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.event.ListSelectionEvent;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndImageImpl;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import java.awt.Cursor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import framework.utils.Converter;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import javax.swing.JEditorPane;
/**
*
* @author vip
*/
public class PanelRSS extends PageGeneric implements ListSelectionListener {
private static ArrayList<EntryChunk> entryList;
public PanelRSS(PageContainer container) throws ClipsException {
super(container);
initComponents();
//formHyperlink();
if (entryList == null) {
entryList = getEntryListFromWeb("http://www.medlinks.ru/ultramode.xml");
}
Collections.sort(entryList, new Comparator<EntryChunk>() {
@Override
public int compare(EntryChunk o1, EntryChunk o2) {
return o1.entry.getPublishedDate().compareTo(o2.entry.getPublishedDate());
}
});
jTable1.setModel(new TableModelRSS(entryList));
jTable1.setDefaultRenderer(Object.class, new TableCellRendererRSS(entryList));
jTable1.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable1.getSelectionModel().addListSelectionListener(this);
resizeColumn();
StateSaver.attachTo(this);
}
protected InputStream getStream(URL page) throws IOException {
final URLConnection conn = page.openConnection();
if (conn instanceof HttpURLConnection) {
HttpURLConnection hconn = (HttpURLConnection) conn;
hconn.setInstanceFollowRedirects(false);
// Object postData = getPostData();
// if (postData != null) {
// handlePostData(hconn, postData);
// }
int response = hconn.getResponseCode();
boolean redirect = (response >= 300 && response <= 399);
/*
* In the case of a redirect, we want to actually change the URL
* that was input to the new, redirected URL
*/
if (redirect) {
String loc = conn.getHeaderField("Location");
if (loc.startsWith("http", 0)) {
page = new URL(loc);
} else {
page = new URL(page, loc);
}
return getStream(page);
}
}
return null;
}
@Override
public void valueChanged(ListSelectionEvent e) {
refreshTextPane();
}
private void refreshTextPane() {
SyndEntryImpl entry;
if (jTable1.getSelectedRowCount() > 0) {
entry = entryList.get(jTable1.getSelectedRowSorted()).entry;
entryList.get(jTable1.getSelectedRowSorted()).isRead = true;
} else {
entry = null;
}
if (entry != null) {
String html = "<HTML><FONT FACE='Times'>";
html += "<P><FONT SIZE=+1><B>" + entry.getTitle() + "</B></FONT><BR>";
html += "<FONT COLOR=BLUE SIZE=-1>" + Converter.dateToString(entry.getPublishedDate()) + "</FONT><BR>";
html += "<B>От: </B> " + entry.getAuthor() + "</P>";
html += "<P>" + entry.getDescription().getValue() + "</P>";
html += "<P>Ссылка: <A href='" + entry.getLink() + "'>" + entry.getLink() + "</A></P>";
html += "<P></P>";
html += "<P></P>";
html += "<P></P>";
html += "<P>Другие новости<BR>";
html += " <A href='http://www.formular.ru/all/index.php'>Формуляр.ру</A><BR>";
html += " <A href='http://asmok.ru/'>АСМОК</A></P>";
html += "</FONT></HTML>";
jTextPane1.setText(html);
} else {
jTextPane1.setText("");
}
}
public static ArrayList<EntryChunk> getEntryListFromWeb(String rssFeedUrl) throws ClipsException {
try {
//open up a connection to the rss feed
URLConnection feedUrl = new URL(rssFeedUrl).openConnection();
//URL local = PanelRSS.class.getResource("/resources/xml/localRSS.xml");
//Create Feed Object
SyndFeedInput input = new SyndFeedInput();
//SyndFeed feed = input.build(new XmlReader(feedUrl));
SyndFeed feed = input.build(new XmlReader(feedUrl));
//System.out.println("Examining rss feed:" + rssFeedUrl + "\n");
//System.out.println("Feed type=" + feed.getFeedType());
String author = feed.getAuthor();
//Iterate through object to get details
List list = feed.getEntries();
System.out.println("Feed image=" + feed.getImage());
if (feed.getImage() != null) {
SyndImageImpl image = (SyndImageImpl) feed.getImage();
String imageInfo = "Image url:" + image.getUrl() + "\n";
System.out.println(imageInfo);
}
ArrayList<EntryChunk> res = new ArrayList<EntryChunk>();
for (int i = 0; i < list.size(); i++) {
SyndEntryImpl entry = (SyndEntryImpl) list.get(i);
entry.setAuthor(author);
res.add(new EntryChunk(entry, false));
}
return res;
} catch (FeedException ex) {
throw new ClipsException("Не удалось загрузить новости", ex);
} catch (IOException ex) {
throw new ClipsException("Не удалось загрузить новости", ex);
}
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new cli_fmw.utils.sorted_table.SortedTable();
jScrollPane2 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jSplitPane1.setDividerLocation(200);
jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
jSplitPane1.setResizeWeight(0.5);
jScrollPane1.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
jScrollPane1ComponentResized(evt);
}
});
jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jTable1KeyPressed(evt);
}
});
jScrollPane1.setViewportView(jTable1);
jSplitPane1.setLeftComponent(jScrollPane1);
jTextPane1.setContentType("text/html");
jTextPane1.setEditable(false);
jTextPane1.setFont(new java.awt.Font("Lucida Sans Typewriter", 0, 13));
jTextPane1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTextPane1MouseClicked(evt);
}
});
jTextPane1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
jTextPane1MouseMoved(evt);
}
});
jScrollPane2.setViewportView(jTextPane1);
jSplitPane1.setRightComponent(jScrollPane2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 456, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
.addContainerGap()))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 265, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
.addContainerGap()))
);
}// </editor-fold>//GEN-END:initComponents
private void jTextPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextPane1MouseClicked
try {
Point pt = new Point(evt.getX(), evt.getY());
int pos = jTextPane1.viewToModel(pt);
if (pos >= 0) {
HTMLDocument doc = (HTMLDocument) jTextPane1.getDocument();
Element e = doc.getCharacterElement(pos);
AttributeSet a = e.getAttributes();
SimpleAttributeSet href = (SimpleAttributeSet) a.getAttribute(HTML.Tag.A);
if (href != null) {
String url = (String) href.getAttribute(HTML.Attribute.HREF);
DefaultBrowser.open(new URI(url));
}
}
} catch (URISyntaxException ex) {
MessageBox.showExceptionOnly(ex);
}
}//GEN-LAST:event_jTextPane1MouseClicked
private void jScrollPane1ComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jScrollPane1ComponentResized
}//GEN-LAST:event_jScrollPane1ComponentResized
private void jTextPane1MouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextPane1MouseMoved
Point pt = new Point(evt.getX(), evt.getY());
int pos = jTextPane1.viewToModel(pt);
if (pos >= 0) {
HTMLDocument doc = (HTMLDocument) jTextPane1.getDocument();
Element e = doc.getCharacterElement(pos);
AttributeSet a = e.getAttributes();
SimpleAttributeSet href = (SimpleAttributeSet) a.getAttribute(HTML.Tag.A);
if (href != null) {
jTextPane1.setCursor(new Cursor(Cursor.HAND_CURSOR));
} else {
jTextPane1.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
}//GEN-LAST:event_jTextPane1MouseMoved
private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
if (evt.getKeyCode() == 127 && jTable1.getSelectedRowSorted() >= 0 && jTable1.getSelectedRowSorted() < entryList.size()) {
entryList.remove(jTable1.getSelectedRowSorted());
jTable1.updateUI();
}
}//GEN-LAST:event_jTable1KeyPressed
private void resizeColumn() {
TableState state = new TableState();
state.setPreferredWidth(TableModelRSS.COL_AUTHOR, 2);
state.setPreferredWidth(TableModelRSS.COL_THEME, 6);
state.setPreferredWidth(TableModelRSS.COL_DATE, 2);
StateSaver.setDefaultState(this, jTable1, state);
}
@Override
public String getPageTitle() {
return "Новости";
}
@Override
public DelegateLine2 getDelegate() {
return null;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private cli_fmw.utils.sorted_table.SortedTable jTable1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration//GEN-END:variables
}