API Reference 0.7.1rikulo_modelAbstractTreeModel<T>

AbstractTreeModel<T> abstract class

A skeletal implementation of TreeModel. To extend from this class, you have to implement getChild, getChildCount and isLeaf. This class provides a default implementation for all other methods.

abstract class AbstractTreeModel<T> extends AbstractDataModel<T>
implements TreeModel<T>, OpensModel<T> {
 T _root;
 Set<T> _opens;

 /** Constructor.
  *
  * + [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.
  */
 AbstractTreeModel(T root, {Set<T> selection, Set<T> disables,
 Set<T> opens, bool multiple: false}):
 super(selection: selection, disables: disables, multiple: multiple) {
   _root = root;
   _opens = opens != null ? opens: new Set();
 }

 void _sendOpen() {
   sendEvent(new DataEvent(this, 'open'));
 }

 //TreeModel//
 T get root => _root;
 /** 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'));
   }
 }

 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 _childCount(T parent) => isLeaf(parent) ? 0: getChildCount(parent);

 //Open//
 Set<T> get opens => _opens;
 void set opens(Iterable<T> opens) {
   if (_opens != opens) {
     _opens.clear();
     _opens.addAll(opens);
     _sendOpen();
   }
 }

 bool isOpened(T node) => _opens.contains(node);
 bool get isOpensEmpty => _opens.isEmpty;

 bool addToOpens(T node) {
   if (_opens.contains(node))
     return false;

   _opens.add(node);
    _sendOpen();
   return true;
 }
 bool removeFromOpens(T node) {
   if (_opens.remove(node)) {
     _sendOpen();
     return true;
   }
   return false;
 }
 void clearOpens() {
   if (!_opens.isEmpty) {
     _opens.clear();
     _sendOpen();
   }
 }

 //Additional API//
 /**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();
 }
}

Extends

DataModel > AbstractDataModel<T> > AbstractTreeModel<T>

Subclasses

DefaultTreeModel<T>

Implements

OpensModel<T>, TreeModel<T>

Constructors

new AbstractTreeModel(T root, {Set<T> selection, Set<T> disables, Set<T> opens, bool multiple: false}) #

Constructor.

  • 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.

AbstractTreeModel(T root, {Set<T> selection, Set<T> disables,
Set<T> opens, bool multiple: false}):
super(selection: selection, disables: disables, multiple: multiple) {
 _root = root;
 _opens = opens != null ? opens: new Set();
}

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 #

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 #

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) #

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 #

Returns the root of the tree model.

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

void set root(T root) #

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) #

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() #

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

abstract T getChild(T parent, int index) #

inherited from TreeModel

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 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.

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

abstract int getChildCount(T parent) #

inherited from TreeModel

Returns the number of children of the given parent.

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

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

abstract bool isLeaf(T node) #

inherited from TreeModel

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.

bool isOpened(T node) #

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) #

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) #

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