Revision: 10197
Author:   jlaba...@google.com
Date:     Thu May 19 09:54:26 2011
Log:      Updating CellTable doc.

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

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

Added:
/trunk/user/javadoc/com/google/gwt/examples/cellview/CellTableFieldUpdaterExampleComplex.java /trunk/user/javadoc/com/google/gwt/examples/view/RangeChangeHandlerExample.java
Modified:
/trunk/user/javadoc/com/google/gwt/examples/cellview/CellTableFieldUpdaterExample.java
 /trunk/user/src/com/google/gwt/user/cellview/client/CellList.java
 /trunk/user/src/com/google/gwt/user/cellview/client/CellTable.java

=======================================
--- /dev/null
+++ /trunk/user/javadoc/com/google/gwt/examples/cellview/CellTableFieldUpdaterExampleComplex.java Thu May 19 09:54:26 2011
@@ -0,0 +1,175 @@
+/*
+ * 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.examples.cellview;
+
+import com.google.gwt.cell.client.DatePickerCell;
+import com.google.gwt.cell.client.FieldUpdater;
+import com.google.gwt.cell.client.TextInputCell;
+import com.google.gwt.core.client.EntryPoint;
+import com.google.gwt.i18n.client.DateTimeFormat;
+import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
+import com.google.gwt.user.cellview.client.CellTable;
+import com.google.gwt.user.cellview.client.Column;
+import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
+import com.google.gwt.user.cellview.client.TextColumn;
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.view.client.ProvidesKey;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * Example of using a {@link FieldUpdater} with a {@link CellTable}.
+ */
+public class CellTableFieldUpdaterExampleComplex implements EntryPoint {
+
+  /**
+   * A simple data type that represents a contact with a unique ID.
+   */
+  private static class Contact {
+    private static int nextId = 0;
+
+    private final int id;
+    private final String address;
+    private Date birthday;
+    private String name;
+
+    public Contact(String name, Date birthday, String address) {
+      nextId++;
+      this.id = nextId;
+      this.name = name;
+      this.birthday = birthday;
+      this.address = address;
+    }
+  }
+
+  /**
+   * The list of data to display.
+   */
+  private static final List<Contact> CONTACTS = Arrays.asList(
+      new Contact("John", new Date(80, 4, 12), "123 Fourth Avenue"),
+      new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln"),
+ new Contact("George", new Date(46, 6, 6), "1600 Pennsylvania Avenue"));
+
+  /**
+   * The key provider that allows us to identify Contacts even if a field
+   * changes. We identify contacts by their unique ID.
+   */
+ private static final ProvidesKey<Contact> KEY_PROVIDER = new ProvidesKey<CellTableFieldUpdaterExampleComplex.Contact>() {
+    @Override
+    public Object getKey(Contact item) {
+      return item.id;
+    }
+  };
+
+  @Override
+  public void onModuleLoad() {
+    // Create a CellTable with a key provider.
+    final CellTable<Contact> table = new CellTable<Contact>(KEY_PROVIDER);
+    table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
+
+    // Add a text input column to edit the name.
+    final TextInputCell nameCell = new TextInputCell();
+ Column<Contact, String> nameColumn = new Column<Contact, String>(nameCell) {
+      @Override
+      public String getValue(Contact object) {
+        return object.name;
+      }
+    };
+    table.addColumn(nameColumn, "Name");
+
+    // Add a field updater to be notified when the user enters a new name.
+    nameColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {
+      @Override
+      public void update(int index, Contact object, String value) {
+        // Validate the data.
+        if (value.length() < 3) {
+          Window.alert("Names must be at least three characters long.");
+
+          /*
+ * Clear the view data. The view data contains the pending change and + * allows the table to render with the pending value until the data is + * committed. If the data is committed into the object, the view data + * is automatically cleared out. If the data is not committed because
+           * it is invalid, you must delete.
+           */
+          nameCell.clearViewData(KEY_PROVIDER.getKey(object));
+
+          // Redraw the table.
+          table.redraw();
+          return;
+        }
+
+        // Inform the user of the change.
+ Window.alert("You changed the name of " + object.name + " to " + value);
+
+ // Push the changes into the Contact. At this point, you could send an
+        // asynchronous request to the server to update the database.
+        object.name = value;
+
+        // Redraw the table with the new data.
+        table.redraw();
+      }
+    });
+
+    // Add a date column to show the birthday.
+    Column<Contact, Date> dateColumn = new Column<Contact, Date>(
+        new DatePickerCell()) {
+      @Override
+      public Date getValue(Contact object) {
+        return object.birthday;
+      }
+    };
+    table.addColumn(dateColumn, "Birthday");
+
+ // Add a field updater to be notified when the user enters a new birthday.
+    dateColumn.setFieldUpdater(new FieldUpdater<Contact, Date>() {
+      @Override
+      public void update(int index, Contact object, Date value) {
+        Window.alert("You changed the birthday of "
+            + object.name
+            + " to "
+ + DateTimeFormat.getFormat(PredefinedFormat.DATE_LONG).format(value));
+
+        // Push the changes into the Contact.
+        object.birthday = value;
+
+        // Redraw the table with the new data.
+        table.redraw();
+      }
+    });
+    // Add a text column to show the address.
+    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
+      @Override
+      public String getValue(Contact object) {
+        return object.address;
+      }
+    };
+    table.addColumn(addressColumn, "Address");
+
+ // Set the total row count. This isn't strictly necessary, but it affects + // paging calculations, so its good habit to keep the row count up to date.
+    table.setRowCount(CONTACTS.size(), true);
+
+    // Push the data into the widget.
+    table.setRowData(0, CONTACTS);
+
+    // Add it to the root panel.
+    RootPanel.get().add(table);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/user/javadoc/com/google/gwt/examples/view/RangeChangeHandlerExample.java Thu May 19 09:54:26 2011
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2011 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.examples.view;
+
+import com.google.gwt.cell.client.TextCell;
+import com.google.gwt.core.client.EntryPoint;
+import com.google.gwt.user.cellview.client.CellList;
+import com.google.gwt.user.cellview.client.SimplePager;
+import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.user.client.ui.VerticalPanel;
+import com.google.gwt.view.client.Range;
+import com.google.gwt.view.client.RangeChangeEvent;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Example of using a {@link RangeChangeEvent.Handler} to push data into a
+ * {@link CellList} when the range changes.
+ */
+public class RangeChangeHandlerExample implements EntryPoint {
+
+  @Override
+  public void onModuleLoad() {
+    // Create a CellList.
+    final CellList<String> cellList = new CellList<String>(new TextCell());
+
+    // Add a range change handler.
+    cellList.addRangeChangeHandler(new RangeChangeEvent.Handler() {
+      @Override
+      public void onRangeChange(RangeChangeEvent event) {
+        Range range = event.getNewRange();
+        int start = range.getStart();
+        int length = range.getLength();
+
+ // Create the data to push into the view. At this point, you could send
+        // an asynchronous RPC request to a server.
+        List<String> data = new ArrayList<String>();
+        for (int i = start; i < start + length; i++) {
+          data.add("Item " + i);
+        }
+
+        // Push the data into the list.
+        cellList.setRowData(start, data);
+      }
+    });
+
+    // Force the cellList to fire an initial range change event.
+    cellList.setVisibleRangeAndClearData(new Range(0, 25), true);
+
+    // Create paging controls.
+    SimplePager pager = new SimplePager();
+    pager.setDisplay(cellList);
+
+    // Add the widgets to the root panel.
+    VerticalPanel vPanel = new VerticalPanel();
+    vPanel.add(pager);
+    vPanel.add(cellList);
+    RootPanel.get().add(vPanel);
+  }
+}
=======================================
--- /trunk/user/javadoc/com/google/gwt/examples/cellview/CellTableFieldUpdaterExample.java Tue Oct 5 12:54:05 2010 +++ /trunk/user/javadoc/com/google/gwt/examples/cellview/CellTableFieldUpdaterExample.java Thu May 19 09:54:26 2011
@@ -15,22 +15,16 @@
  */
 package com.google.gwt.examples.cellview;

