Revision: 8121
Author: rj...@google.com
Date: Wed May 12 17:53:17 2010
Log: Maintain the list's selection as we move between record specific
places. This is most visible when the table selection is cleared
if you click the create button.

Review at http://gwt-code-reviews.appspot.com/517801

Review by: jaime...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=8121

Added:
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/client/place/ScaffoldPlaceToRecordType.java
Modified:
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/client/ScaffoldMasterActivities.java /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/ListActivitiesMapper.java /branches/2.1/bikeshed/src/com/google/gwt/valuestore/ui/AbstractRecordListActivity.java /branches/2.1/bikeshed/src/com/google/gwt/valuestore/ui/AbstractRecordListView.java
 /branches/2.1/bikeshed/src/com/google/gwt/valuestore/ui/RecordListView.java

=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/client/place/ScaffoldPlaceToRecordType.java Wed May 12 17:53:17 2010
@@ -0,0 +1,24 @@
+package com.google.gwt.sample.expenses.gwt.client.place;
+
+import com.google.gwt.sample.expenses.gwt.request.EmployeeRecord;
+import com.google.gwt.sample.expenses.gwt.request.ReportRecord;
+import com.google.gwt.valuestore.shared.Record;
+
+/**
+ * Filters an {...@link ScaffoldPlace} to the corresponding record
+ * type.
+ */
+public final class ScaffoldPlaceToRecordType implements
+    ScaffoldPlaceFilter<Class<? extends Record>> {
+  public Class<? extends Record> filter(EmployeeScaffoldPlace place) {
+    return EmployeeRecord.class;
+  }
+
+  public Class<? extends Record> filter(ListScaffoldPlace place) {
+    return place.getType();
+  }
+
+  public Class<? extends Record> filter(ReportScaffoldPlace place) {
+    return ReportRecord.class;
+  }
+}
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/client/ScaffoldMasterActivities.java Tue May 4 17:35:40 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/client/ScaffoldMasterActivities.java Wed May 12 17:53:17 2010
@@ -19,6 +19,11 @@
 import com.google.gwt.app.place.ActivityMapper;
 import com.google.gwt.sample.expenses.gwt.client.place.ListScaffoldPlace;
 import com.google.gwt.sample.expenses.gwt.client.place.ScaffoldPlace;
+import com.google.gwt.sample.expenses.gwt.client.place.ScaffoldPlaceToRecordType;
+import com.google.gwt.sample.expenses.gwt.client.place.ScaffoldRecordPlace;
+import com.google.gwt.sample.expenses.gwt.ui.ListActivitiesMapper;
+import com.google.gwt.valuestore.shared.Record;
+import com.google.gwt.valuestore.ui.AbstractRecordListActivity;

 /**
* Finds the activity to run for a particular {...@link ScaffoldPlace} in the top
@@ -27,20 +32,28 @@
 public final class ScaffoldMasterActivities implements
     ActivityMapper<ScaffoldPlace> {

-  private final ActivityMapper<ListScaffoldPlace> listActivities;
-
-  private Activity last = null;
-
-  public ScaffoldMasterActivities(
-      ActivityMapper<ListScaffoldPlace> listActivities) {
+  private final ListActivitiesMapper listActivities;
+
+  private AbstractRecordListActivity<?> last = null;
+  private Class<? extends Record> lastType = null;
+
+  public ScaffoldMasterActivities(ListActivitiesMapper listActivities) {
     this.listActivities = listActivities;
   }

   public Activity getActivity(ScaffoldPlace place) {
     if (place instanceof ListScaffoldPlace) {
-      last = listActivities.getActivity((ListScaffoldPlace) place);
+      ListScaffoldPlace listPlace = (ListScaffoldPlace) place;
+      last = listActivities.getActivity(listPlace);
+      lastType = listPlace.getType();
     }

+    if (last != null && place instanceof ScaffoldRecordPlace) {
+ Class<? extends Record> thisType = place.acceptFilter(new ScaffoldPlaceToRecordType());
+      if (lastType.equals(thisType)) {
+        last.select(((ScaffoldRecordPlace) place).getId());
+      }
+    }
     return last;
   }
 }
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/ListActivitiesMapper.java Tue May 4 17:35:40 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/ui/ListActivitiesMapper.java Wed May 12 17:53:17 2010
@@ -26,13 +26,11 @@
 import com.google.gwt.sample.expenses.gwt.request.ReportRecord;
 import com.google.gwt.sample.expenses.gwt.ui.employee.EmployeeListActivity;
 import com.google.gwt.sample.expenses.gwt.ui.report.ReportListActivity;
+import com.google.gwt.valuestore.ui.AbstractRecordListActivity;

 /**
* The class that knows what {...@link Activity} to run when the user goes to a
  * particular {...@link ListScaffoldPlace}.
- * <p>
- * To create a new list activity, copy one of the existing ones (and the
- * corresponding view), and register it here.
  */
public class ListActivitiesMapper implements ActivityMapper<ListScaffoldPlace> {
   private final ExpensesRequestFactory requests;
@@ -47,7 +45,7 @@
     this.placeController = placeController;
   }

