API Reference 0.7.1rikulo_modelDefaultTreeNode<T>

DefaultTreeNode<T> class

The default implementation of TreeNode.

Notice that DefaultTreeModel only assumes TreeNode. In other words, you can use any implementation of TreeNode with DefaultTreeModel.

Load Children Lazily

If you'd like to load children lazily (i.e., load the children nodes when it is used), you can override loadLazily_ and load the children nodes in the method.

class DefaultTreeNode<T> extends TreeNode<T> {
 DefaultTreeModel<T> _model;
 TreeNode<T> _parent;
 List<TreeNode<T>> _children;
 T _data;
 bool _leaf, _loaded = false;

 DefaultTreeNode([T data, Iterable nodes, bool leaf]) {
   _data = data;
   _leaf = leaf;
   if (nodes != null)
     addAll(nodes);
 }
 static int _$uuid = 0;
 DefaultTreeModel<T> get model => _parent != null ? _parent.model: _model;
 void set model(DefaultTreeModel<T> model) {
   _model = model;
 }

 T get data => _data;
 void set data(T data) {
   if (!identical(_data, data)) {
     _data = data;

     final DefaultTreeModel<T> m = model;
     if (m != null)
       m.sendEvent(new TreeDataEvent(model, 'change', this));
   }
 }

 bool get isLeaf => _leaf != null ? _leaf: _children == null || _children.isEmpty;

 TreeNode<T> operator[](int childIndex) {
   _init();
   if (_children == null)
     throw new RangeError(childIndex);
   return _children[childIndex];
 }
 int get length {
    _init();
   return _children != null ? _children.length: 0;
 }
 TreeNode<T> get parent => _parent;

 /**
  * Returns the index of this child ([TreeNode]).
  *
  * The implementation uses `List.indexOf` to retrieve the index, so the
  * performance might not be good if the list is big.
  * If the list is sorted, you can override this method to utilize it.
  */
 int get index {
   if (_parent == null)
     return 0;
   if (_parent is! DefaultTreeNode)
     throw new ModelError("DefaultTreeNode expected, not $_parent");

   final DefaultTreeNode p = _parent;
   return p._children.indexOf(this);
 }

 void add(TreeNode<T> child, [int index]) {
   _init();
   if (_leaf != null && _leaf)
     throw new UnsupportedError("Leaf node doesn't allow child");

   if (child.parent != null)
     child.parent.remove(child.index);

   if (_children == null)
     _children = new List();
   _children.insert(index != null ? index: _children.length, child);

   if (child is DefaultTreeNode) {
     final DefaultTreeNode c = child;
     c._parent = this;
   }

   final DefaultTreeModel<T> m = model;
   if (m != null)
     m.sendEvent(new TreeDataEvent(model, 'add', child));
 }
 void addAll(Iterable nodes, [int index]) {
   _init();
   if (index == null)
     index = _children != null ? _children.length: 0;

   for (final node in nodes)
     add(node is TreeNode ? node: new DefaultTreeNode(node), index++);
 }
 TreeNode<T> remove(int index) {
   _init();
   final DefaultTreeModel<T> m = model;
   TreeNode<T> child = this[index];

   if (m != null)
     _cleanSelOpen(m, child);

   _children.removeAt(index);

   if (child is DefaultTreeNode) {
     final DefaultTreeNode c = child;
     c._parent = null;
   }

   if (m != null)
     m.sendEvent(new TreeDataEvent(model, 'remove', child));
   return child;
 }
 static void _cleanSelOpen(DefaultTreeModel m, TreeNode child) {
   //no need to fire event (since it is convered by DATA_REMOVED)
   m._selection.remove(child);
   m._opens.remove(child);

   if (!child.isLeaf) {
     for (int i = 0, len = child.length; i < len; ++i)
       _cleanSelOpen(m, child[i]);
   }
 }
 void clear() {
   _init();
   if (_children != null && !_children.isEmpty) {
     final DefaultTreeModel<T> m = model;
     if (m != null) {
       for (final TreeNode<T> child in _children)
         _cleanSelOpen(m, child);
     }

     _children = null;

     if (m != null) {
       m.sendEvent(new TreeDataEvent(model, 'change', this));
     }
   }
 }

 void _init() {
   if (!_loaded) {
     _loaded = true;
     _children = loadLazily_();
     if (_children != null) {
       for (final TreeNode<T> child in _children) {
         if (child is DefaultTreeNode) {
           final DefaultTreeNode c = child;
           c._parent = this;
         }
       }
     }
   }
 }
 /**
  * Returns the intial collection of child nodes, or null if there is no children at all.
  *
  * Default: return null.
  *
  * If you'd like to load tree nodes lazily, you can override this method, and
  * return a collection of child nodes.
  */
 Iterable<TreeNode<T>> loadLazily_() => null;
 String toString() => "DefaultTreeNode($data)";
}

Extends

TreeNode<T> > DefaultTreeNode<T>

Constructors

new DefaultTreeNode([T data, Iterable nodes, bool leaf]) #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
DefaultTreeNode([T data, Iterable nodes, bool leaf]) {
 _data = data;
 _leaf = leaf;
 if (nodes != null)
   addAll(nodes);
}

