API Reference 0.7.1rikulo_modelDefaultListModel<T>

DefaultListModel<T> class

The default implementation of ListModel.

class DefaultListModel<T> extends AbstractListModel<T> {
 /** The original data.
  * Don't modify it directly. Otherwise, UI won't be synchronized automatically.
  */
 final List<T> data;

 /** Constructor.
  *
  * Notice that once [data] is assigned to a list model, you shall not
  * modify the data directly. Rather, you shall invoke the methods of this
  * model, such as [add]. Otherwise, UI won't update the changes correctly.
  *
  * + [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.
  */
 DefaultListModel(List<T> data,
 {Set<T> selection, Set<T> disables, bool multiple: false}):
 super(selection: selection, disables: disables, multiple: multiple),
 this.data = data;

 @override
 /** Returns the object of the given index.
  */
 T operator [](int index) => data[index];
 @override
 /** Returns the length of the list.
  */
 int get length => data.length;

 //additional interface//
 /** Returns the index of the given data, or -1 if not found.
  *
  * 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.
  *
  * This method is designed for use in application. The impelmentation of a view
  * shall access API available in [TreeModel].
  */
 int indexOf(T value) => data.indexOf(value);

 /** Assigns a value to the given index.
  */
 void operator[]=(int index, T value) {
   final T old = data[index];
   data[index] = value;

   //Note: no need to send 'select' since 1) 'change' will update UI
   //2) if app modifies model in 'select', a dead loop happens (if we send 'select')
   if (_selection.contains(old)) {
     _selection.remove(old);
     _selection.add(value);
   }
   if (_disables.contains(old)) {
     _disables.remove(old);
     _disables.add(value);
   }
   sendEvent(new ListDataEvent(this, 'change', index, index + 1));
 }
 /** Adds a value to the end of the list.
  */
 void add(T value) {
   data.add(value);
   sendEvent(new ListDataEvent(this, 'add', length - 1, length));
 }
 /** Removes the last value.
  */
 T removeLast() {
   final T value = data.removeLast();
   _selection.remove(value); //no need to fire select
   sendEvent(new ListDataEvent(this, 'remove', length, length + 1));
   return value;
 }
 /** Inserts a range of values to the given index.
  */
 void insert(int index, [T value]) {
   data.insert(index, value);
   sendEvent(new ListDataEvent(this, 'add', index, index + 1));
 }
 /** Removes a range of values starting at the given index.
  */
 void removeRange(int start, int end) {
   for (int i = start, len = end - start; --len >= 0;)
     _selection.remove(data[i++]); //no need to fire select
   data.removeRange(start, end);
   sendEvent(new ListDataEvent(this, 'remove', start, end));
 }
 void clear() {
   final len = data.length;
   if (len > 0) {
     _selection.clear(); //no need to fire select
     data.clear();
     sendEvent(new ListDataEvent(this, 'remove', 0, len));
   }
 }

 @override
 String toString() => "DefaultListModel($data)";
}

Extends

DataModel > AbstractDataModel<T> > AbstractListModel<T> > DefaultListModel<T>

Constructors

new DefaultListModel(List<T> data, {Set<T> selection, Set<T> disables, bool multiple: false}) #

Constructor.

Notice that once data is assigned to a list model, you shall not modify the data directly. Rather, you shall invoke the methods of this model, such as add. Otherwise, UI won't update the changes correctly.

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

DefaultListModel(List<T> data,
{Set<T> selection, Set<T> disables, bool multiple: false}):
super(selection: selection, disables: disables, multiple: multiple),
this.data = data;

Properties

final List<T> data #

The original data. Don't modify it directly. Otherwise, UI won't be synchronized automatically.

final List<T> data

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

inherited from AbstractDataModel

Returns true if the selection is currently empty.

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

final int length #

Returns the length of the list.

@override
/** Returns the length of the list.
*/
int get length => data.length;

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;

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

Operators

T operator [](int index) #

Returns the object of the given index.

@override
/** Returns the object of the given index.
*/
T operator [](int index) => data[index];

void operator []=(int index, T value) #

Assigns a value to the given index.

void operator[]=(int index, T value) {
 final T old = data[index];
 data[index] = value;

 //Note: no need to send 'select' since 1) 'change' will update UI
 //2) if app modifies model in 'select', a dead loop happens (if we send 'select')
 if (_selection.contains(old)) {
   _selection.remove(old);
   _selection.add(value);
 }
 if (_disables.contains(old)) {
   _disables.remove(old);
   _disables.add(value);
 }
 sendEvent(new ListDataEvent(this, 'change', index, index + 1));
}

Methods

void add(T value) #

Adds a value to the end of the list.

void add(T value) {
 data.add(value);
 sendEvent(new ListDataEvent(this, 'add', length - 1, length));
}

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

void clear() {
 final len = data.length;
 if (len > 0) {
   _selection.clear(); //no need to fire select
   data.clear();
   sendEvent(new ListDataEvent(this, 'remove', 0, len));
 }
}

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

int indexOf(T value) #

Returns the index of the given data, or -1 if not found.

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.

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

int indexOf(T value) => data.indexOf(value);

void insert(int index, [T value]) #

Inserts a range of values to the given index.

void insert(int index, [T value]) {
 data.insert(index, value);
 sendEvent(new ListDataEvent(this, 'add', index, index + 1));
}

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

T removeLast() #

Removes the last value.

T removeLast() {
 final T value = data.removeLast();
 _selection.remove(value); //no need to fire select
 sendEvent(new ListDataEvent(this, 'remove', length, length + 1));
 return value;
}

void removeRange(int start, int end) #

Removes a range of values starting at the given index.

void removeRange(int start, int end) {
 for (int i = start, len = end - start; --len >= 0;)
   _selection.remove(data[i++]); //no need to fire select
 data.removeRange(start, end);
 sendEvent(new ListDataEvent(this, 'remove', start, end));
}

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

String toString() #

Returns a string representation of this object.

docs inherited from Object
@override
String toString() => "DefaultListModel($data)";