Source for file TableLayout.class.php

Documentation is available at TableLayout.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."GUIManager/Layout.interface.php");
  4. require_once(HARMONI."GUIManager/StyleCollection.class.php");
  5. require_once(HARMONI."GUIManager/StyleProperties/MarginSP.class.php");
  6. require_once(HARMONI."GUIManager/StyleProperties/PaddingSP.class.php");
  7. require_once(HARMONI."GUIManager/StyleProperties/BorderSP.class.php");
  8.  
  9. /**
  10. * The <code>TableLayout</code> renders components sequentially in rows that contain
  11. * the specifed number of columns. They are a replacement for HTML tables in that
  12. * they allow the same effect while rendering their component elements.
  13. *
  14. * Layouts are assigned to Containers and they specify how (in terms of location,
  15. * not appearance) the sub-<code>Components</code> are going to be rendered on the screen.
  16. *
  17. * @package harmoni.gui.layouts
  18. *
  19. * @copyright Copyright &copy; 2005, Middlebury College
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  21. *
  22. * @version $Id: TableLayout.class.php,v 1.5 2007/09/04 20:25:22 adamfranco Exp $
  23. */
  24. class TableLayout
  25. extends LayoutInterface
  26. {
  27.  
  28. /**
  29. * The constructor.
  30. *
  31. * @param integer $numberOfColumns
  32. * @access public
  33. ***/
  34. function TableLayout( $numberOfColumns, $tdStyles = null ) {
  35. ArgumentValidator::validate($numberOfColumns, IntegerValidatorRule::getRule());
  36. $this->_numColumns = $numberOfColumns;
  37. $this->_tdStyles = $tdStyles;
  38. $this->_renderDirection ='Left-Right/Top-Bottom';
  39. $this->printEmptyCells = true;
  40. }
  41. /**
  42. * Set the tdStyles
  43. *
  44. * @param string $tdStyles
  45. * @return void
  46. * @access public
  47. * @since 9/18/06
  48. */
  49. function setTdStyles ($tdStyles) {
  50. $this->_tdStyles = $tdStyles;
  51. }
  52. /**
  53. * Set the direction of component rendering from the default of Left-Right/Top-Bottom.
  54. * Allowed values:
  55. * Left-Right/Top-Bottom
  56. * Top-Bottom/Left-Right
  57. * Right-Left/Top-Bottom
  58. * Top-Bottom/Right-Left
  59. *
  60. * The other possible directions, listed below, are not implemented due to
  61. * lack of utility:
  62. * Left-Right/Bottom-Top
  63. * Bottom-Top/Left-Right
  64. * Right-Left/Bottom-Top
  65. * Bottom-Top/Right-Left
  66. *
  67. * @param string $direction
  68. * @return void
  69. * @access public
  70. * @since 8/18/06
  71. */
  72. function setRenderDirection ($direction) {
  73. ArgumentValidator::validate($direction, ChoiceValidatorRule::getRule(
  74. 'Left-Right/Top-Bottom',
  75. 'Top-Bottom/Left-Right',
  76. 'Right-Left/Top-Bottom',
  77. 'Top-Bottom/Right-Left'));
  78. $this->_renderDirection = $direction;
  79. }
  80.  
  81. /**
  82. * Lays out and renders the given container and its components. The Layout
  83. * object should arrange the <code>Components</code> in a well-defined manner
  84. * and then call the <code>render()</code> methods of each individual component.
  85. * @access public
  86. * @param ref object The container to render.
  87. * @param ref object theme The Theme object to use in producing the result
  88. * of this method.
  89. * @param string tabs This is a string (normally a bunch of tabs) that will be
  90. * prepended to each text line. This argument is optional but its usage is highly
  91. * recommended in order to produce a nicely formatted HTML output.
  92. ***/
  93. function render($container, $theme, $tabs = "") {
  94. // print html for container (a table)
  95. echo $tabs."<table width=\"100%\" border=\"0\" cellpadding=\"0px\" cellspacing=\"0px\">\n";
  96. echo $tabs."\t<tr>\n";
  97. // extra tdStyles (not themed)
  98. if ($this->_tdStyles)
  99. $tdStyles = "style='".$this->_tdStyles."'";
  100. else
  101. $tdStyles = '';
  102. // Get the components
  103. $components =$container->getComponents();
  104. // Get the cell order, a mapping between the cells in the table
  105. // and the original component order, as based on the flow direction
  106. // of this layout, i.e Left-Right/Top-Bottom or Top-Bottom/Left-Right
  107. $cellOrder = $this->_getCellOrder(count($components));
  108. $numElements = count($cellOrder);
  109. // Go through each cell in the table
  110. for ($i = 0; $i < $numElements; $i++) {
  111. $key = $cellOrder[$i];
  112. // if there isn't a component for that index
  113. // and there are more cells than will fit in one row
  114. // render a blank table cell
  115. if (!isset($components[$key])) {
  116. if ($this->printEmptyCells || $numElements > $this->_numColumns) {
  117. echo $tabs."\t<td $tdStyles>\n";
  118. echo $tabs."\t</td>\n";
  119. }
  120. }
  121. // otherwise render the component in a table cell
  122. else {
  123. $component =$components[$key];
  124. // width and height of the component
  125. $width = $height = "";
  126. $width = $container->getComponentWidth($key + 1);
  127. $height = $container->getComponentHeight($key + 1);
  128. if (isset($width)) $width = " width=\"$width\"";
  129. if (isset($height)) $height = " height=\"$height\"";
  130. // include halign and valign
  131. $halign = $valign = "";
  132. switch ($container->getComponentAlignmentX($key + 1)) {
  133. case LEFT: $halign = " align=\"left\""; break;
  134. case CENTER: $halign = " align=\"center\""; break;
  135. case RIGHT: $halign = " align=\"right\""; break;
  136. }
  137. switch ($container->getComponentAlignmentY($key + 1)) {
  138. case TOP: $valign = " valign=\"top\""; break;
  139. case CENTER: $valign = " valign=\"middle\""; break;
  140. case BOTTOM: $valign = " valign=\"bottom\""; break;
  141. }
  142. // render the component in separate table cell
  143. echo $tabs."\t<td $width$height$halign$valign$tdStyles>\n";
  144. $component->render($theme, $tabs."\t\t");
  145. echo $tabs."\t</td>\n";
  146. }
  147. // Print the table-row divisions if we've reached the end of a row.
  148. if (($i+1) < $numElements
  149. && (($i+1) % $this->_numColumns) == 0)
  150. {
  151. echo $tabs."\t</tr>\n";
  152. echo $tabs."\t<tr>\n";
  153. }
  154. }
  155.  
  156. echo $tabs."\t</tr>\n";
  157. echo $tabs."</table>\n";
  158. }
  159. /**
  160. * Returns any CSS code that might be needed in order for this <code>Layout</code>
  161. * to render properly.
  162. * @access public
  163. * @param string tabs This is a string (normally a bunch of tabs) that will be
  164. * prepended to each text line. This argument is optional but its usage is highly
  165. * recommended in order to produce a nicely formatted HTML output.
  166. * @return string The CSS code that might be needed in order for this <code>Layout</code>
  167. * to render properly.
  168. ***/
  169. function getCSS($tabs = "") {
  170. return "";
  171. }
  172. /**
  173. * Return an array mapping the order of the cells .
  174. *
  175. * @param int $numComponents
  176. * @return array
  177. * @access public
  178. * @since 8/18/06
  179. */
  180. function _getCellOrder ($numComponents) {
  181. // The $order is an array the keys
  182. // are the new positions, the values are the
  183. // old position.
  184. $order = array();
  185. $numCols = $this->_numColumns;
  186. $numRows = ceil($numComponents/$numCols);
  187. switch ($this->_renderDirection) {
  188. // Top-Bottom/Left-Right
  189. case 'Top-Bottom/Left-Right':
  190. $tmpRows = array();
  191. for ($i = 0; $i < ($numRows * $numCols); $i++) {
  192. $row = $i % $numRows;
  193. if (!isset($tmpRows[$row]))
  194. $tmpRows[$row] = array();
  195. $tmpRows[$row][] = $i;
  196. }
  197. foreach ($tmpRows as $row) {
  198. foreach ($row as $i)
  199. $order[] = $i;
  200. }
  201. break;
  202. // Right-Left/Top-Bottom
  203. case 'Right-Left/Top-Bottom':
  204. $tmpRows = array();
  205. $i = 0;
  206. for ($row = 0; $row < $numRows; $row++) {
  207. $tmpRows[$row] = array();
  208. for ($col = $numCols - 1; $col >= 0; $col--) {
  209. $tmpRows[$row][$col] = $i;
  210. $i++;
  211. }
  212. ksort($tmpRows[$row]);
  213. }
  214. foreach ($tmpRows as $row) {
  215. foreach ($row as $i)
  216. $order[] = $i;
  217. }
  218. break;
  219. // Top-Bottom/Right-Left
  220. case 'Top-Bottom/Right-Left':
  221. $tmpRows = array();
  222. for ($i = 0; $i < ($numRows * $numCols); $i++) {
  223. $row = $i % $numRows;
  224. if (!isset($tmpRows[$row]))
  225. $tmpRows[$row] = array();
  226. $tmpRows[$row][] = $i;
  227. }
  228. foreach ($tmpRows as $row) {
  229. krsort($row);
  230. foreach ($row as $i)
  231. $order[] = $i;
  232. }
  233. break;
  234. // Left-Right/Top-Bottom
  235. default:
  236. for ($i = 0; $i < ($numRows * $numCols); $i++)
  237. $order[] = $i;
  238. }
  239. return $order;
  240. }
  241. }
  242.  
  243. ?>

Documentation generated on Wed, 19 Sep 2007 10:27:04 -0400 by phpDocumentor 1.3.0RC3