English

How To: Remove or disable checkbox interaction with a Legend or LegendNode

Summary

By default, when a Table of Contents (TOC) or tree TOC is registered with a map, any layer added to the map is also added as a Legend or LegendNode to the TOC or tree TOC, respectively. A layer's legend to enable/disable visibility, select the layer, view the classification scheme, etc. can be manipulated by the user.

It may be necessary to disable the ability to change visibility of the layer, or remove the layer from the TOC or tree TOC completely. In most cases, this involves more than a simple method call. In addition, a TOC and tree TOC require different techniques to accomplish this task.

Procedure

The examples below use the 'layername' argument to illustrate how to filter the layer on which these changes should be applied.

  • For a tree TOC:

    To remove a layer's LegendNode from the tree TOC, so the layer is visible in the Map but not the tree TOC, use the following code:

    Code:
    MyTreeToc1.removeLegendNode(MyTreeToc1.findLegendNode(layer));


    To view the layer's LegendNode in the tree TOC, but disable the ability to change its visibility, a custom tree TOC and LegendNode class must be generated. The custom LegendNode class overrides the setChecked() method to disable the ability to check\uncheck the checkbox in the LegendNode. The custom tree TOC is created to use the custom LegendNode. Use the following code as a guide:

    Code:
    public class MyTreeToc extends com.esri.mo2.ui.toc.TreeToc{

    public com.esri.mo2.ui.toc.LegendNode createLegendNode(com.esri.mo2.map.dpy.Layer layer){
    if (layer.getName().equalsIgnoreCase("layername")) {
    MyCustomFeatureLegendNode flayernode = new MyCustomFeatureLegendNode(this, (FeatureLayer) layer);
    return flayernode;
    } else {
    return super.createLegendNode(layer);
    }
    }
    }

    public class MyCustomFeatureLegendNode extends FeatureLegendNode{
    public MyCustomFeatureLegendNode(TreeToc toc, FeatureLayer flayer){
    super(toc, flayer);
    }
    public void setChecked(boolean tf){
    // Do nothing, checkbox cannot be changed
    }
    }

  • For a TOC:

    To remove a layer's Legend from a TOC, so the layer is visible in the Map but not the TOC, a custom TOC class must be generated. Override the createLegend() method to return null for the layer to be removed from the TOC. To view the layer's Legend in the TOC, but disable the ability to change its visibility: create a Legend, retrieve the checkbox and remove the MouseListener on the checkbox. The MouseListener is designed to change the visibility of the layer associated with the Legend. See the comments in the code below:

    Code:
    public class MyToc extends Toc {

    public Legend createLegend(com.esri.mo2.map.dpy.Layer layer) {
    if (layer.getName().equalsIgnoreCase("layername")) {

    // To remove a layer from the TOC, but not the map:
    return null;

    // To see the layer in the map and TOC, but disable the ability to change layer visibility:
    Legend legend = super.createLegend(layer);
    JComponent jc = legend.getCheckBox();
    java.awt.event.MouseListener[] mlist = jc.getMouseListeners();
    jc.removeMouseListener(mlist[0]);
    return legend;

    } else {
    return super.createLegend(layer);
    }
    }
    }