All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.view.client.TreeViewModel Maven / Gradle / Ivy

/*
 * 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.view.client;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.event.shared.HandlerRegistration;

/**
 * A model of a tree.
 */
public interface TreeViewModel {

  /**
   * Default implementation of {@link NodeInfo}.
   */
  class DefaultNodeInfo implements NodeInfo {

    private final Cell cell;
    private final AbstractDataProvider dataProvider;
    private final CellPreviewEvent.Handler selectionEventManager;
    private HandlerRegistration selectionEventManagerReg;
    private final SelectionModel selectionModel;
    private final ValueUpdater valueUpdater;
    private HasData display;

    /**
     * Construct a new {@link DefaultNodeInfo}.
     * 
     * @param dataProvider the {@link AbstractDataProvider} that provides the
     *          child values
     * @param cell the {@link Cell} used to render the child values
     */
    public DefaultNodeInfo(AbstractDataProvider dataProvider, Cell cell) {
      this(dataProvider, cell, null, null);
    }

    /**
     * Construct a new {@link DefaultNodeInfo}.
     * 
     * @param dataProvider the {@link AbstractDataProvider} that provides the
     *          child values
     * @param cell the {@link Cell} used to render the child values update when
     *          the selection changes
     * @param selectionModel the {@link SelectionModel} used for selection
     * @param valueUpdater the {@link ValueUpdater}
     */
    public DefaultNodeInfo(AbstractDataProvider dataProvider,
        final Cell cell, SelectionModel selectionModel,
        final ValueUpdater valueUpdater) {
      this(dataProvider, cell, selectionModel,
          DefaultSelectionEventManager. createDefaultManager(), valueUpdater);
    }

    /**
     * Construct a new {@link DefaultNodeInfo}.
     * 
     * @param dataProvider the {@link AbstractDataProvider} that provides the
     *          child values
     * @param cell the {@link Cell} used to render the child values update when
     *          the selection changes
     * @param selectionModel the {@link SelectionModel} used for selection
     * @param selectionEventManager the {@link CellPreviewEvent.Handler} that
     *          handles user selection interaction
     * @param valueUpdater the {@link ValueUpdater}
     */
    public DefaultNodeInfo(AbstractDataProvider dataProvider,
        final Cell cell, SelectionModel selectionModel,
        CellPreviewEvent.Handler selectionEventManager,
        final ValueUpdater valueUpdater) {
      this.dataProvider = dataProvider;
      this.cell = cell;
      this.selectionModel = selectionModel;
      this.valueUpdater = valueUpdater;
      this.selectionEventManager = selectionEventManager;
    }

    public Cell getCell() {
      return cell;
    }

    public ProvidesKey getProvidesKey() {
      return dataProvider;
    }

    public SelectionModel getSelectionModel() {
      return selectionModel;
    }

    public ValueUpdater getValueUpdater() {
      return valueUpdater;
    }

    public void setDataDisplay(HasData display) {
      this.display = display;
      if (selectionEventManager != null) {
        selectionEventManagerReg = display.addCellPreviewHandler(selectionEventManager);
      }
      dataProvider.addDataDisplay(display);
    }

    public void unsetDataDisplay() {
      if (display != null) {
        dataProvider.removeDataDisplay(display);
        if (selectionEventManagerReg != null) {
          selectionEventManagerReg.removeHandler();
          selectionEventManagerReg = null;
        }
        display = null;
      }
    }
  }

  /**
   * The info needed to create the children of a tree node.
   */
  interface NodeInfo {

    /**
     * Get the {@link Cell} used to render the children of this node.
     * 
     * @return the {@link Cell}
     */
    Cell getCell();

    /**
     * Return the key provider for children of this node.
     * 
     * @return the {@link ProvidesKey}
     */
    ProvidesKey getProvidesKey();

    /**
     * Get the {@link SelectionModel} used for the children of this node. To
     * unify selection across all items of the same type, or across the entire
     * tree, return the same instance of {@link SelectionModel} from all
     * {@link NodeInfo}.
     * 
     * @return the {@link SelectionModel}
     */
    SelectionModel getSelectionModel();

    /**
     * Get the value updater associated with the cell.
     * 
     * @return the value updater
     */
    ValueUpdater getValueUpdater();

    /**
     * Set the display that is listening to this {@link NodeInfo}. The
     * implementation should attach the display to the source of data.
     * 
     * @param display the {@link HasData}
     */
    void setDataDisplay(HasData display);

    /**
     * Unset the display from the {@link NodeInfo}. The implementation should
     * detach the display from the source of data.
     */
    void unsetDataDisplay();
  }

  /**
   * Get the {@link NodeInfo} that will provide the {@link ProvidesKey},
   * {@link Cell}, and {@link HasData} instances to retrieve and display the
   * children of the specified value.
   * 
   * @param value the value in the parent node
   * @return the {@link NodeInfo}
   */
   NodeInfo getNodeInfo(T value);

  /**
   * Check if the value is known to be a leaf node.
   * 
   * @param value the value at the node
   * 
   * @return true if the node is known to be a leaf node, false otherwise
   */
  boolean isLeaf(Object value);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy