📄 tree.java
字号:
package util3;import javax.swing.tree.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;import javax.swing.event.*;import javax.xml.parsers.*;import org.xml.sax.*;import org.xml.sax.helpers.*;import java.io.*;import java.util.*;import util5.*;import util4.*;public class Tree extends dragTree.DnDTree implements TreeSelectionListener{ private static Handle hand=new Handle(); private BtnPan pan=null; private Frame frame; private NodeRender nodeRender=new NodeRender(); private ModifyDia modifyDia; private ExampleFileFilter fileFilter=new ExampleFileFilter("ell","ELL File"); //tree menu JPopupMenu treeMenu = new JPopupMenu(); JMenuItem importMI = new JMenuItem(); JMenuItem exportMI = new JMenuItem(); JMenuItem renameMI=new JMenuItem(); JMenuItem addNodMI = new JMenuItem(); JMenuItem sortMI = new JMenuItem(); JMenuItem treeDelMI = new JMenuItem(); //node menu JPopupMenu nodeMenu = new JPopupMenu(); JMenuItem modifyMI = new JMenuItem(); JMenuItem delMI = new JMenuItem(); JMenuItem addLeafMI = new JMenuItem(); JMenuItem nodeImportMI=new JMenuItem(); AddNodeAction addNodeAction=new AddNodeAction(); AddLeafAction addLeafAction=new AddLeafAction(); ModifyAction modifyAction=new ModifyAction(); SortAction sortAction=new SortAction(); ImportAction importAction=new ImportAction(); ExportAction exportAction=new ExportAction(); DelAction delAction=new DelAction(); public Tree(BtnPan pan){ this((Frame)pan.erlistener,pan.name); this.pan=pan; } public Tree(Frame f,String top) { super(new DMTreeNode(new Messager(top))); this.modifyDia=new ModifyDia(f,"",false); this.addMouseListener(new MouseAdapter(){ public void mouseReleased(MouseEvent e) { //System.out.println(Tree.this.getLeadSelectionRow()); if(e.getClickCount()==2){//double click edit TreePath tp=getPathForLocation(e.getX(), e.getY()); if(tp!=null){ DMTreeNode dmtn=(DMTreeNode)tp.getLastPathComponent(); String str=((Messager)dmtn.getUserObject()).getContent(); int offset=0; if((offset=str.indexOf("><")) == -1){ pan.erlistener.btnEdit(str,0); }else{ offset=str.length()-offset-1; pan.erlistener.btnEdit(str,offset); } } } else if(e.isPopupTrigger()){//menu control TreePath[] treepath = {getPathForLocation(e.getX(), e.getY())}; if(treepath[0]==null){ setSelectionPath(Tree.this.getPathForRow(0)); return; } TreePath[] tp=null; if((tp=getSelectionPaths())!=null){ for(int j=0;j<tp.length;j++){ if(tp[j]==treepath[0]) { treepath=tp; break; } } if(treepath.length>1){ modifyMI.setEnabled(false); addLeafMI.setEnabled(false); nodeImportMI.setEnabled(false); delMI.setEnabled(true); displayPopMenu(nodeMenu,e); }else{ setSelectionPath(treepath[0]); if(((DMTreeNode)treepath[0].getLastPathComponent()).isLeaf()){ modifyMI.setEnabled(true); addLeafMI.setEnabled(true); delMI.setEnabled(true); nodeImportMI.setEnabled(true); displayPopMenu(nodeMenu,e); }else{displayPopMenu(treeMenu,e);} } } } } }); this.addMouseMotionListener(new MouseMotionAdapter(){ public void mouseMoved(MouseEvent e) { String txt=null; TreePath tp=getPathForLocation(e.getX(),e.getY()); DefaultMutableTreeNode tn=null; try{ if(tp!=null){ tn=(DefaultMutableTreeNode)tp.getLastPathComponent(); String s=((Messager)tn.getUserObject()).getContent(); txt=(s.length()>0)? s : null; } } catch(Exception ex){} finally{((JComponent)Tree.this.getCellRenderer()).setToolTipText(txt);} } }); this.addTreeWillExpandListener(new TreeWillExpandListener(){ public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException{ } public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException{ } }); this.setCellRenderer(nodeRender); this.putClientProperty("JTree.lineStyle", "Angled"); this.addTreeSelectionListener(this); ToolTipManager.sharedInstance().registerComponent(this); this.setToggleClickCount(0);//屏蔽双击展开 //call jbinit try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //constructor finished private void jbInit() throws Exception { importMI.setText("Import"); exportMI.setText("Export"); renameMI.setText("Modify"); addNodMI.setText("Add node"); sortMI.setText("Sort"); treeDelMI.setText("Delete"); modifyMI.setText("Modify"); delMI.setText("Delete"); addLeafMI.setText("Add leaf"); nodeImportMI.setText("Import"); treeMenu.add(importMI); treeMenu.add(exportMI); treeMenu.add(renameMI); treeMenu.add(addNodMI); treeMenu.add(sortMI); treeMenu.add(treeDelMI); nodeMenu.add(modifyMI); nodeMenu.add(delMI); nodeMenu.add(addLeafMI); nodeMenu.add(nodeImportMI); this.addNodMI.addActionListener(addNodeAction); this.addLeafMI.addActionListener(addLeafAction); this.modifyMI.addActionListener(modifyAction); this.sortMI.addActionListener(sortAction); this.renameMI.addActionListener(modifyAction); this.importMI.addActionListener(importAction); this.exportMI.addActionListener(exportAction); this.nodeImportMI.addActionListener(importAction); this.delMI.addActionListener(delAction); this.treeDelMI.addActionListener(delAction); }//initial and constructor finished. protected void sort(TreePath tp){ DMTreeNode dmtn=(DMTreeNode)tp.getLastPathComponent(); Vector children=dmtn.getChildren(); int count=dmtn.getChildren().size(); DMTreeNode[] nodes=new DMTreeNode[count]; for(int i=0;i<count;i++){ nodes[i]=((DMTreeNode)children.elementAt(i)); } DMTreeNode m=null; for(int j=0;j<count;j++){ for(int k=0;k<count-j-1;k++){ if(1 < Messager.compare((Messager)nodes[k].getUserObject(), (Messager)nodes[k+1].getUserObject())) { m=nodes[k]; nodes[k]=nodes[k+1]; nodes[k+1]=m; } } } for(int n=0;n<count;n++){ children.setElementAt(nodes[n],n); } this.updateUI(); this.setSelectionPath(tp); } protected void importXML(File f)throws Exception{ importXML(f,(DMTreeNode)this.getModel().getRoot()); } protected void importXML(File f,DMTreeNode tn)throws Exception{ this.hand.importXML(f,tn); Tree.this.expandPath(Tree.this.getPathForRow(0));//保证根节点被展开了 //this.scrollPathToVisible(new TreePath(this.getPathForRow(this.getRowCount()))); //this.updateUI(); } protected void exportXML(File file,DMTreeNode tn)throws Exception{ String str=this.hand.exportXML(tn); FileOutputStream fos=new FileOutputStream(file); OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8"); osw.write(str.toCharArray()); osw.flush(); osw.close(); fos.close(); } protected void displayPopMenu(JPopupMenu menu, MouseEvent e){ menu.show(this,e.getX(),e.getY()); } public void valueChanged(TreeSelectionEvent e){ try{ Object o=((DMTreeNode)getSelectionPath().getLastPathComponent()).getUserObject(); this.pan.showAtt(((Messager)o).getAttMsg()); }catch(Exception ex){} } public static void main(String[] arg) throws Exception{ JFrame jf=new JFrame("try"); Tree tree=new Tree(jf,"default"); jf.getContentPane().add(tree); jf.pack(); jf.show(); tree.importXML(new File("try.xml"),(DMTreeNode)tree.getModel().getRoot()); }//inner class Handle static class Handle extends DefaultHandler{ private static final int SETNAME=1; private static final int SETVALUE=2; private static final int SETATT=3; private static final int SETROOTVALUE=4; private static final String xmlstart="<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?> \n<!DOCTYPE root SYSTEM \"define.dtd\"> \n"; private int nextAction=0; private SAXParser saxParser=null; private DMTreeNode treeNode=null; private Stack subTree=new Stack(); private DMTreeNode node=null; private Messager msg=null; //private StringBuffer stringBuffer=new StringBuffer(xmlstart); static class Checker{ static char[] ents={'&','<','>','\'','\"'}; static String[] strs={"&","<",">","'","""}; public static String makeEntity(String str){ StringBuffer sb=new StringBuffer(); char ch=0; for(int i=0;i<str.length();i++){ switch(ch=str.charAt(i)){ case '&' : { sb.append(strs[0]); break; } case '<' : { sb.append(strs[1]); break; } case '>' : { sb.append(strs[2]); break; } case '\'' : { sb.append(strs[3]); break; } case '"' : { sb.append(strs[4]); break; } default : sb.append(ch); } } return sb.toString(); } static boolean hasEntity(String val){ for(int i=0;i<ents.length;i++){ if(val.indexOf(ents[i])!=-1) { return true; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -