|
|
@@ -367,6 +367,89 @@ public class TreeData<T> implements Serializable { |
|
|
|
.unmodifiableList(itemToWrapperMap.get(item).getChildren()); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Moves an item to become a child of the given parent item. The new parent |
|
|
|
* item must exist in the hierarchy. Setting the parent to {@code null} |
|
|
|
* makes the item a root item. After making changes to the tree data, {@link |
|
|
|
* TreeDataProvider#refreshAll()} should be called. |
|
|
|
* |
|
|
|
* @param item |
|
|
|
* the item to be set as the child of {@code parent} |
|
|
|
* @param parent |
|
|
|
* the item to be set as parent or {@code null} to set the item as |
|
|
|
* root |
|
|
|
* @since 8.1 |
|
|
|
*/ |
|
|
|
public void setParent(T item, T parent) { |
|
|
|
if (!contains(item)) { |
|
|
|
throw new IllegalArgumentException( |
|
|
|
"Item '" + item + "' not in the hierarchy"); |
|
|
|
} |
|
|
|
|
|
|
|
if (parent != null && !contains(parent)) { |
|
|
|
throw new IllegalArgumentException( |
|
|
|
"Parent needs to be added before children. " |
|
|
|
+ "To set as root item, call with parent as null"); |
|
|
|
} |
|
|
|
|
|
|
|
if (item.equals(parent)) { |
|
|
|
throw new IllegalArgumentException( |
|
|
|
"Item cannot be the parent of itself"); |
|
|
|
} |
|
|
|
|
|
|
|
T oldParent = itemToWrapperMap.get(item).getParent(); |
|
|
|
|
|
|
|
if (!Objects.equals(oldParent, parent)) { |
|
|
|
// Remove item from old parent's children |
|
|
|
itemToWrapperMap.get(oldParent).removeChild(item); |
|
|
|
|
|
|
|
// Add item to parent's children |
|
|
|
itemToWrapperMap.get(parent).addChild(item); |
|
|
|
|
|
|
|
// Set item's new parent |
|
|
|
itemToWrapperMap.get(item).setParent(parent); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Moves an item to the position immediately after a sibling item. The two |
|
|
|
* items must have the same parent. After making changes to the tree data, |
|
|
|
* {@link TreeDataProvider#refreshAll()} should be called. |
|
|
|
* |
|
|
|
* @param item |
|
|
|
* the item to be moved |
|
|
|
* @param sibling |
|
|
|
* the item after which the moved item will be located |
|
|
|
* @since 8.1 |
|
|
|
*/ |
|
|
|
public void moveAfterSibling(T item, T sibling) { |
|
|
|
if (!contains(item)) { |
|
|
|
throw new IllegalArgumentException( |
|
|
|
"Item '" + item + "' not in the hierarchy"); |
|
|
|
} |
|
|
|
|
|
|
|
if (!contains(sibling)) { |
|
|
|
throw new IllegalArgumentException( |
|
|
|
"Item '" + sibling + "' not in the hierarchy"); |
|
|
|
} |
|
|
|
|
|
|
|
T parent = itemToWrapperMap.get(item).getParent(); |
|
|
|
|
|
|
|
if (!Objects |
|
|
|
.equals(parent, itemToWrapperMap.get(sibling).getParent())) { |
|
|
|
throw new IllegalArgumentException( |
|
|
|
"Items '" + item + "' and '" + sibling |
|
|
|
+ "' don't have the same parent"); |
|
|
|
} |
|
|
|
|
|
|
|
List<T> children = itemToWrapperMap.get(parent).getChildren(); |
|
|
|
|
|
|
|
// Move item to the position after the sibling |
|
|
|
// If sibling is null, item is moved to the first position |
|
|
|
children.remove(item); |
|
|
|
children.add(children.indexOf(sibling) + 1, item); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Check whether the given item is in this hierarchy. |
|
|
|
* |