-import com.google.gwt.cell.client.DatePickerCell;
 import com.google.gwt.cell.client.FieldUpdater;
 import com.google.gwt.cell.client.TextInputCell;
 import com.google.gwt.core.client.EntryPoint;
-import com.google.gwt.i18n.client.DateTimeFormat;
-import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
 import com.google.gwt.user.cellview.client.CellTable;
 import com.google.gwt.user.cellview.client.Column;
-import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
-import com.google.gwt.user.cellview.client.TextColumn;
 import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.RootPanel;
 import com.google.gwt.view.client.ProvidesKey;

 import java.util.Arrays;
-import java.util.Date;
 import java.util.List;

 /**
@@ -45,47 +39,44 @@
     private static int nextId = 0;

     private final int id;
-    private final String address;
-    private Date birthday;
     private String name;

-    public Contact(String name, Date birthday, String address) {
+    public Contact(String name) {
       nextId++;
       this.id = nextId;
       this.name = name;
-      this.birthday = birthday;
-      this.address = address;
     }
   }

   /**
    * The list of data to display.
    */
-  private static final List<Contact> CONTACTS = Arrays.asList(
-      new Contact("John", new Date(80, 4, 12), "123 Fourth Avenue"),
-      new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln"),
- new Contact("George", new Date(46, 6, 6), "1600 Pennsylvania Avenue")); + private static final List<Contact> CONTACTS = Arrays.asList(new Contact("John"), new Contact(
+      "Joe"), new Contact("George"));

   /**
    * The key provider that allows us to identify Contacts even if a field
    * changes. We identify contacts by their unique ID.
    */
