API Reference 0.7.1rikulo_modelDefaultTreeModel<T>

DefaultTreeModel<T> class

The default implementation of TreeModel. It assumes each node of the tree is an instance of TreeNode.

Example,

DefaultTreeModel<String> model = new DefaultTreeModel(nodes: [
  "Wonderland",
  new DefaultTreeNode("Australia",
    ["Sydney", "Melbourne", "Port Hedland"]),
  new DefaultTreeNode("New Zealand",
    ["Cromwell", "Queenstown"])]);
model.addToSelection(model.root[1][2]);
model.on.select.add((event) {
  //do something when it is selected
});

Big Tree

To implement a big tree, it is better to load the children only when they are required. It is called Lazy Loading. Lazy loading is a characteristic of an application when the actual loading and instantiation of a class is delayed until the point just before the instance is actually used.

To implement Lazy Loading, you can implement a tree node by extending DefaultTreeNode and then override DefaultTreeNode.loadLazily_ to return the initial child nodes.

class DefaultTreeModel<T> extends AbstractTreeModel<TreeNode<T>> {
 /** Constructor.
  *
  * Notice that a tree node ([TreeNode]) can't be shared in two tree model.
  *
  * + [root]: the root. If not specified, a default tree node will be instantiated.
  * + [nodes] is a collection of nodes to add. Any element of it can
  * be [TreeNode] or the data.
  * + [selection]: if not null, it will be used to hold the selection.
  * Unlike [set selection], it won't make a copy.
  * + [disables]: if not null, it will be used to hold the list of disabled items.
  * Unlike [set disables], it won't make a copy.
  * + [opens]: if not null, it will be used to hold the list of opened items.
  * Unlike [set opens], it won't make a copy.
  */
 DefaultTreeModel({TreeNode<T> root, Iterable nodes, Set<TreeNode<T>> selection,
 Set<TreeNode<T>> disables, Set<TreeNode<T>> opens, bool multiple:false}):
 super(root != null ? root: new DefaultTreeNode(), selection: selection,
 disables: disables, opens: opens, multiple: multiple) {
   final TreeNode<T> p = _root.parent; //don't use the root argument since it might be null
   if (p != null)
     throw new ModelError("Only root node is allowed, not ${_root}");
   _root.model = this;
   if (nodes != null)
     _root.addAll(nodes);
 }

 TreeNode<T> getChild(TreeNode<T> parent, int index) => parent[index];
 int getChildCount(TreeNode<T> parent) => parent.length;
 bool isLeaf(TreeNode<T> node) => node.isLeaf;

 //Additional API//
 /**
  * Returns the index of the given child in the given parent.
  * If either parent or child is null, returns -1.
  * If either parent or child don't belong to this tree model, returns -1. 
  *
  * This method is designed for use in application. The impelmentation of a view
  * shall access API available in [TreeModel]
  *
  * + [parent] is a node in the tree, obtained from [root] or [getChild].
  * + [child] the node we are interested in 
  */
 int getIndexOfChild(TreeNode<T> parent, TreeNode<T> child)
 => identical(parent, child.parent) ? child.index: -1;

 /**
  * Returns the path from the given child, where the path indicates the child is
  * placed in the whole tree.
  *
  * This method is designed for use in application. The impelmentation of a view
  * shall access API available in [TreeModel]
  */
 List<int> getPath(TreeNode<T> child) { //optional but provided for better performance
   List<int> path = new List();
   for (;;) {
     final TreeNode<T> parent = child.parent;
     if (parent == null)
       break; //child is not in the same model

     path.insert(0, child.index);
     child = parent;
   }
   return path;
 }
}

Extends

DataModel > AbstractDataModel<T> > AbstractTreeModel<TreeNode<T>> > DefaultTreeModel<T>

Constructors

new DefaultTreeModel({TreeNode<T> root, Iterable nodes, Set<TreeNode<T>> selection, Set<TreeNode<T>> disables, Set<TreeNode<T>> opens, bool multiple: false}) #

Constructor.

Notice that a tree node (TreeNode) can't be shared in two tree model.

  • root: the root. If not specified, a default tree node will be instantiated.
  • nodes is a collection of nodes to add. Any element of it can be TreeNode or the data.

  • selection: if not null, it will be used to hold the selection. Unlike set selection, it won't make a copy.

  • disables: if not null, it will be used to hold the list of disabled items. Unlike set disables, it won't make a copy.

  • opens: if not null, it will be used to hold the list of opened items. Unlike set opens, it won't make a copy.

DefaultTreeModel({TreeNode<T> root, Iterable nodes, Set<TreeNode<T>> selection,
Set<TreeNode<T>> disables, Set<TreeNode<T>> opens, bool multiple:false}):
super(root != null ? root: new DefaultTreeNode(), selection: selection,
disables: disables, opens: opens, multiple: multiple) {
 final TreeNode<T> p = _root.parent; //don't use the root argument since it might be null
 if (p != null)
   throw new ModelError("Only root node is allowed, not ${_root}");
 _root.model = this;
 if (nodes != null)
   _root.addAll(nodes);
}

Properties

Set<T> get disables #

inherited from AbstractDataModel

Returns the current list of disabled objects. It is readonly. Don't modify it directly. Otherwise, UI won't be updated correctly.

docs inherited from DisablesModel<T>
Set<T> get disables => _disables;

void set disables(Iterable<T> disables) #

inherited from AbstractDataModel

Replace the current list of disabled objects with the given set.

Notice this method copies the content of disables, so it is OK to use it after the invocation.

docs inherited from DisablesModel<T>
void set disables(Iterable<T> disables) {
 if (!_equals(_disables, disables)) {
   _disables.clear();
   _disables.addAll(disables);
   _sendDisable();
 }
}

final bool isDisablesEmpty #

inherited from AbstractDataModel

Returns true if the list of the disabled objects is currently empty.

docs inherited from DisablesModel<T>
bool get isDisablesEmpty => _disables.isEmpty;

final bool isOpensEmpty #

inherited from AbstractTreeModel

Returns true if the list of opened nodes is empty.

docs inherited from OpensModel<T>
bool get isOpensEmpty => _opens.isEmpty;

final bool isSelectionEmpty #

inherited from AbstractDataModel

Returns true if the selection is currently empty.

docs inherited from SelectionModel<T>
@override
bool get isSelectionEmpty => _selection.isEmpty;

bool get multiple #

inherited from AbstractDataModel

Returns whether the current selection mode is multiple.

docs inherited from SelectionModel<T>
@override
bool get multiple => _multiple;

void set multiple(bool multiple) #

inherited from AbstractDataModel

Sets the selection mode to be multiple.

docs inherited from SelectionModel<T>
@override
void set multiple(bool multiple) {
 if (_multiple != multiple) {
   _multiple = multiple;
   sendEvent(new DataEvent(this, 'multiple'));

   if (!multiple && _selection.length > 1) {
     final T v = _selection.first;
     _selection.clear();
     _selection.add(v);
     _sendSelect();
   }
 }
}

final DataEvents on #

inherited from DataModel

Returns DataEvents for adding or removing event listeners.

DataEvents get on => _on;

Set<T> get opens #

inherited from AbstractTreeModel

Returns the current list of nodes that are opened. It is readonly. Don't modify it directly. Otherwise, UI won't be updated correctly.

docs inherited from OpensModel<T>
Set<T> get opens => _opens;

void set opens(Iterable<T> opens) #

inherited from AbstractTreeModel

Replace the current list of node that are opened with the given set.

Notice this method copies the content of opens, so it is OK to use it after the invocation.

docs inherited from OpensModel<T>
void set opens(Iterable<T> opens) {
 if (_opens != opens) {
   _opens.clear();
   _opens.addAll(opens);
   _sendOpen();
 }
}

T get root #

inherited from AbstractTreeModel

Returns the root of the tree model.

docs inherited from TreeModel<T>
T get root => _root;

void set root(T root) #

inherited from AbstractTreeModel

Sets the root of the tree model.

void set root(T root) {
 if (!identical(_root, root)) {
   _root = root;
   _selection.clear();
   _opens.clear();
   sendEvent(new DataEvent(this, 'structure'));
 }
}

final T selectedValue #

inherited from AbstractDataModel

Returns the first selected value, or null if none is selected.

docs inherited from SelectionModel<T>
T get selectedValue => _selection.isEmpty ? null: _selection.first;

Set<T> get selection #

inherited from AbstractDataModel

Returns the current selection. It is readonly. Don't modify it directly. Otherwise, UI won't be updated correctly.

docs inherited from SelectionModel<T>
Set<T> get selection => _selection;

void set selection(Iterable<T> selection) #

inherited from AbstractDataModel

Replace the current selection with the given set.

Notice this method copies the content of selection, so it is OK to use it after the invocation.

docs inherited from SelectionModel<T>
void set selection(Iterable<T> selection) {
 if (!_equals(_selection, selection)) {
   if (!_multiple && selection.length > 1)
     throw new ModelError("Only one selection is allowed, $selection");
   _selection.clear();
   _selection.addAll(selection);
   _sendSelect();
 }
}

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);
}

bool addToDisables(T obj) #

inherited from AbstractDataModel

Add the specified object into the current list of disabled objects. It returns whether it has been added successfully. Returns false if it is already disabled.

docs inherited from DisablesModel<T>
bool addToDisables(T obj) {
 if (_disables.contains(obj))
   return false;

 _disables.add(obj);
 _sendDisable();
 return true;
}

bool addToOpens(T node) #

inherited from AbstractTreeModel

Adds the given node to the list of opened nodes.

docs inherited from OpensModel<T>
bool addToOpens(T node) {
 if (_opens.contains(node))
   return false;

 _opens.add(node);
  _sendOpen();
 return true;
}

bool addToSelection(T obj) #

inherited from AbstractDataModel

Add the specified object into selection. It returns whether it has been added successfully. Returns false if it is already selected.

docs inherited from SelectionModel<T>
@override
bool addToSelection(T obj) {
 if (_selection.contains(obj))
   return false;

 if (!_multiple)
   _selection.clear();
 _selection.add(obj);
 _sendSelect();
 return true;
}

void clearDisables() #

inherited from AbstractDataModel

Change the current list of disabled objects to the empty set.

docs inherited from DisablesModel<T>
void clearDisables() {
 if (!_disables.isEmpty) {
   _disables.clear();
   _sendDisable();
 }
}

void clearOpens() #

inherited from AbstractTreeModel

Empties the list of opened nodes.

docs inherited from OpensModel<T>
void clearOpens() {
 if (!_opens.isEmpty) {
   _opens.clear();
   _sendOpen();
 }
}

void clearSelection() #

inherited from AbstractDataModel

Change the selection to the empty set.

docs inherited from SelectionModel<T>
@override
void clearSelection() {
 if (!_selection.isEmpty) {
   _selection.clear();
   _sendSelect();
 }
}

