API Reference 0.7.1rikulo_modelTreeModel<T>

TreeModel<T> abstract class

A data model representing a tree of data. Each node of the tree can be anything and represented with the generic type, T.

Instead of implementing this interface from scratch, it is suggested:

  1. Use DefaultTreeModel if you'd like to represent the tree with a tree of TreeNode. In other words, each node of the tree is represented with an instance of TreeNode, and the data can be retrieved by TreeNode.data.

  2. Extends from AbstractTreeModel. AbstractTreeModel provides the implementation of handling of data listeners, maintaining the open states and the selection.

How Data Retrieved

To retrieve the data in the data model,

  1. root is called first to retrieve the root data
  2. isLeaf is called to examine if the data (aka., the node) is a leaf node
  3. If it is not a leaf, getChildCount is called to retrieve the number of children
  4. Then, getChild is called to retrieve each child
  5. Repeat 2-4

Path

A node of the tree model can be presented by an integer array called path. For example, 0, 2, 1 represents a node at the third level (excluding root) since there are three elements. Furthermore, it is the root's the first child's the third child's second child. YOu can retrieve it as follows

model.getChildAt([0, 2, 1]);

TreeModel doesn't not provide API to retrieve the path of a given node (since sometimes it is hard to get). Thus, the view that supports TreeModel shall store the path, if necessary, when rendering.

On the other hand, DefaultTreeModel and TreeNode do provide getPath() to retrieve the path of any given node.

SelectionModel

If you'd like to use TreeModel with a UI object that allows the user to select the data, such as DropDownList. You have to implement SelectionModel. Both DefaultTreeModel and AbstractTreeModel implements it, so you need to implement it only if you implement TreeModel from scratch.

OpensModel

A tree model maintains a list of opened nodes (OpensModel). If a tree node matches one of the open nodes, the tree node will be opened, i.e., the child views will be visible to the user.

abstract class TreeModel<T> extends DataModel {
	/**
	 * Returns the root of the tree model.
	 */
	T get root;
	/**
	 * Returns the child of the given parent at the given index where the index indicates
	 * in the parent's child array.
	 *
	 * + [parent] is a node in the tree, obtained from [root] or [getChild].
	 */
	T getChild(T parent, int index);
	/**
	 * Returns the number of children of the given parent.
	 *
	 * + [parent] is a node in the tree, obtained from [root] or [getChild].
	 */
	int getChildCount(T parent);
	/**
	 * Returns true if node is a leaf.
	 * In file-system terminology, a leaf node is a file, while a non-leaf node is a folder.
	 *
	 * + [node] is the data returned by [root] or [getChild].
	 */
	bool isLeaf(T node);

	/**
	 * Returns the child at the given path where the path indicates the child is
	 * placed in the whole tree.
	 *
	 * + [path] is a list of the index at each level of the tree. For example, [0, 1, 2]
	 * represents the first child's the second child's third child.
	 */
	T getChildAt(List<int> path);
}

Extends

DataModel > TreeModel<T>

Subclasses

AbstractTreeModel<T>

Properties

final DataEvents on #

inherited from DataModel

Returns DataEvents for adding or removing event listeners.

DataEvents get on => _on;

final T root #

Returns the root of the tree model.

T get root;

Methods

void addEventListener(String type, DataEventListener listener) #

inherited from DataModel

Adds an event listener. addEventListener("select", listener) is the same as on.select.add(listener).

void addEventListener(String type, DataEventListener listener) {
 if (listener == null)
   throw new ArgumentError("listener");

 bool first = false;
 _listeners.putIfAbsent(type, () {
   first = true;
   return [];
 }).add(listener);
}

abstract T getChild(T parent, int index) #

Returns the child of the given parent at the given index where the index indicates in the parent's child array.

  • parent is a node in the tree, obtained from root or getChild.

abstract T getChildAt(List<int> path) #

Returns the child at the given path where the path indicates the child is placed in the whole tree.

  • path is a list of the index at each level of the tree. For example, 0, 1, 2 represents the first child's the second child's third child.

abstract int getChildCount(T parent) #

Returns the number of children of the given parent.

  • parent is a node in the tree, obtained from root or getChild.

abstract bool isLeaf(T node) #

Returns true if node is a leaf. In file-system terminology, a leaf node is a file, while a non-leaf node is a folder.

void removeEventListener(String type, DataEventListener listener) #

inherited from DataModel

Removes an event listener. removeEventListener("select", listener) is the same as on.select.remove(listener).

void removeEventListener(String type, DataEventListener listener) {
 final list = _listeners[type];
 if (list != null)
   list.remove(listener);
}

bool sendEvent(DataEvent event) #

inherited from DataModel

Sends an event to this model.

Example: `model.sendEvent(new ListDataEvent(model, "select"))</code>.

bool sendEvent(DataEvent event) {
 final bool
   b1 = _sendEvent(event, _listeners[event.type]),
   b2 = _sendEvent(event, _listeners['all']);
 return b1 || b2;
}