API Reference 0.7.1rikulo_modelAbstractDataModel<T>

AbstractDataModel<T> class

A skeletal implementation of DataModel, SelectionModel and DisablesModel.

class AbstractDataModel<T> extends DataModel
implements SelectionModel<T>, DisablesModel<T> {
 Set<T> _selection, _disables;
 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.
  */
 AbstractDataModel({Set<T> selection, Set<T> disables, bool multiple:false}) {
   _selection = selection != null ? selection: new Set();
   _disables = disables != null ? disables: new Set();
   _multiple = multiple;
 }

 //Event Handling//
 void _sendSelect() {
   sendEvent(new DataEvent(this, 'select'));
 }
 void _sendDisable() {
   sendEvent(new DataEvent(this, 'disable'));
 }

 //SelectionModel//
 T get selectedValue => _selection.isEmpty ? null: _selection.first;
 Set<T> get selection => _selection;

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

 @override
 bool isSelected(Object obj)  => _selection.contains(obj);
 @override
 bool get isSelectionEmpty => _selection.isEmpty;

 @override
 bool addToSelection(T obj) {
   if (_selection.contains(obj))
     return false;

   if (!_multiple)
     _selection.clear();
   _selection.add(obj);
   _sendSelect();
   return true;
 }
 @override
 bool removeFromSelection(Object obj) {
   if (_selection.remove(obj)) {
     _sendSelect();
     return true;
   }
   return false;
 }
 @override
 void clearSelection() {
   if (!_selection.isEmpty) {
     _selection.clear();
     _sendSelect();
   }
 }

 @override
 bool get multiple => _multiple;
 @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();
     }
   }
 }

 //DisablesModel//
 Set<T> get disables => _disables;
 void set disables(Iterable<T> disables) {
   if (!_equals(_disables, disables)) {
     _disables.clear();
     _disables.addAll(disables);
     _sendDisable();
   }
 }
 bool isDisabled(Object obj)  => _disables.contains(obj);
 bool get isDisablesEmpty => _disables.isEmpty;
 bool addToDisables(T obj) {
   if (_disables.contains(obj))
     return false;

   _disables.add(obj);
   _sendDisable();
   return true;
 }
 bool removeFromDisables(Object obj) {
   if (_disables.remove(obj)) {
     _sendDisable();
     return true;
   }
   return false;
 }
 void clearDisables() {
   if (!_disables.isEmpty) {
     _disables.clear();
     _sendDisable();
   }
 }

 //Additional API//
 /**Removes the given collection from the selection.
  */
 void removeAllSelection(Iterable c) {
   final int oldlen = _selection.length;
   _selection.removeAll(c);
   if (oldlen != _selection.length)
     _sendSelect();
 }
 /**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();
 }

 /** Compares a set with a collection.
  */
 static bool _equals(Set set, Iterable col) {
   if (set.length != col.length)
     return false;

   for (final e in col)
     if (!set.contains(e))
       return false;
   return true;
 }
}

Extends

DataModel > AbstractDataModel<T>

Subclasses

AbstractListModel<T>, AbstractTreeModel<T>

Implements

DisablesModel<T>, SelectionModel<T>

Constructors

new AbstractDataModel({Set<T> selection, Set<T> disables, 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.

AbstractDataModel({Set<T> selection, Set<T> disables, bool multiple:false}) {
 _selection = selection != null ? selection: new Set();
 _disables = disables != null ? disables: new Set();
 _multiple = multiple;
}

Properties

Set<T> get disables #

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

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 #

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

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

final bool isSelectionEmpty #

Returns true if the selection is currently empty.

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

bool get multiple #

Returns whether the current selection mode is multiple.

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

void set multiple(bool multiple) #

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;

final T selectedValue #

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 #

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

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

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 addToSelection(T obj) #

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

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

Change the selection to the empty set.

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

bool isDisabled(Object obj) #

Returns whether an object is disabled.

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

bool isSelected(Object obj) #

Returns whether an object is selected.

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

void removeAllDisables(Iterable c) #

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 removeAllSelection(Iterable c) #

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

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

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