From 0efe2d3e9e02ada6fe297af823b90fa967de85df Mon Sep 17 00:00:00 2001
From: xue <>
Date: Fri, 3 Feb 2006 06:11:57 +0000
Subject: Added TCheckBoxColumn.

---
 .gitattributes                                     |   1 +
 framework/Web/UI/WebControls/TBoundColumn.php      |   2 +-
 framework/Web/UI/WebControls/TCheckBoxColumn.php   | 109 +++++++++++++++++++
 framework/Web/UI/WebControls/TDataGrid.php         | 117 +++++++++++++++------
 framework/Web/UI/WebControls/TDataGridColumn.php   |  15 +--
 .../Web/UI/WebControls/TEditCommandColumn.php      |  12 +--
 framework/Web/UI/WebControls/TStyle.php            |  28 +++--
 7 files changed, 234 insertions(+), 50 deletions(-)
 create mode 100644 framework/Web/UI/WebControls/TCheckBoxColumn.php

diff --git a/.gitattributes b/.gitattributes
index 0eede8ab..0072ea5e 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -642,6 +642,7 @@ framework/Web/UI/WebControls/TBulletedList.php -text
 framework/Web/UI/WebControls/TButton.php -text
 framework/Web/UI/WebControls/TButtonColumn.php -text
 framework/Web/UI/WebControls/TCheckBox.php -text
+framework/Web/UI/WebControls/TCheckBoxColumn.php -text
 framework/Web/UI/WebControls/TCheckBoxList.php -text
 framework/Web/UI/WebControls/TCompareValidator.php -text
 framework/Web/UI/WebControls/TContent.php -text
diff --git a/framework/Web/UI/WebControls/TBoundColumn.php b/framework/Web/UI/WebControls/TBoundColumn.php
index e9dc0412..c30d3ff6 100644
--- a/framework/Web/UI/WebControls/TBoundColumn.php
+++ b/framework/Web/UI/WebControls/TBoundColumn.php
@@ -127,7 +127,7 @@ class TBoundColumn extends TDataGridColumn
 		if(($field=$this->getDataField())!=='')
 			$value=$this->formatDataValue($formatString,$this->getDataFieldValue($data,$field));
 		else
-			$value=$this->formatDataValue($data);
+			$value=$this->formatDataValue($formatString,$data);
 		if(($sender instanceof TTableCell) || ($sender instanceof TTextBox))
 			$sender->setText($value);
 	}
