<?php 
/*
 * DON'T REMOVE THE FOLLOWING LICENSE
 *           INFORMATION!
 * ----------------------------------
 * Copyright by
 * Dennis Ritz
 * Author: Dennis Ritz
 * dennis.ritz@gmx.net
 * 2007-2008
 * ----------------------------------
 */


/*
 * CLASS Label
 */
class Label {

    private $active = false;
    private $_childs = array();
    private $_eventListeners = array();
    
    public function __construct($p_first,$p_second = null) {
        if(is_string($p_first)) {
            $this->_childs[1] = $p_first;
            if(is_subclass_of($p_second,"Icon") || is_a($p_second,"Icon")
            || is_subclass_of($p_second,"Input") || is_a($p_second,"Input")) $this->_childs[0]=$p_second;
        } else if(is_subclass_of($p_first,"Icon") || is_a($p_first,"Icon")
            || is_subclass_of($p_first,"Input") || is_a($p_first,"Input")) {
            $this->_childs[0] = $p_first;
            if(is_string($p_second)) $this->_childs[1] = $p_second;
        }
    }
    
    public function setIcon($p_icon) {
        if(is_subclass_of($p_icon,"Icon") || is_a($p_icon,"Icon")) {
            $this->_childs[0] = $p_icon;
        }
    }
    
    public function setText($p_text) {
        if(is_string($p_text)) {
            $this->_childs[1] = $p_text;
        }
    }
    
    public function active($p_on=false) {
        $this->active = $p_on;
    }
    
    public function __toString() {
        $ret = '<table class="';
        if($this->active) $ret .= 'label_active';
        else $ret.= 'label';
        $ret.= '"><tr>';
        if(array_key_exists(0,$this->_childs)) {
            $ret.= '<td class="icon">'.$this->_childs[0]->__toString().'</td>';
        }
        if(array_key_exists(1,$this->_childs)) {
            $ret.= '<td class="text" ';
            foreach($this->_eventListeners as $eventListener) {
                $ret.= $eventListener->__toString();
            }
            $ret.= '>'.$this->_childs[1].'</td>';
        }
        $ret.= '</tr></table>';
        return str_replace("\n", "", $ret);
    }
    public function addEventListener($p_listener) {
        if(is_subclass_of($p_listener,"EventListener") || is_a($p_listener,"EventListener")) {
            $this->_eventListeners[$p_listener->event()] = $p_listener;
        }
    }
}

/*
 * CLASS Icon
 */
class Icon {
    private $_src = null;
    private $_eventListeners = array();
    
    public function __construct($p_src) {
        if(is_string($p_src)) {
            $this->_src = $p_src;
        }
    }
    
    public function __toString() {
        $ret = '<img ';
        foreach($this->_eventListeners as $eventListener) {
            $ret .= $eventListener->__toString();
        }
        $ret .= ' src="'.$this->_src.'">';
        return $ret;
    }
    public function addEventListener($p_listener) {
        if(is_subclass_of($p_listener,"EventListener") || is_a($p_listener,"EventListener")) {
            array_push($this->_eventListeners,$p_listener);
        }
    }
}

/*
 * CLASS Input
 */
class Input {
    private $_ID = null;
    private $_name = null;
    private $_class= null;
    private $_type;
    private $_value;
    private $_eventListeners = array();
    
    public function __construct($p_id,$p_name,$p_class,$p_type,$p_value = null,$p_selected = false) {
        $this->_ID = $p_id;
        $this->_name = $p_name;
        $this->_class = $p_class;
        $this->_type = $p_type;
        $this->_value = $p_value;
        $this->_selected = $p_selected;
    }
    
    public function addEventListener($p_listener) {
        if(is_subclass_of($p_listener,"EventListener") || is_a($p_listener,"EventListener")) {
            $this->_eventListeners[$p_listener->event()] = $p_listener;
        }
    }

    public function getEventListeners() {
        return $this->_eventListeners;
    }
    
    public function __toString() {
        $ret = "<input";
        if(!empty($this->_ID))$ret.= " id='".$this->_ID."'";
        if(!empty($this->_name))$ret.= " name='".$this->_name."'";
        if(!empty($this->_class))$ret.= " class='".$this->_class."'";
        if(!empty($this->_type))$ret.= " type='".$this->_type."'";
        if(!empty($this->_value))$ret.= " value='".$this->_value."'";
        if($this->_selected && $this->_type == "radio") $ret.= " checked='checked'";
        foreach($this->_eventListeners as $event => $eventListener) {
            $ret.= ' '.$eventListener->__toString();
        }
        $ret.= ">";
        return $ret;
    }
}

/*
 * CLASS Tree
 */
class Tree {
    private $_ID        = null;
    private $_renderer  = null;
    private $_childs    = null;
    private $_eventListeners = null;
    
    public function __construct($p_id) {
        $this->_ID          = $p_id;
        $this->_childs      = array();
        $this->_renderer    = new TreeRenderer();
    }
    
    public function getID() {
        return $this->_ID;
    }
    
    public function appendChild($p_node) {
        if(is_subclass_of($p_node,"TreeNode") || is_a($p_node,"TreeNode")) {
            array_push($this->_childs, $p_node);
        }
    }
    
    public function __toString() {
        return $this->_renderer->_toString($this);
    }
    
    public function hasChildNodes() {
        if(count($this->_childs) > 0)
            return true;
        else
            return false;
    }

    public function getChildNodes() {
        return $this->_childs;
    }
    
    public function addEventListener($p_listener) {
        if(is_subclass_of($p_listener,"EventListener") || is_a($p_listener,"EventListener")) {
            array_push($this->_eventListeners,$p_listener);
        }
    }

    public function getEventListeners() {
        return $this->_eventListeners;
    }
}

class TreeRenderer {
    public function __construct() {}
    
    public function _toString($p_tree) {
        $ret = '<div id="'.$p_tree->getID().'" class="tree">';
        $ret.= '<div class="treeDivider">&nbsp;</div>';
        if($p_tree->hasChildNodes()) {
            $childNodes = $p_tree->getChildNodes();
            foreach($childNodes as $childNode) {
                $ret.= $childNode->__toString();
                $ret.= '<div class="treeDivider">&nbsp;</div>'; 
            }
        }   
        $ret.= '</div>';
        return $ret;
    }
}

class TreeNode {
    private $_renderer  = null;
    private $_ID        = null;
    private $_value     = null;
    private $_childs    = null;
    private $_eventListeners = null;
    private $_contextMenu=null;
    
    public function __construct() {
        $this->_renderer = new TreeNodeRenderer();
        $this->_childs = array();
        $this->_eventListeners = array();
        
        $argsArr = func_get_args();
        if(count($argsArr)>0) {
            foreach($argsArr as $arg) {
                if(is_string($arg))
                    $this->_ID = $arg;
                else
                    $this->_value[] = $arg;
            }
        } else {
            $this->_value[] = new Label("no value");
        }
    }
    
    public function appendChild($p_node) {
        if(is_subclass_of($p_node,"TreeNode") || is_a($p_node,"TreeNode")) {
            array_push($this->_childs, $p_node);
        }       
    }
    
    public function __toString() {
        $ret = $this->_renderer->_toString($this);
        return $ret;
    }
    
    public function setRenderer($p_renderer) {
        if(is_subclass_of($p_listener,"TreeNodeRenderer") || is_a($p_listener,"TreeNodeRenderer")) {
            $this->_renderer = $p_renderer;
        }
    }
    
    public function hasChildNodes() {
        if(count($this->_childs) > 0)
            return true;
        else
            return false;
    }
    
    public function getID() {
        return $this->_ID;
    }
    
    public function getValue() {
        return $this->_value;
    }
    
    public function getChildNodes() {
        return $this->_childs;
    }

    public function addEventListener($p_listener) {
        if(is_subclass_of($p_listener,"EventListener") || is_a($p_listener,"EventListener")) {
            $this->_eventListeners[$p_listener->event()] = $p_listener;
        }
    }

    public function getEventListeners() {
        return $this->_eventListeners;
    }
    
    public function addContextMenu($p_menu) {
        if(is_subclass_of($p_menu,"ContextMenu") || is_a($p_menu,"ContextMenu")) {
            $this->_contextMenu = $p_menu;
        }
    }
    
    public function getContextMenu() {
        return $this->_contextMenu;
    }
}

class TreeNodeRenderer {
    public function __constructor() {}
    
    public function _toString($p_node) {
        $value = $p_node->getValue();
        $eventListeners = $p_node->getEventListeners();
        $contextMenu = $p_node->getContextMenu();
        
        if($p_node->hasChildNodes())
            $ret = '<div class="treeNode" id="';
        else
            $ret = '<div class="treeNodeLeaf" id="';
        if($p_node->getID() != null) $ret.= $p_node->getID();
        $ret.= '">';
            $ret.= '<div class="treeNodeValue">';
                $ret.='<div class="treeNodeListener"';
                $onmousedown = "true";
                $ronmousedown = "true";
                foreach($eventListeners as $event => $eventListener) {
                    if($event == "onmousedown") {
                        $onmousedown = $eventListener->action();
                    } elseif($event == "ronmousedown") {
                    	$ronmousedown = $eventListener->action();
                    } else {
                        $ret.= ' '.$eventListener->__toString();
                    }
                }
                if($contextMenu != null) {
                    $ret.=" onmousedown='Tree.select(event,this);if(Mouse.checkRClick(event)) { ContextMenu.show(this,event);".$ronmousedown." } else { ".$onmousedown.";};'";
                } else {
                    $ret.=" onmousedown='Tree.select(event,this);return ".$onmousedown."'";
                }
                $ret.='>&nbsp;</div>';
                if($p_node->hasChildNodes()) {
                    $ret.= "<img class='expander' src='img/GUI/Tree/expand.png' onclick='Tree.expand(this.parentNode.parentNode);'>";
                } else {
                    $ret.= "<img class='expander' src='img/GUI/Tree/transparent.png'>";
                }
                $ret.= '<a';
    
                foreach($eventListeners as $event => $eventListener) {
                    if($event == "onclick") {
                        $ret.= " href='javascript:' ".$eventListener->__toString();
                    }elseif($event != "onmousedown" && $event != "ronmousedown"){
                        $ret.= ' '.$eventListener->__toString();
                    }
                }
                if($contextMenu != null) {
                    $ret.=" onmousedown='Tree.select(event,this);(Mouse.checkRClick(event)) ? ".$ronmousedown." : false; return (Mouse.checkRClick(event)) ? ContextMenu.show(this,event) : ".$onmousedown.";'";
                } else {
                    $ret.=" onmousedown='Tree.select(event,this);return ".$onmousedown.";'";
                }
        $ret.= '>';
        foreach($value as $valueItem) {
            $ret.= $valueItem->__toString();
        }
        $ret.='</a>';
        $ret.='</div>';
        $ret.='<div class="treeNodeSub">';

        if($p_node->hasChildNodes()) {
        	$ret.= '<div class="treeDivider">&nbsp;</div>'; 
            $childs = $p_node->getChildNodes();
            foreach($childs as $child) {
                $ret.= $child->__toString();
                $ret.= '<div class="treeDivider">&nbsp;</div>'; 
            }
        }
        $ret.='</div>';
        $ret.='<div class="contextContainer">';
        if($contextMenu != null) {
            $ret.= $contextMenu->__toString();
        }
        $ret.= '</div>';
        $ret.= '</div>';
        return $ret;
    }
}

class EventListener {
    private $_event;
    private $_action;
    
    public function __construct($p_event,$p_action) {
        $this->_event = $p_event;
        $this->_action = $p_action;
    }
    
    public function __toString() {
        return $this->_event."='".$this->_action."'";
    }
    
    public function event($p_eventType=null) {
        return $this->_event;
    }
    
    public function action($p_action=null) {
        return $this->_action;
    }
}

class ContextMenu {
    private $_renderer  = null;
    private $_childs    = null;
    private $_eventListeners = null;
    
    public function __construct() {
        $this->_childs      = array();
        $this->_renderer    = new ContextMenuRenderer();
    }
    
