convex.gui.state.StateTreeNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of convex-gui Show documentation
Show all versions of convex-gui Show documentation
Convex desktop GUI and test applications
The newest version!
package convex.gui.state;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import convex.core.data.Ref;
import convex.core.data.ACell;
import convex.core.data.Cells;
import convex.core.util.Utils;
@SuppressWarnings("serial")
public class StateTreeNode extends DefaultMutableTreeNode {
private final T object;
private final boolean isContainer;
public StateTreeNode(T o) {
this.object = o;
this.isContainer = Cells.refCount(o)>0;
}
private static StateTreeNode create(R value) {
return new StateTreeNode(value);
}
@Override
public TreeNode getChildAt(int childIndex) {
if (isContainer) {
ACell child = object.getRef(childIndex).getValue();
return StateTreeNode.create(child);
}
return null;
}
@Override
public int getChildCount() {
return isContainer ? ((ACell) object).getRefCount() : 0;
}
@Override
public TreeNode getParent() {
return null;
}
@Override
public int getIndex(TreeNode node) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean getAllowsChildren() {
return isContainer;
}
@Override
public boolean isLeaf() {
return getChildCount() == 0;
}
@SuppressWarnings("unchecked")
@Override
public Enumeration children() {
Ref[] childRefs = (isContainer ? object.getChildRefs() : new Ref[0]);
ArrayList tns = new ArrayList<>();
for (Ref r : childRefs) {
tns.add(StateTreeNode.create(r.getValue()));
}
return Collections.enumeration(tns);
}
@Override
public String toString() {
return Utils.getClassName(object); // +" : "+Utils.toString(object);
}
}