Changes ListRegistration to carry information about its associated handler
and range of interest. This allows ListListModel (and theoretically other
models) to call back directly to views whose range of interest changes
without having to re-render all views.
Review at http://gwt-code-reviews.appspot.com/314801
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@7887 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/bikeshed/src/com/google/gwt/bikeshed/list/client/PagingTableListView.java b/bikeshed/src/com/google/gwt/bikeshed/list/client/PagingTableListView.java
index 339845b..3c5eb67 100644
--- a/bikeshed/src/com/google/gwt/bikeshed/list/client/PagingTableListView.java
+++ b/bikeshed/src/com/google/gwt/bikeshed/list/client/PagingTableListView.java
@@ -59,7 +59,7 @@
private ArrayList<T> data = new ArrayList<T>();
private List<Header<?>> footers = new ArrayList<Header<?>>();
private List<Header<?>> headers = new ArrayList<Header<?>>();
- private ListRegistration listReg;
+ private ListRegistration<T> listReg;
private ListModel<T> listModel;
private int numPages;
@@ -86,7 +86,7 @@
// TODO: Total hack. It would almost definitely be preferable to sink only
// those events actually needed by cells.
sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS | Event.KEYEVENTS
- | Event.ONCHANGE);
+ | Event.ONCHANGE | Event.FOCUSEVENTS);
}
public void addColumn(Column<T, ?, ?> col) {
diff --git a/bikeshed/src/com/google/gwt/bikeshed/list/client/SimpleCellList.java b/bikeshed/src/com/google/gwt/bikeshed/list/client/SimpleCellList.java
index ed7eea3..3f700b7 100644
--- a/bikeshed/src/com/google/gwt/bikeshed/list/client/SimpleCellList.java
+++ b/bikeshed/src/com/google/gwt/bikeshed/list/client/SimpleCellList.java
@@ -45,7 +45,7 @@
private int initialMaxSize;
private int maxSize;
private ListModel<T> model;
- private ListRegistration reg;
+ private ListRegistration<T> reg;
private int seq; // for debugging - TODO: remove
private final Element showFewerElem;
private final Element showMoreElem;
diff --git a/bikeshed/src/com/google/gwt/bikeshed/list/shared/AbstractListModel.java b/bikeshed/src/com/google/gwt/bikeshed/list/shared/AbstractListModel.java
index 66e4a49..b20fbee 100644
--- a/bikeshed/src/com/google/gwt/bikeshed/list/shared/AbstractListModel.java
+++ b/bikeshed/src/com/google/gwt/bikeshed/list/shared/AbstractListModel.java
@@ -57,7 +57,7 @@
* An implementation of {@link ListRegistration} that remembers its own range
* of interest.
*/
- private class DefaultListRegistration implements ListRegistration {
+ private class DefaultListRegistration implements ListRegistration<T> {
private final ListHandler<T> handler;
private int start;
@@ -72,6 +72,18 @@
this.handler = handler;
}
+ public ListHandler<T> getHandler() {
+ return handler;
+ }
+
+ public int getLength() {
+ return length;
+ }
+
+ public int getStart() {
+ return start;
+ }
+
public void removeHandler() {
if (!registrations.contains(this)) {
throw new IllegalStateException("ListHandler has already been removed.");
@@ -82,19 +94,7 @@
public void setRangeOfInterest(int start, int length) {
this.start = start;
this.length = length;
- onRangeChanged(start, length);
- }
-
- protected ListHandler<T> getHandler() {
- return handler;
- }
-
- protected int getLength() {
- return length;
- }
-
- protected int getStart() {
- return start;
+ onRangeChanged(this, start, length);
}
}
@@ -108,7 +108,7 @@
*/
private List<DefaultListRegistration> registrations = new ArrayList<DefaultListRegistration>();
- public ListRegistration addListHandler(ListHandler<T> handler) {
+ public ListRegistration<T> addListHandler(ListHandler<T> handler) {
DefaultListRegistration reg = new DefaultListRegistration(handler);
registrations.add(reg);
return reg;
@@ -159,8 +159,13 @@
/**
* Called when a view changes its range of interest.
+ *
+ * @param reg the list whose range has changed
+ * @param start the start of the list's new range
+ * @param length the length of the list's new range
*/
- protected abstract void onRangeChanged(int start, int length);
+ protected abstract void onRangeChanged(ListRegistration<T> reg, int start,
+ int length);
/**
* Inform the views of the total number of items that are available.
@@ -183,21 +188,33 @@
* @param values the data values
*/
protected void updateViewData(int start, int length, List<T> values) {
+ for (ListRegistration<T> reg : registrations) {
+ updateViewData(reg, start, length, values);
+ }
+ }
+
+ /**
+ * Informs a single view (via its {@link ListRegistration}) of new data.
+ *
+ * @param reg the list registration of the view to be updated
+ * @param start the start index
+ * @param length the length of the data
+ * @param values the data values
+ */
+ protected void updateViewData(ListRegistration<T> reg, int start, int length, List<T> values) {
int end = start + length;
- for (DefaultListRegistration reg : registrations) {
- int curStart = reg.getStart();
- int curLength = reg.getLength();
- int curEnd = curStart + curLength;
- if (curStart < end && curEnd > start) {
- // Fire the handler with the data that is in the range.
- int realStart = curStart < start ? start : curStart;
- int realEnd = curEnd > end ? end : curEnd;
- int realLength = realEnd - realStart;
- List<T> realValues = values.subList(realStart - start, realStart
- - start + realLength);
- ListEvent<T> event = new ListEvent<T>(realStart, realLength, realValues);
- reg.getHandler().onDataChanged(event);
- }
+ int curStart = reg.getStart();
+ int curLength = reg.getLength();
+ int curEnd = curStart + curLength;
+ if (curStart < end && curEnd > start) {
+ // Fire the handler with the data that is in the range.
+ int realStart = curStart < start ? start : curStart;
+ int realEnd = curEnd > end ? end : curEnd;
+ int realLength = realEnd - realStart;
+ List<T> realValues = values.subList(realStart - start, realStart
+ - start + realLength);
+ ListEvent<T> event = new ListEvent<T>(realStart, realLength, realValues);
+ reg.getHandler().onDataChanged(event);
}
}
}
diff --git a/bikeshed/src/com/google/gwt/bikeshed/list/shared/AsyncListModel.java b/bikeshed/src/com/google/gwt/bikeshed/list/shared/AsyncListModel.java
index 4c5f6f7..42c2212 100644
--- a/bikeshed/src/com/google/gwt/bikeshed/list/shared/AsyncListModel.java
+++ b/bikeshed/src/com/google/gwt/bikeshed/list/shared/AsyncListModel.java
@@ -31,6 +31,7 @@
* @param size the new size
* @param exact true if the size is exact, false if it is a guess
*/
+ @Override
public void updateDataSize(int size, boolean exact) {
super.updateDataSize(size, exact);
}
@@ -42,6 +43,7 @@
* @param length the length of the data
* @param values the data values
*/
+ @Override
public void updateViewData(int start, int length, List<T> values) {
super.updateViewData(start, length, values);
}
diff --git a/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListListModel.java b/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListListModel.java
index c1c2645..b23fbdc 100644
--- a/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListListModel.java
+++ b/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListListModel.java
@@ -282,7 +282,23 @@
/**
* The wrapper around the actual list.
*/
- private ListWrapper listWrapper = new ListWrapper(new ArrayList<T>());
+ private ListWrapper listWrapper;
+
+ /**
+ * Creates an empty model.
+ */
+ public ListListModel() {
+ this(new ArrayList<T>());
+ }
+
+ /**
+ * Creates a list model that wraps the given collection. Changes to the
+ * wrapped list must be made via this model in order to be correctly applied
+ * to views.
+ */
+ public ListListModel(List<T> wrappee) {
+ listWrapper = new ListWrapper(wrappee);
+ }
/**
* Get the list that backs this model. Changes to the list will be reflected
@@ -294,11 +310,20 @@
return listWrapper;
}
- @Override
- protected void onRangeChanged(int start, int length) {
- // TODO - update size only when needed
- // TODO - update only relevant range of data
+ /**
+ * Replaces this model's list.
+ *
+ * @param wrappee the model's new list
+ */
+ public void setList(List<T> wrappee) {
+ listWrapper = new ListWrapper(wrappee);
updateDataSize(listWrapper.size(), true);
updateViewData(0, listWrapper.size(), listWrapper);
}
+
+ @Override
+ protected void onRangeChanged(ListRegistration<T> reg, int start, int length) {
+ // TODO - update only relevant range of data
+ updateViewData(reg, 0, listWrapper.size(), listWrapper);
+ }
}
diff --git a/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListModel.java b/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListModel.java
index d16f659..04bb121 100644
--- a/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListModel.java
+++ b/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListModel.java
@@ -27,5 +27,5 @@
*
* @param handler the {@link ListHandler}
*/
- ListRegistration addListHandler(ListHandler<T> handler);
+ ListRegistration<T> addListHandler(ListHandler<T> handler);
}
diff --git a/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListRegistration.java b/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListRegistration.java
index a417fb1..a097446 100644
--- a/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListRegistration.java
+++ b/bikeshed/src/com/google/gwt/bikeshed/list/shared/ListRegistration.java
@@ -19,8 +19,10 @@
/**
* Registration returned from a call to ListModel#addListHandler.
+ *
+ * @param <T> the data type displayed by the list
*/
-public interface ListRegistration extends HandlerRegistration {
+public interface ListRegistration<T> extends HandlerRegistration {
/**
* Set the range of data that is interesting to the {@link ListHandler}.
@@ -29,4 +31,19 @@
* @param length the length
*/
void setRangeOfInterest(int start, int length);
+
+ /**
+ * Gets the {@link ListHandler} associated with this registration.
+ */
+ ListHandler<T> getHandler();
+
+ /**
+ * Gets the length of this registration's range of interest.
+ */
+ int getLength();
+
+ /**
+ * Gets the start of this registration's range of interest.
+ */
+ int getStart();
}
diff --git a/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/StocksDesktop.java b/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/StocksDesktop.java
index 006d166..ed1eb28 100644
--- a/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/StocksDesktop.java
+++ b/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/StocksDesktop.java
@@ -18,6 +18,7 @@
import com.google.gwt.bikeshed.cells.client.FieldUpdater;
import com.google.gwt.bikeshed.list.shared.AsyncListModel;
import com.google.gwt.bikeshed.list.shared.ListListModel;
+import com.google.gwt.bikeshed.list.shared.ListRegistration;
import com.google.gwt.bikeshed.list.shared.Range;
import com.google.gwt.bikeshed.tree.client.SideBySideTreeView;
import com.google.gwt.bikeshed.tree.client.TreeNode;
@@ -113,7 +114,7 @@
// of the UiFactories need the models to instantiate their widgets.
searchListModel = new AsyncListModel<StockQuote>() {
@Override
- protected void onRangeChanged(int start, int length) {
+ protected void onRangeChanged(ListRegistration reg, int start, int length) {
update();
}
};
@@ -121,7 +122,7 @@
favoritesListModel = new AsyncListModel<StockQuote>() {
@Override
- protected void onRangeChanged(int start, int length) {
+ protected void onRangeChanged(ListRegistration reg, int start, int length) {
update();
}
};
@@ -129,7 +130,7 @@
playerScoresListModel = new AsyncListModel<PlayerInfo>() {
@Override
- protected void onRangeChanged(int start, int length) {
+ protected void onRangeChanged(ListRegistration reg, int start, int length) {
}
};
diff --git a/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/StocksMobile.java b/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/StocksMobile.java
index 01ca04e..3e1ba9c 100644
--- a/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/StocksMobile.java
+++ b/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/StocksMobile.java
@@ -17,6 +17,7 @@
import com.google.gwt.bikeshed.list.client.PagingTableListView;
import com.google.gwt.bikeshed.list.shared.AsyncListModel;
+import com.google.gwt.bikeshed.list.shared.ListRegistration;
import com.google.gwt.bikeshed.list.shared.Range;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
@@ -74,7 +75,7 @@
// of the UiFactories need the models to instantiate their widgets.
favoritesListModel = new AsyncListModel<StockQuote>() {
@Override
- protected void onRangeChanged(int start, int length) {
+ protected void onRangeChanged(ListRegistration reg, int start, int length) {
update();
}
};
diff --git a/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/TransactionTreeViewModel.java b/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/TransactionTreeViewModel.java
index d6bc8f3..5daa78d 100644
--- a/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/TransactionTreeViewModel.java
+++ b/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/TransactionTreeViewModel.java
@@ -22,6 +22,7 @@
import com.google.gwt.bikeshed.list.shared.AsyncListModel;
import com.google.gwt.bikeshed.list.shared.ListListModel;
import com.google.gwt.bikeshed.list.shared.ListModel;
+import com.google.gwt.bikeshed.list.shared.ListRegistration;
import com.google.gwt.bikeshed.tree.client.TreeNode;
import com.google.gwt.bikeshed.tree.client.TreeViewModel;
import com.google.gwt.sample.bikeshed.stocks.shared.StockQuote;
@@ -51,7 +52,7 @@
}
@Override
- protected void onRangeChanged(int start, int length) {
+ protected void onRangeChanged(ListRegistration reg, int start, int length) {
updater.update();
}
}
diff --git a/bikeshed/src/com/google/gwt/sample/bikeshed/tree/TreeSample.gwt.xml b/bikeshed/src/com/google/gwt/sample/bikeshed/tree/TreeSample.gwt.xml
index 5b4c821..c757db5 100644
--- a/bikeshed/src/com/google/gwt/sample/bikeshed/tree/TreeSample.gwt.xml
+++ b/bikeshed/src/com/google/gwt/sample/bikeshed/tree/TreeSample.gwt.xml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 0.0.999//EN" "http://google-web-toolkit.googlecode.com/svn/tags/0.0.999/distro-source/core/src/gwt-module.dtd">
-<module rename-to='tree'>
+<module rename-to='treesample'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<inherits name='com.google.gwt.bikeshed.list.List'/>
diff --git a/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/MyTreeViewModel.java b/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/MyTreeViewModel.java
index a47d8e0..ac3d6b3 100644
--- a/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/MyTreeViewModel.java
+++ b/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/MyTreeViewModel.java
@@ -24,6 +24,7 @@
import com.google.gwt.bikeshed.list.shared.AbstractListModel;
import com.google.gwt.bikeshed.list.shared.ListModel;
import com.google.gwt.bikeshed.list.shared.SelectionModel;
+import com.google.gwt.bikeshed.list.shared.ListRegistration;
import com.google.gwt.bikeshed.tree.client.TreeNode;
import com.google.gwt.bikeshed.tree.client.TreeViewModel;
import com.google.gwt.core.client.GWT;
@@ -47,7 +48,7 @@
}
@Override
- protected void onRangeChanged(int start, int length) {
+ protected void onRangeChanged(ListRegistration reg, int start, int length) {
List<Integer> values = new ArrayList<Integer>(1);
values.add(wordLength);
updateDataSize(1, true);
@@ -63,7 +64,7 @@
}
@Override
- protected void onRangeChanged(int start, int length) {
+ protected void onRangeChanged(ListRegistration reg, int start, int length) {
String prefix = value.endsWith("...") ? value.substring(0,
value.length() - 3) : value;
dataService.getNext(prefix, new AsyncCallback<List<String>>() {
diff --git a/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/employee/EmployeeListView.java b/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/employee/EmployeeListView.java
index ef3757a..b241e97 100644
--- a/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/employee/EmployeeListView.java
+++ b/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/employee/EmployeeListView.java
@@ -22,6 +22,7 @@
import com.google.gwt.bikeshed.list.client.TextColumn;
import com.google.gwt.bikeshed.list.client.TextHeader;
import com.google.gwt.bikeshed.list.shared.ListModel;
+import com.google.gwt.bikeshed.list.shared.ListRegistration;
import com.google.gwt.sample.expenses.gwt.place.ExpensesPlaces;
import com.google.gwt.sample.expenses.gwt.request.ExpensesRequestFactory;
import com.google.gwt.sample.expenses.gwt.request.EmployeeKey;
@@ -80,9 +81,8 @@
private static ListModel<Values<EmployeeKey>> getModel(
final ExpensesRequestFactory requests) {
return new ListModelAdapter<EmployeeKey>() {
-
@Override
- protected void onRangeChanged(int start, int length) {
+ protected void onRangeChanged(ListRegistration reg, int start, int length) {
requests.employeeRequest().findAllEmployees().forProperties(
getProperties()).to(this).fire();
}
diff --git a/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/report/ReportListView.java b/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/report/ReportListView.java
index cc4385b..a2838b0 100644
--- a/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/report/ReportListView.java
+++ b/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/report/ReportListView.java
@@ -24,6 +24,7 @@
import com.google.gwt.bikeshed.list.client.TextColumn;
import com.google.gwt.bikeshed.list.client.TextHeader;
import com.google.gwt.bikeshed.list.shared.ListModel;
+import com.google.gwt.bikeshed.list.shared.ListRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.sample.expenses.gwt.place.ExpensesPlaces;
import com.google.gwt.sample.expenses.gwt.request.ExpensesRequestFactory;
@@ -85,9 +86,8 @@
private static ListModel<Values<ReportKey>> getModel(
final ExpensesRequestFactory requests) {
return new ListModelAdapter<ReportKey>() {
-
@Override
- protected void onRangeChanged(int start, int length) {
+ protected void onRangeChanged(ListRegistration reg, int start, int length) {
requests.reportRequest().findAllReports().forProperties(getProperties()).to(
this).fire();
}