📄 importfeeddialog.java
字号:
package mynews;
import java.awt.Toolkit;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.TreeItem;
import org.jdom.input.SAXBuilder;
import com.swtdesigner.SWTResourceManager;
import org.jdom.*;
public class ImportFeedDialog extends Dialog {
private Combo combo;
private Text text;
protected Object result;
protected Shell shell;
/**
* Create the dialog
* @param parent
* @param style
*/
public ImportFeedDialog(Shell parent, int style) {
super(parent, style);
}
/**
* Create the dialog
* @param parent
*/
public ImportFeedDialog(Shell parent) {
this(parent, SWT.NONE);
}
/**
* Open the dialog
* @return the result
*/
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(430, 159);
Toolkit kit = Toolkit.getDefaultToolkit();
shell.setLocation((kit.getScreenSize().width - 430) / 2, (kit.getScreenSize().height - 159) / 2);
shell.setText("Import Feed");
final Label opmlFileLabel = new Label(shell, SWT.NONE);
opmlFileLabel.setText("OPML File:");
opmlFileLabel.setBounds(43, 15, 62, 14);
text = new Text(shell, SWT.BORDER);
text.setBounds(111, 12, 225, 22);
final Button browsersButton = new Button(shell, SWT.NONE);
browsersButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
FileDialog fd = new FileDialog(shell, SWT.OPEN);
fd.setText("Choose a OPML file:");
fd.setFilterExtensions(new String[]{"*.xml", "*.*"});
text.setText(fd.open());
}
});
browsersButton.setText("Browse...");
browsersButton.setBounds(343, 10, 71, 24);
final Label chooseAGroupLabel = new Label(shell, SWT.NONE);
chooseAGroupLabel.setText("Choose a group:");
chooseAGroupLabel.setBounds(15, 45, 90, 14);
combo = new Combo(shell, SWT.READ_ONLY);
combo.setBounds(111, 42, 225, 22);
for(TreeItem tmp : (java.util.Vector<TreeItem>)shell.getParent().getData("groupItems")){
combo.add(tmp.getText());
combo.select(0);
}
final Label weSupportImportingLabel = new Label(shell, SWT.WRAP);
weSupportImportingLabel.setForeground(SWTResourceManager.getColor(128, 128, 255));
weSupportImportingLabel.setText("We support importing feeds from OPML files. First click 'Browse' to locate a OPML file, and then choose a group for the feeds, finally, hit 'Import' button.");
weSupportImportingLabel.setBounds(20, 75, 392, 49);
final Button importButton = new Button(shell, SWT.NONE);
importButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
if (text.getText().equals(""))
return;
Document doc = null;
try {
doc = new SAXBuilder().build(new java.io.File(text.getText()));
} catch (Exception ex){
MessageDialog.openError(shell, "Error", "OPML file: '" + text.getText() + "' seems got some errors, see below:\n\n" + ex.getMessage());
return;
}
TreeItem groupItem = null;
for(TreeItem tmp : (java.util.Vector<TreeItem>)shell.getParent().getData("groupItems")){
if (combo.getItem(combo.getSelectionIndex()).equals(tmp.getText())){
groupItem = tmp;
break;
}
}
java.util.Vector<TreeItem> tmpFeeds = (java.util.Vector<TreeItem>)shell.getParent().getData("feedItems");
Element feeds = doc.getRootElement().getChild("body");
int counter = 0;
for(Object obj : feeds.getChildren()){
for (Object item : ((Element)obj).getChildren()){
counter++;
Element el = (Element)item;
TreeItem ti = new TreeItem(groupItem, SWT.NONE);
ti.setText(el.getAttributeValue("title"));
ti.setImage(SWTResourceManager.getImage(MyNewsMain.class, "/img/mbi_039.gif"));
ti.setData("rss", el.getAttributeValue("xmlUrl"));
ti.setData("url", el.getAttributeValue("htmlUrl"));
ti.setData("title", el.getAttributeValue("title"));
ti.setData("group", groupItem.getText());
tmpFeeds.add(ti);
}
}
MessageDialog.openInformation(shell, "Done", "" + counter + " feeds imported successfully!");
shell.close();
}
});
importButton.setFont(SWTResourceManager.getFont("Tahoma", 10, SWT.BOLD));
importButton.setText("Import");
importButton.setBounds(343, 40, 71, 24);
//
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -