Source for file Container.class.php

Documentation is available at Container.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."GUIManager/Container.interface.php");
  4. require_once(HARMONI."GUIManager/Component.class.php");
  5. require_once(HARMONI."GUIManager/Components/Blank.class.php");
  6. require_once(HARMONI."GUIManager/StyleProperties/WidthSP.class.php");
  7. require_once(HARMONI."GUIManager/StyleProperties/HeightSP.class.php");
  8.  
  9. /**
  10. * This is a generic <code>Container</code> implementation that should be sufficient
  11. * for all means and purposes.
  12. * <br /><br />
  13. * The <code>Container</code> interface is an extension of the <code>Component</code>
  14. * interface; <code>Containers</code> are capable of storing multiple sub-<code>Components</code>
  15. * and when rendering Containers, all sub-<code>Components</code> will be rendered as well.
  16. *
  17. * @package harmoni.gui
  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: Container.class.php,v 1.17 2007/09/04 20:25:21 adamfranco Exp $
  23. */
  24. class Container extends Component /* implements ContainerInterface */ {
  25.  
  26. /**
  27. * The <code>Layout</code> of this <code>Container</code>.
  28. * @var object _layout
  29. * @access private
  30. */
  31. var $_layout;
  32. /**
  33. * The <code>Components</code> of this <code>Container</code>.
  34. * @var array _components
  35. * @access private
  36. */
  37. var $_components;
  38. /**
  39. * An array storing constraint information for each component. Each element is
  40. * another array of four elements storing the width, height, horizontal alignment,
  41. * and vertical alignment of each component.
  42. * @var array _constraints
  43. * @access private
  44. */
  45. var $_constraints;
  46.  
  47. /**
  48. * The constructor.
  49. * @access public
  50. * @param ref object layout The <code>Layout</code> of this container.
  51. * @param integer type The type of this component. One of BLANK, HEADING, FOOTER,
  52. * BLOCK, MENU, MENU_ITEM_LINK_UNSELECTED, MENU_ITEM_LINK_SELECTED, MENU_ITEM_HEADING, OTHER.
  53. * @param integer index The index of this component. The index has no semantic meaning:
  54. * you can think of the index as 'level' of the component. Alternatively,
  55. * the index could serve as means of distinguishing between components with
  56. * the same type. Most often one would use the index in conjunction with
  57. * the <code>getStylesForComponentType()</code> and
  58. * <code>addStyleForComponentType()</code> methods.
  59. * @param optional object StyleCollections styles,... Zero, one, or more StyleCollection
  60. * objects that will be added to the newly created Component. Warning, this will
  61. * result in copying the objects instead of referencing them as using
  62. * <code>addStyle()</code> would do.
  63. ***/
  64. function Container($layout, $type, $index) {
  65. // ** parameter validation
  66. $rule = ExtendsValidatorRule::getRule("LayoutInterface");
  67. ArgumentValidator::validate($layout, $rule, true);
  68. ArgumentValidator::validate($index, IntegerValidatorRule::getRule(), true);
  69. // ** end of parameter validation
  70. $this->Component(null, $type, $index);
  71. $this->_layout =$layout;
  72. $this->_components = array();
  73. $this->_constraints = array();
  74.  
  75. // if there are style collections to add
  76. if (func_num_args() > 3)
  77. for ($i = 3; $i < func_num_args(); $i++)
  78. $this->addStyle(func_get_arg($i));
  79. }
  80.  
  81. /**
  82. * Renders the component on the screen.
  83. * @param ref object theme The Theme object to use in producing the result
  84. * of this method.
  85. * @param string tabs This is a string (normally a bunch of tabs) that will be
  86. * prepended to each text line. This argument is optional but its usage is highly
  87. * recommended in order to produce a nicely formatted HTML output.
  88. * @access public
  89. ***/
  90. function render($theme, $tabs = "") {
  91. echo $this->getPreHTML($theme, $tabs);
  92. $this->_layout->render($this, $theme, $tabs);
  93. echo $this->getPostHTML($theme, $tabs);
  94. }
  95.  
  96. /**
  97. * Adds the given component to this container.
  98. * @access public
  99. * @param ref object component The component to add.
  100. * @param string width The available width for the added component. If null, will be ignored.
  101. * @param string height The available height for the added component. If null, will be ignored.
  102. * @param integer alignmentX The horizontal alignment for the added component. Allowed values are
  103. * <code>LEFT</code>, <code>CENTER</code>, and <code>RIGHT</code>.
  104. * If null, will be ignored.
  105. * @param integer alignmentY The vertical alignment for the added component. Allowed values are
  106. * <code>TOP</code>, <code>CENTER</code>, and <code>BOTTOM</code>.
  107. * If null, will be ignored.
  108. * @return ref object The component that was just added.
  109. ***/
  110. function add($component, $width = NULL, $height = NULL, $alignmentX = NULL, $alignmentY = NULL) {
  111. // ** parameter validation
  112. ArgumentValidator::validate($component, ExtendsValidatorRule::getRule("ComponentInterface"), true);
  113. ArgumentValidator::validate($width, OptionalRule::getRule(StringValidatorRule::getRule(), true));
  114. ArgumentValidator::validate($height, OptionalRule::getRule(StringValidatorRule::getRule(), true));
  115. ArgumentValidator::validate($alignmentX, OptionalRule::getRule(IntegerValidatorRule::getRule(), true));
  116. ArgumentValidator::validate($alignmentY, OptionalRule::getRule(IntegerValidatorRule::getRule(), true));
  117. // ** end of parameter validation
  118. $constraint = array();
  119. $constraint[0] = $width;
  120. $constraint[1] = $height;
  121. $constraint[2] = $alignmentX;
  122. $constraint[3] = $alignmentY;
  123. $this->_constraints[] =$constraint;
  124. $this->_components[] = $component;
  125. return $component;
  126. }
  127. /**
  128. * Add a placeholder and recieve back an id with which to reference it.
  129. * This method can be used in conjunction with insertAtPlaceholder()
  130. * to allow out-of-order addition of components.
  131. *
  132. * @return integer
  133. * @access public
  134. * @since 1/24/07
  135. */
  136. function addPlaceholder () {
  137. $this->add(new Blank(1));
  138. $placeholderId = count($this->_components);
  139. // Record the Id so that we can verify it when inserting later
  140. if (!isset($this->_placeholders))
  141. $this->_placeholders = array();
  142. $this->_placeholders[] = $placeholderId;
  143. return $placeholderId;
  144. }
  145. /**
  146. * Insert a component at the place of a predefined placeholder that had
  147. * been created with addPlaceholder().
  148. *
  149. * @param integer $placeholderId
  150. * @param ref object component The component to add.
  151. * @param string width The available width for the added component. If null, will be ignored.
  152. * @param string height The available height for the added component. If null, will be ignored.
  153. * @param integer alignmentX The horizontal alignment for the added component. Allowed values are
  154. * <code>LEFT</code>, <code>CENTER</code>, and <code>RIGHT</code>.
  155. * If null, will be ignored.
  156. * @param integer alignmentY The vertical alignment for the added component. Allowed values are
  157. * <code>TOP</code>, <code>CENTER</code>, and <code>BOTTOM</code>.
  158. * If null, will be ignored.
  159. * @return ref object The component that was just inserted.
  160. * @access public
  161. * @since 1/24/07
  162. */
  163. function insertAtPlaceholder ($placeholderId, $component, $width = NULL,
  164. $height = NULL, $alignmentX = NULL, $alignmentY = NULL)
  165. {
  166. // ** parameter validation
  167. ArgumentValidator::validate($placeholderId, IntegerValidatorRule::getRule(), true);
  168. ArgumentValidator::validate($component, ExtendsValidatorRule::getRule("ComponentInterface"), true);
  169. ArgumentValidator::validate($width, OptionalRule::getRule(StringValidatorRule::getRule(), true));
  170. ArgumentValidator::validate($height, OptionalRule::getRule(StringValidatorRule::getRule(), true));
  171. ArgumentValidator::validate($alignmentX, OptionalRule::getRule(IntegerValidatorRule::getRule(), true));
  172. ArgumentValidator::validate($alignmentY, OptionalRule::getRule(IntegerValidatorRule::getRule(), true));
  173. // ** end of parameter validation
  174. if (!in_array($placeholderId, $this->_placeholders))
  175. throwError(new Error("Unknown placeholder id, '".$placeholderId."'.", "GUIManager"));
  176. $constraint = array();
  177. $constraint[0] = $width;
  178. $constraint[1] = $height;
  179. $constraint[2] = $alignmentX;
  180. $constraint[3] = $alignmentY;
  181. $this->_constraints[$placeholderId - 1] =$constraint;
  182. // Add any pre and post html that was added to the placeholder
  183. $null = null;
  184. $component->setPreHTML(
  185. $this->_components[$placeholderId - 1]->getPreHTML($null)
  186. .$component->getPreHTML($null));
  187. $component->setPostHTML(
  188. $component->getPostHTML($null)
  189. .$this->_components[$placeholderId - 1]->getPostHTML($null));
  190. // Replace the placeholder with the component
  191. $this->_components[$placeholderId - 1] =$component;
  192. return $this->_components[$placeholderId - 1];
  193. }
  194. /**
  195. * Returns the component of this container with the specified id. Ids
  196. * reflect the order in which components are added. That is, the very first
  197. * component has an id of 1, the second component has an id of 2, and so forth.
  198. * @access public
  199. * @param integer id The id of the component which should be returned.
  200. * @return ref object The component.
  201. ***/
  202. function getComponent($id) {
  203. // ** parameter validation
  204. ArgumentValidator::validate($id, IntegerValidatorRule::getRule(), true);
  205. // ** end of parameter validation
  206.  
  207. if (isset($this->_components[$id-1]))
  208. return $this->_components[$id-1];
  209. else
  210. $null = null;
  211. return $null;
  212. }
  213.  
  214. /**
  215. * Returns the number of components in this container.
  216. * @access public
  217. * @return integer The number of components in this container.
  218. ***/
  219. function getComponentsCount() {
  220. return count($this->_components);
  221. }
  222.  
  223. /**
  224. * Returns all components in this <code>Container</code>.
  225. * @access public
  226. * @return ref array An array of the components in this <code>Container</code>.
  227. ***/
  228. function getComponents() {
  229. return $this->_components;
  230. }
  231.  
  232. /**
  233. * Returns the width for the component of this container with the specified id. Ids
  234. * reflect the order in which components are added. That is, the very first
  235. * component has an id of 1, the second component has an id of 2, and so forth.
  236. * @access public
  237. * @param integer id The id of the component which should be returned.
  238. * @return string The width.
  239. ***/
  240. function getComponentWidth($id) {
  241. return $this->_constraints[$id-1][0];
  242. }
  243. /**
  244. * Returns the height for the component of this container with the specified id. Ids
  245. * reflect the order in which components are added. That is, the very first
  246. * component has an id of 1, the second component has an id of 2, and so forth.
  247. * @access public
  248. * @param integer id The id of the component which should be returned.
  249. * @return string The height.
  250. ***/
  251. function getComponentHeight($id) {
  252. return $this->_constraints[$id-1][1];
  253. }
  254. /**
  255. * Returns the horizontal alignment for the component of this container with the specified id. Ids
  256. * reflect the order in which components are added. That is, the very first
  257. * component has an id of 1, the second component has an id of 2, and so forth.
  258. * @access public
  259. * @param integer id The id of the component which should be returned.
  260. * @return integer The horizontal alignment.
  261. ***/
  262. function getComponentAlignmentX($id) {
  263. return $this->_constraints[$id-1][2];
  264. }
  265.  
  266. /**
  267. * Returns the vertical alignment for the component of this container with the specified id. Ids
  268. * reflect the order in which components are added. That is, the very first
  269. * component has an id of 1, the second component has an id of 2, and so forth.
  270. * @access public
  271. * @param integer id The id of the component which should be returned.
  272. * @return integer The vertical alignment.
  273. ***/
  274. function getComponentAlignmentY($id) {
  275. return $this->_constraints[$id-1][3];
  276. }
  277.  
  278. /**
  279. * Removes the component with the specified id from this container. Ids
  280. * reflect the order in which components are added. That is, the very first
  281. * component has an id of 1, the second component has an id of 2, and so forth.
  282. * @access public
  283. * @param integer id The id of the component which should be removed from
  284. * this container..
  285. * @return ref object The component that was just removed.
  286. ***/
  287. function remove($id) {
  288. // ** parameter validation
  289. ArgumentValidator::validate($id, IntegerValidatorRule::getRule(), true);
  290. // ** end of parameter validation
  291.  
  292. $component =$this->_components[$id-1];
  293. unset($this->_components[$id-1]);
  294. unset($this->_constraints[$id-1]);
  295.  
  296. return $component;
  297. }
  298. /**
  299. * Removes all components from this <code>Container</code>.
  300. * @access public
  301. ***/
  302. function removeAll() {
  303. $this->_components = array();
  304. $this->_constraints = array();
  305. }
  306. /**
  307. * Returns the <code>Layout</code> of this container.
  308. * @access public
  309. * @return ref object The <code>Layout</code> of this container.
  310. ***/
  311. function getLayout() {
  312. return $this->_layout;
  313. }
  314. /**
  315. * Sets the <code>Layout</code> of this container
  316. * @access public
  317. * @param ref object layout The Layout to assign to this container.
  318. ***/
  319. function setLayout($layout) {
  320. // ** parameter validation
  321. $rule = ExtendsValidatorRule::getRule("LayoutInterface");
  322. ArgumentValidator::validate($layout, $rule, true);
  323. // ** end of parameter validation
  324.  
  325. $this->_layout =$layout;
  326. }
  327. }
  328.  
  329. ?>

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