use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class AddEquation method create.
public static NodeBase create(List<String> path, int equationCount, int index, String name, String combinerAfter, String value) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotRedoException();
PanelModel mep = PanelModel.instance;
JTree tree = mep.panelEquations.tree;
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
// Update the database
String parentValueBefore = parent.source.get();
Variable.ParsedValue parentPiecesBefore = new Variable.ParsedValue(parentValueBefore);
// The minimum number of equations is 2. There should never be exactly 1 equation, because that is single-line form, which should have no child equations at all.
if (// We are about to switch from single-line form to multi-conditional, so make a tree node for the existing equation.
equationCount == 0) {
MPart equation = (MPart) parent.source.set("@" + parentPiecesBefore.condition, parentPiecesBefore.expression);
model.insertNodeIntoUnfiltered(new NodeEquation(equation), parent, 0);
}
MPart createdPart = (MPart) parent.source.set(name, value == null ? "0" : value);
boolean parentChanged = false;
if (!combinerAfter.equals(parentValueBefore)) {
parent.source.set(combinerAfter);
parentChanged = true;
}
// Update the GUI
NodeBase createdNode = parent.child(name);
boolean alreadyExists = createdNode != null;
if (!alreadyExists)
createdNode = new NodeEquation(createdPart);
FontMetrics fm = createdNode.getFontMetrics(tree);
if (parent.getChildCount() > 0) {
NodeBase firstChild = (NodeBase) parent.getChildAt(0);
if (firstChild.needsInitTabs())
firstChild.initTabs(fm);
}
if (value == null)
createdNode.setUserObject("");
// preempt initialization
createdNode.updateColumnWidths(fm);
if (!alreadyExists)
model.insertNodeIntoUnfiltered(createdNode, parent, index);
if (parentChanged) {
parent.updateColumnWidths(fm);
NodeBase grandparent = (NodeBase) parent.getParent();
grandparent.updateTabStops(fm);
grandparent.allNodesChanged(model);
}
if (// create was merged with change name/value
value != null) {
parent.updateTabStops(fm);
parent.allNodesChanged(model);
mep.panelEquations.updateVisibility(createdNode.getPath());
}
return createdNode;
}
use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class ChangeVariable method apply.
public static void apply(List<String> path, String nameBefore, String nameAfter, MNode savedTree) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotRedoException();
NodeVariable nodeBefore = (NodeVariable) parent.child(nameBefore);
if (nodeBefore == null)
throw new CannotRedoException();
PanelModel mep = PanelModel.instance;
JTree tree = mep.panelEquations.tree;
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
FontMetrics fm = nodeBefore.getFontMetrics(tree);
NodeVariable nodeAfter;
if (nameBefore.equals(nameAfter)) {
nodeAfter = nodeBefore;
// Same as valueAfter. Sub-tree is not relevant here.
nodeAfter.source.set(savedTree.get());
} else {
// Update database
MPart mparent = parent.source;
mparent.clear(nameBefore);
mparent.set(nameAfter, "").merge(savedTree);
MPart newPart = (MPart) mparent.child(nameAfter);
MPart oldPart = (MPart) mparent.child(nameBefore);
// Update GUI
nodeAfter = (NodeVariable) 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 = new NodeVariable(newPart);
model.insertNodeIntoUnfiltered(nodeAfter, parent, index);
}
nodeBefore.build();
nodeBefore.findConnections();
if (nodeBefore.visible(model.filterLevel))
model.nodeStructureChanged(nodeBefore);
else
parent.hide(nodeBefore, model, true);
}
}
nodeAfter.build();
nodeAfter.findConnections();
nodeAfter.updateColumnWidths(fm);
parent.updateTabStops(fm);
parent.allNodesChanged(model);
TreeNode[] nodePath = nodeAfter.getPath();
mep.panelEquations.updateOrder(nodePath);
mep.panelEquations.updateVisibility(nodePath);
}
use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class AddAnnotation method create.
public static NodeBase create(List<String> path, int index, String name, MNode createSubtree, boolean nameIsGenerated, boolean multi, boolean selectVariable, boolean touchesPin, boolean touchesCategory) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null) {
int last = path.size() - 1;
if (path.get(last).equals("$metadata"))
parent = NodeBase.locateNode(path.subList(0, last));
}
if (parent == null)
throw new CannotRedoException();
// Update database
MPart mparent = parent.source;
if (parent instanceof NodePart || parent instanceof NodeVariable)
mparent = (MPart) mparent.childOrCreate("$metadata");
else if (parent instanceof NodeAnnotation)
mparent = ((NodeAnnotation) parent).folded;
// else parent is a NodeAnnotations, so mparent is $metadata, which can be used directly.
// For a simple add, name has only one path element. However, if a ChangeAnnotation was
// merged into this, then the name may have several path elements.
String[] names = name.split("\\.");
MPart createdPart = (MPart) mparent.childOrCreate(names);
createdPart.merge(createSubtree);
// Update GUI
PanelEquationTree pet = parent.getTree();
FilteredTreeModel model = null;
if (pet != null)
model = (FilteredTreeModel) pet.tree.getModel();
NodeContainer container = (NodeContainer) parent;
if (// If this is a part, then display special block.
parent instanceof NodePart) {
container = (NodeContainer) parent.child("$metadata");
if (container == null) {
container = new NodeAnnotations(mparent);
if (model == null)
FilteredTreeModel.insertNodeIntoUnfilteredStatic(container, parent, index);
else
model.insertNodeIntoUnfiltered(container, parent, index);
// TODO: update order?
index = 0;
}
}
NodeBase createdNode;
if (// pure create, going into edit mode
nameIsGenerated) {
// The given name should be unique, so don't bother checking for an existing node.
createdNode = new NodeAnnotation(createdPart);
// For edit mode. This should only happen on first application of the create action, and should only be possible if visibility is already correct.
createdNode.setUserObject("");
if (model == null)
FilteredTreeModel.insertNodeIntoUnfilteredStatic(createdNode, container, index);
else
model.insertNodeIntoUnfiltered(createdNode, container, index);
} else // create was merged with change name/value
{
List<String> expanded = null;
if (model != null)
expanded = saveExpandedNodes(pet.tree, container);
container.build();
container.filter();
if (model != null && container.visible()) {
model.nodeStructureChanged(container);
restoreExpandedNodes(pet.tree, container, expanded);
}
createdNode = findClosest(container, names);
if (pet != null) {
TreeNode[] parentPath = parent.getPath();
TreeNode[] createdPath = createdNode.getPath();
TreePath createdTreePath = new TreePath(createdPath);
pet.tree.expandPath(createdTreePath);
if (selectVariable)
pet.updateVisibility(parentPath, -2, !multi);
else
pet.updateVisibility(createdPath, -2, !multi);
if (multi) {
if (selectVariable)
pet.tree.addSelectionPath(new TreePath(parentPath));
else
pet.tree.addSelectionPath(createdTreePath);
}
pet.animate();
}
}
update(parent, touchesPin, touchesCategory);
return createdNode;
}
use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class AddAnnotation method destroy.
public static void destroy(List<String> path, boolean canceled, String name, String prefix, boolean multi, boolean multiLast, boolean selectVariable, boolean touchesPin, boolean touchesCategory) {
// Retrieve created node
NodeContainer parent = (NodeContainer) NodeBase.locateNode(path);
if (parent == null)
throw new CannotUndoException();
NodeBase createdNode = findClosest(parent, name.split("\\."));
if (createdNode == parent)
throw new CannotUndoException();
// Update database
MPart mparent = parent.source;
if (parent instanceof NodeVariable)
mparent = (MPart) mparent.child("$metadata");
else if (parent instanceof NodeAnnotation)
mparent = ((NodeAnnotation) parent).folded;
// else parent is a NodeAnnotations, so mparent is $metadata, which should be used directly.
boolean killBlock = false;
if (!prefix.isEmpty()) {
String[] names = prefix.split("\\.");
mparent.clear(names);
if (mparent.key().equals("$metadata") && mparent.size() == 0) {
mparent.parent().clear("$metadata");
killBlock = true;
}
}
// Update GUI
PanelEquationTree pet = parent.getTree();
FilteredTreeModel model = null;
if (pet != null)
model = (FilteredTreeModel) pet.tree.getModel();
TreeNode[] createdPath = createdNode.getPath();
int index = parent.getIndexFiltered(createdNode);
if (canceled)
index--;
if (// We just emptied $metadata, so remove the node.
killBlock && parent instanceof NodeAnnotations) {
if (model == null)
FilteredTreeModel.removeNodeFromParentStatic(parent);
else
model.removeNodeFromParent(parent);
// No need to update order, because we just destroyed $metadata, where order is stored.
// No need to update tab stops in grandparent, because block nodes don't offer any tab stops.
} else // Rebuild container (variable, metadata block, or annotation)
{
List<String> expanded = null;
if (model != null)
expanded = saveExpandedNodes(pet.tree, parent);
parent.build();
parent.filter();
if (model != null && parent.visible()) {
model.nodeStructureChanged(parent);
restoreExpandedNodes(pet.tree, parent, expanded);
}
}
if (pet != null) {
TreeNode[] parentPath = parent.getPath();
if (selectVariable)
pet.updateVisibility(parentPath, index, !multi);
else
pet.updateVisibility(createdPath, index, !multi || multiLast);
// Assumes nodeAfter is directly under a NodeVariable. Note that effect will be redundant with above when multiLast is true.
if (multi && selectVariable)
pet.tree.addSelectionPath(new TreePath(parentPath));
pet.animate();
}
update(parent, touchesPin, touchesCategory);
}
use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class AddEquation method create.
public static NodeBase create(List<String> path, int equationCount, int index, String name, String combinerAfter, String value, boolean multi) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotRedoException();
PanelEquationTree pet = parent.getTree();
FilteredTreeModel model = (FilteredTreeModel) pet.tree.getModel();
// Update the database
String parentValueBefore = parent.source.get();
Variable.ParsedValue parentPiecesBefore = new Variable.ParsedValue(parentValueBefore);
// The minimum number of equations is 2. There should never be exactly 1 equation, because that is single-line form, which should have no child equations at all.
if (// We are about to switch from single-line form to multi-conditional, so make a tree node for the existing equation.
equationCount == 0) {
MPart equation = (MPart) parent.source.set(parentPiecesBefore.expression, "@" + parentPiecesBefore.condition);
model.insertNodeIntoUnfiltered(new NodeEquation(equation), parent, 0);
}
MPart createdPart = (MPart) parent.source.set(value == null ? "0" : value, name);
boolean parentChanged = false;
if (!combinerAfter.equals(parentValueBefore)) {
parent.source.set(combinerAfter);
parentChanged = true;
}
// Update the GUI
NodeBase createdNode = parent.child(name);
boolean alreadyExists = createdNode != null;
if (!alreadyExists)
createdNode = new NodeEquation(createdPart);
if (value == null)
createdNode.setUserObject("");
if (!alreadyExists)
model.insertNodeIntoUnfiltered(createdNode, parent, index);
if (parentChanged) {
parent.setUserObject();
NodeBase grandparent = (NodeBase) parent.getParent();
grandparent.invalidateColumns(model);
}
if (// create was merged with change name/value
value != null) {
parent.invalidateColumns(null);
TreeNode[] createdPath = createdNode.getPath();
pet.updateVisibility(createdPath, -2, !multi);
if (multi)
pet.tree.addSelectionPath(new TreePath(createdPath));
parent.allNodesChanged(model);
pet.animate();
}
return createdNode;
}
Aggregations