package org.hwo.ui.tree; import java.util.LinkedList; import java.util.List; import org.hwo.collections.SortedList; public abstract class AutomatedTreeNode implements Comparable, AutomatedTreeNodeListener { AutomatedTreeNode parent; LinkedList automatedTreeNodeListeners; SortedList children; public AutomatedTreeNode(AutomatedTreeNode parent){ this.parent = parent; this.automatedTreeNodeListeners = new LinkedList(); this.children = new SortedList(); } public void addAutomatedTreeNodeListener(AutomatedTreeNodeListener listener){ this.automatedTreeNodeListeners.add(listener); }; public void removeAutomatedTreeNodeListener(AutomatedTreeNodeListener listener){ this.automatedTreeNodeListeners.remove(listener); }; private void fireChildInserted(AutomatedTreeNode parent,AutomatedTreeNode child){ for (AutomatedTreeNodeListener l: automatedTreeNodeListeners){ l.childInserted(parent, child); } } private void fireChildRemoved(AutomatedTreeNode parent,AutomatedTreeNode child, int index){ for (AutomatedTreeNodeListener l: automatedTreeNodeListeners){ l.childRemoved(parent, child, index); } } public AutomatedTreeNode[] getChildren(){ return children.toArray(new AutomatedTreeNode[0]); }; public boolean addChild(AutomatedTreeNode child){ if (children.add(child)){ child.addAutomatedTreeNodeListener(this); fireChildInserted(this,child); return true; } return false; }; public boolean removeChild(AutomatedTreeNode child){ int index = children.indexOf(child); if (children.remove(child)){ child.removeAutomatedTreeNodeListener(this); fireChildRemoved(this,child,index); return true; } return false; }; public AutomatedTreeNode getChild(int index){ return children.get(index); }; public int getIndexOfChild(AutomatedTreeNode child){ return children.indexOf(child); } public AutomatedTreeNode[] getPath(){ List path = new LinkedList(); getPath( path ); return path.toArray(new AutomatedTreeNode[0]); } protected void getPath(List pathlist){ if (this.parent != null) this.parent.getPath(pathlist); pathlist.add(this); } public boolean hasChild(AutomatedTreeNode child){ return children.contains(child); } @Override public void childInserted(AutomatedTreeNode node, AutomatedTreeNode child) { fireChildInserted(node, child); } @Override public void childRemoved(AutomatedTreeNode node, AutomatedTreeNode child, int index) { fireChildRemoved(node, child, index); } }