?? rssparser.java
字號:
/*
Application運行入口
java RssParser
classname RssParser
名稱:RSS解析器
Version 1.0
Date 2006-06-03
Down www.codefans.net
CopyRight
清華大學數學科學系
數32班
王元濤
2003012142
*/
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.util.*;
import javax.swing.table.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.XmlDocument;
import java.io.*;
import javax.xml.parsers.*;
import javax.swing.plaf.multi.MultiTreeUI;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////表現層
public class RssParser extends Applet{//主類
//全局對象
static RssParser rssParser;
static Frame frame;
static PanelTree pTree;
static PanelTable pTable;
static Label status;
//初始化布局
public void init(){
setLayout(new BorderLayout());
pTable = new PanelTable();
pTree = new PanelTree(pTable);
status = new Label("Status:");
add("West", pTree);
add("Center",pTable);
add("South",status);
status.setText("inited");
}
//程序入口
public static void main(String[] args){
frame = new Frame("RssParser");
rssParser = new RssParser();
rssParser.init();
frame.addWindowListener(new WindowAdapter()//匿名內部類
{
public void windowClosing(WindowEvent e) {//關閉窗口
System.exit(0);
}
}
);
frame.add("Center", rssParser);
frame.setSize(1024, 700);
frame.show();
}
}
class PanelTree extends Panel implements MouseListener{//左側的樹
//全局變量
public JTree tree;
public MenuActions popp;
public RSSNode top;
PanelTable targetTable;
//構造方法初始化布局
public PanelTree(PanelTable targetTable){
this.targetTable=targetTable;
//添加菜單對象
popp=new MenuActions(this.tree);
add(popp);
//添加樹
JScrollPane jsp=creatTree();
// jsp.setBounds(new Rectangle(0,0,300,650));
add(jsp);
}
//創建樹
public JScrollPane creatTree(){
//根節點為RSSNode的對象
top = new RSSNode("MyRSS_root");
top.type=0;
try{
//根據頻道定義文件創建樹內容
XmlProcessor.opmlRead(top,"opml.xml");
}catch(Exception e){
RssParser.status.setText(e.getMessage());
}
tree = new JTree(top);
//監聽鼠標在樹上的動作
tree.addMouseListener(this);
//返回帶自動滾動條的樹
return new JScrollPane(tree,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
public void mouseClicked(MouseEvent e){
TreePath selPath = tree.getPathForLocation(e.getX(),e.getY());
if(selPath!=null){
//返回點中的對象
Object o=selPath.getLastPathComponent();
//如果是左鍵點擊
if(e.getButton()==e.BUTTON1){
//狀態欄輸出RSS的URL
if(((RSSNode)o).url!=null)
RssParser.status.setText(((RSSNode)o).url.toString());
else{
RssParser.status.setText("feed://about:_root");
}
}
//如果是右鍵
if(e.getButton()==e.BUTTON3){
//彈出菜單
popp.curNode=(RSSNode)o;
popp.add(popp.popupMenu1);
popp.popupMenu1.removeAll();
//添加菜單項
if(popp.curNode.type==0){
popp.popupMenu1.add(popp.menuItem1);
popp.popupMenu1.add(popp.menuItem4);
}else{
popp.popupMenu1.add(popp.menuItem2);
popp.popupMenu1.add(popp.menuItem3);
}
//菜單位置與事件發生位置偏差修正,真正顯示
popp.popupMenu1.show(popp,e.getX(),e.getY()-200);
//重繪
popp.repaint();
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
}
class PanelTable extends Panel{//右側的表
//全局變量
public JTable table;
public Vector row;
public DefaultTableModel tableModel;
String[] tableHeads = {"標題","時間","摘要","來源"};
//構造表
public PanelTable(){
tableModel = new DefaultTableModel();
Vector tableHeadName = new Vector();
//寫表頭
for (int i = 0; i < tableHeads.length; i++) {
tableHeadName.add(tableHeads[i]);
}
tableModel.setDataVector(new Vector(),tableHeadName);
//創建表
table = new JTable(tableModel);
//設置表外觀
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
table.setPreferredScrollableViewportSize(new Dimension(700,600));
table.getColumnModel().getColumn(0).setPreferredWidth(200);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.setSurrendersFocusOnKeystroke(false);
// table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setRowHeight(20);
//為表加入滾動條
JScrollPane jsp2=new JScrollPane(table,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(jsp2);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////交互層
class MenuActions extends JPanel implements ActionListener{//菜單
//全局變量
public String title;
public RSSNode curNode;//關鍵變量
JTree tree;
public PopupMenu popupMenu1 = new PopupMenu();
public MenuItem menuItem1 = new MenuItem();
public MenuItem menuItem2 = new MenuItem();
public MenuItem menuItem3 = new MenuItem();
public MenuItem menuItem4 = new MenuItem();
//構造菜單和菜單項
public MenuActions(JTree tree) {
this.tree=tree;
this.setLayout(null);
menuItem1.setLabel("添加RSS");
menuItem2.setLabel("刪除RSS");
menuItem3.setLabel("顯示RSS");
menuItem4.setLabel("顯示全部RSS");
menuItem1.addActionListener(this);
menuItem2.addActionListener(this);
menuItem3.addActionListener(this);
menuItem4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("添加RSS"))addRss(e);
if(e.getActionCommand().equals("刪除RSS"))delRss(e);
if(e.getActionCommand().equals("顯示RSS"))showRss(e);
if(e.getActionCommand().equals("顯示全部RSS"))showAll(e);
}
void addRss(ActionEvent e){//添加RSS
//清空表
RssParser.pTable.tableModel.setRowCount(0);
//彈出對話框,要求輸入RSS的URL,程序自動獲取其名稱
String url=JOptionPane.showInputDialog("Please input RSS Url:(and then the name will be given)");
if(url!=null){
String name="New rssSite";
RSSNode tmp=new RSSNode(name);
try{
//字符串到URL對象的轉換
tmp.url=new URL(url);
//設置為臨時類型
tmp.type=2;
curNode.add(tmp);
//加入后自動更新
XmlProcessor.rssRead(tmp,tree);
}
catch(MalformedURLException ex){
RssParser.status.setText(ex.getMessage());
}catch(Exception ex2){
RssParser.status.setText(ex2.getMessage());
}
}
}
void delRss(ActionEvent e){//刪除RSS
RssParser.pTable.tableModel.setRowCount(0);
int i=JOptionPane.NO_OPTION;
try{//確認刪除
i=JOptionPane.showConfirmDialog(RssParser.frame,
"Are you sure to delete "+curNode.name+"?",
"Confirmer",
JOptionPane.YES_NO_OPTION);
}catch(Exception ex){}
if(i==JOptionPane.YES_OPTION){
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -