⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addfeeddialog.java

📁 RSS一般理解为Rich Site Summary的所写
💻 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.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;
import com.swtdesigner.SWTResourceManager;
import org.jdom.input.SAXBuilder;
import org.jdom.*;

public class AddFeedDialog extends Dialog {

	private Combo combo;
	private Text url;
	private Text desc;
	private Text title;
	private Text rss;
	protected Object result;
	protected Shell shell;
	boolean rssOk = false;

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public AddFeedDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public AddFeedDialog(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(463, 424);
		Toolkit kit = Toolkit.getDefaultToolkit();
		shell.setLocation((kit.getScreenSize().width - 463) / 2, (kit.getScreenSize().height - 424) / 2);
		shell.setText("Add Feed");

		final Label rssUrlLabel = new Label(shell, SWT.NONE);
		rssUrlLabel.setText("RSS Url:");
		rssUrlLabel.setBounds(20, 35, 43, 21);

		rss = new Text(shell, SWT.BORDER);
		rss.setBounds(73, 31, 331, 22);

		final Button button = new Button(shell, SWT.ARROW | SWT.RIGHT);
		button.setText("button");
		button.setBounds(410, 30, 24, 24);

		final Group properitiesGroup = new Group(shell, SWT.NONE);
		properitiesGroup.setText("Properities");
		properitiesGroup.setBounds(20, 63, 414, 230);

		final Label titileLabel = new Label(properitiesGroup, SWT.NONE);
		titileLabel.setText("Titile:");
		titileLabel.setBounds(46, 35, 37, 20);

		title = new Text(properitiesGroup, SWT.BORDER);
		title.setBounds(89, 32, 295, 22);

		final Label descriptionLabel = new Label(properitiesGroup, SWT.NONE);
		descriptionLabel.setText("Description:");
		descriptionLabel.setBounds(19, 62, 64, 14);

		desc = new Text(properitiesGroup, SWT.WRAP | SWT.READ_ONLY | SWT.BORDER);
		desc.setBounds(89, 60, 295, 95);

		final Label siteLabel = new Label(properitiesGroup, SWT.NONE);
		siteLabel.setText("Site:");
		siteLabel.setBounds(58, 171, 25, 20);

		url = new Text(properitiesGroup, SWT.READ_ONLY | SWT.BORDER);
		url.setBounds(89, 169, 295, 22);

		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (rss.getText() == null || rss.getText().equals("")){
					MessageDialog.openWarning(shell, "Warning", "Fill the rss address first!");
					rssOk = false;
					return;
				}
				shell.setText("Add Feed - Testing feed, please wait......");
				Document doc = null;
				try {
					doc = new SAXBuilder().build(new java.net.URL(rss.getText()));
				} catch (Exception ex){
					MessageDialog.openError(shell, "Error", "The rss seems got some errors, see below:\n\n" + ex.getMessage());
					rssOk = false;
					return;
				}
				title.setText(doc.getRootElement().getChild("channel").getChildTextTrim("title"));
				desc.setText(doc.getRootElement().getChild("channel").getChildTextTrim("description"));
				url.setText(doc.getRootElement().getChild("channel").getChildTextTrim("link"));
				shell.setText("Add Feed");
				rssOk = true;
			}
		});
		
		final Label groupLabel = new Label(properitiesGroup, SWT.NONE);
		groupLabel.setText("Group:");
		groupLabel.setBounds(45, 200, 37, 14);

		combo = new Combo(properitiesGroup, SWT.READ_ONLY);
		combo.setBounds(89, 197, 119, 22);

		for(TreeItem tmp : (java.util.Vector<TreeItem>)shell.getParent().getData("groupItems")){
			combo.add(tmp.getText());
			combo.select(0);
		}
		
		final Label tipFirstInputLabel = new Label(shell, SWT.WRAP);
		tipFirstInputLabel.setForeground(SWTResourceManager.getColor(128, 128, 255));
		tipFirstInputLabel.setText("Tip: First input the rss address above, and click the '>' button to retrive the feed properities. When finished, select a group for the feed and press 'OK' button.");
		tipFirstInputLabel.setBounds(20, 308, 414, 49);

		final Button okButton = new Button(shell, SWT.NONE);
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (!rssOk){
					MessageDialog.openWarning(shell, "Warning", "Seems the rss feed is not valid, test it first!");
					return;
				}
				java.util.Vector<TreeItem> tmpFeeds = (java.util.Vector<TreeItem>)shell.getParent().getData("feedItems");
				for(TreeItem tmp : (java.util.Vector<TreeItem>)shell.getParent().getData("groupItems")){
					if (tmp.getText().equals(combo.getItem(combo.getSelectionIndex()))){
						TreeItem ti = new TreeItem(tmp, SWT.NONE);
						ti.setText(title.getText());
						ti.setImage(SWTResourceManager.getImage(MyNewsMain.class, "/img/mbi_039.gif"));
						ti.setData("rss", rss.getText());
						ti.setData("url", url.getText());
						ti.setData("title", title.getText());
						ti.setData("group", combo.getItem(combo.getSelectionIndex()));
						tmpFeeds.add(ti);
						break;
					}
				}
				MessageDialog.openInformation(shell, "Adding Feed", "Feed added successfully!");
				shell.close();
			}
		});
		okButton.setText("OK");
		okButton.setBounds(264, 363, 82, 26);

		final Button cancelButton = new Button(shell, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.close();
			}
		});
		cancelButton.setBounds(352, 363, 82, 26);
		cancelButton.setText("Cancel");
		//
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -