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

goog.provide('org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView');

goog.require('org.apache.royale.collections.ICollectionView');
goog.require('org.apache.royale.core.IBeadModel');
goog.require('org.apache.royale.core.IChild');
goog.require('org.apache.royale.core.IItemRendererClassFactory');
goog.require('org.apache.royale.core.IListPresentationModel');
goog.require('org.apache.royale.core.IParent');
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.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.html.supportClasses.DataItemRenderer');
goog.require('org.apache.royale.jewel.Label');
goog.require('org.apache.royale.jewel.Table');
goog.require('org.apache.royale.jewel.beads.controls.TextAlign');
goog.require('org.apache.royale.jewel.beads.itemRenderers.ITextItemRenderer');
goog.require('org.apache.royale.jewel.beads.models.TableModel');
goog.require('org.apache.royale.jewel.beads.views.TableView');
goog.require('org.apache.royale.jewel.supportClasses.table.TBodyContentArea');
goog.require('org.apache.royale.jewel.supportClasses.table.THead');
goog.require('org.apache.royale.jewel.supportClasses.table.TableColumn');
goog.require('org.apache.royale.jewel.supportClasses.table.TableHeaderCell');
goog.require('org.apache.royale.jewel.supportClasses.table.TableRow');
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.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView = function(target) {
  target = typeof target !== 'undefined' ? target : null;
  org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.base(this, 'constructor', target);
};
goog.inherits(org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView, org.apache.royale.events.EventDispatcher);


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


/**
 * @protected
 * @type {org.apache.royale.core.IStrand}
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype._strand;


/**
 *  finish setup
 *
 *  @langversion 3.0
 *  @playerversion Flash 10.2
 *  @playerversion AIR 2.6
 *  @productversion Royale 0.9.4
 *  @royaleignorecoercion org.apache.royale.events.IEventDispatcher
 *  @royaleignorecoercion org.apache.royale.html.beads.IListView
 * @protected
 * @param {org.apache.royale.events.Event} event
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.initComplete = function(event) {
  this._strand.removeEventListener("initComplete", org.apache.royale.utils.Language.closure(this.initComplete, this, 'initComplete'));
  this.view = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.html.beads.IListView), org.apache.royale.jewel.beads.views.TableView);
  this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView_tbody = org.apache.royale.utils.Language.as(this.view.dataGroup, org.apache.royale.jewel.supportClasses.table.TBodyContentArea);
  this.model = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.core.IBeadModel), org.apache.royale.jewel.beads.models.TableModel);
  this.model.addEventListener("dataProviderChanged", org.apache.royale.utils.Language.closure(this.dataProviderChangeHandler, this, 'dataProviderChangeHandler'));
  this.table = org.apache.royale.utils.Language.as(this._strand, org.apache.royale.jewel.Table);
  this.dataProviderChangeHandler(null);
};


/**
 * @protected
 * @type {string}
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.labelField;


/**
 * @private
 * @type {org.apache.royale.core.IItemRendererClassFactory}
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView__itemRendererFactory;


/**
 * @protected
 * @type {org.apache.royale.jewel.beads.views.TableView}
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.view;


/**
 * @protected
 * @type {org.apache.royale.jewel.beads.models.TableModel}
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.model;


/**
 * @protected
 * @type {org.apache.royale.jewel.Table}
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.table;


/**
 * @private
 * @type {org.apache.royale.jewel.supportClasses.table.TBodyContentArea}
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView_tbody;


/**
 * @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.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.dataProviderChangeHandler = function(event) {
  if (!this.model)
    return;
  var /** @type {Object} */ dp = this.model.dataProvider;
  if (!dp) {
    this.model.selectedIndex = -1;
    this.model.selectedItem = null;
    this.model.selectedItemProperty = null;
    this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView_tbody.removeAllItemRenderers();
    return;
  }
  this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView_tbody.removeAllItemRenderers();
  this.removeElements(this.view.thead);
  this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView_createHeader();
  var /** @type {Object} */ presentationModel = this._strand.getBeadByType(org.apache.royale.core.IListPresentationModel);
  this.labelField = this.model.labelField;
  var /** @type {org.apache.royale.jewel.supportClasses.table.TableColumn} */ column;
  var /** @type {org.apache.royale.jewel.beads.itemRenderers.ITextItemRenderer} */ ir;
  var /** @type {number} */ n = dp.length;
  var /** @type {number} */ index = 0;
  for (var /** @type {number} */ i = 0; i < n; i++) {
    for (var /** @type {number} */ j = 0; j < this.model.columns.length; j++) {
      column = org.apache.royale.utils.Language.as(this.model.columns[j], org.apache.royale.jewel.supportClasses.table.TableColumn);
      if (column.itemRenderer != null) {
        ir = org.apache.royale.utils.Language.as(column.itemRenderer.newInstance(), org.apache.royale.jewel.beads.itemRenderers.ITextItemRenderer);
      } else {
        ir = org.apache.royale.utils.Language.as(this.itemRendererFactory.createItemRenderer(this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView_tbody), org.apache.royale.jewel.beads.itemRenderers.ITextItemRenderer);
      }
      this.labelField = column.dataField;
      var /** @type {Object} */ item = dp.getItemAt(i);
      org.apache.royale.utils.Language.as(ir, org.apache.royale.html.supportClasses.DataItemRenderer).dataField = this.labelField;
      org.apache.royale.utils.Language.as(ir, org.apache.royale.html.supportClasses.DataItemRenderer).rowIndex = i;
      org.apache.royale.utils.Language.as(ir, org.apache.royale.html.supportClasses.DataItemRenderer).columnIndex = j;
      this.fillRenderer(index++, item, ir, presentationModel);
      if (column.align != "") {
        ir.align = column.align;
      }
    }
  }
  this._strand.dispatchEvent(new org.apache.royale.events.Event("itemsCreated"));
  this.table.dispatchEvent(new org.apache.royale.events.Event("layoutNeeded"));
};


