package net.sf.jpluck.apps.jpluckx.ui;
import java.awt.Cursor;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.ByteArrayInputStream;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import net.sf.jpluck.ClientConfiguration;
import net.sf.jpluck.http.HttpResponse;
import net.sf.jpluck.ui.DownloadDialog;
import net.sf.jpluck.ui.StandardDialog;
import net.sf.jpluck.xml.DOMUtil;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.FormLayout;
public class NewDocumentDialog extends StandardDialog {
private JCheckBox extractSettingsCheck = new JCheckBox("Extract settings from feed");
private JRadioButton feedRadio = new JRadioButton("Add New Feed");
private JRadioButton siteRadio = new JRadioButton("Add New Site");
private String feedTitle;
private String url;
public NewDocumentDialog(JFrame owner) {
super(owner);
initComponents();
}
public boolean isFeed() {
return feedRadio.isSelected();
}
public String getFeedTitle() {
return feedTitle;
}
protected boolean ok() {
ClientConfiguration.getDefault().setExtractFeedSettings(extractSettingsCheck.isSelected());
if (feedRadio.isSelected() && extractSettingsCheck.isSelected() && url!=null) {
download();
}
return true;
}
private boolean download() {
try {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
byte[] data = null;
try {
HttpResponse response = DownloadDialog.download(url, this, "Download", "Downloading RSS feed",
"Downloading feed and extracting information...");
if (response.getStatusCode() != 200) {
JOptionPane.showMessageDialog(this,
"Feed could not be downloaded.\n" + response.getStatusCode() +
": " + response.getStatusMessage(), "Error Downloading Feed",
JOptionPane.ERROR_MESSAGE);
return false;
}
data = response.getContent();
} catch (Exception e) {
Throwable t;
if (e.getCause() != null) {
t = e.getCause();
} else {
t = e;
}
JOptionPane.showMessageDialog(this,
"Error occurred during downloading:\n" + t.getClass() + ":" +
t.getMessage(), "Error Downloading Feed", JOptionPane.ERROR_MESSAGE);
return false;
}
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = db.parse(new InputSource(new ByteArrayInputStream(data)));
NodeList nodeList = document.getElementsByTagName("title");
if (nodeList.getLength() > 0) {
feedTitle = DOMUtil.getText(nodeList.item(0));
}
if (feedTitle == null) {
JOptionPane.showMessageDialog(this,
"Could not find this feed's title.\n\nPlease specify the name manually.",
"No Title Found", JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Content could not be parsed as an RSS or Atom feed.",
"Error Parsing Content", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
} finally {
setCursor(Cursor.getDefaultCursor());
}
}
private void initComponents() {
addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent ev) {
if (feedRadio.isSelected()) {
feedRadio.requestFocus();
}
}
});
setTitle("Add New Document");
siteRadio.setMnemonic('S');
feedRadio.setMnemonic('F');
ButtonGroup g = new ButtonGroup();
g.add(siteRadio);
g.add(feedRadio);
siteRadio.setSelected(true);
extractSettingsCheck.setMnemonic('E');
extractSettingsCheck.setSelected(ClientConfiguration.getDefault().isExtractFeedSettings());
DocumentListener documentListener = new DocumentListener();
siteRadio.addChangeListener(documentListener);
feedRadio.addChangeListener(documentListener);
try {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferable = clipboard.getContents(this);
if ((transferable != null) && transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String s = (String) transferable.getTransferData(DataFlavor.stringFlavor);
if (s.startsWith("http://")) {
if (s.indexOf("rss") != -1 || s.indexOf("rdf") != -1 || s.endsWith(".xml") ||
s.indexOf("atom") != -1) {
feedRadio.setSelected(true);
feedRadio.requestFocus();
}
url = s;
}
}
} catch (Exception e) {
}
documentListener.stateChanged(null);
PanelBuilder panelBuilder = new PanelBuilder(new FormLayout("pref:grow",
"pref, 4dlu, pref, 4dlu, pref, 4dlu, pref"));
panelBuilder.addTitle("New Document", "1,1");
panelBuilder.add(siteRadio, "1,3");
panelBuilder.add(feedRadio, "1,5");
if (url!=null) {
panelBuilder.add(extractSettingsCheck, "1,7");
} else {
panelBuilder.addLabel("(No URL on clipboard.)", "1,7").setEnabled(false);
}
panelBuilder.setDefaultDialogBorder();
initDialog(null, panelBuilder.getPanel());
}
private class DocumentListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
extractSettingsCheck.setEnabled(feedRadio.isSelected() && url != null);
}
}
}