Rambler's Top100

А Б В Г Д Е Ж З И К Л М Н О П Р С Т У Ф Х Ц Ч Ш Э Ю Я

Все примеры | Примеры по пакетам

Перетаскивание поддерева в JTree


//Перетаскивание поддерева в JTree
//Любое переопубликование запрещено
//Автор - Е.С.Крылов
//Drag and drop entire subtree from (to) JTree
//Any warranties.
//Any republishing is denied
//You can use as you want except republish it in any media.
//Author is Evgeny S. Krylov
package tree;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.*;
import java.awt.dnd.DropTargetContext;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class TreeDndSubTreeSelf extends JFrame implements DropTargetListener {
static Vector child = new Vector();
static Vector child1 = new Vector();
static Map<  String, Vector> map = new HashMap<  String, Vector >();
static Map<  String, Vector> submap=new HashMap<  String, Vector >();
static DefaultTreeModel tm;
        static String dragged="";
        TreeNode parentDraggednode;
        DropTarget dt;
        JTextArea ta;
        JTree jt=new JTree(tm);
        TreeNode targetNode;
  public TreeDndSubTreeSelf() {
    super("TreeDndSubTreeSelf");
    setSize(400, 500);
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    ta = new JTextArea();
    ta.setBackground(Color.white);   
    jt.setDragEnabled(true);
    expandAll(jt);
    getContentPane().add(jt, BorderLayout.CENTER);
    dt = new DropTarget(jt, this);
    setVisible(true);
  }
  public void dragEnter(DropTargetDragEvent dtde) {
    
  }
  public void dragExit(DropTargetEvent dte) {
    
  }
 private TreeNode getNodeForEvent(DropTargetDragEvent dtde) {
    Point p = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath path = tree.getClosestPathForLocation(p.x, p.y);
    return (TreeNode) path.getLastPathComponent();
  }
  public void dragOver(DropTargetDragEvent dtde) {   
     TreeNode node = getNodeForEvent(dtde);
    if (node.isLeaf()) {
    } else {
      dtde.acceptDrag(dtde.getDropAction());      
    }
     targetNode=node;
     String sel="";
     if(jt.getLastSelectedPathComponent()!=null){
          sel=jt.getLastSelectedPathComponent().toString();
          dragged=sel;
               TreeNode drgnode=(TreeNode)jt.getLastSelectedPathComponent();
          parentDraggednode=drgnode.getParent();
     }
  }
  public void dropActionChanged(DropTargetDragEvent dtde) {
    
  }
  public void drop(DropTargetDropEvent dtde) {
    try {
      String parentDragged=parentDraggednode.toString();
     submap(dragged,map.get(dragged));
     if(map.get(targetNode.toString())==null){
      Vector v2=new Vector();
      v2.add(dragged);
        map.get(parentDragged).remove(dragged);
        submap.put(targetNode.toString(), v2);
        map.putAll(submap);
        tm=  createTreeModel(tm,map,"a");
        jt.setModel(tm);
        expandAll(jt);
     }
     else{
      Vector v2=new Vector();
      v2=map.get(targetNode.toString());
      v2.add(dragged);
        map.get(parentDragged).remove(dragged);
        submap.put(targetNode.toString(), v2);
        map.putAll(submap);
        tm=  createTreeModel(tm,map,"a");
        jt.setModel(tm);
        expandAll(jt);
     }
    } catch (Exception e) {
      e.printStackTrace();
      dtde.rejectDrop();
    }
  }
public void expandAll(JTree tree) {
for (int i = 0; i <  tree.getRowCount(); i++) {
         tree.expandRow(i);
    }
    }
static DefaultTreeModel createTreeModel(DefaultTreeModel tm,Map<  String, Vector > map,String rootnode){
    Vector child=new Vector();
DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootnode);
       tm = new DefaultTreeModel(root);
        child=map.get(rootnode);
      addNewNodes(tm,map,root,child);
      return tm;
}
static void addNewNodes(DefaultTreeModel tm,Map<  String, Vector > map,DefaultMutableTreeNode currNode,Vector vec){
 DefaultMutableTreeNode node0=null;
 for(int i=0;i<  vec.size();i++){
     DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(vec.get(i));
        tm.insertNodeInto(childNode, currNode, currNode.getChildCount());
        if(map.get(childNode.toString())!=null){
            child1=map.get(childNode.toString());
      addNewNodes(tm,map,childNode,child1);
        }
    }
}
static void submap(String key,Vector child){
        if(map.containsKey(key)){
            submap.put(key, child);
            for(int i=0;i<  child.size();i++){
                String key1=child.get(i).toString();
               submap(key1,map.get(key1));
            }
        }
}
  public static void main(String args[]) {
 child = new Vector();
        child.add("b");
        child.add("c");
        map.put("a",child);
        //--добавление детских узлов к "a"
        //add child nodes to "a"
        child = new Vector();
        child.add("b1");
        child.add("b2");
        map.put("b",child);
        //-добавление детских узлов к "b"
        //add child nodes to "b"
        child = new Vector();
        child.add("c1");
        child.add("c2");
        child.add("c3");
        map.put("c",child);
        //---добавление детских узлов к "c"
        //add child nodes to "c"
        child = new Vector();
        child.add("c11");
        child.add("c12");
        child.add("c13");
        child.add("c14");
        map.put("c1",child);
        //add child nodes to "c1"
        child = new Vector();
        child.add("c21");
        child.add("c22");
        child.add("c23");
        map.put("c2",child);
        //add child nodes to "c2"
        child = new Vector();
        child.add("c221");
        child.add("c222");
        child.add("c223");
        map.put("c22",child);
        //добавление детских узлов к "c122"
      //add child nodes to "c22"
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("a");
        tm = new DefaultTreeModel(root);
        child=map.get("a");
         addNewNodes(tm,map,root,child);
      DefaultMutableTreeNode node0 = new DefaultMutableTreeNode(child.get(0));
      child1=map.get(node0.toString());
      System.out.println("child1 "+child1);
      addNewNodes(tm,map,node0,child1);
    
    new TreeDndSubTreeSelf();
  }
}

31.01.2009

Rambler's Top100


Ассоциативные ссылки