-  public Activity getActivity(ListScaffoldPlace place) {
+ public AbstractRecordListActivity<?> getActivity(ListScaffoldPlace place) {
     if (place.getType().equals(EmployeeRecord.class)) {
       return new EmployeeListActivity(eventBus, requests, placeController);
     }
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/valuestore/ui/AbstractRecordListActivity.java Fri May 7 15:22:39 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/valuestore/ui/AbstractRecordListActivity.java Wed May 12 17:53:17 2010
@@ -23,6 +23,7 @@
 import com.google.gwt.view.client.ListView;
 import com.google.gwt.view.client.PagingListView;
 import com.google.gwt.view.client.Range;
+import com.google.gwt.view.client.SingleSelectionModel;

 import java.util.Collections;
 import java.util.HashMap;
@@ -47,6 +48,8 @@
public abstract class AbstractRecordListActivity<R extends Record> implements
     Activity, RecordListView.Delegate<R>, ListView.Delegate<R> {
private final Map<String, Integer> recordToRow = new HashMap<String, Integer>();
+  private final Map<String, R> idToRecord = new HashMap<String, R>();
+  private final SingleSelectionModel<R> selectionModel;

   private RecordListView<R> view;
   private Display display;
@@ -55,6 +58,18 @@
     this.view = view;
     view.setDelegate(this);
     view.asPagingListView().setDelegate(this);
+
+    selectionModel = new SingleSelectionModel<R>() {
+      @Override
+      public void setSelected(R newSelection, boolean selected) {
+        R wasSelected = this.getSelectedObject();
+        super.setSelected(newSelection, selected);
+        if (!newSelection.equals(wasSelected)) {
+          showDetails(newSelection);
+        }
+      }
+    };
+    view.asPagingListView().setSelectionModel(selectionModel);
   }

   public RecordListView<R> getView() {
@@ -78,10 +93,14 @@
           return;
         }
         recordToRow.clear();
- for (int i = 0, r = range.getStart(); i < values.size(); i++, r++) {
-          recordToRow.put(values.get(i).getId(), r);
-        }
- getView().asPagingListView().setData(range.getStart(), range.getLength(), values);
+        idToRecord.clear();
+ for (int i = 0, row = range.getStart(); i < values.size(); i++, row++) {
+          R record = values.get(i);
+          recordToRow.put(record.getId(), row);
+          idToRecord.put(record.getId(), record);
+        }
+        getView().asPagingListView().setData(range.getStart(),
+            range.getLength(), values);
         if (display != null) {
           display.showActivityWidget(getView());
         }
@@ -97,6 +116,24 @@
     view.asPagingListView().setDelegate(null);
     view = null;
   }
+
+  /**
+ * Select the record if it happens to be visible, or clear the selection if
+   * called with null or "".
+   */
+  public void select(String id) {
+    if (id == null || "".equals(id)) {
+      R selected = selectionModel.getSelectedObject();
+      if (selected != null) {
+        selectionModel.setSelected(selected, false);
+      }
+    } else {
+      R record = idToRecord.get(id);
+      if (record != null) {
+        selectionModel.setSelected(record, true);
+      }
+    }
+  }

   public void start(Display display) {
     this.display = display;
@@ -115,8 +152,8 @@

       case CREATE:
         /*
-         * On create, we presume the new record is at the end of the list,
-         * so fetch the last page of items.
+ * On create, we presume the new record is at the end of the list, so
+         * fetch the last page of items.
          */
         getLastPage();
         break;
@@ -131,6 +168,8 @@

   protected abstract void fireCountRequest(Receiver<Long> callback);

+  protected abstract void showDetails(R record);
+
   private void delete(R record) {
     Integer row = recordToRow.get(record.getId());
     if (row != null) {
@@ -167,6 +206,7 @@

   private void update(R record) {
     Integer row = recordToRow.get(record.getId());
- getView().asPagingListView().setData(row, 1, Collections.singletonList(record));
+    getView().asPagingListView().setData(row, 1,
+        Collections.singletonList(record));
   }
 }
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/valuestore/ui/AbstractRecordListView.java Tue May 11 19:14:49 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/valuestore/ui/AbstractRecordListView.java Wed May 12 17:53:17 2010
@@ -24,9 +24,6 @@
 import com.google.gwt.valuestore.shared.Property;
 import com.google.gwt.valuestore.shared.Record;
 import com.google.gwt.view.client.PagingListView;
-import com.google.gwt.view.client.Range;
-import com.google.gwt.view.client.SelectionModel;
-import com.google.gwt.view.client.SingleSelectionModel;

 import java.util.HashSet;
 import java.util.List;
@@ -49,7 +46,7 @@
   public PagingListView<R> asPagingListView() {
     return table;
   }
-
+
   public AbstractRecordListView<R> asWidget() {
     return this;
   }
@@ -57,33 +54,9 @@
   public Set<Property<?>> getProperties() {
     return properties;
   }
-
-  public Range getRange() {
-    return table.getRange();
-  }
-
-  public void setData(int start, int length, List<R> values) {
-    table.setData(start, length, values);
-  }
-
-  public void setDataSize(int size, boolean isExact) {
-    table.setDataSize(size, isExact);
-  }

   public void setDelegate(final Delegate<R> delegate) {
     this.delegate = delegate;
-
-    table.setSelectionModel(new SingleSelectionModel<R>() {
-      @Override
-      public void setSelected(R object, boolean selected) {
-        super.setSelected(object, selected);
-        delegate.showDetails(object);
-      }
-    });
-  }
-
-  public void setSelectionModel(SelectionModel<? super R> selectionModel) {
-    table.setSelectionModel(selectionModel);
   }

   protected void init(Widget root, CellTable<R> table, Button newButton,
@@ -96,7 +69,7 @@
       table.addColumn(column, column.getProperty().getDisplayName());
       properties.add(column.getProperty());
     }
-
+
     newButton.addClickHandler(new ClickHandler() {
       public void onClick(ClickEvent event) {
         delegate.createClicked();
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/valuestore/ui/RecordListView.java Fri May 7 15:22:39 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/valuestore/ui/RecordListView.java Wed May 12 17:53:17 2010
@@ -36,11 +36,6 @@
    * @param<R> the type of the records to display
    */
   interface Delegate<R extends Record> {
-    /**
-     * @param record the record whose details the user wants to see
-     */
-    void showDetails(R record);
-
     void createClicked();
   }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to