package org.mbari.vdm.knowledgebase;


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

import java.awt.Component;
import java.util.Arrays;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

//RxTBD: wcpr - Displayed font is different when traversing up and down the
//tree?

/**
* <p><!-- Class description --></p>
*
* @version    $Id: ConceptTreeCellRenderer.java,v 1.2 2005/12/14 03:24:33 achase Exp $
* @author     <a href="http://www.mbari.org">Monterey Bay Aquarium Research Institute</a>    
*/
public class ConceptTreeCellRenderer extends DefaultTreeCellRenderer {

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

  private static final String DETAILS = "/images/greenball.gif";
  private static final String IMAGE = "/images/showimage.jpg";
  private static final String DEFAULT = "/images/blueball.gif";

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

  private Icon defaultIcon;

  private Icon hasDetailsIcon;
  private Icon hasImageIcon;

  // Buffer for text label
  private StringBuffer textBuf;

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

  /**
   * Constructs ...
   *
   */
  public ConceptTreeCellRenderer() {
      super();
      
      hasDetailsIcon = new ImageIcon( SearchableTreePanel.class.getResource(DETAILS));
      defaultIcon = new ImageIcon( SearchableTreePanel.class.getResource(DEFAULT));
      hasImageIcon = new ImageIcon( SearchableTreePanel.class.getResource(IMAGE));

      // Utility buffer for stringing together Concept names and aliases
      textBuf = new StringBuffer();
  }

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

  // Override

  /**
   * <p><!-- Method description --></p>
   *
   *
   * @param tree
   * @param value
   * @param isSelected
   * @param isExpanded
   * @param isLeaf
   * @param row
   * @param hasFocus
   *
   * @return
   */
  public Component getTreeCellRendererComponent(JTree tree, Object value,
          boolean isSelected, boolean isExpanded, boolean isLeaf, int row,
          boolean hasFocus) {
      // RxNOTE wcpr After great consternation I finally discovered this line in
      // the implementation of this in DefaultTreeCellRenderer. Without it the
      // background of selected cells does not properly set.
      selected = isSelected;

      // Toggle cell background color
      if (isSelected) {
          setForeground(getTextSelectionColor());
      } else {
          setForeground(getTextNonSelectionColor());
      }

      // Get the name from the Object contained in this node
      DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
      Object userObject = node.getUserObject();

      // if the user object is a boolean value, then that means that
      // the concept nodes at this level are currently be retrieved.
      if (userObject instanceof Boolean) {
          setText("Retrieving Concepts...");

          return this;
      }

      TreeConcept treeConcept = (TreeConcept) userObject;

      // Put the capatilized primary ConceptName in first
      textBuf.replace(0, textBuf.length(), treeConcept.getName());
      textBuf.setCharAt(0, Character.toUpperCase(textBuf.charAt(0)));

      // now add the aliases
      String[] secondaryNames = treeConcept.getSecondaryNames();

      Arrays.sort(secondaryNames);

      if (0 < secondaryNames.length) {
          textBuf.append(" (");

          for (int i = 0; i < secondaryNames.length; i++) {
              textBuf.append(secondaryNames[i]);

              if (i != secondaryNames.length - 1) {
                  textBuf.append(", ");
              }
          }

          textBuf.append(")");
      }

      setText(textBuf.toString());

      // set the Icon based on Concept information
      if (treeConcept.hasDetails()) {
          setIcon(hasDetailsIcon);
      } else if (treeConcept.hasPrimaryImage()) {
          setIcon(hasImageIcon);
      } else {
          setIcon(defaultIcon);
      }

      return this;
  }

}
