use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class PanelEquationTree method updateOrder.
/**
* Records the current order of nodes in "gui.order", provided that metadata field exists.
* Otherwise, we assume the user doesn't care.
* @param path To the node that changed (added, deleted, moved). In general, this node's
* parent will be the part that is tracking the order of its children.
*/
public void updateOrder(TreeNode[] path) {
NodePart parent = null;
for (int i = path.length - 2; i >= 0; i--) {
if (path[i] instanceof NodePart) {
parent = (NodePart) path[i];
break;
}
}
// This should never happen, because root of tree is a NodePart.
if (parent == null)
return;
// Find $metadata/gui.order for the currently selected node. If it exists, update it.
// Note that this is a modified version of moveSelected() which does not actually move
// anything, and which only modifies an existing $metadata/gui.order, not create a new one.
NodeAnnotations metadataNode = null;
String order = null;
Enumeration<?> i = parent.children();
while (i.hasMoreElements()) {
NodeBase c = (NodeBase) i.nextElement();
String key = c.source.key();
if (order == null)
order = key;
else
order = order + "," + key;
if (key.equals("$metadata"))
metadataNode = (NodeAnnotations) c;
}
if (metadataNode == null)
return;
i = metadataNode.children();
while (i.hasMoreElements()) {
NodeAnnotation a = (NodeAnnotation) i.nextElement();
if (a.source.key().equals("gui.order")) {
a.source.set(order);
FontMetrics fm = a.getFontMetrics(tree);
metadataNode.updateTabStops(fm);
model.nodeChanged(a);
break;
}
}
}
use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class AddAnnotation method create.
public static NodeBase create(List<String> path, int index, String name, String value, String blockName, NodeFactory factory, NodeFactory factoryBlock) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotRedoException();
MPart block = (MPart) parent.source.childOrCreate(blockName);
PanelEquationTree pet = PanelModel.instance.panelEquations;
JTree tree = pet.tree;
FilteredTreeModel model = (FilteredTreeModel) 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 = factoryBlock.create(block);
model.insertNodeIntoUnfiltered(container, parent, index);
index = 0;
} else // the node is present, so retrieve it
{
container = parent.child(blockName);
}
}
NodeBase createdNode = container.child(name);
boolean alreadyExists = createdNode != null;
MPart createdPart = (MPart) block.set(name, value);
if (!alreadyExists)
createdNode = factory.create(createdPart);
FontMetrics fm = createdNode.getFontMetrics(tree);
if (container.getChildCount() > 0) {
NodeBase firstChild = (NodeBase) container.getChildAt(0);
if (firstChild.needsInitTabs())
firstChild.initTabs(fm);
}
// 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("");
// preempt initialization; uses actual name, not user value
createdNode.updateColumnWidths(fm);
if (!alreadyExists)
model.insertNodeIntoUnfiltered(createdNode, container, index);
if (// create was merged with change name/value
value != null) {
container.updateTabStops(fm);
container.allNodesChanged(model);
TreeNode[] createdPath = createdNode.getPath();
pet.updateOrder(createdPath);
pet.updateVisibility(createdPath);
if (path.size() == 1 && name.equals("lock"))
pet.updateLock();
}
return createdNode;
}
use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class AddEquation method destroy.
public static void destroy(List<String> path, int equationCount, boolean canceled, String name, String combinerBefore) {
// Retrieve created node
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotUndoException();
NodeBase createdNode = parent.child(name);
PanelModel mep = PanelModel.instance;
JTree tree = mep.panelEquations.tree;
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
FontMetrics fm = createdNode.getFontMetrics(tree);
TreeNode[] createdPath = createdNode.getPath();
int index = parent.getIndexFiltered(createdNode);
if (canceled)
index--;
// Update database
MPart mparent = parent.source;
mparent.clear(name);
boolean parentChanged = false;
if (!mparent.get().equals(combinerBefore)) {
// This value may be replaced below if we switch back to single-line.
mparent.set(combinerBefore);
parentChanged = true;
}
if (// There is no overridden value, so this node goes away completely.
mparent.child(name) == null) {
model.removeNodeFromParent(createdNode);
if (// The node used to be single-line, so fold the last equation back into it.
equationCount == 0) {
NodeEquation lastEquation = null;
// unfiltered
Enumeration<?> i = parent.children();
while (i.hasMoreElements()) {
Object o = i.nextElement();
if (o instanceof NodeEquation) {
lastEquation = (NodeEquation) o;
break;
}
}
String lastCondition = lastEquation.source.key();
String lastExpression = lastEquation.source.get();
mparent.clear(lastCondition);
if (lastCondition.equals("@"))
mparent.set(combinerBefore + lastExpression);
else
mparent.set(combinerBefore + lastExpression + lastCondition);
parentChanged = true;
model.removeNodeFromParent(lastEquation);
}
} else // Just exposed an overridden value, so update display.
{
createdNode.updateColumnWidths(fm);
}
if (// Update tabs among this variable's siblings
parentChanged) {
parent.updateColumnWidths(fm);
NodeBase grandparent = (NodeBase) parent.getParent();
grandparent.updateTabStops(fm);
grandparent.allNodesChanged(model);
}
parent.updateTabStops(fm);
parent.allNodesChanged(model);
mep.panelEquations.updateOrder(createdPath);
mep.panelEquations.updateVisibility(createdPath, index);
}
use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class AddPart method destroy.
public static void destroy(List<String> path, boolean canceled, String name) {
// Retrieve created node
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotUndoException();
NodePart createdNode = (NodePart) parent.child(name);
PanelModel mep = PanelModel.instance;
JTree tree = mep.panelEquations.tree;
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
TreeNode[] createdPath = createdNode.getPath();
int index = parent.getIndexFiltered(createdNode);
if (canceled)
index--;
MPart mparent = parent.source;
mparent.clear(name);
if (// Node is fully deleted
mparent.child(name) == null) {
model.removeNodeFromParent(createdNode);
} else // Just exposed an overridden node
{
createdNode.build();
createdNode.findConnections();
createdNode.filter(model.filterLevel);
}
mep.panelEquations.updateOrder(createdPath);
// includes nodeStructureChanged(), if necessary
mep.panelEquations.updateVisibility(createdPath, index);
}
use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class ChangeAnnotation method apply.
public static void apply(List<String> path, String nameBefore, String nameAfter, String valueAfter, String blockName, NodeFactory factory) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotRedoException();
NodeBase nodeBefore = parent.child(nameBefore);
if (nodeBefore == null)
throw new CannotRedoException();
PanelEquationTree pet = PanelModel.instance.panelEquations;
JTree tree = pet.tree;
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
FontMetrics fm = nodeBefore.getFontMetrics(tree);
NodeBase nodeAfter;
if (nameBefore.equals(nameAfter)) {
nodeAfter = nodeBefore;
nodeAfter.source.set(valueAfter);
} else {
// Update database
MPart mparent;
if (parent instanceof NodeVariable)
mparent = (MPart) parent.source.child(blockName);
else
mparent = parent.source;
// should directly change destinationNode if it exists
MPart newPart = (MPart) mparent.set(nameAfter, valueAfter);
mparent.clear(nameBefore);
MPart oldPart = (MPart) mparent.child(nameBefore);
// Update GUI
nodeAfter = parent.child(nameAfter);
if (oldPart == null) {
if (nodeAfter == null) {
nodeAfter = nodeBefore;
nodeAfter.source = newPart;
} else {
model.removeNodeFromParent(nodeBefore);
}
} else {
if (nodeAfter == null) {
int index = parent.getIndex(nodeBefore);
nodeAfter = factory.create(newPart);
model.insertNodeIntoUnfiltered(nodeAfter, parent, index);
}
}
}
nodeAfter.updateColumnWidths(fm);
parent.updateTabStops(fm);
parent.allNodesChanged(model);
TreeNode[] nodePath = nodeAfter.getPath();
pet.updateOrder(nodePath);
pet.updateVisibility(nodePath);
// Only an inherited lock node can be touched by editing. It is possible to activate (make local) if the user assigns a specific value to it.
if (path.size() == 2 && path.get(1).equals("$metadata") && (nameBefore.equals("lock") || nameAfter.equals("lock")))
pet.updateLock();
}
Aggregations