Alpha Framework alpha--util--graphs
[ class tree: alpha--util--graphs ] [ index: alpha--util--graphs ] [ all elements ]

Source for file AlphaGraphNode.inc

Documentation is available at AlphaGraphNode.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/exceptions/IllegalArguementException.inc';
  4.  
  5. /**
  6.  *
  7.  * Maintains the geometry for a tree node
  8.  * 
  9.  * @package alpha::util::graphs
  10.  * @since 1.0
  11.  * @author John Collins <dev@alphaframework.org>
  12.  * @version $Id: AlphaGraphNode.inc 1454 2011-12-04 15:14:05Z johnc $
  13.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  14.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  15.  *  All rights reserved.
  16.  * 
  17.  *  <pre>
  18.  *  Redistribution and use in source and binary forms, with or
  19.  *  without modification, are permitted provided that the
  20.  *  following conditions are met:
  21.  * 
  22.  *  * Redistributions of source code must retain the above
  23.  *    copyright notice, this list of conditions and the
  24.  *    following disclaimer.
  25.  *  * Redistributions in binary form must reproduce the above
  26.  *    copyright notice, this list of conditions and the
  27.  *    following disclaimer in the documentation and/or other
  28.  *    materials provided with the distribution.
  29.  *  * Neither the name of the Alpha Framework nor the names
  30.  *    of its contributors may be used to endorse or promote
  31.  *    products derived from this software without specific
  32.  *    prior written permission.
  33.  *   
  34.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  35.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  36.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  37.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  39.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  44.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  45.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  46.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  *  </pre>
  48.  *  
  49.  */
  50. class AlphaGraphNode {
  51.     /**
  52.      * The id of the node
  53.      * 
  54.      * @var integer 
  55.      * @since 1.0
  56.      */
  57.     private $id 0;
  58.     
  59.     /**
  60.      * The height of the node
  61.      * 
  62.      * @var integer 
  63.      * @since 1.0
  64.      */
  65.     private $height 0;
  66.     
  67.     /**
  68.      * The width of the node
  69.      * 
  70.      * @var integer 
  71.      * @since 1.0
  72.      */
  73.     private $width 0;
  74.     
  75.     /**
  76.      * The x position of the node
  77.      * 
  78.      * @var integer 
  79.      * @since 1.0
  80.      */
  81.     private $x 0;
  82.     
  83.     /**
  84.      * The y position of the node
  85.      * 
  86.      * @var integer 
  87.      * @since 1.0
  88.      */
  89.     private $y 0;
  90.     
  91.     /**
  92.      * The node to the left of this one
  93.      * 
  94.      * @var AlphaGraphNode 
  95.      * @since 1.0
  96.      */
  97.     private $leftNode;
  98.     
  99.     /**
  100.      * The node to the right of this one
  101.      * 
  102.      * @var AlphaGraphNode 
  103.      * @since 1.0
  104.      */
  105.     private $rightNode;
  106.     
  107.     /**
  108.      * An array of child nodes of this node
  109.      * 
  110.      * @var array 
  111.      * @since 1.0
  112.      */
  113.     private $children array();
  114.     
  115.     /**
  116.      * The margin offset of the current node
  117.      * 
  118.      * @var integer 
  119.      * @since 1.0
  120.      */
  121.     private $offset 0;
  122.     
  123.     /**
  124.      * Optional positional modifier
  125.      * 
  126.      * @var integer 
  127.      * @since 1.0
  128.      */
  129.     private $modifier 0;
  130.     
  131.     /**
  132.      * Parent node of this node (if any)
  133.      * 
  134.      * @var AlphaGraphNode 
  135.      * @since 1.0
  136.      */
  137.     private $parentNode;
  138.  
  139.     /**
  140.      * The text message to display on the node
  141.      * 
  142.      * @var string 
  143.      * @since 1.0
  144.      */
  145.     private $message;
  146.     
  147.     /**
  148.      * A 2D array of the coordinates of the endpoints for connectots on this node
  149.      * 
  150.      * @var array 
  151.      * @since 1.0
  152.      */
  153.     private $links array();
  154.     
  155.     /**
  156.      * An array containing the R,G,B values for the colour of this node
  157.      * 
  158.      * @var array 
  159.      * @since 1.0
  160.      */
  161.     private $nodeColour;
  162.     
  163.     /**
  164.      * If the node is clickable in an image map, use this property to hold the target URL
  165.      * 
  166.      * @var string 
  167.      * @since 1.0
  168.      */
  169.     private $URL;
  170.     
  171.     /**
  172.      * Constructor
  173.      * 
  174.      * @param integer $id 
  175.      * @param integer $width 
  176.      * @param integer $height 
  177.      * @param string $message 
  178.      * @param array $nodeColour 
  179.      * @param string $URL 
  180.      */
  181.     public function __construct($id$width$height$message ''$nodeColour null$URL null{
  182.         $this->id $id;
  183.         $this->width $width;
  184.         $this->height $height;
  185.         $this->message $message;
  186.         $this->nodeColour $nodeColour;
  187.         $this->URL $URL;
  188.     }
  189.     
  190.     /**
  191.      * Get the node colour array
  192.      * 
  193.      * @return array 
  194.      * @since 1.0
  195.      */
  196.     public function getNodeColour({
  197.         return $this->nodeColour;
  198.     }
  199.     
  200.     /**
  201.      * Set the node colour array
  202.      * 
  203.      * @param array $nodeColour 
  204.      * @throws IllegalArguementException
  205.      * @since 1.0
  206.      */
  207.     public function setNodeColour($nodeColour{
  208.         if(is_array($nodeColour&& count($nodeColour== 3)
  209.             $this->nodeColour $nodeColour;
  210.         else
  211.             throw new IllegalArguementException('The nodeColour value passed ['.$nodeColour.'] is not a valid array!');
  212.     }
  213.     
  214.     /**
  215.      * Get the node URL
  216.      * 
  217.      * @return string 
  218.      * @since 1.0
  219.      */
  220.     public function getURL({
  221.         return $this->URL;
  222.     }
  223.     
  224.     /**
  225.      * Set the node ULR
  226.      * 
  227.      * @param string $URL 
  228.      * @since 1.0
  229.      */
  230.     public function setURL($URL{
  231.         $this->URL $URL;
  232.     }
  233.     
  234.     /**
  235.      * Get the node text message
  236.      * 
  237.      * @return string 
  238.      * @since 1.0
  239.      */
  240.     public function getMessage({
  241.         return $this->message;
  242.     }
  243.     
  244.     /**
  245.      * Set the node text message
  246.      * 
  247.      * @param string $message 
  248.      * @since 1.0
  249.      */
  250.     public function setMessage($message{
  251.         $this->message $message;
  252.     }
  253.     
  254.     /**
  255.      * Get the node offset
  256.      * 
  257.      * @return string 
  258.      * @since 1.0
  259.      */
  260.     public function getOffset({
  261.         return $this->offset;
  262.     }
  263.     
  264.     /**
  265.      * Set the node offset
  266.      * 
  267.      * @param integer $offset 
  268.      * @since 1.0
  269.      */
  270.     public function setOffset($offset{
  271.         $this->offset $offset;
  272.     }
  273.     
  274.     /**
  275.      * Get the node modifier
  276.      * 
  277.      * @return integer 
  278.      * @since 1.0
  279.      */
  280.     public function getModifier({
  281.         return $this->modifier;
  282.     }
  283.     
  284.     /**
  285.      * Set the node modifier
  286.      * 
  287.      * @param integer $modifier 
  288.      * @since 1.0
  289.      */
  290.     public function setModifier($modifier{
  291.         $this->modifier $modifier;
  292.     }
  293.     
  294.     /**
  295.      * Get the number of child nodes attached to this node
  296.      * 
  297.      * @return integer 
  298.      * @since 1.0
  299.      */
  300.     public function childCount({
  301.         return count($this->children);
  302.     }
  303.     
  304.     /**
  305.      * Get the parent node of this node (if any)
  306.      * 
  307.      * @return AlphaGraphNode 
  308.      * @since 1.0
  309.      */
  310.     public function getParentNode({
  311.         return $this->parentNode;
  312.     }
  313.     
  314.     /**
  315.      * Set the parent node
  316.      * 
  317.      * @param AlphaGraphNode $node 
  318.      * @throws IllegalArguementException
  319.      * @since 1.0
  320.      */
  321.     public function setParentNode($node{
  322.         if($node instanceof AlphaGraphNode)
  323.             $this->parentNode $node;
  324.         else
  325.             throw new IllegalArguementException('The node object passed to setParentNode is not a valid AlphaGraphNode instance!');
  326.     }
  327.     
  328.     /**
  329.      * Get the node to the left of this one (if any)
  330.      * 
  331.      * @return AlphaGraphNode 
  332.      * @since 1.0
  333.      */
  334.     public function getLeftSibling({
  335.         if($this->leftNode){
  336.             return $this->leftNode;
  337.         }else{
  338.             return null;    
  339.         }
  340.     }
  341.     
  342.     /**
  343.      * Sets the node to the left of this node
  344.      * 
  345.      * @param AlphaGraphNode $node 
  346.      * @throws IllegalArguementException
  347.      * @since 1.0
  348.      */
  349.     public function setLeftSibling($node{
  350.         if($node instanceof AlphaGraphNode)
  351.             $this->leftNode $node;
  352.         else
  353.             throw new IllegalArguementException('The node object passed to setLeftSibling is not a valid AlphaGraphNode instance!');
  354.     }
  355.     
  356.     /**
  357.      * Get the node to the right of this one (if any)
  358.      * 
  359.      * @return AlphaGraphNode 
  360.      * @since 1.0
  361.      */
  362.     public function getRightSibling({
  363.         if($this->rightNode{
  364.             return $this->rightNode;
  365.         }else{
  366.             return null;
  367.         }
  368.     }
  369.     
  370.     /**
  371.      * Sets the node to the right of this node
  372.      * 
  373.      * @param AlphaGraphNode $node 
  374.      * @throws IllegalArguementException
  375.      * @since 1.0
  376.      */
  377.     public function setRightSibling($node{
  378.         if($node instanceof AlphaGraphNode)
  379.             $this->rightNode $node;
  380.         else
  381.             throw new IllegalArguementException('The node object passed to setRightSibling is not a valid AlphaGraphNode instance!');
  382.     }
  383.     
  384.     /**
  385.      * Gets the child node at the index provided, or returns false if none is found
  386.      * 
  387.      * @param integer $i 
  388.      * @return mixed 
  389.      * @since 1.0
  390.      */
  391.     public function getChildAt($i{
  392.         if(isset($this->children[$i])) {
  393.             return $this->children[$i];
  394.         }else{
  395.             return false;
  396.         }
  397.     }
  398.     
  399.     /**
  400.      * Calculates and returns the midpoint X coordinate of the children of this node
  401.      * 
  402.      * @return integer 
  403.      * @since 1.0
  404.      */
  405.     public function getChildrenCenter()    {
  406.         $node $this->getChildAt(0);
  407.         $node1 $this->getChildAt(count($this->children)-1);
  408.         return $node->getOffset((($node1->getOffset($node->getOffset()) $node1->getWidth()) 2;
  409.     }
  410.     
  411.     /**
  412.      * Returns the array of child AlphaGraphNode objects
  413.      * 
  414.      * @return array 
  415.      * @since 1.0
  416.      */
  417.     public function getChildren({
  418.         return $this->children;
  419.     }
  420.     
  421.     /**
  422.      * Add a new node to the children array of this node
  423.      * 
  424.      * @param AlphaGraphNode $node 
  425.      * @throws IllegalArguementException
  426.      * @since 1.0
  427.      */
  428.     public function addChild($node{
  429.         if($node instanceof AlphaGraphNode)
  430.             array_push($this->children$node);
  431.         else
  432.             throw new IllegalArguementException('The node object passed to addChild is not a valid AlphaGraphNode instance!');
  433.     }
  434.     
  435.     /**
  436.      * Returns the links array
  437.      * 
  438.      * @return array 
  439.      * @since 1.0
  440.      */
  441.     public function getLinks({
  442.         return $this->links;
  443.     }
  444.     
  445.     /**
  446.      * Sets up the array of connector endpoints
  447.      * 
  448.      * @since 1.0
  449.      */
  450.     public function setUpLinks({
  451.         $xa 0$ya 0$xb 0$yb 0$xc 0$yc 0$xd 0$yd 0;
  452.         $xa $this->($this->width 2);
  453.         $ya $this->$this->height;
  454.     
  455.         foreach($this->children as $child{
  456.             $xd $xc $child->getX(($child->getWidth(2);
  457.             $yd $child->getY();
  458.             $xb $xa;
  459.             $yb $yc $ya ($yd $ya2;
  460.             $this->links[$child->id]['xa'$xa;
  461.             $this->links[$child->id]['ya'$ya;
  462.             $this->links[$child->id]['xb'$xb;
  463.             $this->links[$child->id]['yb'$yb;
  464.             $this->links[$child->id]['xc'$xc;
  465.             $this->links[$child->id]['yc'$yc;
  466.             $this->links[$child->id]['xd'$xd;
  467.             $this->links[$child->id]['yd'$yd;
  468.         }
  469.     }
  470.  
  471.     /**
  472.      * Returns the node height
  473.      * 
  474.      * @return integer 
  475.      * @since 1.0
  476.      */
  477.     public function getHeight({
  478.         return $this->height;
  479.     }
  480.     
  481.     /**
  482.      * Returns the node width
  483.      * 
  484.      * @return integer 
  485.      * @since 1.0
  486.      */
  487.     public function getWidth({
  488.         return $this->width;
  489.     }
  490.     
  491.     /**
  492.      * Returns the node X-coordinate
  493.      * 
  494.      * @return integer 
  495.      * @since 1.0
  496.      */
  497.     public function getX({
  498.         return $this->x;
  499.     }
  500.     
  501.     /**
  502.      * Returns the node Y-coordinate
  503.      * 
  504.      * @return integer 
  505.      * @since 1.0
  506.      */
  507.     public function getY({
  508.         return $this->y;
  509.     }
  510.     
  511.     /**
  512.      * Sets the node X-coordinate
  513.      * 
  514.      * @param integer $x 
  515.      * @since 1.0
  516.      */
  517.     public function setX($x{
  518.         $this->$x;
  519.     }
  520.     
  521.     /**
  522.      * Sets the node Y-coordinate
  523.      * 
  524.      * @param integer $y 
  525.      * @since 1.0
  526.      */
  527.     public function setY($y{
  528.         $this->$y;
  529.     }
  530.  
  531. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:08 +0000 by phpDocumentor 1.4.3