- private static final ProvidesKey<Contact> KEY_PROVIDER = new ProvidesKey<CellTableFieldUpdaterExample.Contact>() {
-    public Object getKey(Contact item) {
-      return item.id;
-    }
-  };
-
+  private static final ProvidesKey<Contact> KEY_PROVIDER =
+      new ProvidesKey<CellTableFieldUpdaterExample.Contact>() {
+        @Override
+        public Object getKey(Contact item) {
+          return item.id;
+        }
+      };
+
+  @Override
   public void onModuleLoad() {
     // Create a CellTable with a key provider.
     final CellTable<Contact> table = new CellTable<Contact>(KEY_PROVIDER);
-    table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

     // Add a text input column to edit the name.
     final TextInputCell nameCell = new TextInputCell();
Column<Contact, String> nameColumn = new Column<Contact, String>(nameCell) {
       @Override
       public String getValue(Contact object) {
+        // Return the name as the value of this column.
         return object.name;
       }
     };
@@ -93,25 +84,8 @@

     // Add a field updater to be notified when the user enters a new name.
     nameColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {
+      @Override
       public void update(int index, Contact object, String value) {
-        // Validate the data.
-        if (value.length() < 3) {
-          Window.alert("Names must be at least three characters long.");
-
-          /*
- * Clear the view data. The view data contains the pending change and - * allows the table to render with the pending value until the data is - * committed. If the data is committed into the object, the view data - * is automatically cleared out. If the data is not committed because
-           * it is invalid, you must delete.
-           */
-          nameCell.clearViewData(KEY_PROVIDER.getKey(object));
-
-          // Redraw the table.
-          table.redraw();
-          return;
-        }
-
         // Inform the user of the change.
Window.alert("You changed the name of " + object.name + " to " + value);

@@ -124,46 +98,8 @@
       }
     });

