use of gov.sandia.n2a.ui.eq.PanelModel 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.PanelModel 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.PanelModel in project n2a by frothga.
the class AddDoc method create.
public static void create(String name, MNode saved, List<String> pathAfter, boolean fromSearchPanel, boolean wasShowing) {
PanelModel pm = PanelModel.instance;
// Note that lastSelection will end up pointing to new entry, not pathAfter.
pm.panelSearch.insertNextAt(pathAfter);
// Triggers PanelModel.childAdded(name), which updates the select and MRU panels, but not the equation tree panel.
MDoc doc = (MDoc) AppData.models.childOrCreate(name);
doc.merge(saved);
new MPart(doc).clearRedundantOverrides();
AppData.set(doc.get("$metadata", "id"), doc);
// update for multiple categories
if (doc.get("$metadata", "gui", "category").contains(","))
pm.panelSearch.search();
// Takes focus
if (wasShowing)
pm.panelEquations.load(doc);
if (fromSearchPanel) {
pm.panelSearch.tree.clearSelection();
if (// For some reason tree still thinks it has the focus, so takeFocus() doesn't work correctly. This call forces it back.
wasShowing)
// For some reason tree still thinks it has the focus, so takeFocus() doesn't work correctly. This call forces it back.
pm.panelSearch.tree.requestFocusInWindow();
else
pm.panelSearch.takeFocus();
}
}
Aggregations