Revision: 7848
Author: [email protected]
Date: Thu Apr  1 12:13:45 2010
Log: Implement selection and multiple cells for Tree nodes

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

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

Added:
 /trunk/bikeshed/src/com/google/gwt/bikeshed/list/client/HasCell.java
Modified:
 /trunk/bikeshed/src/com/google/gwt/bikeshed/cells/client/ButtonCell.java
 /trunk/bikeshed/src/com/google/gwt/bikeshed/cells/client/CheckboxCell.java
 /trunk/bikeshed/src/com/google/gwt/bikeshed/list/client/Column.java
 /trunk/bikeshed/src/com/google/gwt/bikeshed/list/shared/SelectionModel.java
/trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/SideBySideTreeNodeView.java /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/SideBySideTreeView.java /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/StandardTreeNodeView.java /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/StandardTreeView.java
 /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/TreeNodeView.java
 /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/TreeView.java
 /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/TreeViewModel.java
/trunk/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/Columns.java /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/TransactionTreeViewModel.java /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/MyTreeViewModel.java /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/TreeSample.java
 /trunk/bikeshed/war/Tree.css

=======================================
--- /dev/null
+++ /trunk/bikeshed/src/com/google/gwt/bikeshed/list/client/HasCell.java Thu Apr 1 12:13:45 2010
@@ -0,0 +1,37 @@
+/*
+ * 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.bikeshed.list.client;
+
+import com.google.gwt.bikeshed.cells.client.Cell;
+import com.google.gwt.bikeshed.cells.client.FieldUpdater;
+
+/**
+ * An interface for extracting a value from an underlying data type, provide a + * cell to render that value, and provide a FieldUpdater to perform notification
+ * of updates to the cell.
+ *
+ * @param <T> the underlying data type
+ * @param <C> the cell data type
+ * @param <V> the view data type
+ */
+public interface HasCell<T, C, V> {
+
+  Cell<C, V> getCell();
+
+  FieldUpdater<T, C, V> getFieldUpdater();
+
+  C getValue(T object);
+}
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/cells/client/ButtonCell.java Mon Mar 29 05:42:31 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/cells/client/ButtonCell.java Thu Apr 1 12:13:45 2010
@@ -23,6 +23,18 @@
  */
 public class ButtonCell extends Cell<String, Void> {

+  private static ButtonCell instance;
+
+  public static ButtonCell getInstance() {
+    if (instance == null) {
+      instance = new ButtonCell();
+    }
+    return instance;
+  }
+
+  private ButtonCell() {
+  }
+
   @Override
   public boolean consumesEvents() {
     return true;
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/cells/client/CheckboxCell.java Mon Mar 29 05:42:31 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/cells/client/CheckboxCell.java Thu Apr 1 12:13:45 2010
@@ -32,7 +32,8 @@
   @Override
   public Void onBrowserEvent(Element parent, Boolean value, Void viewData,
       NativeEvent event, ValueUpdater<Boolean, Void> valueUpdater) {
-    if (valueUpdater != null && "change".equals(event.getType())) {
+    String type = event.getType();
+    if (valueUpdater != null && "change".equals(type)) {
       InputElement input = parent.getFirstChild().cast();
       valueUpdater.update(input.isChecked(), viewData);
     }
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/list/client/Column.java Wed Mar 31 11:23:01 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/list/client/Column.java Thu Apr 1 12:13:45 2010
@@ -37,7 +37,7 @@
 // TODO - when can we get rid of a view data object?
// TODO - should viewData implement some interface? (e.g., with commit/rollback/dispose)
 // TODO - have a ViewDataColumn superclass / SimpleColumn subclass
-public abstract class Column<T, C, V> {
+public abstract class Column<T, C, V> implements HasCell<T, C, V> {

   protected final Cell<C, V> cell;

@@ -64,6 +64,14 @@
   public boolean consumesEvents() {
     return cell.consumesEvents();
   }
+
+  public Cell<C, V> getCell() {
+    return cell;
+  }
+
+  public FieldUpdater<T, C, V> getFieldUpdater() {
+    return fieldUpdater;
+  }

   public abstract C getValue(T object);

@@ -91,12 +99,4 @@
   public void setFieldUpdater(FieldUpdater<T, C, V> fieldUpdater) {
     this.fieldUpdater = fieldUpdater;
   }
-
-  protected Cell<C, V> getCell() {
-    return cell;
-  }
-
-  protected FieldUpdater<T, C, V> getFieldUpdater() {
-    return fieldUpdater;
-  }
-}
+}
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/list/shared/SelectionModel.java Wed Mar 31 11:23:01 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/list/shared/SelectionModel.java Thu Apr 1 12:13:45 2010
@@ -70,5 +70,5 @@

   void setSelected(T object, boolean selected);

-  void setSelected(List<T> object, boolean selected);
-}
+  void setSelected(List<T> objects, boolean selected);
+}
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/SideBySideTreeNodeView.java Mon Mar 29 04:52:13 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/SideBySideTreeNodeView.java Thu Apr 1 12:13:45 2010
@@ -15,7 +15,7 @@
  */
 package com.google.gwt.bikeshed.tree.client;

-import com.google.gwt.bikeshed.cells.client.Cell;
+import com.google.gwt.bikeshed.list.client.HasCell;
 import com.google.gwt.bikeshed.tree.client.TreeViewModel.NodeInfo;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
@@ -76,7 +76,7 @@

   @Override
   protected <C> void emitHtml(StringBuilder sb, List<C> childValues,
-      List<TreeNodeView<?>> savedViews, Cell<C, Void> cell) {
+ List<HasCell<C, ?, Void>> hasCells, List<TreeNodeView<?>> savedViews) {
     TreeView tree = getTree();
     TreeViewModel model = tree.getTreeViewModel();
     int imageWidth = tree.getImageWidth();
@@ -97,7 +97,13 @@
         sb.append(tree.getClosedImageHtml(imageLeft));
       }
       sb.append("<div class=\"gwt-sstree-cell\">");
-      cell.render(childValue, null, sb);
+      for (int i = 0; i < hasCells.size(); i++) {
+        sb.append("<span __idx='");
+        sb.append(i);
+        sb.append("'>");
+        render(sb, childValue, hasCells.get(i));
+        sb.append("</span>");
+      }
       sb.append("</div></div>");

       idx++;
@@ -244,4 +250,9 @@

     setChildContainer(null);
   }
-}
+
+  private <C, X> void render(StringBuilder sb, C childValue,
+      HasCell<C, X, Void> hc) {
+    hc.getCell().render(hc.getValue(childValue), null, sb);
+  }
+}
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/SideBySideTreeView.java Mon Mar 29 04:52:13 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/SideBySideTreeView.java Thu Apr 1 12:13:45 2010
@@ -73,7 +73,7 @@
     setElement(rootElement);

     // Add event handlers.