diff --git a/framework/Web/UI/WebControls/TCheckBoxColumn.php b/framework/Web/UI/WebControls/TCheckBoxColumn.php
new file mode 100644
index 00000000..4f8c5ff3
--- /dev/null
+++ b/framework/Web/UI/WebControls/TCheckBoxColumn.php
@@ -0,0 +1,109 @@
+<?php
+/**
+ * TCheckBoxColumn class file
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Revision: $  $Date: $
+ * @package System.Web.UI.WebControls
+ */
+
+/**
+ * TDataGridColumn class file
+ */
+Prado::using('System.Web.UI.WebControls.TDataGridColumn');
+
+/**
+ * TCheckBoxColumn class
+ *
+ * TCheckBoxColumn represents a column that is bound to a field in a data source.
+ * The cells in the column will be displayed using the data indexed by
+ * <b>DataField</b>. You can customize the display by setting <b>DataFormatString</b>.
+ *
+ * If <b>ReadOnly</b> is false, TCheckBoxColumn will display cells in edit mode
+ * with textboxes. Otherwise, a static text is displayed.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $  $Date: $
+ * @package System.Web.UI.WebControls
+ * @since 3.0
+ */
+class TCheckBoxColumn extends TDataGridColumn
+{
+	/**
+	 * @return string the field name from the data source to bind to the column
+	 */
+	public function getDataField()
+	{
+		return $this->getViewState('DataField','');
+	}
+
+	/**
+	 * @param string the field name from the data source to bind to the column
+	 */
+	public function setDataField($value)
+	{
+		$this->setViewState('DataField',$value,'');
+		$this->onColumnChanged();
+	}
+
+	/**
+	 * @return boolean whether the items in the column can be edited. Defaults to false.
+	 */
+	public function getReadOnly()
+	{
+		return $this->getViewState('ReadOnly',false);
+	}
+
+	/**
+	 * @param boolean whether the items in the column can be edited
+	 */
+	public function setReadOnly($value)
+	{
+		$this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
+		$this->onColumnChanged();
+	}
+
+	/**
+	 * Initializes the specified cell to its initial values.
+	 * This method overrides the parent implementation.
+	 * It creates a textbox for item in edit mode and the column is not read-only.
+	 * Otherwise it displays a static text.
+	 * The caption of the button and the static text are retrieved
+	 * from the datasource.
+	 * @param TTableCell the cell to be initialized.
+	 * @param integer the index to the Columns property that the cell resides in.
+	 * @param string the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
+	 */
+	public function initializeCell($cell,$columnIndex,$itemType)
+	{
+		parent::initializeCell($cell,$columnIndex,$itemType);
+		if($itemType==='EditItem' || $itemType==='Item'
+				|| $itemType==='AlternatingItem' || $itemType==='SelectedItem')
+		{
+			$checkBox=Prado::createComponent('System.Web.UI.WebControls.TCheckBox');
+			if($this->getReadOnly() || $itemType!=='EditItem')
+				$checkBox->setEnabled(false);
+			$cell->setHorizontalAlign('Center');
+			$cell->getControls()->add($checkBox);
+			if(($dataField=$this->getDataField())!=='')
+				$checkBox->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
+		}
+	}
+
+	public function dataBindColumn($sender,$param)
+	{
+		$item=$sender->getNamingContainer();
+		$data=$item->getDataItem();
+		if(($field=$this->getDataField())!=='')
+			$value=TPropertyValue::ensureBoolean($this->getDataFieldValue($data,$field));
+		else
+			$value=TPropertyValue::ensureBoolean($data);
+		if($sender instanceof TCheckBox)
+			$sender->setChecked($value);
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/framework/Web/UI/WebControls/TDataGrid.php b/framework/Web/UI/WebControls/TDataGrid.php
index 2636bd05..81fe2ce1 100644
--- a/framework/Web/UI/WebControls/TDataGrid.php
+++ b/framework/Web/UI/WebControls/TDataGrid.php
@@ -722,6 +722,8 @@ class TDataGrid extends TBaseDataList
 	public function saveState()
 	{
 		parent::saveState();
+		if(!$this->getEnableViewState(true))
+			return;
 		if($this->_items)
 			$this->setViewState('ItemCount',$this->_items->getCount(),0);
 		else
@@ -731,10 +733,19 @@ class TDataGrid extends TBaseDataList
 			$state=array();
 			foreach($this->_autoColumns as $column)
 				$state[]=$column->saveState();
-			$this->setViewState('ColumnState',$state,array());
+			$this->setViewState('AutoColumns',$state,array());
 		}
 		else
-			$this->clearViewState('ColumnState');
+			$this->clearViewState('AutoColumns');
+		if($this->_columns)
+		{
+			$state=array();
+			foreach($this->_columns as $column)
+				$state[]=$column->saveState();
+			$this->setViewState('Columns',$state,array());
+		}
+		else
+			$this->clearViewState('Columns');
 	}
 
 	/**
@@ -744,15 +755,32 @@ class TDataGrid extends TBaseDataList
 	public function loadState()
 	{
 		parent::loadState();
+		if(!$this->getEnableViewState(true))
+			return;
 		if(!$this->getIsDataBound())
 		{
-			$state=$this->getViewState('ColumnState',array());
-			$columns=$this->getAutoColumns();
-			foreach($state as $st)
+			$state=$this->getViewState('AutoColumns',array());
+			if(!empty($state))
 			{
-				$column=new TBoundColumn;
-				$column->loadState($st);
-				$columns->add($column);
+				$this->_autoColumns=new TDataGridColumnCollection;
+				foreach($state as $st)
+				{
+					$column=new TBoundColumn;
+					$column->loadState($st);
+					$this->_autoColumns->add($column);
+				}
+			}
+			else
+				$this->_autoColumns=null;
+			$state=$this->getViewState('Columns',array());
+			if($this->_columns)
+			{
+				$i=0;
+				foreach($this->_columns as $column)
+				{
+					$column->loadState($state[$i]);
+					$i++;
+				}
 			}
 			$this->restoreGridFromViewState();
 		}
@@ -787,7 +815,8 @@ class TDataGrid extends TBaseDataList
 		if(($itemCount=$this->getViewState('ItemCount',0))<=0)
 			return;
 		$this->_pagedDataSource=$ds=$this->createPagedDataSource();
-		if($ds->getAllowCustomPaging())
+		$allowPaging=$ds->getAllowPaging();
+		if($allowPaging)
 			$ds->setDataSource(new TDummyDataSource($itemCount));
 		else
 			$ds->setDataSource(new TDummyDataSource($this->getViewState('DataSourceCount',0)));
@@ -846,8 +875,11 @@ class TDataGrid extends TBaseDataList
 			throw new TInvalidDataValueException('datagrid_currentpageindex_invalid');
 		// get all columns
 		$columns=new TList($this->getColumns());
-		$autoColumns=$this->createAutoColumns($ds);
-		$columns->mergeWith($autoColumns);
+		if($this->getAutoGenerateColumns())
+		{
+			$autoColumns=$this->createAutoColumns($ds);
+			$columns->mergeWith($autoColumns);
+		}
 
 		$items=$this->getItems();
 
@@ -888,9 +920,9 @@ class TDataGrid extends TBaseDataList
 		}
 		else
 		{
-			$this->setViewState('ItemCount',$index,0);
-			$this->setViewState('PageCount',0,0);
-			$this->setViewState('DataSourceCount',0,0);
+			$this->clearViewState('ItemCount');
+			$this->clearViewState('PageCount');
+			$this->clearViewState('DataSourceCount');
 		}
 		$this->_pagedDataSource=null;
 	}
@@ -1134,8 +1166,9 @@ class TDataGrid extends TBaseDataList
 		if($itemStyle!==null)
 		{
 			if($alternatingItemStyle===null)
-				$alternatingItemStyle=new TTableItemStyle;
-			$alternatingItemStyle->mergeWith($itemStyle);
+				$alternatingItemStyle=$itemStyle;
+			else
+				$alternatingItemStyle->mergeWith($itemStyle);
 		}
 		$selectedItemStyle=$this->getViewState('SelectedItemStyle',null);
 		if($alternatingItemStyle!==null)
@@ -1152,62 +1185,86 @@ class TDataGrid extends TBaseDataList
 			$editItemStyle->mergeWith($selectedItemStyle);
 		}
 
-		foreach($this->getControls() as $index=>$control)
+		foreach($this->getControls() as $index=>$item)
 		{
-			switch($control->getItemType())
+			$itemType=$item->getItemType();
+			switch($itemType)
 			{
 				case 'Header':
 					if($headerStyle)
-						$control->getStyle()->mergeWith($headerStyle);
+						$item->getStyle()->mergeWith($headerStyle);
 					if(!$this->getShowHeader())
-						$control->setVisible(false);
+						$item->setVisible(false);
 					break;
 				case 'Footer':
 					if($footerStyle)
-						$control->getStyle()->mergeWith($footerStyle);
+						$item->getStyle()->mergeWith($footerStyle);
 					if(!$this->getShowFooter())
-						$control->setVisible(false);
+						$item->setVisible(false);
 					break;
 				case 'Separator':
 					if($separatorStyle)
-						$control->getStyle()->mergeWith($separatorStyle);
+						$item->getStyle()->mergeWith($separatorStyle);
 					break;
 				case 'Item':
 					if($itemStyle)
-						$control->getStyle()->mergeWith($itemStyle);
+						$item->getStyle()->mergeWith($itemStyle);
 					break;
 				case 'AlternatingItem':
 					if($alternatingItemStyle)
-						$control->getStyle()->mergeWith($alternatingItemStyle);
+						$item->getStyle()->mergeWith($alternatingItemStyle);
 					break;
 				case 'SelectedItem':
 					if($selectedItemStyle)
-						$control->getStyle()->mergeWith($selectedItemStyle);
+						$item->getStyle()->mergeWith($selectedItemStyle);
 					break;
 				case 'EditItem':
 					if($editItemStyle)
-						$control->getStyle()->mergeWith($editItemStyle);
+						$item->getStyle()->mergeWith($editItemStyle);
 					break;
 				case 'Pager':
 					if($pagerStyle)
 					{
-						$control->getStyle()->mergeWith($pagerStyle);
+						$item->getStyle()->mergeWith($pagerStyle);
 						$mode=$pagerStyle->getMode();
 						if($index===0)
 						{
 							if($mode==='Bottom')
-								$control->setVisible(false);
+								$item->setVisible(false);
 						}
 						else
 						{
 							if($mode==='Top')
-								$control->setVisible(false);
+								$item->setVisible(false);
 						}
 					}
 					break;
 				default:
 					break;
 			}
+			if($this->_columns && $itemType!=='Pager')
+			{
+				$n=$this->_columns->getCount();
+				$cells=$item->getCells();
+				for($i=0;$i<$n;++$i)
+				{
+					$cell=$cells->itemAt($i);
+					$column=$this->_columns->itemAt($i);
+					if(!$column->getVisible())
+						$cell->setVisible(false);
+					else
+					{
+						if($itemType==='Header')
+							$style=$column->getHeaderStyle(false);
+						else if($itemType==='Footer')
+							$style=$column->getFooterStyle(false);
+						else
+							$style=$column->getItemStyle(false);
+						if($style!==null)
+							$cell->getStyle()->mergeWith($style);
+					}
+				}
+			}
 		}
 	}
 
diff --git a/framework/Web/UI/WebControls/TDataGridColumn.php b/framework/Web/UI/WebControls/TDataGridColumn.php
index a48efa1d..a5e939ef 100644
--- a/framework/Web/UI/WebControls/TDataGridColumn.php
+++ b/framework/Web/UI/WebControls/TDataGridColumn.php
@@ -79,11 +79,12 @@ abstract class TDataGridColumn extends TComponent
 	}
 
 	/**
+	 * @param boolean whether to create a style if previously not existing
 	 * @return TTableItemStyle the style for header
 	 */
-	public function getHeaderStyle()
+	public function getHeaderStyle($createStyle=true)
 	{
-		if(($style=$this->getViewState('HeaderStyle',null))===null)
+		if(($style=$this->getViewState('HeaderStyle',null))===null && $createStyle)
 		{
 			$style=new TTableItemStyle;
 			$this->setViewState('HeaderStyle',$style,null);
@@ -109,11 +110,12 @@ abstract class TDataGridColumn extends TComponent
 	}
 
 	/**
+	 * @param boolean whether to create a style if previously not existing
 	 * @return TTableItemStyle the style for footer
 	 */
-	public function getFooterStyle()
+	public function getFooterStyle($createStyle=true)
 	{
-		if(($style=$this->getViewState('FooterStyle',null))===null)
+		if(($style=$this->getViewState('FooterStyle',null))===null && $createStyle)
 		{
 			$style=new TTableItemStyle;
 			$this->setViewState('FooterStyle',$style,null);
@@ -122,11 +124,12 @@ abstract class TDataGridColumn extends TComponent
 	}
 
 	/**
+	 * @param boolean whether to create a style if previously not existing
 	 * @return TTableItemStyle the style for item
 	 */
-	public function getItemStyle()
+	public function getItemStyle($createStyle=true)
 	{
-		if(($style=$this->getViewState('ItemStyle',null))===null)
+		if(($style=$this->getViewState('ItemStyle',null))===null && $createStyle)
 		{
 			$style=new TTableItemStyle;
 			$this->setViewState('ItemStyle',$style,null);
diff --git a/framework/Web/UI/WebControls/TEditCommandColumn.php b/framework/Web/UI/WebControls/TEditCommandColumn.php
index a51b703f..b0814a1a 100644
--- a/framework/Web/UI/WebControls/TEditCommandColumn.php
+++ b/framework/Web/UI/WebControls/TEditCommandColumn.php
@@ -62,7 +62,7 @@ class TEditCommandColumn extends TDataGridColumn
 	 */
 	public function getEditText()
 	{
-		return $this->getViewState('EditText','');
+		return $this->getViewState('EditText','Edit');
 	}
 
 	/**
@@ -70,7 +70,7 @@ class TEditCommandColumn extends TDataGridColumn
 	 */
 	public function setEditText($value)
 	{
-		$this->setViewState('EditText',$value,'');
+		$this->setViewState('EditText',$value,'Edit');
 		$this->onColumnChanged();
 	}
 
@@ -79,7 +79,7 @@ class TEditCommandColumn extends TDataGridColumn
 	 */
 	public function getUpdateText()
 	{
-		return $this->getViewState('UpdateText','');
+		return $this->getViewState('UpdateText','Update');
 	}
 
 	/**
@@ -87,7 +87,7 @@ class TEditCommandColumn extends TDataGridColumn
 	 */
 	public function setUpdateText($value)
 	{
-		$this->setViewState('UpdateText',$value,'');
+		$this->setViewState('UpdateText',$value,'Update');
 		$this->onColumnChanged();
 	}
 
@@ -96,7 +96,7 @@ class TEditCommandColumn extends TDataGridColumn
 	 */
 	public function getCancelText()
 	{
-		return $this->getViewState('CancelText','');
+		return $this->getViewState('CancelText','Cancel');
 	}
 
 	/**
@@ -104,7 +104,7 @@ class TEditCommandColumn extends TDataGridColumn
 	 */
 	public function setCancelText($value)
 	{
-		$this->setViewState('CancelText',$value,'');
+		$this->setViewState('CancelText',$value,'Cancel');
 		$this->onColumnChanged();
 	}
 
diff --git a/framework/Web/UI/WebControls/TStyle.php b/framework/Web/UI/WebControls/TStyle.php
index fd92df8a..fa3bcb28 100644
--- a/framework/Web/UI/WebControls/TStyle.php
+++ b/framework/Web/UI/WebControls/TStyle.php
@@ -44,6 +44,15 @@ class TStyle extends TComponent
 	 */
 	private $_customStyle=null;
 
+	/**
+	 * Constructor.
+	 * @param TStyle style to copy from
+	 */
+	public function __construct($style=null)
+	{
+		$this->copyFrom($style);
+	}
+
 	/**
 	 * @return string the background color of the control
 	 */
@@ -260,7 +269,6 @@ class TStyle extends TComponent
 	 */
 	public function reset()
 	{
-		parent::reset();
 		$this->_fields=array();
 		$this->_font=null;
 		$this->_class=null;
@@ -274,9 +282,9 @@ class TStyle extends TComponent
 	 */
 	public function copyFrom($style)
 	{
-		if($style!==null)
+		$this->reset();
+		if($style instanceof TStyle)
 		{
-			$this->reset();
 			$this->_fields=$style->_fields;
 			$this->_class=$style->_class;
 			$this->_customStyle=$style->_customStyle;
@@ -295,9 +303,12 @@ class TStyle extends TComponent
 	{
 		if($style!==null)
 		{
+			//$this->_fields=array_merge($this->_fields,$style->_fields);
 			$this->_fields=array_merge($style->_fields,$this->_fields);
 			if($this->_class===null)
 				$this->_class=$style->_class;
+			if($this->_customStyle===null)
+				$this->_customStyle=$style->_customStyle;
 			if($this->_font===null && $style->_font!==null)
 				$this->getFont()->mergeWith($style->_font);
 		}
@@ -309,7 +320,7 @@ class TStyle extends TComponent
 	 */
 	public function addAttributesToRender($writer)
 	{
-		if($this->_customStyle!=='')
+		if($this->_customStyle!==null)
 		{
 			foreach(explode(';',$this->_customStyle) as $style)
 			{
@@ -594,9 +605,12 @@ class TTableItemStyle extends TStyle
 	public function copyFrom($style)
 	{
 		parent::copyFrom($style);
-		$this->_verticalAlign=$style->_verticalAlign;
-		$this->_horizontalAlign=$style->_horizontalAlign;
-		$this->_wrap=$style->_wrap;
+		if($style instanceof TTableItemStyle)
+		{
+			$this->_verticalAlign=$style->_verticalAlign;
+			$this->_horizontalAlign=$style->_horizontalAlign;
+			$this->_wrap=$style->_wrap;
+		}
 	}
 
 	/**
-- 
cgit v1.2.3