    public function appendChild($p_node) {
        if(is_subclass_of($p_node,"ContextMenuNode") || is_a($p_node,"ContextMenuNode")) {
            array_push($this->_childs, $p_node);
        }
    }
    
    public function __toString() {
        return $this->_renderer->_toString($this);
    }
    
    public function hasChildNodes() {
        if(count($this->_childs) > 0)
            return true;
        else
            return false;
    }

    public function getChildNodes() {
        return $this->_childs;
    }
    
    public function addEventListener($p_listener) {
        if(is_subclass_of($p_listener,"EventListener") || is_a($p_listener,"EventListener")) {
            array_push($this->_eventListeners,$p_listener);
        }
    }

    public function getEventListeners() {
        return $this->_eventListeners;
    }
}

class ContextMenuRenderer {
    public function __construct() {}
    
    public function _toString($p_menu) {
        $ret = '<div class="contextMenu">';
        $ret.= '<div class="contextMenuDivider">&nbsp;</div>';
        if($p_menu->hasChildNodes()) {
            $childNodes = $p_menu->getChildNodes();
            foreach($childNodes as $childNode) {
                $ret.= $childNode->__toString();
                $ret.= '<div class="contextMenuDivider">&nbsp;</div>';  
            }
        }   
        $ret.= '</div>';
        return $ret;
    }
}

class ContextMenuNode {
    private $_renderer  = null;
    private $_value     = null;
    private $_childs    = null;
    private $_eventListeners = null;
    
    public function __construct($p_value = null) {
        $this->_renderer = new ContextMenuNodeRenderer();
        $this->_childs = array();
        $this->_eventListeners = array();
        
        if(!empty($p_value)) {
            $this->_value = $p_value;
        } else {
            $this->_value = new Label("undefined value");
        }
    }
    
    public function appendChild($p_node) {
        if(is_subclass_of($p_node,"ContextMenuNode") || is_a($p_node,"ContextMenuNode")) {
            array_push($this->_childs, $p_node);
        }       
    }
    
    public function __toString() {
        $ret = $this->_renderer->_toString($this);
        return $ret;
    }
    
    public function setRenderer($p_renderer) {
        if(is_subclass_of($p_listener,"ContextMenuNodeRenderer") || is_a($p_listener,"ContextMenuNodeRenderer")) {
            $this->_renderer = $p_renderer;
        }
    }
    
    public function hasChildNodes() {
        if(count($this->_childs) > 0)
            return true;
        else
            return false;
    }
    
    public function getValue() {
        return $this->_value;
    }
    
    public function getChildNodes() {
        return $this->_childs;
    }

    public function addEventListener($p_listener) {
        if(is_subclass_of($p_listener,"EventListener") || is_a($p_listener,"EventListener")) {
            array_push($this->_eventListeners,$p_listener);
        }
    }

    public function getEventListeners() {
        return $this->_eventListeners;
    }
}

class ContextMenuNodeRenderer {
    public function __constructor() {}
    
    public function _toString($p_node) {
        $value = $p_node->getValue();
        $eventListeners = $p_node->getEventListeners();
        
        $ret = '<div class="contextMenuNode">';
        $ret.= '<div class="contextMenuNodeValue">';
        $ret.= '<div class="contextMenuNodeListener"';
        foreach($eventListeners as $eventListener) {
            $ret.= ' '.$eventListener->__toString();
        }   
        $ret.= '>&nbsp;</div>';
        $ret.= '<a';
        foreach($eventListeners as $eventListener) {
            if($eventListener->event() == "onclick") {
                $ret.= " href='javascript:' onclick='".$eventListener->action()."'";
            }else{
                $ret.= ' '.$eventListener->__toString();
            }
        }
        $ret.= '>';
            $ret.= $value->__toString();
        $ret.='</a>';
        $ret.='</div>';
        $ret.='<div class="contextMenuNodeSub">';
        if($p_node->hasChildNodes()) {
            $childs = $p_node->getChildNodes();
            foreach($childs as $child) {
                $ret.= $child->__toString();
            }
        }
        $ret.='</div>';
        $ret.= '</div>';
        return $ret;
    }
}

?>