TreeNode<T> getChild(TreeNode<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.
docs inherited from TreeModel<T>
TreeNode<T> getChild(TreeNode<T> parent, int index) => parent[index];

T getChildAt(List<int> path) #

inherited from AbstractTreeModel

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.

docs inherited from TreeModel<T>
T getChildAt(List<int> path) {
 if (path == null || path.length == 0)
   return root;

 T parent = root;
 T node = null;
 int childCount = _childCount(parent);
 for (int i = 0; i < path.length; i++) {
   if (path[i] < 0 || path[i] > childCount //out of bound
   || (node = getChild(parent, path[i])) == null //model is wrong
   || ((childCount = _childCount(node)) <= 0 && i != path.length - 1)) //no more child
     return null;

   parent = node;
 }
 return node;
}

int getChildCount(TreeNode<T> parent) #

Returns the number of children of the given parent.

  • parent is a node in the tree, obtained from root or getChild.
docs inherited from TreeModel<T>
int getChildCount(TreeNode<T> parent) => parent.length;

int getIndexOfChild(TreeNode<T> parent, TreeNode<T> child) #

Returns the index of the given child in the given parent. If either parent or child is null, returns -1. If either parent or child don't belong to this tree model, returns -1.

This method is designed for use in application. The impelmentation of a view shall access API available in TreeModel

  • parent is a node in the tree, obtained from root or getChild.
  • child the node we are interested in
int getIndexOfChild(TreeNode<T> parent, TreeNode<T> child)
=> identical(parent, child.parent) ? child.index: -1;

List<int> getPath(TreeNode<T> child) #

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

This method is designed for use in application. The impelmentation of a view shall access API available in TreeModel

List<int> getPath(TreeNode<T> child) { //optional but provided for better performance
 List<int> path = new List();
 for (;;) {
   final TreeNode<T> parent = child.parent;
   if (parent == null)
     break; //child is not in the same model

   path.insert(0, child.index);
   child = parent;
 }
 return path;
}

bool isDisabled(Object obj) #

inherited from AbstractDataModel

Returns whether an object is disabled.

docs inherited from DisablesModel<T>
bool isDisabled(Object obj)  => _disables.contains(obj);

bool isLeaf(TreeNode<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.

docs inherited from TreeModel<T>
bool isLeaf(TreeNode<T> node) => node.isLeaf;

bool isOpened(T node) #

inherited from AbstractTreeModel

Returns true if the node shall be opened. That is, it tests if the given node is in the list of opened nodes.

docs inherited from OpensModel<T>
bool isOpened(T node) => _opens.contains(node);

bool isSelected(Object obj) #

inherited from AbstractDataModel

Returns whether an object is selected.

docs inherited from SelectionModel<T>
@override
bool isSelected(Object obj)  => _selection.contains(obj);

void removeAllDisables(Iterable c) #

inherited from AbstractDataModel

Removes the given collection from the list of disabled object.

void removeAllDisables(Iterable c) {
 final int oldlen = _disables.length;
 _disables.removeAll(c);
 if (oldlen != _disables.length)
   _sendDisable();
}

void removeAllOpens(Iterable c) #

inherited from AbstractTreeModel

Removes the given collection from the list of opened nodes.

void removeAllOpens(Iterable c) {
 final int oldlen = _opens.length;
 _opens.removeAll(c);
 if (oldlen != _opens.length)
   _sendOpen();
}

void removeAllSelection(Iterable c) #

inherited from AbstractDataModel

Removes the given collection from the selection.

void removeAllSelection(Iterable c) {
 final int oldlen = _selection.length;
 _selection.removeAll(c);
 if (oldlen != _selection.length)
   _sendSelect();
}

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 removeFromDisables(Object obj) #

inherited from AbstractDataModel

Remove the specified object from the current list of disabled objects. It returns whether it is removed successfully. Returns false if it is not disabled.

docs inherited from DisablesModel<T>
bool removeFromDisables(Object obj) {
 if (_disables.remove(obj)) {
   _sendDisable();
   return true;
 }
 return false;
}

bool removeFromOpens(T node) #

inherited from AbstractTreeModel

Removes the given node from the list of opened nodes.

docs inherited from OpensModel<T>
bool removeFromOpens(T node) {
 if (_opens.remove(node)) {
   _sendOpen();
   return true;
 }
 return false;
}

bool removeFromSelection(Object obj) #

inherited from AbstractDataModel

Remove the specified object from selection. It returns whether it is removed successfully. Returns false if it is not selected.

docs inherited from SelectionModel<T>
@override
bool removeFromSelection(Object obj) {
 if (_selection.remove(obj)) {
   _sendSelect();
   return true;
 }
 return false;
}

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;
}