-    sinkEvents(Event.ONCLICK | Event.ONMOUSEDOWN | Event.ONMOUSEUP);
+ sinkEvents(Event.ONCLICK | Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONCHANGE);

     // Associate a view with the item.
     TreeNodeView<T> root = new SideBySideTreeNodeView<T>(this, null, null,
@@ -88,7 +88,7 @@

     int eventType = DOM.eventGetType(event);
     switch (eventType) {
-      case Event.ONMOUSEUP:
+      case Event.ONMOUSEUP: case Event.ONCHANGE:
         Element currentTarget = event.getCurrentEventTarget().cast();
         if (currentTarget == getElement()) {
           Element target = event.getEventTarget().cast();
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/StandardTreeNodeView.java Mon Mar 22 05:11:01 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/StandardTreeNodeView.java Thu Apr 1 12:13:45 2010
@@ -15,7 +15,8 @@
  */
 package com.google.gwt.bikeshed.tree.client;

-import com.google.gwt.bikeshed.cells.client.Cell;
+import com.google.gwt.bikeshed.list.client.HasCell;
+import com.google.gwt.bikeshed.list.shared.SelectionModel;
 import com.google.gwt.bikeshed.tree.client.TreeViewModel.NodeInfo;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
@@ -46,11 +47,6 @@
     super(tree, parent, parentNodeInfo, value);
     setElement(elem);
   }
-
-  @Override
-  protected void postClose() {
-    getTree().maybeAnimateTreeNode(this);
-  }

   @Override
   protected <C> TreeNodeView<C> createTreeNodeView(NodeInfo<C> nodeInfo,
@@ -61,11 +57,13 @@

   @Override
   protected <C> void emitHtml(StringBuilder sb, List<C> childValues,
-      List<TreeNodeView<?>> savedViews, Cell<C, Void> cell) {
+ List<HasCell<C, ?, Void>> hasCells, List<TreeNodeView<?>> savedViews) {
     TreeView tree = getTree();
     TreeViewModel model = tree.getTreeViewModel();
     int imageWidth = tree.getImageWidth();

+    SelectionModel<Object> selectionModel = tree.getSelectionModel();
+
     int idx = 0;
     for (C childValue : childValues) {
       sb.append("<div style=\"position:relative;padding-left:");
@@ -78,8 +76,20 @@
       } else {
         sb.append(tree.getClosedImageHtml(0));
       }
-      sb.append("<div>");
-      cell.render(childValue, null, sb);
+ if (selectionModel != null && selectionModel.isSelected(childValue)) {
+        sb.append("<div class='gwt-stree-selectedItem'>");
+      } else {
+        sb.append("<div>");
+      }
+
+      for (int i = 0; i < hasCells.size(); i++) {
+        sb.append("<span __idx='");
+        sb.append(i);
+        sb.append("'>");
+        render(sb, childValue, hasCells.get(i));
+        sb.append("</span>");
+      }
+
       sb.append("</div></div>");
     }
   }
@@ -118,4 +128,14 @@
   protected Element getImageElement() {
     return getElement().getFirstChildElement();
   }
-}
+
+  @Override
+  protected void postClose() {
+    getTree().maybeAnimateTreeNode(this);
+  }
+
+  private <C, X> void render(StringBuilder sb, C childValue,
+      HasCell<C, X, Void> hc) {
+    hc.getCell().render(hc.getValue(childValue), null, sb);
+  }
+}
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/StandardTreeView.java Thu Mar 11 03:44:25 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/StandardTreeView.java Thu Apr 1 12:13:45 2010
@@ -212,7 +212,7 @@
     setAnimation(SlideAnimation.create());

     // Add event handlers.