-    // Add a date column to show the birthday.
-    Column<Contact, Date> dateColumn = new Column<Contact, Date>(
-        new DatePickerCell()) {
-      @Override
-      public Date getValue(Contact object) {
-        return object.birthday;
-      }
-    };
-    table.addColumn(dateColumn, "Birthday");
-
- // Add a field updater to be notified when the user enters a new birthday.
-    dateColumn.setFieldUpdater(new FieldUpdater<Contact, Date>() {
-      public void update(int index, Contact object, Date value) {
-        Window.alert("You changed the birthday of "
-            + object.name
-            + " to "
- + DateTimeFormat.getFormat(PredefinedFormat.DATE_LONG).format(value));
-
-        // Push the changes into the Contact.
-        object.birthday = value;
-
-        // Redraw the table with the new data.
-        table.redraw();
-      }
-    });
-    // Add a text column to show the address.
-    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
-      @Override
-      public String getValue(Contact object) {
-        return object.address;
-      }
-    };
-    table.addColumn(addressColumn, "Address");
-
- // Set the total row count. This isn't strictly necessary, but it affects - // paging calculations, so its good habit to keep the row count up to date.
-    table.setRowCount(CONTACTS.size(), true);
-
     // Push the data into the widget.
-    table.setRowData(0, CONTACTS);
+    table.setRowData(CONTACTS);

     // Add it to the root panel.
     RootPanel.get().add(table);
=======================================
--- /trunk/user/src/com/google/gwt/user/cellview/client/CellList.java Mon Mar 21 12:22:19 2011 +++ /trunk/user/src/com/google/gwt/user/cellview/client/CellList.java Thu May 19 09:54:26 2011
@@ -57,9 +57,15 @@
  * <dl>
  * <dt>Trivial example</dt>
  * <dd>{@example com.google.gwt.examples.cellview.CellListExample}</dd>
- * <dt>ValueUpdater example</dt>
+ * <dt>Handling user input with ValueUpdater</dt>
* <dd>{@example com.google.gwt.examples.cellview.CellListValueUpdaterExample}</dd>
- * <dt>Key provider example</dt>
+ * <dt>Pushing data with List Data Provider (backed by {@link List})</dt>
+ * <dd>{@example com.google.gwt.examples.view.ListDataProviderExample}</dd>
+ * <dt>Pushing data asynchronously with Async Data Provider</dt>
+ * <dd>{@example com.google.gwt.examples.view.AsyncDataProviderExample}</dd>
+ * <dt>Writing a custom data provider</dt>
+ * <dd>{@example com.google.gwt.examples.view.RangeChangeHandlerExample}</dd>
+ * <dt>Using a key provider to track objects as they change</dt>
  * <dd>{@example com.google.gwt.examples.view.KeyProviderExample}</dd>
  * </dl>
  * </p>
=======================================
--- /trunk/user/src/com/google/gwt/user/cellview/client/CellTable.java Thu Mar 3 07:09:10 2011 +++ /trunk/user/src/com/google/gwt/user/cellview/client/CellTable.java Thu May 19 09:54:26 2011
@@ -90,9 +90,17 @@
  * <dl>
  * <dt>Trivial example</dt>
  * <dd>{@example com.google.gwt.examples.cellview.CellTableExample}</dd>
- * <dt>FieldUpdater example</dt>
+ * <dt>Handling user input with trivial FieldUpdater example</dt>
* <dd>{@example com.google.gwt.examples.cellview.CellTableFieldUpdaterExample}</dd>
- * <dt>Key provider example</dt>
+ * <dt>Handling user input with complex FieldUpdater example</dt>
+ * <dd>{@example com.google.gwt.examples.cellview.CellTableFieldUpdaterExampleComplex}</dd>
+ * <dt>Pushing data with List Data Provider (backed by {@link List})</dt>
+ * <dd>{@example com.google.gwt.examples.view.ListDataProviderExample}</dd>
+ * <dt>Pushing data asynchronously with Async Data Provider</dt>
+ * <dd>{@example com.google.gwt.examples.view.AsyncDataProviderExample}</dd>
+ * <dt>Writing a custom data provider</dt>
+ * <dd>{@example com.google.gwt.examples.view.RangeChangeHandlerExample}</dd>
+ * <dt>Using a key provider to track objects as they change</dt>
  * <dd>{@example com.google.gwt.examples.view.KeyProviderExample}</dd>
  * </dl>
  * </p>

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

Reply via email to