Revision: 8464
Author: [email protected]
Date: Mon Aug  2 18:09:28 2010
Log: Finish modernizing the DynaTableRF sample to use an event-based application model.
Patch by: bobv
Review by: rjrjr

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

http://code.google.com/p/google-web-toolkit/source/detail?r=8464

Added:
/trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/NavBar.java /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/events /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/events/DataAvailableEvent.java /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/events/FilterChangeEvent.java /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/events/NavigationEvent.java
Deleted:
/trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableDataProvider.java
Modified:
/trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/CalendarProvider.java /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DayCheckBox.java /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DayFilterWidget.java /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DayFilterWidget.ui.xml /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableRf.java /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableWidget.java /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableWidget.ui.xml /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/SchoolCalendarWidget.java

=======================================
--- /dev/null
+++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/NavBar.java Mon Aug 2 18:09:28 2010
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.sample.dynatablerf.client;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.DivElement;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.sample.dynatablerf.client.events.NavigationEvent;
+import com.google.gwt.sample.dynatablerf.client.events.NavigationEvent.Direction;
+import com.google.gwt.uibinder.client.UiBinder;
+import com.google.gwt.uibinder.client.UiConstructor;
+import com.google.gwt.uibinder.client.UiField;
+import com.google.gwt.uibinder.client.UiHandler;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.Widget;
+
+class NavBar extends Composite {
+  interface Binder extends UiBinder<Widget, NavBar> {
+  }
+
+  @UiField
+  Button gotoFirst;
+
+  @UiField
+  Button gotoNext;
+
+  @UiField
+  Button gotoPrev;
+
+  @UiField
+  DivElement status;
+
+  @UiConstructor
+  public NavBar() {
+    Binder binder = GWT.create(Binder.class);
+    initWidget(binder.createAndBindUi(this));
+  }
+
+  public HandlerRegistration addNavigationEventHandler(
+      NavigationEvent.Handler handler) {
+    return addHandler(handler, NavigationEvent.TYPE);
+  }
+
+  @UiHandler("gotoFirst")
+  void onFirst(ClickEvent event) {
+    fireEvent(new NavigationEvent(Direction.START));
+  }
+
+  @UiHandler("gotoNext")
+  void onNext(ClickEvent event) {
+    fireEvent(new NavigationEvent(Direction.FORWARD));
+  }
+
+  @UiHandler("gotoPrev")
+  void onPrev(ClickEvent event) {
+    fireEvent(new NavigationEvent(Direction.BACKWARD));
+  }
+}
=======================================
--- /dev/null
+++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/events/DataAvailableEvent.java Mon Aug 2 18:09:28 2010
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.sample.dynatablerf.client.events;
+
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.GwtEvent;
+import com.google.gwt.sample.dynatablerf.shared.PersonProxy;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Indicates that new data is available to the application.
+ */
+public class DataAvailableEvent extends GwtEvent<DataAvailableEvent.Handler> {
+  /**
+   * Handles {...@link DataAvailableEvent}.
+   */
+  public interface Handler extends EventHandler {
+    void onRowData(DataAvailableEvent event);
+  }
+
+  public static final Type<Handler> TYPE = new Type<Handler>();
+  private final List<PersonProxy> people;
+  private final int startRow;
+
+  public DataAvailableEvent(int startRow, List<PersonProxy> people) {
+    this.startRow = startRow;
+    this.people = Collections.unmodifiableList(new ArrayList<PersonProxy>(
+        people));
+  }
+
+  @Override
+  public Type<Handler> getAssociatedType() {
+    return TYPE;
+  }
+
+  public List<PersonProxy> getPeople() {
+    return people;
+  }
+
+  public int getStartRow() {
+    return startRow;
+  }
+
+  @Override
+  protected void dispatch(Handler handler) {
+    handler.onRowData(this);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/events/FilterChangeEvent.java Mon Aug 2 18:09:28 2010
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.sample.dynatablerf.client.events;
+
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.GwtEvent;
+
+/**
+ * An event to indicate a change in the filter options.
+ */
+public class FilterChangeEvent extends GwtEvent<FilterChangeEvent.Handler> {
+  /**
+   * Handles {...@link FilterChangeEvent}.
+   */
+  public interface Handler extends EventHandler {
+    void onFilterChanged(FilterChangeEvent e);
+  }
+
+  public static final Type<Handler> TYPE = new Type<Handler>();
+  private final int day;
+  private final boolean selected;
+
+  public FilterChangeEvent(int day, boolean selected) {
+    this.day = day;
+    this.selected = selected;
+  }
+
+  @Override
+  public Type<Handler> getAssociatedType() {
+    return TYPE;
+  }
+
+  public int getDay() {
+    return day;
+  }
+
+  public boolean isSelected() {
+    return selected;
+  }
+
+  @Override
+  protected void dispatch(Handler handler) {
+    handler.onFilterChanged(this);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/events/NavigationEvent.java Mon Aug 2 18:09:28 2010
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.sample.dynatablerf.client.events;
+
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.GwtEvent;
+
+/**
+ * An event that indicates the user wishes to navigate within the data being
+ * displayed.
+ */
+public class NavigationEvent extends GwtEvent<NavigationEvent.Handler> {
+  /**
+   * Indicates the direction of motion within the data set.
+   */
+  public enum Direction {
+    START, FORWARD, BACKWARD
+  }
+
+  /**
+   * Handles {...@link NavigationEvent}.
+   */
+  public interface Handler extends EventHandler {
+    void onNavigation(NavigationEvent e);
+  }
+
+  public static final Type<Handler> TYPE = new Type<Handler>();
+  private final Direction direction;
+
+  public NavigationEvent(Direction direction) {
+    this.direction = direction;
+  }
+
+  @Override
+  public Type<Handler> getAssociatedType() {
+    return TYPE;
+  }
+
+  public Direction getDirection() {
+    return direction;
+  }
+
+  @Override
+  protected void dispatch(Handler handler) {
+    handler.onNavigation(this);
+  }
+}
=======================================
--- /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableDataProvider.java Fri Jul 23 15:42:40 2010
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright 2007 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-package com.google.gwt.sample.dynatablerf.client;
-
-/**
- * An interface for providing row-level updates of data, intended here to used
- * to update a DynaTableWidget.
- */
-public interface DynaTableDataProvider {
-
-  /**
- * An interface allow a widget to accept or report failure when a row data
-   * is issued for update.
-   */
-  interface RowDataAcceptor {
-    void accept(int startRow, String[][] rows);
-    void failed(Throwable caught);
-  }
-
-  void updateRowData(int startRow, int maxRows, RowDataAcceptor acceptor);
-}
=======================================
--- /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/CalendarProvider.java Thu Jul 29 12:54:53 2010 +++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/CalendarProvider.java Mon Aug 2 18:09:28 2010
@@ -15,7 +15,10 @@
  */
 package com.google.gwt.sample.dynatablerf.client;

+import com.google.gwt.event.shared.HandlerManager;
+import com.google.gwt.event.shared.HandlerRegistration;
 import com.google.gwt.requestfactory.shared.Receiver;
+import com.google.gwt.sample.dynatablerf.client.events.DataAvailableEvent;
 import com.google.gwt.sample.dynatablerf.shared.DynaTableRequestFactory;
 import com.google.gwt.sample.dynatablerf.shared.PersonProxy;
 import com.google.gwt.valuestore.shared.SyncResult;
@@ -24,67 +27,54 @@
 import java.util.Set;

 /**
- * A data provider that bridges the provides row level updates from the data
-   * available through a <@link SchoolCalendarService>.
-   */
-  public class CalendarProvider implements DynaTableDataProvider {
-
-    private int lastMaxRows = -1;
-
-    private PersonProxy[] lastPeople;
-
-    private int lastStartRow = -1;
-
-    private final DynaTableRequestFactory requests;
-
-    public CalendarProvider(DynaTableRequestFactory requests) {
-      // Initialize the service.
-      //
-      this.requests =  requests;
-    }
-
-    public void updateRowData(final int startRow, final int maxRows,
-        final RowDataAcceptor acceptor) {
-      // Check the simple cache first.
-      //
-      if (startRow == lastStartRow) {
-        if (maxRows == lastMaxRows) {
-          // Use the cached batch.
-          //
-          pushResults(acceptor, startRow, lastPeople);
-          return;
-        }
-      }
-
-      // Fetch the data remotely.
-      //
-
- requests.schoolCalendarRequest().getPeople(startRow, maxRows).fire(new Receiver<List<PersonProxy>>() {
-
-        // TODO onError call RowDataAcceptor#fail, not yet provided by RF
-
- public void onSuccess(List<PersonProxy> response, Set<SyncResult> syncResults) {
-          lastStartRow = startRow;
-          lastMaxRows = maxRows;
-          lastPeople = response.toArray(new PersonProxy[response.size()]);
- PersonProxy[] result = response.toArray(new PersonProxy[response.size()]);
-          pushResults(acceptor, startRow, result);
-        }
-      });
-    }
-
-    private void pushResults(RowDataAcceptor acceptor, int startRow,
-        PersonProxy[] people) {
-      String[][] rows = new String[people.length][];
-      for (int i = 0, n = rows.length; i < n; i++) {
-        PersonProxy person = people[i];
-        rows[i] = new String[3];
-        rows[i][0] = person.getName();
-        rows[i][1] = person.getDescription();
-        rows[i][2] = person.getSchedule();
-        // TODO bring back filtering
-//        rows[i][2] = person.getSchedule(daysFilter);
-      }
-      acceptor.accept(startRow, rows);
-    }
-  }
+ * A data provider that bridges the provides row level updates from the data
+ * available through a <@link SchoolCalendarService>.
+ */
+public class CalendarProvider {
+  private final HandlerManager eventBus = new HandlerManager(this);
+
+  private int lastMaxRows = -1;
+
+  private List<PersonProxy> lastPeople;
+
+  private int lastStartRow = -1;
+
+  private final DynaTableRequestFactory requests;
+
+  public CalendarProvider(DynaTableRequestFactory requests) {
+    this.requests = requests;
+  }
+
+  public HandlerRegistration addRowDataHandler(
+      DataAvailableEvent.Handler handler) {
+    return eventBus.addHandler(DataAvailableEvent.TYPE, handler);
+  }
+
+  public void updateRowData(final int startRow, final int maxRows) {
+    // Check the simple cache first.
+    if (startRow == lastStartRow) {
+      if (maxRows == lastMaxRows) {
+        // Use the cached batch.
+        pushResults(startRow, lastPeople);
+        return;
+      }
+    }
+
+    requests.schoolCalendarRequest().getPeople(startRow, maxRows).fire(
+        new Receiver<List<PersonProxy>>() {
+          // TODO onError call RowDataAcceptor#fail, not yet provided by RF
+          public void onSuccess(List<PersonProxy> response,
+              Set<SyncResult> syncResults) {
+            lastStartRow = startRow;
+            lastMaxRows = maxRows;
+            lastPeople = response;
+            pushResults(startRow, response);
+          }
+        });
+  }
+
+  private void pushResults(int startRow, List<PersonProxy> people) {
+    // TODO(rjrjr) RequestFactory should probably provide this event.
+    eventBus.fireEvent(new DataAvailableEvent(startRow, people));
+  }
+}
=======================================
--- /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DayCheckBox.java Thu Jul 29 06:36:34 2010 +++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DayCheckBox.java Mon Aug 2 18:09:28 2010
@@ -15,9 +15,11 @@
  */
 package com.google.gwt.sample.dynatablerf.client;

+import com.google.gwt.event.logical.shared.ValueChangeEvent;
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
+import com.google.gwt.event.shared.HandlerManager;
 import com.google.gwt.event.shared.HandlerRegistration;
-import com.google.gwt.uibinder.client.UiConstructor;
+import com.google.gwt.sample.dynatablerf.client.events.FilterChangeEvent;
 import com.google.gwt.user.client.ui.CheckBox;
 import com.google.gwt.user.client.ui.Composite;

@@ -25,26 +27,21 @@
  * Used by DayFilterWidget.
  */
 class DayCheckBox extends Composite {
-  public final int day;
-  private final CheckBox cb;
-
-  @UiConstructor
- public DayCheckBox(SchoolCalendarWidget calendar, String caption, int day) {
-    cb = new CheckBox(caption);
+  private final CheckBox cb = new CheckBox();
+  private int day;
+  private final HandlerManager eventBus;
+  private HandlerRegistration filterRegistration;
+
+  public DayCheckBox(HandlerManager eventBus) {
+    this.eventBus = eventBus;
     initWidget(cb);
-
     cb.setEnabled(false);
-
-    // Remember custom data for this widget.
-    this.day = day;
-
-    // Initialize based on the calendar's current value.
-    cb.setValue(calendar.getDayIncluded(day));
-  }
-
-  public HandlerRegistration addValueChangeHandler(
-      ValueChangeHandler<Boolean> handler) {
-    return cb.addValueChangeHandler(handler);
+    cb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
+      public void onValueChange(ValueChangeEvent<Boolean> event) {
+        DayCheckBox.this.eventBus.fireEvent(new FilterChangeEvent(getDay(),
+            getValue()));
+      }
+    });
   }

   public int getDay() {
@@ -54,8 +51,37 @@
   public boolean getValue() {
     return cb.getValue();
   }
+
+  public void setCaption(String caption) {
+    cb.setText(caption);
+  }
+
+  public void setDay(int day) {
+    this.day = day;
+  }

   public void setValue(boolean value) {
     cb.setValue(value);
   }
-}
+
+  /**
+ * Attach to the event bus only when the widget is actually attached to the
+   * DOM.
+   */
+  @Override
+  protected void onLoad() {
+    filterRegistration = eventBus.addHandler(FilterChangeEvent.TYPE,
+        new FilterChangeEvent.Handler() {
+          public void onFilterChanged(FilterChangeEvent e) {
+            if (e.getDay() == getDay()) {
+              setValue(e.isSelected());
+            }
+          }
+        });
+  }
+
+  @Override
+  protected void onUnload() {
+    filterRegistration.removeHandler();
+  }
+}
=======================================
--- /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DayFilterWidget.java Thu Jul 29 06:36:34 2010 +++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DayFilterWidget.java Mon Aug 2 18:09:28 2010
@@ -17,8 +17,11 @@

 import com.google.gwt.core.client.GWT;
 import com.google.gwt.event.dom.client.ClickEvent;
-import com.google.gwt.event.logical.shared.ValueChangeEvent;
+import com.google.gwt.event.shared.HandlerManager;
+import com.google.gwt.sample.dynatablerf.client.events.FilterChangeEvent;
 import com.google.gwt.uibinder.client.UiBinder;
+import com.google.gwt.uibinder.client.UiConstructor;
+import com.google.gwt.uibinder.client.UiFactory;
 import com.google.gwt.uibinder.client.UiField;
 import com.google.gwt.uibinder.client.UiHandler;
 import com.google.gwt.user.client.ui.Button;
@@ -34,9 +37,6 @@
   interface Binder extends UiBinder<Widget, DayFilterWidget> {
   };

-  @UiField(provided = true)
-  final SchoolCalendarWidget calendar;
-
   @UiField
   DayCheckBox sunday;
   @UiField
@@ -56,13 +56,12 @@
   @UiField
   Button none;

-  private final DayCheckBox[] allDays;
-
-  public DayFilterWidget(SchoolCalendarWidget calendar) {
-    this.calendar = calendar;
+  private final HandlerManager eventBus;
+
+  @UiConstructor
+  public DayFilterWidget(HandlerManager eventBus) {
+    this.eventBus = eventBus;
     initWidget(GWT.<Binder> create(Binder.class).createAndBindUi(this));
-    allDays = new DayCheckBox[] {
-        sunday, monday, tuesday, wednesday, thursday, friday, saturday};
   }

   @UiHandler(value = {"all", "none"})
@@ -70,22 +69,14 @@
     setAllCheckBoxes(all == e.getSource());
   }

-  @UiHandler(value = {
-      "sunday", "monday", "tuesday", "wednesday", "thursday", "friday",
-      "saturday"})
-  public void handleClick(ValueChangeEvent<Boolean> e) {
-    DayCheckBox box = (DayCheckBox) e.getSource();
-    onToggle(box);
-  }
-
-  private void onToggle(DayCheckBox box) {
-    calendar.setDayIncluded(box.getDay(), box.getValue());
+  @UiFactory
+  DayCheckBox makeDayCheckBox() {
+    return new DayCheckBox(eventBus);
   }

   private void setAllCheckBoxes(boolean checked) {
-    for (DayCheckBox box : allDays) {
-      box.setValue(checked);
-      onToggle(box);
+    for (int day = 0; day < 7; day++) {
+      eventBus.fireEvent(new FilterChangeEvent(day, checked));
     }
   }
 }
=======================================
--- /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DayFilterWidget.ui.xml Thu Jul 29 06:36:34 2010 +++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DayFilterWidget.ui.xml Mon Aug 2 18:09:28 2010
@@ -13,23 +13,21 @@
        margin-left: 5px;
     }
   </ui:style>
- <ui:with type="com.google.gwt.sample.dynatablerf.client.SchoolCalendarWidget"
-    field="calendar"></ui:with>
   <g:FlowPanel>
-    <dt:DayCheckBox ui:field="sunday" calendar="{calendar}"
-      caption="Sunday" day="0" styleName="{style.cb}" />
-    <dt:DayCheckBox ui:field="monday" calendar="{calendar}"
-      caption="Monday" day="0" styleName="{style.cb}" />
-    <dt:DayCheckBox ui:field="tuesday" calendar="{calendar}"
-      caption="Tuesday" day="0" styleName="{style.cb}" />
-    <dt:DayCheckBox ui:field="wednesday" calendar="{calendar}"
-      caption="Wednesday" day="0" styleName="{style.cb}" />
-    <dt:DayCheckBox ui:field="thursday" calendar="{calendar}"
-      caption="Thursday" day="0" styleName="{style.cb}" />
-    <dt:DayCheckBox ui:field="friday" calendar="{calendar}"
-      caption="Friday" day="0" styleName="{style.cb}" />
-    <dt:DayCheckBox ui:field="saturday" calendar="{calendar}"
-      caption="Saturday" day="0" styleName="{style.cb}" />
+    <dt:DayCheckBox ui:field="sunday" caption="Sunday" day="0"
+      styleName="{style.cb}" />
+    <dt:DayCheckBox ui:field="monday" caption="Monday" day="0"
+      styleName="{style.cb}" />
+    <dt:DayCheckBox ui:field="tuesday" caption="Tuesday" day="0"
+      styleName="{style.cb}" />
+    <dt:DayCheckBox ui:field="wednesday" caption="Wednesday" day="0"
+      styleName="{style.cb}" />
+    <dt:DayCheckBox ui:field="thursday" caption="Thursday" day="0"
+      styleName="{style.cb}" />
+    <dt:DayCheckBox ui:field="friday" caption="Friday" day="0"
+      styleName="{style.cb}" />
+    <dt:DayCheckBox ui:field="saturday" caption="Saturday" day="0"
+      styleName="{style.cb}" />

<g:Button ui:field="all" enabled="false" styleName="{style.all}">All</g:Button> <g:Button ui:field="none" enabled="false" styleName="{style.none}">None</g:Button>
=======================================
--- /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableRf.java Thu Jul 29 06:36:34 2010 +++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableRf.java Mon Aug 2 18:09:28 2010
@@ -36,17 +36,20 @@
   @UiField(provided = true)
   SchoolCalendarWidget calendar;

+  HandlerManager eventBus = new HandlerManager(null);
+
   @UiField(provided = true)
   DayFilterWidget filter;

   public void onModuleLoad() {
-    HandlerManager eventBus = new HandlerManager(null);

DynaTableRequestFactory requests = GWT.create(DynaTableRequestFactory.class);
     requests.init(eventBus);

- calendar = new SchoolCalendarWidget(new CalendarProvider(requests), 15);
-    filter = new DayFilterWidget(calendar);
+    CalendarProvider provider = new CalendarProvider(requests);
+
+    calendar = new SchoolCalendarWidget(provider, 15);
+    filter = new DayFilterWidget(eventBus);

     RootLayoutPanel.get().add(
         GWT.<Binder> create(Binder.class).createAndBindUi(this));
=======================================
--- /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableWidget.java Thu Jul 29 06:36:34 2010 +++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableWidget.java Mon Aug 2 18:09:28 2010
@@ -16,14 +16,15 @@
 package com.google.gwt.sample.dynatablerf.client;

 import com.google.gwt.core.client.GWT;
-import com.google.gwt.dom.client.DivElement;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
-import com.google.gwt.sample.dynatablerf.client.DynaTableDataProvider.RowDataAcceptor;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.sample.dynatablerf.client.events.NavigationEvent;
+import com.google.gwt.sample.dynatablerf.client.events.DataAvailableEvent;
+import com.google.gwt.sample.dynatablerf.shared.PersonProxy;
 import com.google.gwt.uibinder.client.UiBinder;
 import com.google.gwt.uibinder.client.UiField;
 import com.google.gwt.uibinder.client.UiHandler;
-import com.google.gwt.uibinder.client.UiTemplate;
 import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.DialogBox;
@@ -32,59 +33,16 @@
 import com.google.gwt.user.client.ui.VerticalPanel;
 import com.google.gwt.user.client.ui.Widget;

+import java.util.List;
+
 /**
* A composite Widget that implements the main interface for the dynamic table,
  * including the data table, status indicators, and paging buttons.
  */
 public class DynaTableWidget extends Composite {

-  class NavBar extends Composite {
-
-    @UiField
-    Button gotoFirst;
-
-    @UiField
-    Button gotoNext;
-
-    @UiField
-    Button gotoPrev;
-
-    @UiField
-    DivElement status;
-
-    public NavBar() {
-      NavBarBinder b = GWT.create(NavBarBinder.class);
-      initWidget(b.createAndBindUi(this));
-    }
-
-    @UiHandler("gotoFirst")
-    void onFirst(ClickEvent event) {
-      startRow = 0;
-      refresh();
-    }
-
-    @UiHandler("gotoNext")
-    void onNext(ClickEvent event) {
-      startRow += getDataRowCount();
-      refresh();
-    }
-
-    @UiHandler("gotoPrev")
-    void onPrev(ClickEvent event) {
-      startRow -= getDataRowCount();
-      if (startRow < 0) {
-        startRow = 0;
-      }
-      refresh();
-    }
-  }
-
   interface Binder extends UiBinder<Widget, DynaTableWidget> {
   }
-
-  @UiTemplate("NavBar.ui.xml")
-  interface NavBarBinder extends UiBinder<Widget, NavBar> {
-  }

   /**
    * A dialog box for displaying an error.
@@ -112,57 +70,8 @@
     }
   }

-  private class RowDataAcceptorImpl implements RowDataAcceptor {
-    public void accept(int startRow, String[][] data) {
-
-      int destRowCount = getDataRowCount();
-      int destColCount = grid.getCellCount(0);
-      assert (data.length <= destRowCount) : "Too many rows";
-
-      int srcRowIndex = 0;
-      int srcRowCount = data.length;
-      int destRowIndex = 1; // skip navbar row
-      for (; srcRowIndex < srcRowCount; ++srcRowIndex, ++destRowIndex) {
-        String[] srcRowData = data[srcRowIndex];
- assert (srcRowData.length == destColCount) : " Column count mismatch"; - for (int srcColIndex = 0; srcColIndex < destColCount; ++srcColIndex) {
-          String cellHTML = srcRowData[srcColIndex];
-          grid.setText(destRowIndex, srcColIndex, cellHTML);
-        }
-      }
-
-      // Clear remaining table rows.
-      //
-      boolean isLastPage = false;
-      for (; destRowIndex < destRowCount + 1; ++destRowIndex) {
-        isLastPage = true;
- for (int destColIndex = 0; destColIndex < destColCount; ++destColIndex) {
-          grid.clearCell(destRowIndex, destColIndex);
-        }
-      }
-
-      // Synchronize the nav buttons.
-      navbar.gotoNext.setEnabled(!isLastPage);
-      navbar.gotoFirst.setEnabled(startRow > 0);
-      navbar.gotoPrev.setEnabled(startRow > 0);
-
-      // Update the status message.
-      //
-      setStatusText((startRow + 1) + " - " + (startRow + srcRowCount));
-    }
-
-    public void failed(Throwable caught) {
-      setStatusText("Error");
-      if (errorDialog == null) {
-        errorDialog = new ErrorDialog();
-      }
-      errorDialog.setText("Unexcepted Error processing remote call");
-      errorDialog.setBody(caught.getMessage());
-
-      errorDialog.center();
-    }
-  }
-
+  // TODO: Re-add error handling
+  @SuppressWarnings("unused")
private static final String NO_CONNECTION_MESSAGE = "<p>The DynaTableRf example uses a "
       + "RequestFactory "
+ "to request data from the server. In order for the RequestFactory to "
@@ -172,23 +81,24 @@
+ "DynaTableRf. Try running DynaTable in development mode to see the demo "
       + "in action.</p> ";

-  private final RowDataAcceptor acceptor = new RowDataAcceptorImpl();
-
   @UiField
   Grid grid;

-  @UiField(provided = true)
-  NavBar navbar = new NavBar();
+  @UiField
+  NavBar navbar;

   private ErrorDialog errorDialog = null;

-  private final DynaTableDataProvider provider;
-
   private int startRow = 0;

-  public DynaTableWidget(DynaTableDataProvider provider, int rowCount) {
+  private final CalendarProvider provider;
+
+  private HandlerRegistration rowDataRegistration;
+
+  public DynaTableWidget(CalendarProvider provider, int rowCount) {
     this.provider = provider;
-    initWidget(GWT.<Binder> create(Binder.class).createAndBindUi(this));
+    Binder binder = GWT.create(Binder.class);
+    initWidget(binder.createAndBindUi(this));
     initTable(rowCount);
   }

@@ -204,7 +114,7 @@
     navbar.gotoNext.setEnabled(false);

     setStatusText("Please wait...");
-    provider.updateRowData(startRow, grid.getRowCount() - 1, acceptor);
+    provider.updateRowData(startRow, grid.getRowCount() - 1);
   }

   public void setRowCount(int rows) {
@@ -214,6 +124,87 @@
   public void setStatusText(String text) {
     navbar.status.setInnerText(text);
   }
+
+  /**
+   * Attach to the event bus only when the widget is attached to the DOM.
+   */
+  @Override
+  protected void onLoad() {
+ rowDataRegistration = provider.addRowDataHandler(new DataAvailableEvent.Handler() {
+      public void onRowData(DataAvailableEvent event) {
+        accept(event.getStartRow(), event.getPeople());
+      }
+    });
+  }
+
+  @Override
+  protected void onUnload() {
+    rowDataRegistration.removeHandler();
+  }
+
+  @UiHandler("navbar")
+  void onNavigation(NavigationEvent e) {
+    switch (e.getDirection()) {
+      case BACKWARD:
+        startRow -= getDataRowCount();
+        break;
+      case FORWARD:
+        startRow += getDataRowCount();
+        break;
+      case START:
+        startRow = 0;
+        break;
+    }
+    refresh();
+  }
+
+  private void accept(int startRow, List<PersonProxy> people) {
+
+    int destRowCount = getDataRowCount();
+    int destColCount = grid.getCellCount(0);
+    assert (people.size() <= destRowCount) : "Too many rows";
+
+    int srcRowIndex = 0;
+    int srcRowCount = people.size();
+    int destRowIndex = 1; // skip navbar row
+    for (; srcRowIndex < srcRowCount; ++srcRowIndex, ++destRowIndex) {
+      PersonProxy p = people.get(srcRowIndex);
+      grid.setText(destRowIndex, 0, p.getName());
+      grid.setText(destRowIndex, 1, p.getDescription());
+      grid.setText(destRowIndex, 2, p.getSchedule());
+    }
+
+    // Clear remaining table rows.
+    //
+    boolean isLastPage = false;
+    for (; destRowIndex < destRowCount + 1; ++destRowIndex) {
+      isLastPage = true;
+ for (int destColIndex = 0; destColIndex < destColCount; ++destColIndex) {
+        grid.clearCell(destRowIndex, destColIndex);
+      }
+    }
+
+    // Synchronize the nav buttons.
+    navbar.gotoNext.setEnabled(!isLastPage);
+    navbar.gotoFirst.setEnabled(startRow > 0);
+    navbar.gotoPrev.setEnabled(startRow > 0);
+
+    // Update the status message.
+    setStatusText((startRow + 1) + " - " + (startRow + srcRowCount));
+  }
+
+  // TODO: Re-add error handling
+  @SuppressWarnings("unused")
+  private void failed(Throwable caught) {
+    setStatusText("Error");
+    if (errorDialog == null) {
+      errorDialog = new ErrorDialog();
+    }
+    errorDialog.setText("Unexcepted Error processing remote call");
+    errorDialog.setBody(caught.getMessage());
+
+    errorDialog.center();
+  }

   private int getDataRowCount() {
     return grid.getRowCount() - 1;
=======================================
--- /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableWidget.ui.xml Thu Jul 29 06:36:34 2010 +++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/DynaTableWidget.ui.xml Mon Aug 2 18:09:28 2010
@@ -25,7 +25,7 @@
   </ui:style>
   <g:DockLayoutPanel unit="EX">
     <g:north size="5">
-      <g:Widget ui:field="navbar" />
+      <dt:NavBar ui:field="navbar" />
     </g:north>
     <g:center>
       <g:Grid ui:field="grid" styleName="{style.grid}">
=======================================
--- /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/SchoolCalendarWidget.java Thu Jul 29 06:36:34 2010 +++ /trunk/samples/dynatablerf/src/com/google/gwt/sample/dynatablerf/client/SchoolCalendarWidget.java Mon Aug 2 18:09:28 2010
@@ -15,8 +15,8 @@
  */
 package com.google.gwt.sample.dynatablerf.client;

-import com.google.gwt.user.client.Command;
-import com.google.gwt.user.client.DeferredCommand;
+import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.core.client.Scheduler.ScheduledCommand;
 import com.google.gwt.user.client.ui.Composite;

 /**
@@ -29,10 +29,10 @@

   private final DynaTableWidget dynaTable;

-  private Command pendingRefresh;
-
- public SchoolCalendarWidget(DynaTableDataProvider calProvider, int visibleRows) {
-    dynaTable = new DynaTableWidget(calProvider, visibleRows);
+  private ScheduledCommand pendingRefresh;
+
+  public SchoolCalendarWidget(CalendarProvider provider, int visibleRows) {
+    dynaTable = new DynaTableWidget(provider, visibleRows);
     initWidget(dynaTable);
   }

@@ -54,13 +54,13 @@

     daysFilter[day] = included;
     if (pendingRefresh == null) {
-      pendingRefresh = new Command() {
+      pendingRefresh = new ScheduledCommand() {
         public void execute() {
           pendingRefresh = null;
           dynaTable.refresh();
         }
       };
-      DeferredCommand.addCommand(pendingRefresh);
+      Scheduler.get().scheduleDeferred(pendingRefresh);
     }
   }
 }

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

Reply via email to