-    sinkEvents(Event.ONCLICK | Event.ONMOUSEDOWN | Event.ONMOUSEUP);
+ sinkEvents(Event.ONCLICK | Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONCHANGE);

     // Associate a view with the item.
TreeNodeView<T> root = new StandardTreeNodeView<T>(this, null, null, getElement(), rootValue);
@@ -226,7 +226,7 @@

     int eventType = DOM.eventGetType(event);
     switch (eventType) {
-      case Event.ONMOUSEUP:
+      case Event.ONMOUSEUP: case Event.ONCHANGE:
         Element currentTarget = event.getCurrentEventTarget().cast();
         if (currentTarget == getElement()) {
           Element target = event.getEventTarget().cast();
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/TreeNodeView.java Mon Mar 22 05:11:01 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/TreeNodeView.java Thu Apr 1 12:13:45 2010
@@ -15,7 +15,7 @@
  */
 package com.google.gwt.bikeshed.tree.client;

-import com.google.gwt.bikeshed.cells.client.Cell;
+import com.google.gwt.bikeshed.list.client.HasCell;
 import com.google.gwt.bikeshed.list.shared.ListEvent;
 import com.google.gwt.bikeshed.list.shared.ListHandler;
 import com.google.gwt.bikeshed.list.shared.ListModel;
@@ -253,13 +253,13 @@
    * @param <C> the data type of the child nodes
    * @param sb a StringBuilder to write to
    * @param childValues a List of child node values
+ * @param hasCells the cells and values to use for rendering each child value
    * @param savedViews a List of TreeNodeView instances corresponding to
* the child values; a non-null value indicates a TreeNodeView previously
    *   associated with a given child value
-   * @param cell the Cell to use for rendering each child value
    */
protected abstract <C> void emitHtml(StringBuilder sb, List<C> childValues,
-      List<TreeNodeView<?>> savedViews, Cell<C, Void> cell);
+ List<HasCell<C, ?, Void>> hasCells, List<TreeNodeView<?>> savedViews);

   /**
    * Ensure that the animation frame exists and return it.
@@ -388,7 +388,7 @@

         // Construct the child contents.
         StringBuilder sb = new StringBuilder();
-        emitHtml(sb, event.getValues(), savedViews, nodeInfo.getCell());
+ emitHtml(sb, event.getValues(), nodeInfo.getHasCells(), savedViews);
         childContainer.setInnerHTML(sb.toString());

         // Create the child TreeNodeViews from the new elements.
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/TreeView.java Thu Mar 11 03:44:25 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/TreeView.java Thu Apr 1 12:13:45 2010
@@ -1,12 +1,12 @@
 /*
  * 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
@@ -16,6 +16,7 @@
 package com.google.gwt.bikeshed.tree.client;

 import com.google.gwt.animation.client.Animation;
+import com.google.gwt.bikeshed.list.shared.SelectionModel;
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.resources.client.ClientBundle;
 import com.google.gwt.resources.client.ImageResource;
@@ -114,6 +115,8 @@
    */
   private TreeNodeView<?> rootNode;

+  private SelectionModel<Object> selectionModel;
+
   /**
    * Construct a new {...@link TreeView}.
    *
@@ -133,10 +136,14 @@
   public TreeNodeViewAnimation getAnimation() {
     return animation;
   }
-
+
   public TreeNode<?> getRootNode() {
     return rootNode;
   }
+
+  public SelectionModel<Object> getSelectionModel() {
+    return selectionModel;
+  }

   public TreeViewModel getTreeViewModel() {
     return model;
@@ -164,6 +171,11 @@
       animation.cancel();
     }
   }
+
+  public void setSelectionModel(SelectionModel<Object> selectionModel) {
+    this.selectionModel = selectionModel;
+  }
+
   /**
    * @return the HTML to render the closed image.
    */
=======================================
--- /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/TreeViewModel.java Mon Mar 29 04:52:13 2010 +++ /trunk/bikeshed/src/com/google/gwt/bikeshed/tree/client/TreeViewModel.java Thu Apr 1 12:13:45 2010
@@ -1,12 +1,12 @@
 /*
  * 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
@@ -16,12 +16,17 @@
 package com.google.gwt.bikeshed.tree.client;

 import com.google.gwt.bikeshed.cells.client.Cell;
+import com.google.gwt.bikeshed.cells.client.FieldUpdater;
 import com.google.gwt.bikeshed.cells.client.ValueUpdater;
+import com.google.gwt.bikeshed.list.client.HasCell;
 import com.google.gwt.bikeshed.list.client.HasKey;
 import com.google.gwt.bikeshed.list.shared.ListModel;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.NativeEvent;

+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * A model of a tree.
  */
@@ -30,85 +35,122 @@
   /**
    * The info needed to create a {...@link TreeNodeView}.
    */
-  interface NodeInfo<C> extends HasKey<C> {
-    /**
-     * Get the {...@link Cell} used to render child nodes.
-     *
-     * @return the cell
-     */
-    Cell<C, Void> getCell();
+  interface NodeInfo<T> extends HasKey<T> {
+
+    List<HasCell<T, ?, Void>> getHasCells();

     /**
      * Get the {...@link ListModel} used to retrieve child node values.
-     *
+     *
      * @return the list model
      */
-    ListModel<C> getListModel();
+    ListModel<T> getListModel();

     /**
      * Handle an event that is fired on one of the children of this item.
-     *
+     *
      * @param elem the parent element of the item
      * @param object the data value of the item
      * @param event the event that was fired
      */
-    void onBrowserEvent(Element elem, final C object, NativeEvent event);
+    void onBrowserEvent(Element elem, final T object, NativeEvent event);
   }

   /**
    * Default implementation of {...@link NodeInfo}.
    */
-  class DefaultNodeInfo<C> implements NodeInfo<C> {
-
-    private Cell<C, Void> cell;
-    private ListModel<C> listModel;
-    private ValueUpdater<C, Void> valueUpdater;
+  class DefaultNodeInfo<T> implements NodeInfo<T> {
+ private List<HasCell<T, ?, Void>> hasCells = new ArrayList<HasCell<T, ?, Void>>();
+    private ListModel<T> listModel;

     /**
-     * Construct a new {...@link DefaultNodeInfo}.
-     *
+     * Construct a new {...@link DefaultNodeInfo} with a single cell and a
+     * {...@link ValueUpdater}.
+     *
* @param listModel the {...@link ListModel} that provides the child values
      * @param cell the {...@link Cell} used to render the child values
+     * @param valueUpdater the {...@link ValueUpdater}
      */
-    public DefaultNodeInfo(ListModel<C> listModel, Cell<C, Void> cell) {
-      this.cell = cell;
+ public DefaultNodeInfo(ListModel<T> listModel, final Cell<T, Void> cell,
+        final ValueUpdater<T, Void> valueUpdater) {
+      hasCells.add(new HasCell<T, T, Void>() {
+        public Cell<T, Void> getCell() {
+          return cell;
+        }
+
+        public FieldUpdater<T, T, Void> getFieldUpdater() {
+ return valueUpdater == null ? null : new FieldUpdater<T, T, Void>() { + public void update(int index, T object, T value, Void viewData) {
+              valueUpdater.update(value, viewData);
+            }
+          };
+        }
+
+        public T getValue(T object) {
+          return object;
+        }
+      });
       this.listModel = listModel;
     }

     /**
      * Construct a new {...@link DefaultNodeInfo}.
-     *
+     *
* @param listModel the {...@link ListModel} that provides the child values
      * @param cell the {...@link Cell} used to render the child values
-     * @param valueUpdater the {...@link ValueUpdater}
      */
-    public DefaultNodeInfo(ListModel<C> listModel, Cell<C, Void> cell,
-        ValueUpdater<C, Void> valueUpdater) {
-      this(listModel, cell);
-      this.valueUpdater = valueUpdater;
+    public DefaultNodeInfo(ListModel<T> listModel, Cell<T, Void> cell) {
+      this(listModel, cell, null);
     }

-    public Cell<C, Void> getCell() {
-      return cell;
+ public DefaultNodeInfo(ListModel<T> listModel, List<HasCell<T, ?, Void>> hasCells) {
+      this.hasCells.addAll(hasCells);
+      this.listModel = listModel;
     }

-    public Object getKey(C value) {
+    public List<HasCell<T, ?, Void>> getHasCells() {
+      return hasCells;
+    }
+
+    public Object getKey(T value) {
       return value;
     }

-    public ListModel<C> getListModel() {
+    public ListModel<T> getListModel() {
       return listModel;
     }

- public void onBrowserEvent(Element elem, final C object, NativeEvent event) {
-      cell.onBrowserEvent(elem, object, null, event, valueUpdater);
+    // TODO - dispatch into cells
+ public void onBrowserEvent(Element elem, final T object, NativeEvent event) {
+      Element target = event.getEventTarget().cast();
+      String idxString = "";
+      while ((target != null)
+          && ((idxString = target.getAttribute("__idx")).length() == 0)) {
+        target = target.getParentElement();
+      }
+      if (idxString.length() > 0) {
+        int idx = Integer.parseInt(idxString);
+        dispatch(target, object, event, hasCells.get(idx));
+      }
+    }
+
+ private <X> void dispatch(Element target, final T object, NativeEvent event,
+        HasCell<T, X, Void> hc) {
+      final FieldUpdater<T, X, Void> fieldUpdater = hc.getFieldUpdater();
+      hc.getCell().onBrowserEvent(target, hc.getValue(object), null, event,
+          fieldUpdater == null ? null
+              : new ValueUpdater<X, Void>() {
+                public void update(X value, Void viewData) {
+                  fieldUpdater.update(0, object, value, viewData);
+                }
+              });
     }
   }

   /**
    * Get the {...@link NodeInfo} that will provide the {...@link ListModel} and
    * {...@link Cell} to retrieve the children of the specified value.
-   *
+   *
    * @param value the value in the parent node
    * @param treeNode the {...@link TreeNode} that contains the value
    * @return the {...@link NodeInfo}
@@ -117,7 +159,7 @@

   /**
    * Check if the value is known to be a leaf node.
-   *
+   *
    * @param value the value at the node
    * @param treeNode the {...@link TreeNode} that contains the value
    *
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/Columns.java Thu Apr 1 10:30:40 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/Columns.java Thu Apr 1 12:13:45 2010
@@ -30,7 +30,7 @@
 public class Columns {

static SimpleColumn<StockQuote, String> buyColumn = new SimpleColumn<StockQuote, String>(
-      new ButtonCell()) {
+      ButtonCell.getInstance()) {
     @Override
     public String getValue(StockQuote object) {
       return "Buy";
@@ -89,7 +89,7 @@
   };

static SimpleColumn<StockQuote, String> sellColumn = new SimpleColumn<StockQuote, String>(
-      new ButtonCell()) {
+      ButtonCell.getInstance()) {
     @Override
     public String getValue(StockQuote object) {
       return "Sell";
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/TransactionTreeViewModel.java Thu Apr 1 10:30:40 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/stocks/client/TransactionTreeViewModel.java Thu Apr 1 12:13:45 2010
@@ -119,7 +119,7 @@
       List<String> list = listModel.getList();
       list.add("Buy");
       list.add("Sell");
- return new TreeViewModel.DefaultNodeInfo<String>(listModel, new ButtonCell(), + return new TreeViewModel.DefaultNodeInfo<String>(listModel, ButtonCell.getInstance(),
           new ValueUpdater<String, Void>() {
             public void update(String value, Void viewData) {
StockQuote stockQuote = (StockQuote) treeNode.getParentNode().getValue();
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/MyTreeViewModel.java Tue Mar 23 06:01:07 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/MyTreeViewModel.java Thu Apr 1 12:13:45 2010
@@ -1,12 +1,12 @@
 /*
  * 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
@@ -17,9 +17,13 @@

 import com.google.gwt.bikeshed.cells.client.ButtonCell;
 import com.google.gwt.bikeshed.cells.client.Cell;
+import com.google.gwt.bikeshed.cells.client.CheckboxCell;
+import com.google.gwt.bikeshed.cells.client.FieldUpdater;
 import com.google.gwt.bikeshed.cells.client.ValueUpdater;
+import com.google.gwt.bikeshed.list.client.HasCell;
 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.tree.client.TreeNode;
 import com.google.gwt.bikeshed.tree.client.TreeViewModel;
 import com.google.gwt.core.client.GWT;
@@ -37,11 +41,11 @@

private static class IntegerListModel extends AbstractListModel<Integer> {
     int wordLength;
-
+
     public IntegerListModel(int wordLength) {
       this.wordLength = wordLength;
     }
-
+
     @Override
     protected void onRangeChanged(int start, int length) {
       List<Integer> values = new ArrayList<Integer>(1);
@@ -53,11 +57,11 @@

   private static class StringListModel extends AbstractListModel<String> {
     String value;
-
+
     public StringListModel(final String value) {
       this.value = value;
     }
-
+
     @Override
     protected void onRangeChanged(int start, int length) {
       String prefix = value.endsWith("...") ? value.substring(0,
@@ -98,6 +102,48 @@
       sb.append(value);
     }
   };
+
+  private List<HasCell<String, ?, Void>> hasCells =
+    new ArrayList<HasCell<String, ?, Void>>();
+
+  public MyTreeViewModel(final SelectionModel<Object> selectionModel) {
+    hasCells.add(new HasCell<String, Boolean, Void>() {
+      public Cell<Boolean, Void> getCell() {
+        return new CheckboxCell();
+      }
+
+      public FieldUpdater<String, Boolean, Void> getFieldUpdater() {
+        return new FieldUpdater<String, Boolean, Void>() {
+          public void update(int index, String object, Boolean value,
+              Void viewData) {
+            selectionModel.setSelected(object, value);
+          }
+        };
+      }
+
+      public Boolean getValue(String object) {
+        return selectionModel.isSelected(object);
+      }
+    });
+    hasCells.add(new HasCell<String, String, Void>() {
+      public Cell<String, Void> getCell() {
+        return ButtonCell.getInstance();
+      }
+
+      public FieldUpdater<String, String, Void> getFieldUpdater() {
+        return new FieldUpdater<String, String, Void>() {
+          public void update(int index, String object, String value,
+              Void viewData) {
+            Window.alert("Clicked " + object);
+          }
+        };
+      }
+
+      public String getValue(String object) {
+        return object;
+      }
+    });
+  }

   public <T> NodeInfo<?> getNodeInfo(T value, TreeNode<T> treeNode) {
     if (value instanceof String) {
@@ -116,12 +162,7 @@
   private NodeInfo<?> getNodeInfoHelper(final String value) {
     if (value.endsWith("...")) {
       ListModel<String> listModel = new StringListModel(value.toString());
-      return new DefaultNodeInfo<String>(listModel, new ButtonCell(),
-          new ValueUpdater<String, Void>() {
-            public void update(String value, Void viewData) {
-              Window.alert("Clicked: " + value);
-            }
-          });
+      return new DefaultNodeInfo<String>(listModel, hasCells);
     } else {
       ListModel<Integer> listModel = new IntegerListModel(value.length());
       return new DefaultNodeInfo<Integer>(listModel, INTEGER_CELL,
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/TreeSample.java Mon Mar 29 04:52:13 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/bikeshed/tree/client/TreeSample.java Thu Apr 1 12:13:45 2010
@@ -1,12 +1,12 @@
 /*
  * 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
@@ -15,26 +15,81 @@
  */
 package com.google.gwt.sample.bikeshed.tree.client;

+import com.google.gwt.bikeshed.list.shared.SelectionModel.DefaultSelectionModel;
 import com.google.gwt.bikeshed.tree.client.SideBySideTreeView;
 import com.google.gwt.bikeshed.tree.client.StandardTreeView;
 import com.google.gwt.core.client.EntryPoint;
 import com.google.gwt.user.client.ui.HTML;
+import com.google.gwt.user.client.ui.Label;
 import com.google.gwt.user.client.ui.RootPanel;

+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
 /**
  * A demo of the asynchronous Tree model.
  */
 public class TreeSample implements EntryPoint {

+  class MySelectionModel extends DefaultSelectionModel<Object> {
+
+    private Label label;
+    private Set<Object> selectedSet = new TreeSet<Object>();
+
+    public MySelectionModel(Label label) {
+      this.label = label;
+    }
+
+    public boolean isSelected(Object object) {
+      return selectedSet.contains(object);
+    }
+
+    public void setSelected(Object object, boolean selected) {
+      setSelectedHelper(object, selected);
+      label.setText("Selected " + selectedSet.toString());
+    }
+
+    public void setSelected(List<Object> objects, boolean selected) {
+      for (Object object : objects) {
+        setSelectedHelper(object, selected);
+      }
+      label.setText("Selected " + selectedSet.toString());
+    }
+
+    public void setSelectedHelper(Object object, boolean selected) {
+      if (selected) {
+        selectedSet.add(object);
+      } else {
+        selectedSet.remove(object);
+      }
+    }
+  }
+
   public void onModuleLoad() {
- StandardTreeView tree = new StandardTreeView(new MyTreeViewModel(), "...");
-    tree.setAnimationEnabled(true);
-    RootPanel.get().add(tree);
-
+    Label label1 = new Label();
+    MySelectionModel selectionModel1 = new MySelectionModel(label1);
+
+    StandardTreeView stree = new StandardTreeView(new MyTreeViewModel(
+        selectionModel1), "...");
+    stree.setSelectionModel(selectionModel1);
+    stree.setAnimationEnabled(true);
+
+    RootPanel.get().add(stree);
     RootPanel.get().add(new HTML("<hr>"));
-
- SideBySideTreeView sstree = new SideBySideTreeView(new MyTreeViewModel(), "...", 100, 4);
+    RootPanel.get().add(label1);
+    RootPanel.get().add(new HTML("<hr>"));
+
+    Label label2 = new Label();
+    MySelectionModel selectionModel2 = new MySelectionModel(label2);
+    SideBySideTreeView sstree = new SideBySideTreeView(new MyTreeViewModel(
+        selectionModel2), "...", 100, 4);
+    sstree.setSelectionModel(selectionModel2);
     sstree.setHeight("200px");
+
     RootPanel.get().add(sstree);
+    RootPanel.get().add(new HTML("<hr>"));
+    RootPanel.get().add(label2);
+    RootPanel.get().add(new HTML("<hr>"));
   }
 }
=======================================
--- /trunk/bikeshed/war/Tree.css        Fri Mar  5 07:05:48 2010
+++ /trunk/bikeshed/war/Tree.css        Thu Apr  1 12:13:45 2010
@@ -1,5 +1,9 @@
 /** Add css rules here for your application. */

+div.gwt-stree-selectedItem {
+  border-style: solid;
+}
+
 div.gwt-sstree-column {
   overflow-y: scroll;
   overflow-x: auto;

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

To unsubscribe, reply using "remove me" as the subject.

Reply via email to