Properties

T get data #

Returns the application-specific data held in this node.

docs inherited from TreeNode<T>
T get data => _data;

void set data(T data) #

Sets the application-specific data held in this node.

docs inherited from TreeNode<T>
void set data(T data) {
 if (!identical(_data, data)) {
   _data = data;

   final DefaultTreeModel<T> m = model;
   if (m != null)
     m.sendEvent(new TreeDataEvent(model, 'change', this));
 }
}

final int index #

Returns the index of this child (TreeNode).

The implementation uses List.indexOf to retrieve the index, so the performance might not be good if the list is big. If the list is sorted, you can override this method to utilize it.

int get index {
 if (_parent == null)
   return 0;
 if (_parent is! DefaultTreeNode)
   throw new ModelError("DefaultTreeNode expected, not $_parent");

 final DefaultTreeNode p = _parent;
 return p._children.indexOf(this);
}

final bool isLeaf #

Returns true if this node is a leaf.

docs inherited from TreeNode<T>
bool get isLeaf => _leaf != null ? _leaf: _children == null || _children.isEmpty;

final int length #

Returns the number of children TreeNodes this node contains.

docs inherited from TreeNode<T>
int get length {
  _init();
 return _children != null ? _children.length: 0;
}

DefaultTreeModel<T> get model #

Returns the tree model this node belongs to.

docs inherited from TreeNode<T>
DefaultTreeModel<T> get model => _parent != null ? _parent.model: _model;

void set model(DefaultTreeModel<T> model) #

Sets the tree model it belongs to. This method is invoked automatically by DefaultTreeModel, so you don't have to invoke it.

It can be called only if this node is a root. If a node has a parent, its model shall be the same as its parent.

docs inherited from TreeNode<T>
void set model(DefaultTreeModel<T> model) {
 _model = model;
}

final TreeNode<T> parent #

Returns the parent TreeNode of this node.

docs inherited from TreeNode<T>
TreeNode<T> get parent => _parent;

Operators

TreeNode<T> operator [](int childIndex) #

Returns the child (TreeNode) at the given index.

docs inherited from TreeNode<T>
TreeNode<T> operator[](int childIndex) {
 _init();
 if (_children == null)
   throw new RangeError(childIndex);
 return _children[childIndex];
}

Methods

void add(TreeNode<T> child, [int index]) #

Adds child to this node.

  • index the index that child will be added at. If null, child will be added to the end.

docs inherited from TreeNode<T>
void add(TreeNode<T> child, [int index]) {
 _init();
 if (_leaf != null && _leaf)
   throw new UnsupportedError("Leaf node doesn't allow child");

 if (child.parent != null)
   child.parent.remove(child.index);

 if (_children == null)
   _children = new List();
 _children.insert(index != null ? index: _children.length, child);

 if (child is DefaultTreeNode) {
   final DefaultTreeNode c = child;
   c._parent = this;
 }

 final DefaultTreeModel<T> m = model;
 if (m != null)
   m.sendEvent(new TreeDataEvent(model, 'add', child));
}

void addAll(Iterable nodes, [int index]) #

Adds a collection of children nodes.

Each element of the given nodes can be an instance of TreeNode or the data. For example,

node.addAll(["apple", "orange"]);
node.addAll([new DefaultTreeNode("group", ["item1", "item2"])]);
  • nodes is a collection of nodes to add. Any element of it can be TreeNode or the data.

  • index is the index that nodes will be added at. If null, nodes will be added to the end.

docs inherited from TreeNode<T>
void addAll(Iterable nodes, [int index]) {
 _init();
 if (index == null)
   index = _children != null ? _children.length: 0;

 for (final node in nodes)
   add(node is TreeNode ? node: new DefaultTreeNode(node), index++);
}

void clear() #

Removes all children nodes.

docs inherited from TreeNode<T>
void clear() {
 _init();
 if (_children != null && !_children.isEmpty) {
   final DefaultTreeModel<T> m = model;
   if (m != null) {
     for (final TreeNode<T> child in _children)
       _cleanSelOpen(m, child);
   }

   _children = null;

   if (m != null) {
     m.sendEvent(new TreeDataEvent(model, 'change', this));
   }
 }
}

Iterable<TreeNode<T>> loadLazily_() #

Returns the intial collection of child nodes, or null if there is no children at all.

Default: return null.

If you'd like to load tree nodes lazily, you can override this method, and return a collection of child nodes.

Iterable<TreeNode<T>> loadLazily_() => null;

TreeNode<T> remove(int index) #

Removes the child at index from this node.

This method returns the tree node being removed.

docs inherited from TreeNode<T>
TreeNode<T> remove(int index) {
 _init();
 final DefaultTreeModel<T> m = model;
 TreeNode<T> child = this[index];

 if (m != null)
   _cleanSelOpen(m, child);

 _children.removeAt(index);

 if (child is DefaultTreeNode) {
   final DefaultTreeNode c = child;
   c._parent = null;
 }

 if (m != null)
   m.sendEvent(new TreeDataEvent(model, 'remove', child));
 return child;
}

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() => "DefaultTreeNode($data)";