Class TreeNode
- All Implemented Interfaces:
TreeTable.Node
- Direct Known Subclasses:
TreeNode.Element
TreeTableView view. The TreeTableView class is used directly
only for the root node, or for nodes containing a fixed value instead of a value fetched from
the metadata object. For all other nodes, the actual node class shall be either TreeNode.Element
or TreeNode.CollectionElement.
The value of a node is extracted from the metadata object by getUserObject().
For each instance of TreeTableView, that value is always a singleton, never a collection.
If a metadata property is a collection, then there is an instance of the TreeNode.CollectionElement
subclass for each element in the collection.
The newChild() operation is supported if the node is not a leaf. The user shall
set the identifier and the value, in that order, before any other operation on the new child.
See newChild() javadoc for an example.
TreeNode.Element.indexInData
and TreeNode.CollectionElement.indexInList fields may not be stable.
The former may be invalid if the node is serialized and deserialized by two different versions of Apache SIS
having properties in different order. The second may be invalid if the collection is not guaranteed to preserve
order on serialization (e.g. CodeListSet with user supplied elements, in which case the elements order
depends on the instantiation order).- Since:
- 0.3
- Version:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classA node for an element in a collection.(package private) static classA node for a metadata property value.private final classThe proxy to be returned bynewChild(). -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final Class<?>The return type of the getter method that provides the value encapsulated by this node.(package private) ObjectThe value which existed when theTreeNodeChildren.iterator()traversed this node.private Collection<TreeTable.Node>The children of this node, ornullif not yet computed.private static final Collection<TreeTable.Node>The collection of children to return when the node does not allow children (i.e.(package private) final ObjectThe metadata object from which thegetUserObject()method will fetch the value.private CharSequenceThe value ofTableColumn.NAME, computed bygetName()then cached.private final TreeNodeThe parent of this node to be returned bygetParent(), ornullif this node is the root of the tree.(package private) final TreeTableViewThe table for which this node is an element. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidappendIdentifier(StringBuilder buffer) Appends an identifier for this node in the given buffer, fortoString()implementation.(package private) final voidappendStringTo(StringBuilder buffer) Implementation oftoString()appending the string representation in the given buffer.booleanReturnstrueif the given object is of the same class than this node and contains a reference to the same metadata object.final Collection<TreeTable.Node>Returns the children of this node, or an empty set if none.private TreeNodeChildrenReturns the children if the value policy isValueExistencePolicy.COMPACT, ornullotherwise.(package private) StringReturns the UML identifier defined by the standard.(package private) IntegergetIndex()Returns the index in the collection if the metadata property type is a collection, ornullotherwise.(package private) CharSequencegetName()Gets the human-readable name of this node.final TreeTable.NodeReturns the parent node, ornullif this node is the root of the tree.(package private) CharSequenceGets remarks about the value in this node, ornullif none.The metadata value for this node, to be returned bygetValue(TableColumn.VALUE).final <V> VgetValue(TableColumn<V> column) Returns the value of this node in the given column, ornullif none.inthashCode()Returns a hash code value for this node.final booleanisEditable(TableColumn<?> column) Returnstrueif the given column isTableColumn.VALUEand the property is writable, orfalsein all other cases.final booleanisLeaf()Returnsfalseif the value is a metadata object (and consequently can have children), ortrueif the value is not a metadata object.(package private) final booleanisMetadata(Class<?> type) Returnstrueif nodes for values of the given type can be expanded with more children.(package private) booleanReturnstrueif the metadata value can be set.private CacheKeykey()Returns the key to use for calls toMetadataStandardmethods.final TreeTable.NodenewChild()Returns a proxy for a new property to be defined in the metadata object.(package private) voidsetUserObject(Object value) Sets the metadata value for this node.final <V> voidsetValue(TableColumn<V> column, V value) Sets the value if the given column isTableColumn.VALUE.final StringtoString()Returns a string representation of this node for debugging purpose.private StringunmodifiableCellValue(TableColumn<?> column) Returns the error message for an unmodifiable cell value in the given column.
-
Field Details
-
LEAF
The collection of children to return when the node does not allow children (i.e. is a leaf). This constant is also used as a sentinel value byisLeaf().We choose an empty set instead of an empty list because
TreeNodeChildrendoes not implement theListinterface. So we are better to never give to the user a collection implementingListin order to signal incorrect casts sooner. -
table
The table for which this node is an element. Contains information like the metadata standard and the value existence policy.All
TreeNodeinstances in the same tree have a reference to the sameTreeTableViewinstance. -
parent
The parent of this node to be returned bygetParent(), ornullif this node is the root of the tree.- See Also:
-
metadata
The metadata object from which thegetUserObject()method will fetch the value. The value is fetched in different ways, which depend on theTreeNodesubclass:- For
TreeNode(the root of the tree), the value is directlymetadata. - For
TreeNode.Element(a metadata property which is not a collection), the value isaccessor.get(indexInData, metadata). - For
TreeNode.CollectionElement(an element in a collection), another index is used for fetching the element in that collection.
- See Also:
- For
-
baseType
The return type of the getter method that provides the value encapsulated by this node. This information is used for filtering aspects when a class opportunistically implements many interfaces. This value is part of theCacheKeyneeded for invokingMetadataStandardmethods. -
name
The value ofTableColumn.NAME, computed bygetName()then cached.- See Also:
-
children
The children of this node, ornullif not yet computed. If and only if the node cannot have children (i.e. is a leaf), then this field is set toLEAF.- See Also:
-
cachedValue
The value which existed when theTreeNodeChildren.iterator()traversed this node. This value is cached on the assumption that users will ask for value or for children soon after they iterated over this node. The cached value is cleared after its first use.This value shall be either
null, or the exact same value than what a call togetUserObject()would return, assuming that the underlying metadata object didn't changed.The purpose of this cache is to avoid invoking (by reflection) the same getter methods twice in common situations like the
TreeTableView.toString()implementation or in Graphical User Interface. However, we may remove this field in any future SIS version if experience shows that it is more problematic than helpful.
-
-
Constructor Details
-
TreeNode
TreeNode(TreeTableView table, Object metadata, Class<?> baseType) Creates the root node of a new metadata tree table.- Parameters:
table- the table which is creating this root node.metadata- the root metadata object (cannot be null).baseType- the return type of the getter method that provides the value encapsulated by this node.
-
TreeNode
Creates a new child for an element of the given metadata. This constructor is for theTreeNode.Elementsubclass only.- Parameters:
parent- the parent of this node.metadata- the metadata object for which this node will be a value.baseType- the return type of the getter method that provides the value encapsulated by this node.
-
-
Method Details
-
isMetadata
Returnstrueif nodes for values of the given type can be expanded with more children. A return value offalsemeans that values of the given type are leaves. -
key
Returns the key to use for calls toMetadataStandardmethods. This key is used only for some default method implementations in the root node; children will use the class of their node value instead. -
getIdentifier
String getIdentifier()Returns the UML identifier defined by the standard. The default implementation is suitable only for the root node, since it returns the class identifier. Subclasses must override in order to return the property identifier instead. -
getIndex
Integer getIndex()Returns the index in the collection if the metadata property type is a collection, ornullotherwise. The (identifier, index) pair can be used as a primary key for identifying this node among its siblings. -
getName
CharSequence getName()Gets the human-readable name of this node. The name shall be stable, since it will be cached by the caller. The name typically contains identifier and index information, eventually localized.The default implementation is suitable only for the root node - subclasses must override.
-
getRemarks
CharSequence getRemarks()Gets remarks about the value in this node, ornullif none. -
appendIdentifier
Appends an identifier for this node in the given buffer, fortoString()implementation. The appended value is similar to the value returned bygetIdentifier()(except for the root node), but may contains additional information like the index in a collection.The default implementation is suitable only for the root node - subclasses must override.
-
getUserObject
The metadata value for this node, to be returned bygetValue(TableColumn.VALUE). The default implementation is suitable only for the root node - subclasses must override.- Specified by:
getUserObjectin interfaceTreeTable.Node- Returns:
- any object stored at this node by the user, or
nullif none.
-
setUserObject
Sets the metadata value for this node. Subclasses must override this method.- Throws:
UnsupportedOperationException- if the metadata value is not writable.
-
isWritable
boolean isWritable()Returnstrueif the metadata value can be set. Subclasses must override this method. -
equals
Returnstrueif the given object is of the same class than this node and contains a reference to the same metadata object. SinceTreeNodegenerates all content from the wrapped metadata, this condition should ensure that two equal nodes have the same values and children.- Specified by:
equalsin interfaceTreeTable.Node- Overrides:
equalsin classObject- Parameters:
other- the other object to compare with this node.- Returns:
- whether the two objects are nodes with equal values and equal children, ignoring parents.
-
hashCode
public int hashCode()Returns a hash code value for this node.- Specified by:
hashCodein interfaceTreeTable.Node- Overrides:
hashCodein classObject- Returns:
- a hash code for this node, potentially based on values and children but ignoring parent.
-
getParent
Returns the parent node, ornullif this node is the root of the tree.- Specified by:
getParentin interfaceTreeTable.Node- Returns:
- the parent, or
nullif none.
-
isLeaf
public final boolean isLeaf()Returnsfalseif the value is a metadata object (and consequently can have children), ortrueif the value is not a metadata object.- Specified by:
isLeafin interfaceTreeTable.Node- Returns:
trueif this node cannot have any children.
-
getChildren
Returns the children of this node, or an empty set if none. Only metadata object can have children.- Specified by:
getChildrenin interfaceTreeTable.Node- Returns:
- the children, or an empty collection if none.
-
newChild
Returns a proxy for a new property to be defined in the metadata object. The user shall set the identifier and the value, in that order, before any other operation on the new child. Example: Do not keep a reference to the returned node for a long time, since it is only a proxy toward the real node to be created once the identifier is known.- Specified by:
newChildin interfaceTreeTable.Node- Returns:
- the new child.
- Throws:
UnsupportedOperationException- if this node is a leaf.
-
getCompactChildren
Returns the children if the value policy isValueExistencePolicy.COMPACT, ornullotherwise. -
getValue
Returns the value of this node in the given column, ornullif none. This method verifies thecolumnargument, then delegates togetName(),getUserObject()or other properties.- Specified by:
getValuein interfaceTreeTable.Node- Type Parameters:
V- the base type of values in the given column.- Parameters:
column- identifier of the column from which to get the value.- Returns:
- the value in the given column, or
nullif none. - See Also:
-
setValue
Sets the value if the given column isTableColumn.VALUE. This method verifies thecolumnargument, then delegates tosetUserObject(Object).This method does not accept null value, because setting a singleton property to null with
ValueExistencePolicy.NON_EMPTYis equivalent to removing the property, and setting a collection element to null is not allowed. Those various behavior are at risk of causing confusion, so we are better to never allow null.- Specified by:
setValuein interfaceTreeTable.Node- Type Parameters:
V- the base type of values in the given column.- Parameters:
column- identifier of the column into which to set the value.value- the value to set.- Throws:
UnsupportedOperationException- if values in the given column cannot be modified.- See Also:
-
unmodifiableCellValue
Returns the error message for an unmodifiable cell value in the given column. -
isEditable
Returnstrueif the given column isTableColumn.VALUEand the property is writable, orfalsein all other cases. This method verifies thecolumnargument, then delegates toisWritable().- Specified by:
isEditablein interfaceTreeTable.Node- Parameters:
column- the column to query.- Returns:
trueif the given column is a legal column for thisNodeimplementation and the corresponding value is editable, orfalseotherwise.
-
toString
Returns a string representation of this node for debugging purpose. -
appendStringTo
Implementation oftoString()appending the string representation in the given buffer.
-