FileDocCategorySizeDatePackage
GenealogyModel.javaAPI DocExample4041Tue Dec 12 18:59:20 GMT 2000None

GenealogyModel

public class GenealogyModel extends Object implements TreeModel

Fields Summary
private boolean
showAncestors
private Vector
treeModelListeners
private Person
rootPerson
Constructors Summary
public GenealogyModel(Person root)


       
        showAncestors = false;
        rootPerson = root;
    
Methods Summary
public voidaddTreeModelListener(javax.swing.event.TreeModelListener l)
Adds a listener for the TreeModelEvent posted after the tree changes.

        treeModelListeners.addElement(l);
    
protected voidfireTreeStructureChanged(Person oldRoot)
The only event raised by this model is TreeStructureChanged with the root as path, i.e. the whole tree has changed.

        int len = treeModelListeners.size();
        TreeModelEvent e = new TreeModelEvent(this, 
                                              new Object[] {oldRoot});
        for (int i = 0; i < len; i++) {
            ((TreeModelListener)treeModelListeners.elementAt(i)).
                    treeStructureChanged(e);
        }
    
public java.lang.ObjectgetChild(java.lang.Object parent, int index)
Returns the child of parent at index index in the parent's child array.

        Person p = (Person)parent;
        if (showAncestors) {
            if ((index > 0) && (p.getFather() != null)) {
                return p.getMother();
            }
            return p.getFather();
        }
        return p.getChildAt(index);
    
public intgetChildCount(java.lang.Object parent)
Returns the number of children of parent.

        Person p = (Person)parent;
        if (showAncestors) {
            int count = 0;
            if (p.getFather() != null) { 
                count++;
            }
            if (p.getMother() != null) { 
                count++;
            }
            return count;
        }
        return p.getChildCount();
    
public intgetIndexOfChild(java.lang.Object parent, java.lang.Object child)
Returns the index of child in parent.

        Person p = (Person)parent;
        if (showAncestors) {
            int count = 0;
            Person father = p.getFather();
            if (father != null) {
                count++;
                if (father == child) {
                    return 0;
                }
            }
            if (p.getMother() != child) {
                return count;
            }
            return -1;
        }
        return p.getIndexOfChild((Person)child);
    
public java.lang.ObjectgetRoot()
Returns the root of the tree.

        return rootPerson;
    
public booleanisLeaf(java.lang.Object node)
Returns true if node is a leaf.

        Person p = (Person)node;
        if (showAncestors) {
            return ((p.getFather() == null)
                 && (p.getMother() == null));
        }
        return p.getChildCount() == 0;
    
public voidremoveTreeModelListener(javax.swing.event.TreeModelListener l)
Removes a listener previously added with addTreeModelListener().

        treeModelListeners.removeElement(l);
    
public voidshowAncestor(boolean b, java.lang.Object newRoot)
Used to toggle between show ancestors/show descendant and to change the root of the tree.

        showAncestors = b;
        Person oldRoot = rootPerson;
        if (newRoot != null) {
           rootPerson = (Person)newRoot;
        }
        fireTreeStructureChanged(oldRoot);
    
public voidvalueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newValue)
Messaged when the user has altered the value for the item identified by path to newValue. Not used by this model.

        System.out.println("*** valueForPathChanged : "
                           + path + " --> " + newValue);