to.etc.domui.component.tbl
Class TableKeyModelBase<K,T>

java.lang.Object
  extended by to.etc.domui.component.tbl.TableModelBase<T>
      extended by to.etc.domui.component.tbl.TableKeyModelBase<K,T>
All Implemented Interfaces:
IModifyableTableModel<K>, ITableModel<T>

public abstract class TableKeyModelBase<K,T>
extends TableModelBase<T>
implements ITableModel<T>, IModifyableTableModel<K>

This model maintains a set of Keys K, and translates those keys to Model items T where needed. This model should be used when maintaining a full set of T instances is too expensive. This model only instantiates those T instances that are needed to display on a table's page. This is done by loading all T's for a set of K's passed to getItems(List).

Author:
Frits Jalvingh Created on Feb 7, 2011

Field Summary
 
Fields inherited from interface to.etc.domui.component.tbl.ITableModel
DEFAULT_MAX_SIZE
 
Constructor Summary
TableKeyModelBase(java.util.Collection<K> keycoll)
          Create an unsortable model using the specified collection as source.
TableKeyModelBase(java.util.Collection<K> keycoll, java.util.Comparator<K> comp)
          Create a model where the keys are sorted using the specified comparator.
 
Method Summary
 void add(int index, K key)
          Add a new key to show @ the specified location in the list(!).
 void add(K row)
          Add a new key to the model.
 K delete(int index)
          Delete the specified index from the backing list.
 boolean delete(K val)
          Delete the specified key from the list and the backing set.
protected  T getItem(int index)
          Internal: get a T for the specified index, to properly send events.
 java.util.List<T> getItems(int start, int end)
          
protected abstract  java.util.List<T> getItems(java.util.List<K> keys)
          This method must return, for every K in the keys parameter, the proper T that belongs to that K, in the same order as the K's specified.
 int getRows()
          This must return the total #of rows in this table.
 void modified(int index)
          Send a "modified" message for the specified index.
 void modified(K key)
          Send a modified event for the specified key, if found.
 void move(int to, int from)
          Convenience method to move a key from index from to index to.
 
Methods inherited from class to.etc.domui.component.tbl.TableModelBase
addChangeListener, fireAdded, fireDeleted, fireModelChanged, fireModified, getListeners, refresh, removeChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface to.etc.domui.component.tbl.ITableModel
addChangeListener, refresh, removeChangeListener
 

Constructor Detail

TableKeyModelBase

public TableKeyModelBase(@Nonnull
                         java.util.Collection<K> keycoll)
Create an unsortable model using the specified collection as source. This collection will be modified if one of the IModifyableTableModel methods is called.

Parameters:
keycoll -

TableKeyModelBase

public TableKeyModelBase(@Nonnull
                         java.util.Collection<K> keycoll,
                         @Nonnull
                         java.util.Comparator<K> comp)
Create a model where the keys are sorted using the specified comparator. For this to work the keys must contain enough information so that they can be sorted. It is explicitly forbidden to implement this sort as loading the T then sorting on it's fields because this would instantiate all records in the collection..

Parameters:
keycoll -
comp -
Method Detail

getItems

@Nonnull
protected abstract java.util.List<T> getItems(java.util.List<K> keys)
                                       throws java.lang.Exception
This method must return, for every K in the keys parameter, the proper T that belongs to that K, in the same order as the K's specified. If calculating a T for a K is very expensive the implementation may decide to cache the mapping for a certain number of K's. The model itself does not cache any T's returned by it.

Parameters:
keys -
Returns:
Throws:
java.lang.Exception

getItems

@Nonnull
public java.util.List<T> getItems(int start,
                                          int end)
                           throws java.lang.Exception

Specified by:
getItems in interface ITableModel<T>
Throws:
java.lang.Exception

getItem

@Nullable
protected final T getItem(int index)
                   throws java.lang.Exception
Internal: get a T for the specified index, to properly send events.

Specified by:
getItem in class TableModelBase<T>
Throws:
java.lang.Exception
See Also:
TableModelBase.getItem(int)

getRows

public int getRows()
            throws java.lang.Exception
Description copied from interface: ITableModel
This must return the total #of rows in this table.

Specified by:
getRows in interface ITableModel<T>
Returns:
Throws:
java.lang.Exception
See Also:
ITableModel.getRows()

add

public void add(int index,
                @Nonnull
                K key)
         throws java.lang.Exception
Add a new key to show @ the specified location in the list(!). If the source object is List itself it will be added there at the same location. If the object is another kind of Collection it will be added using add(). This cannot be used for models that have a sortable key, because the sort order will determine the index to use.

Specified by:
add in interface IModifyableTableModel<K>
Throws:
java.lang.IllegalStateException - when the model is sortable.
java.lang.Exception
See Also:
IModifyableTableModel.add(int, java.lang.Object)

add

public void add(@Nonnull
                K row)
         throws java.lang.Exception
Add a new key to the model. If the key model itself is sortable then the key is added at the appropriate location in the backing key list. If the set is not sorted the key is added at the end of the backing list. The key is added to the backing Collection too; if that collection is a List itself the key will be added at the exact same location as in the backing list, otherwise it gets added to the source model using Collection.add(Object).

Specified by:
add in interface IModifyableTableModel<K>
Throws:
java.lang.Exception
See Also:
IModifyableTableModel.add(java.lang.Object)

delete

@Nullable
public K delete(int index)
         throws java.lang.Exception
Delete the specified index from the backing list. The key is also removed from the source collection using the key's value with Collection.remove(Object).

Specified by:
delete in interface IModifyableTableModel<K>
Throws:
java.lang.Exception
See Also:
IModifyableTableModel.delete(int)

delete

public boolean delete(@Nonnull
                      K val)
               throws java.lang.Exception
Delete the specified key from the list and the backing set.

Specified by:
delete in interface IModifyableTableModel<K>
Throws:
java.lang.Exception
See Also:
IModifyableTableModel.delete(java.lang.Object)

modified

public void modified(int index)
              throws java.lang.Exception
Send a "modified" message for the specified index.

Parameters:
index -
Throws:
java.lang.Exception

modified

public void modified(K key)
              throws java.lang.Exception
Send a modified event for the specified key, if found.

Parameters:
key -
Throws:
java.lang.Exception

move

public void move(int to,
                 int from)
          throws java.lang.Exception
Convenience method to move a key from index from to index to.

Parameters:
to -
from -
Throws:
java.lang.Exception