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

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

goog.require('org.apache.royale.collections.ICollectionView');
goog.require('org.apache.royale.core.IBeadModel');
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.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.DataItemRendererFactoryForCollectionView = function(target) {
  target = typeof target !== 'undefined' ? target : null;
  org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView.base(this, 'constructor', target);
};
goog.inherits(org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView, org.apache.royale.events.EventDispatcher);


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


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


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


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


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


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


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


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


/**
 * @asprivate
 * @royaleignorecoercion org.apache.royale.collections.ICollectionView
 * @royaleignorecoercion org.apache.royale.core.IListPresentationModel
 * @royaleignorecoercion org.apache.royale.core.ISelectableItemRenderer
 * @royaleignorecoercion org.apache.royale.events.IEventDispatcher
 * @protected
 * @param {org.apache.royale.events.CollectionEvent} event
 */
org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView.prototype.itemAddedHandler = function(event) {
  if (!this.dataProviderModel)
    return;
  var /** @type {Object} */ dp = this.dataProviderModel.dataProvider;
  if (!dp)
    return;
  var /** @type {Object} */ presentationModel = this._strand.getBeadByType(org.apache.royale.core.IListPresentationModel);
  var /** @type {Object} */ ir = this.itemRendererFactory.createItemRenderer(this.dataGroup);
  this.labelField = this.dataProviderModel.labelField;
  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 = this.dataGroup.getItemRendererAt(i);
    ir.index = i;
  }
  this._strand.dispatchEvent(new org.apache.royale.events.Event("itemsCreated"));
  this._strand.dispatchEvent(new org.apache.royale.events.Event("layoutNeeded"));
};


/**
 * @asprivate
 * @royaleignorecoercion org.apache.royale.collections.ICollectionView
 * @royaleignorecoercion org.apache.royale.core.IListPresentationModel
 * @royaleignorecoercion org.apache.royale.core.ISelectableItemRenderer
 * @royaleignorecoercion org.apache.royale.events.IEventDispatcher
 * @protected
 * @param {org.apache.royale.events.CollectionEvent} event
 */
org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView.prototype.itemRemovedHandler = function(event) {
  if (!this.dataProviderModel)
    return;
  var /** @type {Object} */ dp = this.dataProviderModel.dataProvider;
  if (!dp)
    return;
  var /** @type {Object} */ ir = this.dataGroup.getItemRendererAt(event.index);
  this.dataGroup.removeItemRenderer(ir);
  var /** @type {number} */ n = this.dataGroup.numItemRenderers;
  for (var /** @type {number} */ i = event.index; i < n; i++) {
    ir = this.dataGroup.getItemRendererAt(i);
    ir.index = i;
  }
  this._strand.dispatchEvent(new org.apache.royale.events.Event("layoutNeeded"));
};


/**
 * @asprivate
 * @royaleignorecoercion org.apache.royale.collections.ICollectionView
 * @royaleignorecoercion org.apache.royale.core.ISelectableItemRenderer
 * @protected
 * @param {org.apache.royale.events.CollectionEvent} event
 */
org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView.prototype.itemUpdatedHandler = function(event) {
  if (!this.dataProviderModel)
    return;
  var /** @type {Object} */ dp = this.dataProviderModel.dataProvider;
  if (!dp)
    return;
  var /** @type {Object} */ ir = this.dataGroup.getItemRendererAt(event.index);
  this.setData(ir, event.item, event.index);
};


/**
 * @asprivate
 * @royaleignorecoercion org.apache.royale.core.UIBase
 * @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.DataItemRendererFactoryForCollectionView.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;
    itemRenderer.style = style;
    itemRenderer.height = presentationModel.rowHeight;
    itemRenderer.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.DataItemRendererFactoryForCollectionView.prototype.setData = function(itemRenderer, data, index) {
  itemRenderer.index = index;
  itemRenderer.data = data;
};


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


org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView.prototype.get__dataProviderModel = function() {
  if (this._dataProviderModel == null) {
    this._dataProviderModel = this._strand.getBeadByType(org.apache.royale.core.IBeadModel);
  }
  return this._dataProviderModel;
};


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


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


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


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



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView.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.DataItemRendererFactoryForCollectionView'},
        'dataProviderModel': { type: 'org.apache.royale.core.IDataProviderModel', access: 'readonly', declaredBy: 'org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView'},
        'itemRendererFactory': { type: 'org.apache.royale.core.IItemRendererClassFactory', access: 'readwrite', declaredBy: 'org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView'}
      };
    },
    methods: function () {
      return {
        'DataItemRendererFactoryForCollectionView': { type: '', declaredBy: 'org.apache.royale.html.beads.DataItemRendererFactoryForCollectionView', parameters: function () { return [  { index: 1, type: 'Object', optional: true } ]; }}
      };
    }
  };
};