arcgissamples\scenario\toc\TocTreeRenderer.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
GIS Client
arcgissamples\scenario\toc\TocTreeRenderer.java
/* Copyright 2015 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.scenario.toc;

/**Class which defines custom renderer for TocControl.
 * Implements javax.swing.tree.TreeCellRenderer.
 */

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;


public class TocTreeRenderer  implements  javax.swing.tree.TreeCellRenderer {

  //CheckBoxPanel for displaying checkbox and layername
  CheckBoxPanel checkboxPanel = null;
  //LabelPanel to display mapframe title.
  LabelPanel labelPanel = null;
  //Default label to display if the node is neither a layer node nor mapframe node.
  JLabel label;
  //Mapframe icon.
  Icon mapFrameIcon = null;

  public TocTreeRenderer() {
    //Instantiate all classes
    checkboxPanel = new CheckBoxPanel();
    labelPanel = new LabelPanel();
    //Create mapframe icon using the layers_2.gif
    mapFrameIcon = new javax.swing.ImageIcon(
      getClass().getResource(
      "/arcgissamples/scenario/icons/layers_2.gif"));
    //Create a label
    label = new JLabel();
  }

  /** Method which returns the component based on the node type.
   * If node is of type of map frame then the LabelPanel is used.
   * LabelPanel is set with the mapframe icon and the title.
   * If the node is of type layer, then a check box panel is used with check box
   * to display the visibility of the layer and the check box label is set with the
   * layer name.
   * Note : When a visibility of layer is changed, this method is called as a result
   * of nodechanged event. The check box value is set based on the layer visibility.
   * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent
   * (JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
   * @param tree
   * @param value
   * @param sel
   * @param expanded
   * @param leaf
   * @param row
   * @param hasFocus
   * @return
   */

  public java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree,
      Object value, boolean sel, boolean expanded, boolean leaf, int row,
      boolean hasFocus) {
    //Get the string value of the selected node.
    String stringValue = tree.convertValueToText(value, sel,
                                                 expanded, leaf, row, hasFocus);
    //Get the path of the current node.
    javax.swing.tree.TreePath treePath = tree.getPathForRow(row);

    //Check if it is null. If so return the default label.
    if (treePath == null)
      return label;

    //Get node which is of type defaultmutable node.
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();

    //If it is map frame node.
    if (treePath.getPathCount() == 2) {
      //set the map frame icon
      labelPanel.setIcon(mapFrameIcon);
      //set the title
      labelPanel.setText(stringValue);
      return labelPanel;
    }

    //If it is a layer node.
    if (node instanceof LayerNode) {
      //Get the visibility of the layer
      boolean isVisible = ( (LayerNode) node).isVisible();
      //set the checkbox value based on the layer visibility
      checkboxPanel.setSelected(isVisible);
      //set the layer name
      checkboxPanel.setText(stringValue);
      return checkboxPanel;
    }

    //If not mapframe node or layer node return the default label.
    return label;
  }
}