use of javax.swing.tree.TreeNode in project logisim-evolution by reds-heig.
the class SimulationTreeCircuitNode method computeChildren.
// returns true if changed
private boolean computeChildren() {
ArrayList<TreeNode> newChildren = new ArrayList<TreeNode>();
ArrayList<Component> subcircs = new ArrayList<Component>();
for (Component comp : circuitState.getCircuit().getNonWires()) {
if (comp.getFactory() instanceof SubcircuitFactory) {
subcircs.add(comp);
} else {
TreeNode toAdd = model.mapComponentToNode(comp);
if (toAdd != null) {
newChildren.add(toAdd);
}
}
}
Collections.sort(newChildren, new CompareByName());
Collections.sort(subcircs, this);
for (Component comp : subcircs) {
SubcircuitFactory factory = (SubcircuitFactory) comp.getFactory();
CircuitState state = factory.getSubstate(circuitState, comp);
SimulationTreeCircuitNode toAdd = null;
for (TreeNode o : children) {
if (o instanceof SimulationTreeCircuitNode) {
SimulationTreeCircuitNode n = (SimulationTreeCircuitNode) o;
if (n.circuitState == state) {
toAdd = n;
break;
}
}
}
if (toAdd == null) {
toAdd = new SimulationTreeCircuitNode(model, this, state, comp);
}
newChildren.add(toAdd);
}
if (!children.equals(newChildren)) {
children = newChildren;
return true;
} else {
return false;
}
}
use of javax.swing.tree.TreeNode in project sis by apache.
the class Assert method assertTreeEquals.
/**
* Ensures that a tree is equals to an other tree.
* This method invokes itself recursively for every child nodes.
*
* @param expected the expected tree, or {@code null}.
* @param actual the tree to compare with the expected one, or {@code null}.
* @return the number of nodes.
*/
public static int assertTreeEquals(final TreeNode expected, final TreeNode actual) {
if (expected == null) {
assertNull(actual);
return 0;
}
int n = 1;
assertNotNull(actual);
assertEquals("isLeaf()", expected.isLeaf(), actual.isLeaf());
assertEquals("getAllowsChildren()", expected.getAllowsChildren(), actual.getAllowsChildren());
assertEquals("getChildCount()", expected.getChildCount(), actual.getChildCount());
@SuppressWarnings("unchecked") final Enumeration<? extends TreeNode> ec = expected.children();
@SuppressWarnings("unchecked") final Enumeration<? extends TreeNode> ac = actual.children();
int childIndex = 0;
while (ec.hasMoreElements()) {
assertTrue("hasMoreElements()", ac.hasMoreElements());
final TreeNode nextExpected = ec.nextElement();
final TreeNode nextActual = ac.nextElement();
final String message = "getChildAt(" + childIndex + ')';
assertSame(message, nextExpected, expected.getChildAt(childIndex));
assertSame(message, nextActual, actual.getChildAt(childIndex));
assertSame("getParent()", expected, nextExpected.getParent());
assertSame("getParent()", actual, nextActual.getParent());
assertSame("getIndex(TreeNode)", childIndex, expected.getIndex(nextExpected));
assertSame("getIndex(TreeNode)", childIndex, actual.getIndex(nextActual));
n += assertTreeEquals(nextExpected, nextActual);
childIndex++;
}
assertFalse("hasMoreElements()", ac.hasMoreElements());
assertEquals("toString()", expected.toString(), actual.toString());
return n;
}
use of javax.swing.tree.TreeNode in project CCDD by nasa.
the class CcddCommonTreeHandler method collapseTreePath.
/**
********************************************************************************************
* Collapse the specified tree path and all of its child paths. This is a recursive method
*
* @param path
* tree path to collapse
********************************************************************************************
*/
private void collapseTreePath(TreePath path) {
// Get the node for this path
TreeNode node = (TreeNode) path.getLastPathComponent();
// Check if the node has any child nodes
if (node.getChildCount() >= 0) {
// Step through each child node
for (Enumeration<?> e = node.children(); e.hasMoreElements(); ) {
// Get the child node's path and collapse it
TreeNode childNode = (TreeNode) e.nextElement();
TreePath childPath = path.pathByAddingChild(childNode);
collapseTreePath(childPath);
}
}
// the root's children when the root is invisible
if (path.getParentPath() != null || isRootVisible()) {
// Collapse the current path
collapsePath(path);
}
}
use of javax.swing.tree.TreeNode in project CCDD by nasa.
the class CcddTableTreeHandler method buildNodes.
/**
********************************************************************************************
* Build the table tree nodes. This is a recursive method. In order to prevent an infinite
* loop, a check is made for a child node that exists in its own path; if found the recursion
* is terminated for that node
*
* @param thisMember
* TableMember class
*
* @param parentNode
* current working node for the table tree
*
* @param childNode
* new child node to add to the working node
********************************************************************************************
*/
private void buildNodes(TableMembers thisMember, ToolTipTreeNode parentNode, ToolTipTreeNode childNode) {
// Step through each node in the parent's path
for (TreeNode node : parentNode.getPath()) {
if (((ToolTipTreeNode) node).getLevel() >= getHeaderNodeLevel()) {
// Check if the child is in the path
if (((ToolTipTreeNode) node).getUserObject().toString().equals(childNode.getUserObject().toString())) {
// Store the name of the recursively referenced node
recursionTable = childNode.getUserObject().toString();
break;
}
}
}
// Check that a recursion error wasn't found; this prevents an infinite loop from occurring
if (recursionTable == null) {
// Add the child node to its parent
parentNode.add(childNode);
// Get the parent table and variable path for this variable
String fullTablePath = getFullVariablePath(childNode.getPath());
// Step through each table/variable referenced by the table member
for (int memIndex = 0; memIndex < thisMember.getDataTypes().size(); memIndex++) {
// Check if this data type is a primitive
if (dataTypeHandler.isPrimitive(thisMember.getDataTypes().get(memIndex))) {
String tablePath = fullTablePath;
// Set to true if the variable has a path (i.e., this is not a prototype's
// variable)
boolean isChildVariable = tablePath.contains(",");
// Check if the variable has a path
if (isChildVariable) {
// Add the data type and variable name to the variable path
tablePath += "," + thisMember.getDataTypes().get(memIndex) + "." + thisMember.getVariableNames().get(memIndex);
}
String rate = null;
// Check if a rate filter is in effect
if (rateFilter != null) {
// Get the rate value for this variable. Use the prototype's value if the
// variable doesn't have a specific rate assigned
int index = rateValues.indexOf(tablePath);
rate = isChildVariable && index != -1 ? rateValues.get(index)[2] : thisMember.getRates().get(memIndex)[rateIndex];
}
// specified rate filter
if (rateFilter == null || rate.equals(rateFilter)) {
// Get the full variable name in the form
// data_type.variable_name[:bit_length]
String variable = thisMember.getFullVariableNameWithBits(memIndex);
// the variable, using its full path and name, is not in the exclusion list
if (excludedVariables == null || !excludedVariables.contains(tablePath)) {
// Add the primitive as a node to this child node
childNode.add(new ToolTipTreeNode(variable, ""));
} else // The variable is in the exclusion list
{
// Add the variable with the node text grayed out
childNode.add(new ToolTipTreeNode(DISABLED_TEXT_COLOR + variable, ""));
}
}
} else // Data type is not a primitive, it's a structure
{
// Step through the other tables
for (TableMembers member : tableMembers) {
// Check if the table is a member of the target table
if (thisMember.getDataTypes().get(memIndex).equals(member.getTableName())) {
// Build the node name from the prototype and variable names
String nodeName = thisMember.getDataTypes().get(memIndex) + "." + thisMember.getVariableNames().get(memIndex);
// Get the variable name path to this node
String tablePath = fullTablePath + "," + nodeName;
// Add this table to the current table's node. The node name is in the
// format 'dataType.variableName<[arrayIndex]>'. If a specific
// description exists for the table then use it for the tool tip text;
// otherwise use the prototype's description
buildNodes(member, childNode, new ToolTipTreeNode(nodeName, getTableDescription(tablePath, thisMember.getDataTypes().get(memIndex))));
}
}
}
}
// (variables)
if ((treeType == STRUCTURES_WITH_PRIMITIVES || treeType == INSTANCE_STRUCTURES_WITH_PRIMITIVES || treeType == INSTANCE_STRUCTURES_WITH_PRIMITIVES_AND_RATES) && childNode.getChildCount() == 0) {
// Remove the node
parentNode.remove(childNode);
}
}
}
use of javax.swing.tree.TreeNode in project freeplane by freeplane.
the class ImportAttributesDialog method setParentSelectionType.
private void setParentSelectionType(final DefaultMutableTreeNode selectedNode, final int newSelectionType) {
final TreeNode parentNode = selectedNode.getParent();
if (parentNode == null) {
return;
}
final DefaultMutableTreeNode defaultMutableParentNode = (DefaultMutableTreeNode) parentNode;
final TreeNodeInfo info = (TreeNodeInfo) (defaultMutableParentNode).getUserObject();
if (newSelectionType == TreeNodeInfo.PARTIAL_SELECTED) {
if (info.getSelected() != TreeNodeInfo.PARTIAL_SELECTED) {
info.setSelected(TreeNodeInfo.PARTIAL_SELECTED);
treeModel.nodeChanged(defaultMutableParentNode);
}
setParentSelectionType(defaultMutableParentNode, TreeNodeInfo.PARTIAL_SELECTED);
return;
}
for (int i = 0; i < defaultMutableParentNode.getChildCount(); i++) {
final TreeNodeInfo childInfo = (TreeNodeInfo) ((DefaultMutableTreeNode) defaultMutableParentNode.getChildAt(i)).getUserObject();
if (childInfo.getSelected() != newSelectionType) {
if (info.getSelected() != TreeNodeInfo.PARTIAL_SELECTED) {
info.setSelected(TreeNodeInfo.PARTIAL_SELECTED);
treeModel.nodeChanged(defaultMutableParentNode);
}
setParentSelectionType(defaultMutableParentNode, TreeNodeInfo.PARTIAL_SELECTED);
return;
}
}
if (info.getSelected() != newSelectionType) {
info.setSelected(newSelectionType);
treeModel.nodeChanged(defaultMutableParentNode);
}
setParentSelectionType(defaultMutableParentNode, newSelectionType);
}
Aggregations