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

📄 tree.java

📁 JTREE的例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        return false;      }    }//Checker finished    String getNode(Messager msg){      String name=msg.getName();      String value=msg.getContent();      String att=msg.getAttMsg();      if(msg.hasEntity()){        if(name.indexOf("]]>")!=-1) name=Checker.makeEntity(name);        else name="<![CDATA["+name+"]]>";        if(value.indexOf("]]>")!=-1) value=Checker.makeEntity(value);        else value="<![CDATA["+value+"]]>";        if(att.indexOf("]]>")!=-1) att=Checker.makeEntity(att);        else att="<![CDATA["+att+"]]>";      }      return "<node>\n  <nodeName>"+name+"</nodeName>\n"+          "  <nodeValue>"+value+"</nodeValue>\n"+          "  <nodeAtt>"+att+"</nodeAtt>\n</node>\n";    }    String toXML(DMTreeNode node,StringBuffer sb){//System.out.println("toXML");      //String name=((Messager)node.getUserObject()).getName();      Messager message=null;      if((message=(Messager)node.getUserObject()).hasEntity()){        sb.append("<root name=\""+Checker.makeEntity(message.getName())+"\">\n");        if(message.getContent().length()!=0){          sb.append("  <rootValue>"+Checker.makeEntity(message.getContent())+"</rootValue>");        }      }      else {        sb.append("<root name=\""+message.getName()+"\">\n");        if(message.getContent().length()!=0){          sb.append("  <rootValue>"+message.getContent()+"</rootValue>");        }      }      Enumeration enu=node.children();      while(enu.hasMoreElements()){        DMTreeNode dmtn=(DMTreeNode)enu.nextElement();        if(dmtn.isLeaf()){          Messager msg=(Messager)dmtn.getUserObject();          sb.append(getNode(msg));        }else{          toXML(dmtn,sb);        }      }      sb.append("</root>\n");      return sb.toString();    }     protected void importXML(File file,DMTreeNode treeNode)throws Exception{      this.treeNode=treeNode;      //System.out.println(((DefaultMutableTreeNode)tree.getPathForRow(0).getLastPathComponent()).getUserObject().toString());      //this.subTree.push(tree.getPathForRow(0).getLastPathComponent());      this.saxParser.parse(file,this);      this.treeNode=null;    }    protected String exportXML(DMTreeNode treeNode){      StringBuffer sb=new StringBuffer(this.xmlstart);      return this.toXML(treeNode,sb);    }    public Handle(){      try{this.saxParser=SAXParserFactory.newInstance().newSAXParser();}      catch(Exception saxe){saxe.printStackTrace();}    }    public void startDocument ()throws SAXException    {      this.subTree.push(treeNode);    }    public void ignorableWhitespace (char ch[], int start, int length)      throws SAXException    {        //ignore    }    public void startElement (String uri, String localName,			      String qName, Attributes attributes)	throws SAXException    {//System.out.println("<"+qName+">");      if("node".equals(qName)){        this.msg=new Messager();      }else if("nodeName".equals(qName)){        if(this.msg==null) throw new SAXException("document error.");        this.nextAction=this.SETNAME;      }else if("nodeValue".equals(qName)){        if(this.msg==null) throw new SAXException("document error.");        this.nextAction=this.SETVALUE;      }else if("nodeAtt".equals(qName)){        if(this.msg==null) throw new SAXException("document error.");        this.nextAction=this.SETATT;      }else if("root".equals(qName)){        DMTreeNode r=new DMTreeNode(this.msg=          new Messager(attributes.getValue(0)));        ((DMTreeNode)subTree.peek()).add(r);        subTree.push(r);        //System.out.println("add tree");      }else if("rootValue".equals(qName)){        if(this.msg==null) throw new SAXException("document error.");        this.nextAction=this.SETROOTVALUE;      }    }    public void characters (char ch[], int start, int length) throws SAXException    {//System.out.print(new String(ch,start,length));      if(this.msg==null) throw new SAXException("document error.");      switch(this.nextAction){        case SETNAME : {          this.msg.setName(new String(ch,start,length));          break;        }        case SETVALUE : {          this.msg.setContent(new String(ch,start,length));          break;        }        case SETATT : {          this.msg.setAttMsg(new String(ch,start,length));          break;        }        case SETROOTVALUE : {          this.msg.setContent(new String(ch,start,length));          break;        }        case 0 : throw new SAXException("document error.");      }    }    public void endElement (String uri, String localName, String qName)	throws SAXException    { //System.out.println("</"+qName+">");      if("node".equals(qName)){        ((DMTreeNode)subTree.peek()).add(new DMTreeNode(this.msg));        this.msg=null;        //System.out.println("add node");      }/*else if("nodeName".equals(qName)){      }else if("nodeValue".equals(qName)){      }else if("nodeAtt".equals(qName)){      }*/else if("root".equals(qName)) this.subTree.pop();    }    public void endDocument ()throws SAXException    {      subTree.pop();    }  }//inner class NodeRender  class NodeRender extends DefaultTreeCellRenderer{    public NodeRender(){      super.setBorderSelectionColor(Color.blue);    }    public Component getTreeCellRendererComponent(        JTree tree,        Object value,        boolean sel,        boolean expanded,        boolean leaf,        int row,        boolean hasFocus) {      super.getTreeCellRendererComponent(          tree, value, sel,          expanded, leaf, row,          hasFocus);      return this;    }  }  protected class ImportAction extends AbstractAction{    public void actionPerformed(ActionEvent e){      DMTreeNode tn=(DMTreeNode)getSelectionPath().getLastPathComponent();      if(tn.isLeaf()){((Messager)tn.getUserObject()).setContent(null);}      JFileChooser jfc=pan.erlistener.shareFileChooser();      jfc.setFileFilter(null);      jfc.setFileFilter(fileFilter);      jfc.setCurrentDirectory(pan.erlistener.shareIniFile());      int val=jfc.showOpenDialog((Frame)pan.erlistener);      if(val==JFileChooser.APPROVE_OPTION){        File f=jfc.getSelectedFile();        try{importXML(f,tn);}        catch(Exception ex){ex.printStackTrace();          Reporter rep=pan.erlistener.shareReporter();          rep.setStateLab("File not found or document invalidation.");          rep.show();        }        Tree.this.updateUI();      }    }  }  protected class ExportAction extends AbstractAction{    public void actionPerformed(ActionEvent e){      JFileChooser jfc=pan.erlistener.shareFileChooser();      jfc.setFileFilter(null);      jfc.setFileFilter(fileFilter);      jfc.setCurrentDirectory(pan.erlistener.shareIniFile());      int val=jfc.showSaveDialog((Frame)pan.erlistener);      if(val==JFileChooser.APPROVE_OPTION){        File f=jfc.getSelectedFile();        String s=f.toString();        if(s.lastIndexOf('.')==-1) f=new File(s+".ell");        DMTreeNode tn=(DMTreeNode)            Tree.this.getSelectionPath().getLastPathComponent();        try{Tree.this.exportXML(f,tn);}        catch(Exception ex){          Reporter rep=pan.erlistener.shareReporter();          rep.setStateLab("Out put error! ");          rep.show();          ex.printStackTrace();        }      }    }  }  protected class AddNodeAction extends AbstractAction implements YorNListener{    public void actionPerformed(ActionEvent e){      modifyDia.setNameRow("Name : ",new Messager());      modifyDia.addYorNListener(this);      modifyDia.showIt("Make a new node");    }    public void gotYes(int val){      if(val==YorNListener.yes){        DefaultMutableTreeNode parent=null;        parent=(DefaultMutableTreeNode)getSelectionPath().getLastPathComponent();        DMTreeNode child=new DMTreeNode(modifyDia.getMesg());        ((DefaultTreeModel)Tree.this.getModel()).insertNodeInto(child,parent,0);        Tree.this.scrollPathToVisible(new TreePath(child.getPath()));      }    }  }  protected class SortAction extends AbstractAction{    public void actionPerformed(ActionEvent e){//do much to the cursor      sort(getSelectionPath());    }  }  protected class ModifyAction extends AbstractAction implements YorNListener{    private TreePath tp=null;    public void actionPerformed(ActionEvent e){      DMTreeNode tn=(DMTreeNode)(tp=getSelectionPath()).getLastPathComponent();      Messager msg=(Messager)tn.getUserObject();      String s=new String(msg.getName());      modifyDia.setNameRow("Rename : ",msg);      modifyDia.addYorNListener(this);      if(tn.isLeaf()) modifyDia.showIt("Modify node : "+msg.getName());      else modifyDia.showIt("Rename branch : "+msg.getName());    }    public void gotYes(int val){      if(val==YorNListener.yes){        updateUI();        setSelectionPath(tp);        this.tp=null;      }    }  }  protected class DelAction extends AbstractAction implements YorNListener{    TreePath tps[]=null;    public void actionPerformed(ActionEvent e){      tps=Tree.this.getSelectionPaths();      YorNDia ynDia=pan.erlistener.shareYorNDia();      ynDia.setLab("Going to Delete ?");      ynDia.addYorNListener(this);    }    public void gotYes(int val){      if(val==this.yes){        try{          for(int i=0;i<tps.length;i++){            /*System.out.println(              ((Messager)((DMTreeNode)tps[i].getLastPathComponent()).getUserObject()).getName()            );*/            DMTreeNode tn=(DMTreeNode)tps[i].getLastPathComponent();            DMTreeNode par=(DMTreeNode)tps[i].getParentPath().getLastPathComponent();            if(par!=null){              ((DefaultTreeModel)Tree.this.treeModel).removeNodeFromParent(tn);            }          }        }catch(NullPointerException e){          Reporter r=pan.erlistener.shareReporter();          r.setStateLab("Root node should not be delete.");          r.show();        }      }    }  }  protected class AddLeafAction extends AbstractAction{    public void actionPerformed(ActionEvent e){      //DMTreeNode tn=(DMTreeNode)getSelectionPath().getLastPathComponent();      //((Messager)tn.getUserObject()).setContent(null);      addNodeAction.actionPerformed(null);    }  }}

⌨️ 快捷键说明

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