use of gov.sandia.n2a.ui.eq.tree.NodeReferences in project n2a by frothga.
the class ChangeReferences method apply.
public void apply(MNode add, MNode remove) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotUndoException();
PanelEquationTree pet = parent.getTree();
FilteredTreeModel model = null;
StoredPath sp = null;
if (pet != null) {
model = (FilteredTreeModel) pet.tree.getModel();
sp = new StoredPath(pet.tree);
}
// The immediate container of reference items in the tree.
NodeContainer referenceNode;
// The $reference node under which all changes are made.
MNode referenceSource = null;
if (parent instanceof NodeVariable) {
referenceNode = (NodeContainer) parent;
referenceSource = referenceNode.source.child("$reference");
} else // NodePart is the default case
{
referenceNode = (NodeContainer) parent.child("$reference");
if (referenceNode != null)
referenceSource = referenceNode.source;
}
boolean needBuild = true;
if (// This is an undo, and $reference did not exist before, so remove it.
add == null) {
// We can safely assume that referenceSource is non-null, since we only get here during an undo.
referenceSource.parent().clear("$reference");
if (parent instanceof NodePart) {
if (model == null)
FilteredTreeModel.removeNodeFromParentStatic(referenceNode);
else
model.removeNodeFromParent(referenceNode);
needBuild = false;
}
} else // Update $reference node. Create if it doesn't exist.
{
if (referenceSource == null)
referenceSource = parent.source.childOrCreate("$reference");
if (// only happens when parent is NodePart
referenceNode == null) {
referenceNode = new NodeReferences((MPart) referenceSource);
if (model == null)
FilteredTreeModel.insertNodeIntoUnfilteredStatic(referenceNode, parent, index);
else
model.insertNodeIntoUnfiltered(referenceNode, parent, index);
}
if (remove != null)
referenceSource.uniqueNodes(remove);
referenceSource.merge(add);
}
if (needBuild) {
referenceNode.build();
referenceNode.filter();
if (model != null && referenceNode.visible()) {
model.nodeStructureChanged(referenceNode);
}
}
PanelEquationTree.updateVisibility(pet, referenceNode.getPath(), -1, false);
// This forces focus back to original location.
if (!multi && sp != null)
sp.restore(pet.tree, true);
if (pet != null)
pet.animate();
}
use of gov.sandia.n2a.ui.eq.tree.NodeReferences in project n2a by frothga.
the class AddReference method create.
public static NodeBase create(List<String> path, int index, String name, String value, boolean multi) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotRedoException();
MPart block = (MPart) parent.source.childOrCreate("$reference");
PanelEquationTree pet = parent.getTree();
FilteredTreeModel model = (FilteredTreeModel) pet.tree.getModel();
// If this is a variable, then mix metadata with equations and references
NodeBase container = parent;
if (// If this is a part, then display special block
parent instanceof NodePart) {
if (// empty implies the node is absent
block.size() == 0) {
container = new NodeReferences(block);
model.insertNodeIntoUnfiltered(container, parent, index);
index = 0;
} else // the node is present, so retrieve it
{
container = parent.child("$reference");
}
}
NodeBase createdNode = container.child(name);
boolean alreadyExists = createdNode != null;
MPart createdPart = (MPart) block.set(value, name);
if (!alreadyExists)
createdNode = new NodeReference(createdPart);
// pure create, so about to go into edit mode. This should only happen on first application of the create action, and should only be possible if visibility is already correct.
if (value == null)
createdNode.setUserObject("");
if (!alreadyExists)
model.insertNodeIntoUnfiltered(createdNode, container, index);
if (// create was merged with change name/value
value != null) {
container.invalidateColumns(null);
TreeNode[] createdPath = createdNode.getPath();
pet.updateOrder(createdPath);
pet.updateVisibility(createdPath, -2, !multi);
if (multi)
pet.tree.addSelectionPath(new TreePath(createdPath));
container.allNodesChanged(model);
pet.animate();
}
return createdNode;
}
Aggregations