Source for file Menu.class.php

Documentation is available at Menu.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."GUIManager/Container.class.php");
  4. require_once(HARMONI."GUIManager/Components/Menu.interface.php");
  5.  
  6. /**
  7. * A <code>Menu</code> is a <code>Container</code> that stores a number of
  8. * MenuItem objects. The familiar add/get/remove <code>Container</code> methods
  9. * can be used to manage the <code>MenuItems</code>.
  10. *
  11. * @package harmoni.gui.components
  12. *
  13. * @copyright Copyright &copy; 2005, Middlebury College
  14. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  15. *
  16. * @version $Id: Menu.class.php,v 1.14 2007/09/04 20:25:22 adamfranco Exp $
  17. */
  18. class Menu extends Container /* implements MenuInterface */ {
  19.  
  20. /**
  21. * This is the id of the menu item that is currently selected.
  22. * @var integer _selectedId
  23. * @access private
  24. */
  25. var $_selectedId;
  26.  
  27. /**
  28. * The constructor.
  29. * @access public
  30. * @param ref object layout The <code>Layout</code> of this container.
  31. * @param integer index The index of this component. The index has no semantic meaning:
  32. * you can think of the index as 'level' of the component. Alternatively,
  33. * the index could serve as means of distinguishing between components with
  34. * the same type. Most often one would use the index in conjunction with
  35. * the <code>getStylesForComponentType()</code> and
  36. * <code>addStyleForComponentType()</code> methods.
  37. * @param optional object StyleCollections styles,... Zero, one, or more StyleCollection
  38. * objects that will be added to the newly created Component. Warning, this will
  39. * result in copying the objects instead of referencing them as using
  40. * <code>addStyle()</code> would do.
  41. ***/
  42. function Menu($layout, $index) {
  43. $this->Container($layout, MENU, $index);
  44. $this->_selectedId = null;
  45.  
  46. // if there are style collections to add
  47. if (func_num_args() > 2)
  48. for ($i = 2; $i < func_num_args(); $i++)
  49. $this->addStyle(func_get_arg($i));
  50. }
  51.  
  52. /**
  53. * Adds the given menu item to this container. The only difference from the
  54. * familiar <code>add()</code> method of the Container class is that now
  55. * explicit checking is performed to make sure that the given component
  56. * is indeed a <code>menuItem</code> and not just any component.
  57. * <br /><br />
  58. * Warning: The <code>add()</code> method allows the user to add the same
  59. * instance of an object multiple times. With menus, this is extremely unadvised because
  60. * the menu might end up with multiple selected menu items at the same time.
  61. * @access public
  62. * @param ref object menuItem The menuItem to add. If it is selected, then the
  63. * old selected menu item will be automatically deselected.
  64. * @param string width The available width for the added menuItem. If null, will be ignored.
  65. * @param string height The available height for the added menuItem. If null, will be ignored.
  66. * @param integer alignmentX The horizontal alignment for the added menuItem. Allowed values are
  67. * <code>LEFT</code>, <code>CENTER</code>, and <code>RIGHT</code>.
  68. * If null, will be ignored.
  69. * @param integer alignmentY The vertical alignment for the added menuItem. Allowed values are
  70. * <code>TOP</code>, <code>CENTER</code>, and <code>BOTTOM</code>.
  71. * If null, will be ignored.
  72. * @return ref object The menuItem that was just added.
  73. ***/
  74. function add($menuItem, $width = null, $height = null, $alignmentX = null, $alignmentY = null) {
  75. // ** parameter validation
  76. // some weird class checking here - would have been much simpler if PHP
  77. // supported multiple inheritance
  78. $rule1 = ExtendsValidatorRule::getRule("MenuItemLink");
  79. $rule2 = ExtendsValidatorRule::getRule("MenuItemHeading");
  80. $rule3 = ExtendsValidatorRule::getRule("MenuItem");
  81. $rule4 = ExtendsValidatorRule::getRule("SubMenu");
  82. ArgumentValidator::validate($menuItem,
  83. OrValidatorRule::getRule(
  84. OrValidatorRule::getRule(
  85. OrValidatorRule::getRule($rule1, $rule2), $rule3), $rule4),
  86. true);
  87. // ** end of parameter validation
  88.  
  89. parent::add($menuItem, $width, $height, $alignmentX, $alignmentY);
  90. // if the given menuItem is selected, then select it
  91. if (is_a($menuItem, "MenuItemLink"))
  92. if ($menuItem->isSelected()) {
  93. $id = $this->getComponentsCount();
  94. $this->select($id);
  95. }
  96. return $menuItem;
  97. }
  98.  
  99. /**
  100. * Returns the menu item that is currently selected.
  101. * @access public
  102. * @return ref object The menu item that is currently selected.
  103. ***/
  104. function getSelected() {
  105. if ($this->_selectedId)
  106. return $this->getComponent($this->_selectedId);
  107. else {
  108. $null = null;
  109. return $null;
  110. }
  111. }
  112.  
  113. /**
  114. * Determines whether the <code>MenuItem</code> with the given id is selected. Ids
  115. * reflect the order in which menu items are added. That is, the very first
  116. * menu item has an id of 1, the second menu item has an id of 2, and so forth.
  117. * @access public
  118. * @param integer id The id of the menu item.
  119. * @return boolean <code>TRUE</code>, if the menu item with the given id is selected.
  120. ***/
  121. function isSelected($id) {
  122. // ** parameter validation
  123. ArgumentValidator::validate($id, IntegerValidatorRule::getRule(), true);
  124. // ** end of parameter validation
  125.  
  126. return $this->_selectedId === $id;
  127. }
  128. /**
  129. * Selects the <code>MenuItem</code> with the given id, and deselects the one
  130. * that was previoiusly selected. Ids reflect the order in which menu items
  131. * are added. That is, the very first menu item has an id of 1, the second
  132. * menu item has an id of 2, and so forth.
  133. * @access public
  134. * @param integer id The id of the menu item to select.
  135. ***/
  136. function select($id) {
  137. // ** parameter validation
  138. ArgumentValidator::validate($id, IntegerValidatorRule::getRule(), true);
  139. // ** end of parameter validation
  140.  
  141. // make sure the id is valid
  142. $newSelected =$this->getComponent($id);
  143. if (!isset($newSelected))
  144. return;
  145.  
  146. // deselect the old one
  147. $oldSelected =$this->getSelected();
  148. if (isset($oldSelected))
  149. $oldSelected->setSelected(false);
  150.  
  151. // select the new component
  152. $newSelected->setSelected(true);
  153. $this->_selectedId = $id;
  154. }
  155.  
  156. }
  157.  
  158. ?>

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