WidgetCollection class
A simple collection of widgets to be used by {@link com.google.gwt.user.client.ui.Panel panels} and {@link com.google.gwt.user.client.ui.Composite composites}.
The main purpose of this specialized collection is to implement {@link java.util.Iterator#remove()} in a way that delegates removal to its panel. This makes it much easier for the panel to implement an {@link com.google.gwt.user.client.ui.HasWidgets#iterator() iterator} that supports removal of widgets.
class WidgetCollection extends Iterable<Widget> { static int _INITIAL_SIZE = 4; List<Widget> _array; HasWidgets _parent; int _size = 0; /** * Constructs a new widget collection. * * @param _parent the container whose {@link HasWidgets#remove(Widget)} will be * delegated to by the iterator's {@link Iterator#remove()} method. */ WidgetCollection(this._parent) { this._parent = _parent; _array = new List<Widget>(_INITIAL_SIZE); } /** * Adds a widget to the end of this collection. * * @param w the widget to be added */ void add(Widget w) { insert(w, _size); } /** * Determines whether a given widget is contained in this collection. * * @param w the widget to be searched for * @return <code>true</code> if the widget is present */ bool contains(Widget w) { return (indexOf(w) != -1); } /** * Gets the widget at the given index. * * @param index the index to be retrieved * @return the widget at the specified index * @throws IndexOutOfBoundsException if the index is out of range */ Widget get(int index) { if ((index < 0) || (index >= _size)) { throw new Exception("IndexOutOfBoundsException"); } return _array[index]; } /** * Gets the index of the specified index. * * @param w the widget to be found * @return the index of the specified widget, or <code>-1</code> if it is * not found */ int indexOf(Widget w) { for (int i = 0; i < _size; ++i) { if (_array[i] == w) { return i; } } return -1; } /** * Inserts a widget before the specified index. * * @param w the widget to be inserted * @param beforeIndex the index before which the widget will be inserted * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of * range */ void insert(Widget w, int beforeIndex) { if ((beforeIndex < 0) || (beforeIndex > _size)) { throw new Exception("IndexOutOfBoundsException"); } // Realloc _array if necessary (doubling). if (_size == _array.length) { List<Widget> newArray = new List<Widget>(_array.length * 2); for (int i = 0; i < _array.length; ++i) { newArray[i] = _array[i]; } _array = newArray; } ++_size; // Move all widgets after 'beforeIndex' back a slot. for (int i = _size - 1; i > beforeIndex; --i) { _array[i] = _array[i - 1]; } _array[beforeIndex] = w; } /** * Gets an iterator on this widget collection. This iterator is guaranteed to * implement remove() in terms of its containing {@link HasWidgets}. * * @return an iterator */ RemoveIterator<Widget> get iterator { return new WidgetIterator(this); } /** * Removes the widget at the specified index. * * @param index the index of the widget to be removed * @throws IndexOutOfBoundsException if <code>index</code> is out of range */ void remove(int index) { if ((index < 0) || (index >= _size)) { throw new Exception("IndexOutOfBounds"); } --_size; for (int i = index; i < _size; ++i) { _array[i] = _array[i + 1]; } _array[_size] = null; } /** * Removes the specified widget. * * @param w the widget to be removed * @throws NoSuchElementException if the widget is not present */ void removeWidget(Widget w) { int index = indexOf(w); if (index == -1) { throw new Exception("NoSuchElement"); } remove(index); } /** * Gets the number of widgets in this collection. * * @return the number of widgets */ int size() { return _size; } //********************** int get length { return _size;} bool get isEmpty { return _size == 0;} bool get isNotEmpty { return _size != 0; } Widget get first { throw new UnsupportedError("");} Widget get last { throw new UnsupportedError("");} Widget get single { throw new UnsupportedError("");} Iterable map(f(Widget element)) { throw new UnsupportedError("");} Iterable<Widget> where(bool f(Widget element)) { throw new UnsupportedError("");} Iterable expand(Iterable f(Widget element)) { throw new UnsupportedError("");} void forEach(void f(Widget element)) { for (int i = 0; i < _size; ++i) { f(_array[i]); } } Widget reduce(Widget combine(Widget value, Widget element)) { throw new UnsupportedError("");} dynamic fold(var initialValue, dynamic combine(var previousValue, Widget element)) { throw new UnsupportedError("");} bool every(bool f(Widget element)) { throw new UnsupportedError("");} bool any(bool f(Widget element)) { throw new UnsupportedError("");} List<Widget> toList({ bool growable: true }) { throw new UnsupportedError("");} Set<Widget> toSet() { throw new UnsupportedError("");} Iterable<Widget> take(int n) { throw new UnsupportedError("");} Iterable<Widget> takeWhile(bool test(Widget value)) { throw new UnsupportedError("");} Iterable<Widget> skip(int n) { throw new UnsupportedError("");} Iterable<Widget> skipWhile(bool test(Widget value)) { throw new UnsupportedError("");} Widget firstWhere(bool test(Widget value), { Widget orElse() }) { throw new UnsupportedError("");} Widget lastWhere(bool test(Widget value), {Widget orElse()}) { throw new UnsupportedError("");} Widget singleWhere(bool test(Widget value)) { throw new UnsupportedError("");} Widget elementAt(int index) { throw new UnsupportedError("");} }
Extends
Iterable<Widget> > WidgetCollection
Constructors
new WidgetCollection(HasWidgets _parent) #
Constructs a new widget collection.
@param _parent the container whose {@link HasWidgets#remove(Widget)} will be
delegated to by the iterator's {@link Iterator#remove()} method.
WidgetCollection(this._parent) { this._parent = _parent; _array = new List<Widget>(_INITIAL_SIZE); }
Properties
final Widget first #
Returns the first element.
If this
is empty throws a StateError. Otherwise this method is
equivalent to this.elementAt(0)
Widget get first { throw new UnsupportedError("");}
final bool isEmpty #
Returns true if there is no element in this collection.
bool get isEmpty { return _size == 0;}
final bool isNotEmpty #
Returns true if there is at least one element in this collection.
bool get isNotEmpty { return _size != 0; }
final RemoveIterator<Widget> iterator #
Gets an iterator on this widget collection. This iterator is guaranteed to implement remove() in terms of its containing {@link HasWidgets}.
@return an iterator
RemoveIterator<Widget> get iterator { return new WidgetIterator(this); }
final Widget last #
Returns the last element.
If this
is empty throws a StateError.
Widget get last { throw new UnsupportedError("");}
final int length #
Returns the number of elements in this
.
Counting all elements may be involve running through all elements and can therefore be slow.
int get length { return _size;}
final Widget single #
Returns the single element in this
.
If this
is empty or has more than one element throws a StateError.
Widget get single { throw new UnsupportedError("");}
Methods
void add(Widget w) #
Adds a widget to the end of this collection.
@param w the widget to be added
void add(Widget w) { insert(w, _size); }
bool any(bool f(Widget element)) #
Returns true if one element of this collection satisfies the
predicate test
. Returns false otherwise.
bool any(bool f(Widget element)) { throw new UnsupportedError("");}
bool contains(Widget w) #
Determines whether a given widget is contained in this collection.
@param w the widget to be searched for @return <code>true</code> if the widget is present
bool contains(Widget w) { return (indexOf(w) != -1); }
Widget elementAt(int index) #
Returns the indexth element.
If this
has fewer than
index elements throws a RangeError.
Note: if this
does not have a deterministic iteration order then the
function may simply return any element without any iteration if there are
at least
index elements in this
.
Widget elementAt(int index) { throw new UnsupportedError("");}
bool every(bool f(Widget element)) #
Returns true if every elements of this collection satisify the
predicate test
. Returns false
otherwise.
bool every(bool f(Widget element)) { throw new UnsupportedError("");}
Iterable expand(Iterable f(Widget element)) #
Expands each element of this Iterable into zero or more elements.
The resulting Iterable runs through the elements returned by f for each element of this, in order.
The returned Iterable is lazy, and calls f for each element of this every time it's iterated.
Iterable expand(Iterable f(Widget element)) { throw new UnsupportedError("");}
Widget firstWhere(bool test(Widget value), {Widget orElse()}) #
Returns the first element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
Widget firstWhere(bool test(Widget value), { Widget orElse() }) { throw new UnsupportedError("");}
dynamic fold(initialValue, combine(previousValue, Widget element)) #
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.
Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.
Example of calculating the sum of an iterable:
iterable.fold(0, (prev, element) => prev + element);
dynamic fold(var initialValue, dynamic combine(var previousValue, Widget element)) { throw new UnsupportedError("");}
void forEach(void f(Widget element)) #
Applies the function f to each element of this collection.
void forEach(void f(Widget element)) { for (int i = 0; i < _size; ++i) { f(_array[i]); } }
Widget get(int index) #
Gets the widget at the given index.
@param index the index to be retrieved @return the widget at the specified index @throws IndexOutOfBoundsException if the index is out of range
Widget get(int index) { if ((index < 0) || (index >= _size)) { throw new Exception("IndexOutOfBoundsException"); } return _array[index]; }
int indexOf(Widget w) #
Gets the index of the specified index.
@param w the widget to be found @return the index of the specified widget, or <code>-1</code> if it is
not found
int indexOf(Widget w) { for (int i = 0; i < _size; ++i) { if (_array[i] == w) { return i; } } return -1; }
void insert(Widget w, int beforeIndex) #
Inserts a widget before the specified index.
@param w the widget to be inserted @param beforeIndex the index before which the widget will be inserted @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of
range
void insert(Widget w, int beforeIndex) { if ((beforeIndex < 0) || (beforeIndex > _size)) { throw new Exception("IndexOutOfBoundsException"); } // Realloc _array if necessary (doubling). if (_size == _array.length) { List<Widget> newArray = new List<Widget>(_array.length * 2); for (int i = 0; i < _array.length; ++i) { newArray[i] = _array[i]; } _array = newArray; } ++_size; // Move all widgets after 'beforeIndex' back a slot. for (int i = _size - 1; i > beforeIndex; --i) { _array[i] = _array[i - 1]; } _array[beforeIndex] = w; }
String join([String separator = ""]) #
Converts each element to a String and concatenates the strings.
Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.
String join([String separator = ""]) { StringBuffer buffer = new StringBuffer(); buffer.writeAll(this, separator); return buffer.toString(); }
Widget lastWhere(bool test(Widget value), {Widget orElse()}) #
Returns the last element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
Widget lastWhere(bool test(Widget value), {Widget orElse()}) { throw new UnsupportedError("");}
Iterable map(f(Widget element)) #
Returns a lazy Iterable where each element e
of this
is replaced
by the result of f(e)
.
This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.
Iterable map(f(Widget element)) { throw new UnsupportedError("");}
Widget reduce(Widget combine(Widget value, Widget element)) #
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
Example of calculating the sum of an iterable:
iterable.reduce((value, element) => value + element);
Widget reduce(Widget combine(Widget value, Widget element)) { throw new UnsupportedError("");}
void remove(int index) #
Removes the widget at the specified index.
@param index the index of the widget to be removed @throws IndexOutOfBoundsException if <code>index</code> is out of range
void remove(int index) { if ((index < 0) || (index >= _size)) { throw new Exception("IndexOutOfBounds"); } --_size; for (int i = index; i < _size; ++i) { _array[i] = _array[i + 1]; } _array[_size] = null; }
void removeWidget(Widget w) #
Removes the specified widget.
@param w the widget to be removed @throws NoSuchElementException if the widget is not present
void removeWidget(Widget w) { int index = indexOf(w); if (index == -1) { throw new Exception("NoSuchElement"); } remove(index); }
Widget singleWhere(bool test(Widget value)) #
Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.
Widget singleWhere(bool test(Widget value)) { throw new UnsupportedError("");}
int size() #
Gets the number of widgets in this collection.
@return the number of widgets
int size() { return _size; }
Iterable<Widget> skip(int n) #
Returns an Iterable that skips the first n elements.
If this
has fewer than
n elements, then the resulting Iterable is
empty.
It is an error if n is negative.
Iterable<Widget> skip(int n) { throw new UnsupportedError("");}
Iterable<Widget> skipWhile(bool test(Widget value)) #
Returns an Iterable that skips elements while test is satisfied.
The filtering happens lazily. Every new Iterator of the returned
Iterable iterates over all elements of this
.
As long as the iterator's elements satisfy
test they are
discarded. Once an element does not satisfy the
test the iterator stops
testing and uses every later element unconditionally. That is, the elements
of the returned Iterable are the elements of this
starting from the
first element that does not satisfy
test.
Iterable<Widget> skipWhile(bool test(Widget value)) { throw new UnsupportedError("");}
Iterable<Widget> takeWhile(bool test(Widget value)) #
Returns an Iterable that stops once test is not satisfied anymore.
The filtering happens lazily. Every new Iterator of the returned
Iterable starts iterating over the elements of this
.
When the iterator encounters an element e
that does not satisfy
test,
it discards e
and moves into the finished state. That is, it does not
get or provide any more elements.
Iterable<Widget> takeWhile(bool test(Widget value)) { throw new UnsupportedError("");}
Iterable<Widget> where(bool f(Widget element)) #
Returns a lazy Iterable with all elements that satisfy the
predicate test
.
This method returns a view of the mapped elements. As long as the
returned Iterable is not iterated over, the supplied function test
will
not be invoked. Iterating will not cache results, and thus iterating
multiple times over the returned Iterable will invoke the supplied
function test
multiple times on the same element.
Iterable<Widget> where(bool f(Widget element)) { throw new UnsupportedError("");}