Class Tree<T>

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.Composite<com.vaadin.flow.component.orderedlayout.VerticalLayout>
org.vaadin.firitin.components.Tree<T>
Type Parameters:
T - the type of items listed as nodes of Tree. Use Object if nothing else.
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasStyle, Serializable

@StyleSheet("context://frontend/org/vaadin/firitin/components/tree.css") public class Tree<T> extends com.vaadin.flow.component.Composite<com.vaadin.flow.component.orderedlayout.VerticalLayout>
A Tree component to display hierarchical data sets.
Author:
mstahv
See Also:
  • Constructor Details

    • Tree

      public Tree()
  • Method Details

    • setItems

      public void setItems(List<? extends T> rootNodes, Tree.ChildrenProvider<T> childrenProvider)
      Sets the root nodes for the tree with a strategy to fetch children for items (recursively). The nodes will be closed by default and tree structure will be populated lazily when nodes are opened.

      Code example:

         List<Dude> rootNodes = getRootNodes();
         dudeTree.setItems(rootNodes, Dude::getSubordinates);
         dudeTree.showChildrenRecursively(rootNodes.get(0));
       
      Parameters:
      rootNodes - the items to be shown in the Tree at the root level
      childrenProvider - the strategy to fetch children from the nodes
    • setItems

      public void setItems(T rootNode, Tree.ChildrenProvider<T> childrenProvider)
      Sets the root node for the tree with a strategy to fetch children for items (recursively). The nodes will be closed by default and tree structure will be populated lazily when nodes are opened.

      Code example:

         dudeTree.setItems(dude, Dude::getSubordinates);
         dudeTree.showChildrenRecursively(dude);
       
      Parameters:
      rootNode - the item to be shown in the Tree at the root level
      childrenProvider - the strategy to fetch children from the nodes
    • setItems

      public void setItems(List<? extends T> roots, Tree.ChildrenProvider<? extends T>... providersForNextLevels)
      Sets root items with multiple children providers used to fetch sub-sequent levels. This approach is handy if the hierarchy in the Tree comes from class hierarchy (different levels are different Java classes). The T type of the Tree in this case needs to be some common super type of the classes (java.lang.Object if no other exist).

      Code example:

        Tree<AbstractPlace> tree = new Tree<>();
        tree.setItemLabelGenerator(AbstractPlace::getName);
        tree.seItems(cities,
                (Tree.ChildrenProvider<City>) c -> c.getStreets(),
                (Tree.ChildrenProvider<Street>) s -> s.getHouses()
         );
       
      Parameters:
      roots - root items in the Tree
      providersForNextLevels - the children providers for next levels of tree, that fetch the children for given item representing the tree node.
    • setItems

      public void setItems(T root, Tree.ChildrenProvider<? extends T>... providersForNextLevels)
      Sets root item with multiple children providers used to fetch sub-sequent levels. This approach is handy if the hierarchy in the Tree comes from class hierarchy (different levels are different Java classes). The T type of the Tree in this case needs to be some common super type of the classes (java.lang.Object if no other exist).

      Code example:

        Tree<AbstractPlace> tree = new Tree<>();
        tree.setItemLabelGenerator(AbstractPlace::getName);
        tree.seItems(city,
                (Tree.ChildrenProvider<City>) c -> c.getStreets(),
                (Tree.ChildrenProvider<Street>) s -> s.getHouses()
         );
       
      Parameters:
      root - root item in the Tree
      providersForNextLevels - the children providers for next levels of tree, that fetch the children for given item representing the tree node.
    • fillWithProviders

      protected void fillWithProviders(int level, T item, TreeItem treeItem, Tree.ChildrenProvider... providers)
    • setItems

      @Deprecated public void setItems(com.vaadin.flow.data.provider.hierarchy.TreeData<T> treeData, Tree.ChildrenProvider<T> childrenProvider)
      Deprecated.
      Helper to move from TreeGrid usage, in case the TreeData (from Vaadin core) happens to be used to describe the hierarchy.
      Parameters:
      treeData - d
      childrenProvider - p
    • createTreeItem

      protected TreeItem createTreeItem(T item)
    • fillTree

      protected void fillTree(Tree.ChildrenProvider<T> childrenProvider, T item, TreeItem treeItem)
    • addItemDecorator

      public void addItemDecorator(Tree.ItemDecorator<T> decorator)
      Adds an Tree.ItemDecorator to further configure TreeItems generated automatically when setItems(List, ChildrenProvider) method is called.
      Parameters:
      decorator - the Tree.ItemDecorator
    • removeItemDecorator

      public void removeItemDecorator(Tree.ItemDecorator<T> decorator)
    • setItemLabelGenerator

      public void setItemLabelGenerator(com.vaadin.flow.component.ItemLabelGenerator<T> itemLabelGenerator)
      Sets the strategy to generate label texts for the items.
      Parameters:
      itemLabelGenerator - the ItemLabelGenerator
    • setItemIconGenerator

      public void setItemIconGenerator(Tree.ItemIconGenerator<T> itemIconGenerator)
      Sets the strategy to generate icons for the items.
      Parameters:
      itemIconGenerator - the Tree.ItemIconGenerator
    • setItemGenerator

      public void setItemGenerator(Tree.ItemGenerator<T> itemGenerator)
      Sets the strategy to generate component for for the items.

      Note that this overrides possibly configured Tree.ItemIconGenerator and ItemLabelGenerator.

      Parameters:
      itemGenerator - the Tree.ItemGenerator
    • addSelectionListener

      public void addSelectionListener(Tree.SelectionListener<T> listener)
      Adds selection listener to nodes.
      Parameters:
      listener - the listener to be called when selected item changes
    • removeSelectionListener

      public void removeSelectionListener(Tree.SelectionListener<T> listener)
    • showChildren

      public void showChildren(T item)
      Shows children of the node in UI. Same as user would click on the caret in the UI.
      Parameters:
      item - the item whose children should be visible in the UI
    • showChildrenRecursively

      public void showChildrenRecursively(T item)
      Shows children of the node in UI recursively. Same as user would click on the caret in the UI.
      Parameters:
      item - the item whose children should be visible in the UI
    • hideChildren

      public void hideChildren(T item)
      Hides children of the node in UI. Same as user would click on the caret in the UI when children are visible.
      Parameters:
      item - the item whose children should be hidden in the UI
    • moveChild

      public void moveChild(T parent, List<T> items, int index, boolean up)
      Moves child of the node in UI.
      Parameters:
      parent - the parent whose child should be moved in the UI, may be null
      items - the items within which one is to be moved
      index - the index of the item to be moved
      up - True then move up else move down
    • addChild

      public void addChild(T parent, T item)
      Adds child of the node in UI.
      Parameters:
      parent - the parent whose child should be added in the UI, may be null
      item - the item to be added in the UI
    • removeChild

      public void removeChild(T parent, T item)
      Removes child of the node in UI.
      Parameters:
      parent - the parent whose child should be removed in the UI, may be null
      item - the item to be removed in the UI
    • editChild

      public void editChild(T item)
      Edits child in UI.
      Parameters:
      item - the item to be edited in the UI
    • styleChild

      public void styleChild(T item, String styleName, String styleValue)
      Parameters:
      item - the item to be styled in the UI
      styleName - the style property name as camelCase, not null
      styleValue - the style property value (if null, the property will be removed)
    • scrollItemToView

      public TreeItem scrollItemToView(T item)
      Scrolls the tree item representing given item to be visible in the UI.

      Note, the item needs to be visible for the method to work.

      Parameters:
      item - the item to be shown in the UI
      Returns:
      The TreeItem for the scroll
    • selectItem

      public void selectItem(T item, boolean selected)
      Selects (or deselects) the tree item representing given item in the UI.
      Parameters:
      item - item the item to be selected in the UI
      selected - if True then select else deselect
    • deselectAllItems

      public void deselectAllItems()
      Deselects all tree items in the UI.