Is it possible to detect whether a node is expanded or collapsed from within SwingX's TreeTableModel/AbstractTreeTableModel (specifically the getValueAt(Object node, int index)
method)? I need to display different values for a parent node, depending on whether it's expanded or collapsed. (I realize this violates the principle of separating model and view, sorry!)
I know you can check this from the JXTreeTable object, using the standard isExpanded()
and isCollapsed()
methods, but I need to know this from within the model.
Specifically, I have objects which have multiple versions, and I'd like to use JXTreeTable to support expanding/collapsing the versions. If the object is collapsed, I want the parent node to display a summary of the values across all versions. If it's expanded, I want the parent to display only the values for the current version, (the most important one), and a summary is no longer needed or desired.
Some pseudo-code to give you an idea of what I mean:
getValueAt(Object node, int index) {
if (node.hasChildren()) {
if (node.isExpanded()) { // this is the part I'm not sure how to implement
return node.getCurrentValue(); // eg "Current version: C"
}
else {
return node.getSummaryValue(); // eg "Current version: C; previous versions: A, B"
}
}
else {
return node.getValue(); // eg "Version: B"
}
}
Thank you camickr, you're of course right that my question is invalid! (I feel a bit stupid now.)
Would you suggest using a custom TreeCellRenderer, such that it selects which value to display based on the expansion state of the node? That is, have the model provide an object that implements something like this:
public interface ExpansionStateDependentValue {
public Object getDisplayValue(boolean expanded);
}
and then have the TreeCellRenderer use this method (assuming the cell value implements the interface) to display the appropriate value based on the expanded
flag in the getTreeCellRendererComponent()
method?
but I need to know this from within the model.
A Model can be shared by many View component. That is it could be used by two different JTree Objects. So it is possible that a node code be expanded in one view of the tree but not in the other.
So the answer to your question is that the Model does not contain this information because it is a function of the view. So your approach of trying to do this in the model is not correct.