Java Swing TreeModel create custom tree model

Description

Java Swing TreeModel create custom tree model


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

class Person {/*  ww  w . j a v  a2s  . com*/
   Person father;
   Person mother;
   Vector<Person> children;
   private String name;

   public Person(String name) {
      this.name = name;
      mother = father = null;
      children = new Vector<Person>();
   }

   public static void linkFamily(Person pa, Person ma, Person[] kids) {
      for (Person kid : kids) {
         pa.children.addElement(kid);
         ma.children.addElement(kid);
         kid.father = pa;
         kid.mother = ma;
      }
   }

   public String toString() {
      return name;
   }

   public String getName() {
      return name;
   }

   public Person getFather() {
      return father;
   }

   public Person getMother() {
      return mother;
   }

   public int getChildCount() {
      return children.size();
   }

   public Person getChildAt(int i) {
      return (Person) children.elementAt(i);
   }

   public int getIndexOfChild(Person kid) {
      return children.indexOf(kid);
   }
}

class FamilyTree extends JTree {
   FamilyModel model;

   public FamilyTree(Person graphNode) {
      super(new FamilyModel(graphNode));
      getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
   }

   public void showAncestor(boolean b) {
      Object newRoot = null;
      TreePath path = getSelectionModel().getSelectionPath();
      if (path != null) {
         newRoot = path.getLastPathComponent();
      }
      ((FamilyModel) getModel()).showAncestor(b, newRoot);
   }
}

class FamilyModel implements TreeModel {
   private boolean showAncestors;
   private Vector<TreeModelListener> treeModelListeners = new Vector<TreeModelListener>();
   private Person rootPerson;

   public FamilyModel(Person root) {
      showAncestors = false;
      rootPerson = root;
   }

   public void showAncestor(boolean b, Object newRoot) {
      showAncestors = b;
      Person oldRoot = rootPerson;
      if (newRoot != null) {
         rootPerson = (Person) newRoot;
      }
      fireTreeStructureChanged(oldRoot);
   }

   protected void fireTreeStructureChanged(Person oldRoot) {
      TreeModelEvent e = new TreeModelEvent(this, new Object[] { oldRoot });
      for (TreeModelListener tml : treeModelListeners) {
         tml.treeStructureChanged(e);
      }
   }

   public void addTreeModelListener(TreeModelListener l) {
      treeModelListeners.addElement(l);
   }

   public Object getChild(Object parent, int index) {
      Person p = (Person) parent;
      if (showAncestors) {
         if ((index > 0) && (p.getFather() != null)) {
            return p.getMother();
         }
         return p.getFather();
      }
      return p.getChildAt(index);
   }

   public int getChildCount(Object 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 int getIndexOfChild(Object parent, Object child) {
      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 Object getRoot() {
      return rootPerson;
   }

   public boolean isLeaf(Object node) {
      Person p = (Person) node;
      if (showAncestors) {
         return ((p.getFather() == null) && (p.getMother() == null));
      }
      return p.getChildCount() == 0;
   }

   public void removeTreeModelListener(TreeModelListener l) {
      treeModelListeners.removeElement(l);
   }

   public void valueForPathChanged(TreePath path, Object newValue) {
      System.out.println("*** valueForPathChanged : " + path + " --> " + newValue);
   }
}

public class Main extends JPanel implements ActionListener {
   FamilyTree tree;
   private static String SHOW_ANCESTOR_CMD = "showAncestor";

   public Main() {
      super(new BorderLayout());

      // Construct the panel with the toggle buttons.
      JRadioButton showDescendant = new JRadioButton("Show descendants", true);
      final JRadioButton showAncestor = new JRadioButton("Show ancestors");
      ButtonGroup bGroup = new ButtonGroup();
      bGroup.add(showDescendant);
      bGroup.add(showAncestor);
      showDescendant.addActionListener(this);
      showAncestor.addActionListener(this);
      showAncestor.setActionCommand(SHOW_ANCESTOR_CMD);
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(showDescendant);
      buttonPanel.add(showAncestor);

      // Construct the tree.
      tree = new FamilyTree(getGenealogyGraph());
      JScrollPane scrollPane = new JScrollPane(tree);
      scrollPane.setPreferredSize(new Dimension(200, 200));

      // Add everything to this panel.
      add(buttonPanel, BorderLayout.PAGE_START);
      add(scrollPane, BorderLayout.CENTER);
   }

   public void actionPerformed(ActionEvent ae) {
      if (ae.getActionCommand() == SHOW_ANCESTOR_CMD) {
         tree.showAncestor(true);
      } else {
         tree.showAncestor(false);
      }
   }

   public Person getGenealogyGraph() {
      // the greatgrandparent generation
      Person a1 = new Person("A");
      Person a2 = new Person("B");
      Person a3 = new Person("C");
      Person a4 = new Person("D");
      Person a5 = new Person("E");
      Person a6 = new Person("F");

      // the grandparent generation
      Person b1 = new Person("G");
      Person b2 = new Person("H");
      Person b3 = new Person("I");
      Person b4 = new Person("J");
      Person b5 = new Person("K");
      Person b6 = new Person("L");
      Person b7 = new Person("M");
      Person b8 = new Person("N");
      Person b9 = new Person("O");

      // the parent generation
      Person c1 = new Person("P");
      Person c2 = new Person("Q");
      Person c3 = new Person("R");
      Person c4 = new Person("S");
      Person c5 = new Person("T");
      Person c6 = new Person("U");
      Person c7 = new Person("V");
      Person c8 = new Person("W");
      Person c9 = new Person("X");

      // the youngest generation
      Person d1 = new Person("X");
      Person d2 = new Person("Y");

      Person.linkFamily(a1, a2, new Person[] { b1, b2, b3, b4 });
      Person.linkFamily(a3, a4, new Person[] { b5, b6, b7 });
      Person.linkFamily(a5, a6, new Person[] { b8, b9 });
      Person.linkFamily(b3, b6, new Person[] { c1, c2, c3 });
      Person.linkFamily(b4, b5, new Person[] { c4, c5, c6 });
      Person.linkFamily(b8, b7, new Person[] { c7, c8, c9 });
      Person.linkFamily(c4, c7, new Person[] { d1, d2 });

      return a1;
   }

   public static void main(String[] args) {
      JFrame frame = new JFrame("GenealogyExample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      Main newContentPane = new Main();
      frame.setContentPane(newContentPane);

      frame.pack();
      frame.setVisible(true);
   }
}



PreviousNext

Related