// ------------------------------------------------------------ // this is What our base Object Do ! // Author: Walter D'Avila Neto. // All Rights... oh come on ... // ------------------------------------------------------------ // different from C++ where CObject Extends CNode .... class CNode extends Object { private String nodeName; private CNode nextNode = null; private CNode prevNode = null; private CNode parentNode = null; private CNode childNode = null; public CNode(){ parentNode = null; childNode = null; nextNode = this; prevNode = this; }; public CNode(String aNodeName){ nodeName = new String(aNodeName); parentNode = null; childNode = null; nextNode = this; prevNode = this; }; // Basic Getters .. public CNode getNextNode(){return nextNode; }; public CNode getPreviousNode() { return prevNode; }; public CNode getParentNode(){return parentNode;}; public CNode getChildNode() {return childNode;} public boolean hasParent() { return (parentNode != null) ;}; public boolean hasChild() { return (childNode != null) ;} ; public boolean hasNextNode() {return ( (nextNode != null) && (nextNode!= this) ); }; public boolean isFirstChild() { if (parentNode!= null) return (parentNode.getChildNode() == this); else return false; } // is this node the last child? public boolean isLastChild() { if (parentNode != null) return (parentNode.getChildNode().getPreviousNode() == this); else return false; } // Basic Setters .. public void setNextNode(CNode newBrother ){ this.nextNode = newBrother ; }; public void setParent(CNode newParent){ this.parentNode = newParent; }; public void setChild(CNode newChield){ this.childNode = newChield; newChield.setParent(this); }; public void setPreviousNode(CNode newBrother) { this.prevNode = newBrother; }; public void attachTo(CNode newParent) { if (!newParent.hasChild()) { newParent.setChild(this); } else { //CNode tempNode = new CNode(); this.nextNode = newParent.getChildNode(); this.prevNode = newParent.getChildNode().getPreviousNode(); newParent.getChildNode().getPreviousNode().setNextNode(this) ; newParent.getChildNode().setPreviousNode(this); } } // to be implemented ..... public void Attach( CNode newChild ){ return ; }; public void printRecursive(){ System.out.println(this.nodeName); if ( hasChild() ) { childNode.printRecursive(); } if (hasNextNode() && (!!isLastChild()) ) { nextNode.printRecursive(); } } // Recursive Method to Find Root // This can Help Alot .. well .. at least in game Engines : ) public CNode findRoot(){ if (this.hasParent()) { return parentNode.findRoot(); } return this; }; public static void main(String[] args) { CNode root = new CNode("Root"); CNode aChield = new CNode("1"); aChield.attachTo(root); aChield = new CNode("2");aChield.attachTo(root); aChield = new CNode("3");aChield.attachTo(root); aChield = new CNode("11");aChield.attachTo(root.getChildNode()); aChield = new CNode("12");aChield.attachTo(root.getChildNode()); root.printRecursive(); } }