Class TreeDragAndDrop<V,N extends AbstractTreeNode<V,N>,R>

java.lang.Object
com.amalgamasimulation.desktop.ui.tables.TreeDragAndDrop<V,N,R>
Type Parameters:
V - the type of the row values
N - the type of the node
R - the type of the builder that end() returns

public class TreeDragAndDrop<V,N extends AbstractTreeNode<V,N>,R> extends Object
Settings for moving the nodes of an ObjectTree with the mouse: the action that moves the object in the model, and the rules that tell where the node can be dropped. dragAndDrop(action) in Trees.objects(roots) or Pages.objectTree(type) creates this object. The rules are added in a chain, and end() returns the tree builder.

Relation to TreeDrop. The tree describes each possible move with a TreeDrop: which node, onto which row, and to which place. TreeDragAndDrop decides what to do with this TreeDrop:

  1. While the user moves the node over the rows, the tree creates a TreeDrop for the row under the cursor and asks TreeDragAndDrop whether the node can be dropped there.
  2. TreeDragAndDrop forbids a drop of a group, a drop onto a group, and a drop of a node onto itself or onto its descendants. Then it checks all rules. The drop is allowed only if all rules return true.
  3. When the user releases the node at an allowed place, TreeDragAndDrop calls the action with this TreeDrop. The action moves the object in the model.
  4. After the action the tree itself moves the row of the node with all its children.

Rules

Without rules, the node can be dropped at any place except the places in step 2. Ready rules: withinParent() keeps the node under its parent, sameLevel() keeps the node on its level, and forbid(fromLevel, toLevel) forbids a move from one level to another. rule(predicate) sets your own condition. The variants with the level parameter apply only to the nodes of this level and let the other nodes pass. The level of a node is the number of its ancestors, 0 for a root (see TreeDrop).
Author:
Ionov Ivan
  • Constructor Details

    • TreeDragAndDrop

      public TreeDragAndDrop(R owner, Consumer<TreeDrop<V,N>> action)
      Creates the settings for the builder owner with the action. Usually dragAndDrop(action) of the tree builder creates them.
      Parameters:
      owner - the builder that end() returns
      action - the action that moves the object in the model
  • Method Details

    • withinParent

      public TreeDragAndDrop<V,N,R> withinParent()
      Allows a drop only if the node keeps the same parent. So only the order of the children can change.
      Returns:
      this object
    • withinParent

      public TreeDragAndDrop<V,N,R> withinParent(int level)
      Same as withinParent(), but only for the nodes of the given level.
      Parameters:
      level - the level of the nodes that the rule applies to
      Returns:
      this object
    • sameLevel

      public TreeDragAndDrop<V,N,R> sameLevel()
      Allows a drop only if the level of the node does not change. The parent can change.
      Returns:
      this object
    • sameLevel

      public TreeDragAndDrop<V,N,R> sameLevel(int level)
      Same as sameLevel(), but only for the nodes of the given level.
      Parameters:
      level - the level of the nodes that the rule applies to
      Returns:
      this object
    • forbid

      public TreeDragAndDrop<V,N,R> forbid(int fromLevel, int toLevel)
      Forbids a move of a node from level fromLevel to level toLevel.
      Parameters:
      fromLevel - the level of the node before the drop
      toLevel - the level of the node after the drop
      Returns:
      this object
    • rule

      public TreeDragAndDrop<V,N,R> rule(Predicate<TreeDrop<V,N>> rule)
      Adds your own rule. The drop is allowed only if the rule returns true.
      Parameters:
      rule - the rule that gets the TreeDrop
      Returns:
      this object
    • end

      public R end()
      Returns:
      the tree builder, to continue the chain