Search in sources :

Example 6 with NodeEquation

use of gov.sandia.n2a.ui.eq.tree.NodeEquation 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;
}
Also used : MPart(gov.sandia.n2a.eqset.MPart) Variable(gov.sandia.n2a.eqset.Variable) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) CannotRedoException(javax.swing.undo.CannotRedoException) NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) NodeEquation(gov.sandia.n2a.ui.eq.tree.NodeEquation) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) PanelEquationTree(gov.sandia.n2a.ui.eq.PanelEquationTree) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 7 with NodeEquation

use of gov.sandia.n2a.ui.eq.tree.NodeEquation in project n2a by frothga.

the class ChangeEquation method apply.

public void apply(String nameBefore, String nameAfter, String combinerAfter, String valueAfter, boolean multi, boolean multiLast) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    NodeBase nodeBefore = parent.child(nameBefore);
    if (nodeBefore == null)
        throw new CannotRedoException();
    PanelEquationTree pet = parent.getTree();
    FilteredTreeModel model = (FilteredTreeModel) pet.tree.getModel();
    NodeBase nodeAfter;
    if (nameBefore.equals(nameAfter)) {
        nodeAfter = nodeBefore;
        nodeAfter.source.set(valueAfter);
    } else {
        // Update the database
        MPart mparent = parent.source;
        MPart newPart = (MPart) mparent.set(valueAfter, nameAfter);
        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 = new NodeEquation(newPart);
                model.insertNodeIntoUnfiltered(nodeAfter, parent, index);
            }
            if (nodeBefore.visible())
                model.nodeChanged(nodeBefore);
            else
                parent.hide(nodeBefore, model);
        }
    }
    if (!parent.source.get().equals(combinerAfter)) {
        parent.source.set(combinerAfter);
        parent.setUserObject();
        NodeBase grandparent = (NodeBase) parent.getParent();
        grandparent.invalidateColumns(model);
    }
    nodeAfter.setUserObject();
    parent.invalidateColumns(null);
    TreeNode[] afterPath = nodeAfter.getPath();
    boolean killed = valueAfter.isEmpty();
    boolean setSelection;
    if (// Revoke, which hides the node, so like delete.
    killed)
        // Revoke, which hides the node, so like delete.
        setSelection = !multi || multiLast;
    else
        setSelection = !multi;
    pet.updateVisibility(afterPath, -2, setSelection);
    if (multi && !killed)
        pet.tree.addSelectionPath(new TreePath(afterPath));
    parent.allNodesChanged(model);
    pet.animate();
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) NodeEquation(gov.sandia.n2a.ui.eq.tree.NodeEquation) MPart(gov.sandia.n2a.eqset.MPart) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) CannotRedoException(javax.swing.undo.CannotRedoException) PanelEquationTree(gov.sandia.n2a.ui.eq.PanelEquationTree) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Aggregations

NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)7 NodeEquation (gov.sandia.n2a.ui.eq.tree.NodeEquation)7 MPart (gov.sandia.n2a.eqset.MPart)5 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)5 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)4 TreePath (javax.swing.tree.TreePath)4 CannotRedoException (javax.swing.undo.CannotRedoException)4 PanelEquationTree (gov.sandia.n2a.ui.eq.PanelEquationTree)3 TreeNode (javax.swing.tree.TreeNode)3 Variable (gov.sandia.n2a.eqset.Variable)2 PanelModel (gov.sandia.n2a.ui.eq.PanelModel)2 FontMetrics (java.awt.FontMetrics)2 JTree (javax.swing.JTree)2 UndoManager (gov.sandia.n2a.ui.UndoManager)1 Undoable (gov.sandia.n2a.ui.Undoable)1 NodeInherit (gov.sandia.n2a.ui.eq.tree.NodeInherit)1 NodePart (gov.sandia.n2a.ui.eq.tree.NodePart)1 CompoundEditView (gov.sandia.n2a.ui.eq.undo.CompoundEditView)1 UndoableView (gov.sandia.n2a.ui.eq.undo.UndoableView)1 Rectangle (java.awt.Rectangle)1