/**
 * @export
 * @param {org.apache.royale.core.IParent} container
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.removeElements = function(container) {
  if (container != null) {
    while (container.numElements > 0) {
      var /** @type {org.apache.royale.core.IChild} */ child = container.getElementAt(0);
      container.removeElement(child);
    }
  }
};


/**
 * @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.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.fillRenderer = function(index, item, itemRenderer, presentationModel) {
  this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView_tbody.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.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.setData = function(itemRenderer, data, index) {
  itemRenderer.index = index;
  itemRenderer.data = data;
};


/**
 * @private
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView_createHeader = function() {
  var /** @type {boolean} */ createHeaderRow = false;
  var /** @type {org.apache.royale.jewel.supportClasses.table.TableColumn} */ test;
  var /** @type {number} */ c = 0;
  for (c = 0; c < this.model.columns.length; c++) {
    test = org.apache.royale.utils.Language.as(this.model.columns[c], org.apache.royale.jewel.supportClasses.table.TableColumn);
    if (test.label != null) {
      createHeaderRow = true;
      break;
    }
  }
  if (createHeaderRow) {
    if (this.view.thead == null)
      this.view.thead = new org.apache.royale.jewel.supportClasses.table.THead();
    var /** @type {org.apache.royale.jewel.supportClasses.table.THead} */ thead = this.view.thead;
    var /** @type {org.apache.royale.jewel.supportClasses.table.TableRow} */ headerRow = new org.apache.royale.jewel.supportClasses.table.TableRow();
    for (c = 0; c < this.model.columns.length; c++) {
      test = org.apache.royale.utils.Language.as(this.model.columns[c], org.apache.royale.jewel.supportClasses.table.TableColumn);
      var /** @type {org.apache.royale.jewel.supportClasses.table.TableHeaderCell} */ tableHeader = new org.apache.royale.jewel.supportClasses.table.TableHeaderCell();
      var /** @type {org.apache.royale.jewel.Label} */ label = new org.apache.royale.jewel.Label();
      tableHeader.addElement(label);
      label.text = test.label == null ? "" : test.label;
      var /** @type {org.apache.royale.jewel.beads.controls.TextAlign} */ columnLabelTextAlign = new org.apache.royale.jewel.beads.controls.TextAlign();
      columnLabelTextAlign.align = test.columnLabelAlign;
      label.addBead(columnLabelTextAlign);
      headerRow.addElement(tableHeader);
    }
    thead.addElement(headerRow);
    this.table.addElement(thead);
  }
};


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


org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.get__itemRendererFactory = function() {
  if (!this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView__itemRendererFactory)
    this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView__itemRendererFactory = org.apache.royale.utils.loadBeadFromValuesManager(org.apache.royale.core.IItemRendererClassFactory, "iItemRendererClassFactory", this._strand);
  return this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView__itemRendererFactory;
};


org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.set__itemRendererFactory = function(value) {
  this.org_apache_royale_jewel_beads_itemRenderers_TableItemRendererFactoryForCollectionView__itemRendererFactory = value;
};


Object.defineProperties(org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype, /** @lends {org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype} */ {
/**
  * @export
  * @type {org.apache.royale.core.IStrand} */
strand: {
set: org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.set__strand},
/**
  * @export
  * @type {org.apache.royale.core.IItemRendererClassFactory} */
itemRendererFactory: {
get: org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.get__itemRendererFactory,
set: org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.prototype.set__itemRendererFactory}}
);


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



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView.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.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView'},
        'itemRendererFactory': { type: 'org.apache.royale.core.IItemRendererClassFactory', access: 'readwrite', declaredBy: 'org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView'}
      };
    },
    methods: function () {
      return {
        'TableItemRendererFactoryForCollectionView': { type: '', declaredBy: 'org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView', parameters: function () { return [  { index: 1, type: 'Object', optional: true } ]; }},
        'removeElements': { type: 'void', declaredBy: 'org.apache.royale.jewel.beads.itemRenderers.TableItemRendererFactoryForCollectionView', parameters: function () { return [  { index: 1, type: 'org.apache.royale.core.IParent', optional: false } ]; }}
      };
    }
  };
};