/**
 * Generated by Apache Royale Compiler from org\apache\royale\html\beads\DynamicItemsRendererFactoryForArrayListData.as
 * org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData
 *
 * @fileoverview
 *
 * @suppress {checkTypes|accessControls}
 */

goog.provide('org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData');

goog.require('org.apache.royale.collections.IArrayList');
goog.require('org.apache.royale.core.IDataProviderModel');
goog.require('org.apache.royale.core.IItemRendererClassFactory');
goog.require('org.apache.royale.core.IItemRendererParent');
goog.require('org.apache.royale.core.IListPresentationModel');
goog.require('org.apache.royale.core.ISelectableItemRenderer');
goog.require('org.apache.royale.core.ISelectionModel');
goog.require('org.apache.royale.core.IStrand');
goog.require('org.apache.royale.core.SimpleCSSStyles');
goog.require('org.apache.royale.core.UIBase');
goog.require('org.apache.royale.events.CollectionEvent');
goog.require('org.apache.royale.events.Event');
goog.require('org.apache.royale.events.EventDispatcher');
goog.require('org.apache.royale.events.IEventDispatcher');
goog.require('org.apache.royale.html.beads.IListView');
goog.require('org.apache.royale.utils.loadBeadFromValuesManager');
goog.require('org.apache.royale.core.IBead');
goog.require('org.apache.royale.core.IDataProviderItemRendererMapper');
goog.require('org.apache.royale.utils.Language');



/**
 * @constructor
 * @extends {org.apache.royale.events.EventDispatcher}
 * @implements {org.apache.royale.core.IBead}
 * @implements {org.apache.royale.core.IDataProviderItemRendererMapper}
 * @param {Object=} target
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData = function(target) {
  target = typeof target !== 'undefined' ? target : null;
  org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.base(this, 'constructor', target);
};
goog.inherits(org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData, org.apache.royale.events.EventDispatcher);


/**
 * Prevent renaming of class. Needed for reflection.
 */
goog.exportSymbol('org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData', org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData);


/**
 * @protected
 * @type {string}
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.labelField;


/**
 * @protected
 * @type {org.apache.royale.core.IStrand}
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype._strand;


/**
 *  finish setup
 *
 *  @langversion 3.0
 *  @playerversion Flash 10.2
 *  @playerversion AIR 2.6
 *  @productversion Royale 0.8
 * @protected
 * @param {org.apache.royale.events.Event} event
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.initComplete = function(event) {
  org.apache.royale.utils.Language.as(this._strand, org.apache.royale.events.IEventDispatcher, true).removeEventListener("initComplete", org.apache.royale.utils.Language.closure(this.initComplete, this, 'initComplete'));
  this._dataProviderModel = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.core.ISelectionModel), org.apache.royale.core.ISelectionModel);
  var /** @type {org.apache.royale.html.beads.IListView} */ listView = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.html.beads.IListView), org.apache.royale.html.beads.IListView);
  this.dataGroup = listView.dataGroup;
  this.dataProviderModel.addEventListener("dataProviderChanged", org.apache.royale.utils.Language.closure(this.dataProviderChangeHandler, this, 'dataProviderChangeHandler'));
  this.labelField = this.dataProviderModel.labelField;
  this.dataProviderChangeHandler(null);
};


/**
 * @protected
 * @type {org.apache.royale.core.ISelectionModel}
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype._dataProviderModel;


/**
 * @private
 * @type {org.apache.royale.core.IItemRendererClassFactory}
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.org_apache_royale_html_beads_DynamicItemsRendererFactoryForArrayListData__itemRendererFactory;


/**
 * @protected
 * @type {org.apache.royale.core.IItemRendererParent}
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.dataGroup;


/**
 * @asprivate
 * @protected
 * @param {org.apache.royale.events.Event} event
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.dataProviderChangeHandler = function(event) {
  var /** @type {org.apache.royale.collections.IArrayList} */ dp = org.apache.royale.utils.Language.as(this.dataProviderModel.dataProvider, org.apache.royale.collections.IArrayList);
  if (!dp)
    return;
  org.apache.royale.utils.Language.as(dp, org.apache.royale.events.IEventDispatcher).addEventListener(org.apache.royale.events.CollectionEvent.ITEM_ADDED, org.apache.royale.utils.Language.closure(this.itemAddedHandler, this, 'itemAddedHandler'));
  this.dataGroup.removeAllItemRenderers();
  var /** @type {org.apache.royale.core.IListPresentationModel} */ presentationModel = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.core.IListPresentationModel), org.apache.royale.core.IListPresentationModel);
  var /** @type {number} */ n = dp.length;
  for (var /** @type {number} */ i = 0; i < n; i++) {
    var /** @type {org.apache.royale.core.ISelectableItemRenderer} */ ir = org.apache.royale.utils.Language.as(this.itemRendererFactory.createItemRenderer(this.dataGroup), org.apache.royale.core.ISelectableItemRenderer);
    var /** @type {Object} */ item = dp.getItemAt(i);
    this.fillRenderer(i, item, ir, presentationModel);
  }
  org.apache.royale.utils.Language.as(this._strand, org.apache.royale.events.IEventDispatcher, true).dispatchEvent(new org.apache.royale.events.Event("itemsCreated"));
};


