use of gov.sandia.n2a.eqset.MPart in project n2a by frothga.
the class NodePart method build.
@Override
public void build() {
setUserObject();
removeAllChildren();
String order = source.get("$metadata", "gui.order");
Set<String> sorted = new HashSet<String>();
// comma-separated list
String[] subkeys = order.split(",");
for (String k : subkeys) {
MNode c = source.child(k);
if (c != null) {
buildTriage((MPart) c);
sorted.add(k);
}
}
// Build everything else. Sort all subparts to the end.
ArrayList<MNode> subparts = new ArrayList<MNode>();
for (MNode c : source) {
if (sorted.contains(c.key()))
continue;
if (MPart.isPart(c))
subparts.add(c);
else
buildTriage((MPart) c);
}
for (MNode c : subparts) buildTriage((MPart) c);
}
use of gov.sandia.n2a.eqset.MPart in project n2a by frothga.
the class NodeReference method applyEdit.
@Override
public void applyEdit(JTree tree) {
String input = (String) getUserObject();
if (input.isEmpty()) {
delete(tree, true);
return;
}
String[] parts = input.split("=", 2);
String name = parts[0].trim().replaceAll("[ \\n\\t]", "");
String value;
if (parts.length > 1)
value = parts[1].trim();
else
value = "";
NodeBase parent = (NodeBase) getParent();
String oldName = source.key();
String oldValue = source.get();
if (!name.equals(oldName)) {
// Check if name change is forbidden
if (name.isEmpty()) {
name = oldName;
} else {
MPart mparent = source.getParent();
MPart partAfter = (MPart) mparent.child(name);
if (partAfter != null && partAfter.isFromTopDocument())
name = oldName;
}
}
if (name.equals(oldName) && value.equals(oldValue)) {
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
FontMetrics fm = getFontMetrics(tree);
parent.updateTabStops(fm);
// Our siblings should not change, because we did not really change. Just repaint in non-edit mode.
model.nodeChanged(this);
return;
}
PanelModel.instance.undoManager.add(new ChangeReference(parent, oldName, oldValue, name, value));
}
use of gov.sandia.n2a.eqset.MPart in project n2a by frothga.
the class NodeVariable method build.
@Override
public void build() {
removeAllChildren();
// but should do little harm.
if (source.isFromTopDocument())
enforceOneLine(source);
setUserObject(source.key() + "=" + getValue());
for (MNode n : source) {
if (n.key().startsWith("@"))
add(new NodeEquation((MPart) n));
}
MPart metadata = (MPart) source.child("$metadata");
if (metadata != null) {
for (MNode m : metadata) add(new NodeAnnotation((MPart) m));
}
MPart references = (MPart) source.child("$reference");
if (references != null) {
for (MNode r : references) add(new NodeReference((MPart) r));
}
}
use of gov.sandia.n2a.eqset.MPart 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.eqset.MPart in project n2a by frothga.
the class AddAnnotation method uniqueName.
public static String uniqueName(NodeBase parent, String blockName, String prefix, boolean allowEmptySuffix) {
MPart block = (MPart) parent.source.child(blockName);
if (allowEmptySuffix && (block == null || block.child(prefix) == null))
return prefix;
int suffix = 1;
if (block != null) {
while (block.child(prefix + suffix) != null) suffix++;
}
return prefix + suffix;
}
Aggregations