コード例 #1
0
  private void addNodeToTree(
      JTree.DropLocation dropLocation,
      LayerTreeModel model,
      INode node,
      boolean alreadyInTree,
      int offset) {
    TreePath p = dropLocation.getPath();
    boolean noparent = p == null;
    INode parent = null;
    int index = 0;

    if (!noparent) {
      parent = (INode) p.getLastPathComponent();
      index = dropLocation.getChildIndex();

      if (index < 0) index = parent.getChildCount();
      else index += offset;
    }

    if (alreadyInTree) {
      if (!noparent) {
        if (node == parent || nodeAncestorOf(node, parent)) return;
        if (node.getParent() == parent && index > model.getIndexOfChild(parent, node)) index--;
      }

      model.removeNodeFromParent(node, false);
    }

    if (noparent) model.addToRoot(node, true);
    else model.insertNodeInto(node, parent, index, true);
  }
コード例 #2
0
ファイル: MyTransferHandler.java プロジェクト: judu/AlmaGTD
  @Override
  public boolean importData(TransferHandler.TransferSupport th) {
    Transferable tr = th.getTransferable();
    Component cp = th.getComponent();
    if (tr.isDataFlavorSupported(MyTransferable.data)) {
      if (cp instanceof JTree) {
        JTree tree = (JTree) cp;
        JTree.DropLocation location = tree.getDropLocation();
        TreePath path = location.getPath();
        int index = location.getChildIndex();
        if (index == -1) {
          index = 0;
        }
        // System.out.println(index);
        DefaultMutableTreeNode node;
        try {
          node = (DefaultMutableTreeNode) tr.getTransferData(MyTransferable.data);
          DefaultMutableTreeNode parent = (DefaultMutableTreeNode) path.getLastPathComponent();

          // vérifie si on ne drop pas dans un noeud fils
          if (node.isNodeChild(parent)) {
            return false;
          }
          Controleur controleur = Controleur.getInstance();
          controleur.deplacerElement(node, parent, index);

          DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
          model.removeNodeFromParent(node);
          // vérifie si on est plus hors index lors d'une réorganisation par ex
          if (index > parent.getChildCount()) {
            index = parent.getChildCount();
          }
          model.insertNodeInto(node, parent, index);
          //					controleur.ajoutElement(node, parent, index);

          TreePath newPath = path.pathByAddingChild(node);
          tree.makeVisible(newPath);
          tree.scrollRectToVisible(tree.getPathBounds(newPath));
        } catch (Exception e) {
          e.printStackTrace();
        }
        return true;
      }
    }
    return false;
  }