java-org.hwo.ui/src/org/hwo/ui/tree/AutomatedTreeNode.java

99 lines
2.6 KiB
Java

package org.hwo.ui.tree;
import java.util.LinkedList;
import java.util.List;
import org.hwo.collections.SortedList;
public abstract class AutomatedTreeNode implements Comparable<AutomatedTreeNode>, AutomatedTreeNodeListener {
AutomatedTreeNode parent;
LinkedList<AutomatedTreeNodeListener>
automatedTreeNodeListeners;
SortedList<AutomatedTreeNode> children;
public AutomatedTreeNode(AutomatedTreeNode parent){
this.parent = parent;
this.automatedTreeNodeListeners = new LinkedList<AutomatedTreeNodeListener>();
this.children = new SortedList<AutomatedTreeNode>();
}
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<AutomatedTreeNode> path = new LinkedList<AutomatedTreeNode>();
getPath( path );
return path.toArray(new AutomatedTreeNode[0]);
}
protected void getPath(List<AutomatedTreeNode> 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);
}
}