gov.nasa.worldwind.util.layertree.LayerTreeModel Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2012 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
package gov.nasa.worldwind.util.layertree;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.layers.*;
import gov.nasa.worldwind.util.Logging;
import gov.nasa.worldwind.util.tree.*;
/**
* A tree model representing {@link gov.nasa.worldwind.layers.Layer}
objects and their content.
* LayerTreeModel
initializes itself with a default root node. By default, this root node is of type
* BasicTreeNode
and its text is "Layers". Nodes added under the tree model's root should always be of type
* {@link LayerTreeNode}
. a LayerTreeNode
may be of any type.
*
* LayerTreeModel
provides operations for performing the following common tasks on a layer tree:
* - Adding a layer node.
- Removing all layer nodes.
- Refreshing the layer nodes from a
{@link
* gov.nasa.worldwind.layers.LayerList}
.
*
* By default, the tree model does not include layers marked as hidden. This allows an application to prevent certain
* layers in a LayerList
from appearing in the tree. For example, the layer that renders the tree itself
* usually should not appear in the tree. If it did then the user could turn off the tree layer and have no way of
* getting it back. A layer can be marked as hidden by setting AVKey.HIDDEN
to true
:
*
* hiddenLayer.setValue(AVKey.HIDDEN, true); // Prevent layer from being displayed in the layer tree
*
* @author dcollins
* @version $Id: LayerTreeModel.java 1171 2013-02-11 21:45:02Z dcollins $
*/
public class LayerTreeModel extends BasicTreeModel
{
/** The default root name: "Layers". */
protected static final String DEFAULT_ROOT_NAME = "Layers";
/** Indicates whether or not the tree model must include hidden layers. */
protected boolean includeHiddenLayers;
/** Creates a new LayerTreeModel
with the default root node. Otherwise the new model is empty. */
public LayerTreeModel()
{
this.initialize();
}
/**
* Creates a new LayerTreeModel
with the default root node and adds a new LayerTreeNode
* for each non-hidden Layer
in the specified layerList
. The tree will not include layers
* marked as hidden.
*
* @param layerList the list of Layer
objects to the new model represents.
*
* @throws IllegalArgumentException if the layerList
is null
.
*/
public LayerTreeModel(LayerList layerList)
{
this(layerList, false);
}
/**
* Creates a new LayerTreeModel
with the default root node and adds a new LayerTreeNode
* for each Layer
in the specified layerList
.
*
* @param layerList the list of Layer
objects to the new model represents.
* @param includeHiddenLayers if this parameter is true
, layers marked as hidden will be included in
* the tree. Otherwise hidden layers will not be included in the tree.
*
* @throws IllegalArgumentException if the layerList
is null
.
*/
public LayerTreeModel(LayerList layerList, boolean includeHiddenLayers)
{
if (layerList == null)
{
String message = Logging.getMessage("nullValue.LayersListArrayIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
this.initialize();
this.includeHiddenLayers = includeHiddenLayers;
this.refresh(layerList);
}
/**
* Indicates whether or not this tree model includes layers marked as hidden.
*
* @return true
if hidden layers are included in the tree mode. false
if hidden layers are
* not included.
*/
public boolean isIncludeHiddenLayers()
{
return this.includeHiddenLayers;
}
/**
* Specifies whether or not this tree model includes layers marked as hidden. Changes will take effect on the next
* call to {@link #refresh(gov.nasa.worldwind.layers.LayerList) refresh}. A layer can be marked as hidden by setting
* the value for key AVKey.HIDDEN
to true
.
*
* @param includeHiddenLayers true
if the tree model should include hidden layers. false
* if the model should ignore layers marked as hidden.
*/
public void setIncludeHiddenLayers(boolean includeHiddenLayers)
{
this.includeHiddenLayers = includeHiddenLayers;
}
/** Initializes this tree model with the default root node. */
protected void initialize()
{
this.setRoot(this.createRootNode());
}
/**
* Returns a new root TreeNode
for this tree model. Called from initialize
.
*
* @return a new TreeNode
.
*/
protected TreeNode createRootNode()
{
return new BasicTreeNode(DEFAULT_ROOT_NAME);
}
/**
* Adds the specified layerNode
to this tree model's root node. Nodes added under this tree model's
* root should always be of type {@link LayerTreeNode}
. Note: this method adds the layer to the tree
* model regardless of whether or not the layer is marked as hidden.
*
* @param layerNode the layer node to add.
*
* @throws IllegalArgumentException if the layerNode
is null
.
*/
public void addLayer(LayerTreeNode layerNode)
{
if (layerNode == null)
{
String message = Logging.getMessage("nullValue.TreeNodeIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
this.getRoot().addChild(layerNode);
}
/**
* Adds the a new LayerTreeNode
created with the specified layer
to this tree model's root
* node. Nodes added under this tree model's root should always be of type {@link LayerTreeNode}
. Note:
* this method adds the layer to the tree model regardless of whether or not the layer is marked as hidden.
*
* @param layer the layer to add.
*
* @return the LayerTreeNode
created for the specified layer
.
*
* @throws IllegalArgumentException if the layer
is null
.
*/
public LayerTreeNode addLayer(Layer layer)
{
if (layer == null)
{
String message = Logging.getMessage("nullValue.LayerIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
LayerTreeNode layerNode = this.createLayerNode(layer);
if (layerNode == null)
return layerNode;
this.addLayer(layerNode);
return layerNode;
}
/**
* Returns a new root LayerTreeNode
for the specified layer
. Called from
* addLayer(Layer)
.
*
* @param layer the Layer
to create a new LayerTreeNode
for.
*
* @return a new LayerTreeNode
.
*/
protected LayerTreeNode createLayerNode(Layer layer)
{
return new LayerTreeNode(layer);
}
/** Clears this tree model by removing all children of the root node. */
public void removeAllLayers()
{
this.getRoot().removeAllChildren();
}
/**
* Refreshes this tree model's layer nodes with the specified layerList
. Clears this tree model by
* removing all children of the root node, then adds a new LayerTreeNode
for each Layer
in
* the specified layerList
. Layers marked as hidden will not be included in the tree model, unless the
* includeHiddenLayers
property is set to true
.
*
* @param layerList the list of Layer
objects to the new model represents.
*
* @throws IllegalArgumentException if the layerList
is null
.
* @see #setIncludeHiddenLayers(boolean)
*/
public void refresh(LayerList layerList)
{
if (layerList == null)
{
String message = Logging.getMessage("nullValue.LayersListArrayIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// Replace all the layer nodes in the tree with nodes for the current layers.
this.removeAllLayers();
for (Layer layer : layerList)
{
if (this.mustIncludeLayer(layer))
{
this.addLayer(layer);
}
}
}
/**
* Determines if a layer must be included in the layer tree.
*
* @param layer Layer to test.
*
* @return true
if the layer must be included in the tree, false
if the layer must not be
* included.
*/
protected boolean mustIncludeLayer(Layer layer)
{
return this.isIncludeHiddenLayers() || layer.getValue(AVKey.HIDDEN) != Boolean.TRUE;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy