package org.mbari.vdm.knowledgebase;

//~--- JDK imports ------------------------------------------------------------

import javax.swing.SwingUtilities;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
* @author achase
*
*
*/
public class ConceptTreeLazyLoader implements TreeExpansionListener {

  //~--- fields -------------------------------------------------------------

  private final DefaultTreeModel treeModel;

  //~--- constructors -------------------------------------------------------

  /**
   * Constructs ...
   *
   *
   * @param model
   */
  public ConceptTreeLazyLoader(DefaultTreeModel model) {
      treeModel = model;
  }

  //~--- methods ------------------------------------------------------------

  /*
   *  (non-Javadoc)
   * @see javax.swing.event.TreeExpansionListener#treeCollapsed(javax.swing.event.TreeExpansionEvent)
   */

  /**
   * <p><!-- Method description --></p>
   *
   *
   * @param event
   */
  public void treeCollapsed(TreeExpansionEvent event) {
      // TODO Auto-generated method stub

  }

  /*
   *  (non-Javadoc)
   * @see javax.swing.event.TreeExpansionListener#treeExpanded(javax.swing.event.TreeExpansionEvent)
   */

  /**
   * <p><!-- Method description --></p>
   *
   *
   * @param event
   */
  public void treeExpanded(final TreeExpansionEvent event) {
      final DefaultMutableTreeNode node =
          (DefaultMutableTreeNode) event.getPath().getLastPathComponent();
      final TreeConcept treeConcept = (TreeConcept) node.getUserObject();
      Thread lazyLoader = new Thread() {

          public void run() {
              if ((treeConcept != null) && treeConcept.lazyExpand(node)) {
                  SwingUtilities.invokeLater(new Runnable() {

                      public void run() {
                          treeModel.reload(node);
                      }

                  });
              }
          }
      };

      lazyLoader.start();
  }

}
