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

com.extjs.gxt.ui.client.data.TreeBeanModelReader Maven / Gradle / Ivy

There is a newer version: 2.3.1-gwt22
Show newest version
/*
 * Sencha GXT 2.3.1a - Sencha for GWT
 * Copyright(c) 2007-2013, Sencha, Inc.
 * [email protected]
 * 
 * http://www.sencha.com/products/gxt/license/
 */
 package com.extjs.gxt.ui.client.data;

import java.util.ArrayList;
import java.util.List;

/**
 * A DataReader for beans using a BeanModelFactory.
 * Valid return types are a list of beans . Beans must be of the same type and
 * beans must implement {@link BeanModelTag} or have a companion
 * {@link BeanModelMarker} marker class.
 */
public class TreeBeanModelReader implements DataReader> {

  private boolean factoryForEachBean;

  /**
   * Return if a BeanModelFactory is created for each bean or not.
   * 
   * @return true if a BeanModelFactory is created for each bean or not
   */
  public boolean isFactoryForEachBean() {
    return factoryForEachBean;
  }

  @SuppressWarnings({"unchecked", "rawtypes"})
  public List read(Object loadConfig, Object data) {
    if (data instanceof List) {
      List beans = (List) data;
      if (beans.size() > 0) {
        if (factoryForEachBean) {
          List models = new ArrayList(beans.size());
          for (Object o : beans) {
            BeanModelFactory factory = BeanModelLookup.get().getFactory(o.getClass());
            assert factory != null : "No BeanModelFactory found for " + o.getClass();
            models.add(factory.createModel(o));
          }
          return models;
        } else {
          BeanModelFactory factory = BeanModelLookup.get().getFactory(beans.get(0).getClass());
          assert factory != null : "No BeanModelFactory found for " + beans.get(0).getClass();
          return (List) factory.createModel(beans);
        }
      }
      return (List) beans;

    }

    assert false : "Error converting data";

    return null;
  }

  /**
   * Set to true to create a BeanModelFactory for each bean in the list
   * (defaults to false).
   * 
   * @param factoryForEachBean true to enable
   */
  public void setFactoryForEachBean(boolean factoryForEachBean) {
    this.factoryForEachBean = factoryForEachBean;
  }

}