/**
 * @asprivate
 * @protected
 * @param {org.apache.royale.events.CollectionEvent} event
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.itemAddedHandler = function(event) {
  var /** @type {org.apache.royale.collections.IArrayList} */ dp = org.apache.royale.utils.Language.as(this.dataProviderModel.dataProvider, org.apache.royale.collections.IArrayList);
  if (!dp)
    return;
  var /** @type {org.apache.royale.core.IListPresentationModel} */ presentationModel = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.core.IListPresentationModel), org.apache.royale.core.IListPresentationModel);
  var /** @type {org.apache.royale.core.ISelectableItemRenderer} */ ir = org.apache.royale.utils.Language.as(this.itemRendererFactory.createItemRenderer(this.dataGroup), org.apache.royale.core.ISelectableItemRenderer);
  this.fillRenderer(event.index, event.item, ir, presentationModel);
  var /** @type {number} */ n = this.dataGroup.numItemRenderers;
  for (var /** @type {number} */ i = event.index; i < n; i++) {
    ir = org.apache.royale.utils.Language.as(this.dataGroup.getItemRendererAt(i), org.apache.royale.core.ISelectableItemRenderer);
    ir.index = i;
  }
  org.apache.royale.utils.Language.as(this._strand, org.apache.royale.events.IEventDispatcher).dispatchEvent(new org.apache.royale.events.Event("itemsCreated"));
  org.apache.royale.utils.Language.as(this._strand, org.apache.royale.events.IEventDispatcher).dispatchEvent(new org.apache.royale.events.Event("layoutNeeded"));
};


/**
 * @asprivate
 * @protected
 * @param {number} index
 * @param {Object} item
 * @param {org.apache.royale.core.ISelectableItemRenderer} itemRenderer
 * @param {org.apache.royale.core.IListPresentationModel} presentationModel
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.fillRenderer = function(index, item, itemRenderer, presentationModel) {
  this.dataGroup.addItemRendererAt(itemRenderer, index);
  itemRenderer.labelField = this.labelField;
  if (presentationModel) {
    var /** @type {org.apache.royale.core.SimpleCSSStyles} */ style = new org.apache.royale.core.SimpleCSSStyles();
    style.marginBottom = presentationModel.separatorThickness;
    org.apache.royale.utils.Language.as(itemRenderer, org.apache.royale.core.UIBase, true).style = style;
    org.apache.royale.utils.Language.as(itemRenderer, org.apache.royale.core.UIBase, true).height = presentationModel.rowHeight;
    org.apache.royale.utils.Language.as(itemRenderer, org.apache.royale.core.UIBase, true).percentWidth = 100;
  }
  this.setData(itemRenderer, item, index);
};


/**
 * @asprivate
 * @protected
 * @param {org.apache.royale.core.ISelectableItemRenderer} itemRenderer
 * @param {Object} data
 * @param {number} index
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.setData = function(itemRenderer, data, index) {
  itemRenderer.index = index;
  itemRenderer.data = data;
};


org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.set__strand = function(value) {
  this._strand = value;
  org.apache.royale.utils.Language.as(value, org.apache.royale.events.IEventDispatcher, true).addEventListener("initComplete", org.apache.royale.utils.Language.closure(this.initComplete, this, 'initComplete'));
};


org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.get__dataProviderModel = function() {
  return this._dataProviderModel;
};


org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.get__itemRendererFactory = function() {
  if (!this.org_apache_royale_html_beads_DynamicItemsRendererFactoryForArrayListData__itemRendererFactory)
    this.org_apache_royale_html_beads_DynamicItemsRendererFactoryForArrayListData__itemRendererFactory = org.apache.royale.utils.Language.as(org.apache.royale.utils.loadBeadFromValuesManager(org.apache.royale.core.IItemRendererClassFactory, "iItemRendererClassFactory", this._strand), org.apache.royale.core.IItemRendererClassFactory);
  return this.org_apache_royale_html_beads_DynamicItemsRendererFactoryForArrayListData__itemRendererFactory;
};


org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.set__itemRendererFactory = function(value) {
  this.org_apache_royale_html_beads_DynamicItemsRendererFactoryForArrayListData__itemRendererFactory = value;
};


Object.defineProperties(org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype, /** @lends {org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype} */ {
/**
  * @export
  * @type {org.apache.royale.core.IStrand} */
strand: {
set: org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.set__strand},
/**
  * @export
  * @type {org.apache.royale.core.IDataProviderModel} */
dataProviderModel: {
get: org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.get__dataProviderModel},
/**
  * @export
  * @type {org.apache.royale.core.IItemRendererClassFactory} */
itemRendererFactory: {
get: org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.get__itemRendererFactory,
set: org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.set__itemRendererFactory}}
);


/**
 * Metadata
 *
 * @type {Object.<string, Array.<Object>>}
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.ROYALE_CLASS_INFO = { names: [{ name: 'DynamicItemsRendererFactoryForArrayListData', qName: 'org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData', kind: 'class' }], interfaces: [org.apache.royale.core.IBead, org.apache.royale.core.IDataProviderItemRendererMapper] };



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData.prototype.ROYALE_REFLECTION_INFO = function () {
  return {
    variables: function () {return {};},
    accessors: function () {
      return {
        'strand': { type: 'org.apache.royale.core.IStrand', access: 'writeonly', declaredBy: 'org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData'},
        'dataProviderModel': { type: 'org.apache.royale.core.IDataProviderModel', access: 'readonly', declaredBy: 'org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData'},
        'itemRendererFactory': { type: 'org.apache.royale.core.IItemRendererClassFactory', access: 'readwrite', declaredBy: 'org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData'}
      };
    },
    methods: function () {
      return {
        'DynamicItemsRendererFactoryForArrayListData': { type: '', declaredBy: 'org.apache.royale.html.beads.DynamicItemsRendererFactoryForArrayListData', parameters: function () { return [  { index: 1, type: 'Object', optional: true } ]; }}
      };
    },
    metadata: function () { return [  ]; }
  };
};