Ejemplo n.º 1
0
      public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (isTargetNode) {
          DefaultMutableTreeNode destinyNode = (DefaultMutableTreeNode) dropTargetNode;
          DefaultMutableTreeNode dropNode = (DefaultMutableTreeNode) draggedNode;
          DefaultMutableTreeNode selectedNode =
              (DefaultMutableTreeNode) dragAndDropTree.getLastSelectedPathComponent();
          if ((dropNode.getAllowsChildren()
                  && !dropNode.isNodeDescendant(destinyNode)
                  && !destinyNode.isNodeChild(dropNode))
              || (!dropNode.getAllowsChildren()
                  && !destinyNode.getUserObject().equals(selectedNode.getUserObject()))) {
            g.setColor(new Color(0, 29, 255, 100));
            g.fillRect(17, 0, getSize().width - 17, getSize().height - 1);
          }
        } else g.setColor(new Color(0, 29, 255, 0));
      }
Ejemplo n.º 2
0
 @Override
 public void drop(DropTargetDropEvent dtde) {
   Point dropPoint = dtde.getLocation();
   TreePath path = getPathForLocation(dropPoint.x, dropPoint.y);
   System.out.println("drop path is " + path);
   boolean dropped = false;
   try {
     dtde.acceptDrop(DnDConstants.ACTION_MOVE);
     // Object dropObject = dtde.getTransferable().getTransferData(dataFlavor);
     // if (dropObject instanceof DefaultMutableTreeNode) {
     if (draggedNode instanceof DefaultMutableTreeNode) {
       DefaultMutableTreeNode dropNode = (DefaultMutableTreeNode) draggedNode;
       if (dropNode.getAllowsChildren()) {
         DefaultMutableTreeNode destinyNode =
             (DefaultMutableTreeNode) path.getLastPathComponent();
         if (!dropNode.isNodeDescendant(destinyNode) && !destinyNode.isNodeChild(dropNode)) {
           if (moveDirectory(dropNode, destinyNode)) {
             ((DefaultTreeModel) getModel()).removeNodeFromParent(dropNode);
             ((DefaultTreeModel) getModel())
                 .insertNodeInto(dropNode, destinyNode, destinyNode.getChildCount());
             dropped = true;
           }
         }
       } else {
         DefaultMutableTreeNode destinyNode =
             (DefaultMutableTreeNode) path.getLastPathComponent();
         DefaultMutableTreeNode selectedNode =
             (DefaultMutableTreeNode) this.getLastSelectedPathComponent();
         if (!selectedNode.getUserObject().equals(destinyNode.getUserObject())) {
           if (moveDocument(dropNode, destinyNode)) {
             setSelectionPath(path);
             dropped = true;
             dragDropEnd();
           }
         }
       }
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
   dtde.dropComplete(dropped);
 }
  private void restoreSelection(Pair<ElementNode, List<ElementNode>> pair) {
    List<ElementNode> selectedNodes = pair.second;

    DefaultMutableTreeNode root = getRootNode();

    ArrayList<TreePath> toSelect = new ArrayList<TreePath>();
    for (ElementNode node : selectedNodes) {
      DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
      if (root.isNodeDescendant(treeNode)) {
        toSelect.add(new TreePath(treeNode.getPath()));
      }
    }

    if (!toSelect.isEmpty()) {
      myTree.setSelectionPaths(toSelect.toArray(new TreePath[toSelect.size()]));
    }

    ElementNode leadNode = pair.first;
    if (leadNode != null) {
      myTree.setLeadSelectionPath(new TreePath(((DefaultMutableTreeNode) leadNode).getPath()));
    }
  }