Search in sources :

Example 21 with NodeBase

use of gov.sandia.n2a.ui.eq.tree.NodeBase 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;
}
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) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) NodeEquation(gov.sandia.n2a.ui.eq.tree.NodeEquation) JTree(javax.swing.JTree) FontMetrics(java.awt.FontMetrics) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 22 with NodeBase

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

the class AddInherit method destroy.

public static void destroy(List<String> path, boolean canceled) {
    NodePart parent = (NodePart) NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotUndoException();
    NodeBase node = parent.child("$inherit");
    TreeNode[] nodePath = node.getPath();
    int index = parent.getIndexFiltered(node);
    if (canceled)
        index--;
    PanelModel mep = PanelModel.instance;
    JTree tree = mep.panelEquations.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    MPart mparent = parent.source;
    // Complex restructuring happens here.
    mparent.clear("$inherit");
    // Handles all cases (complete deletion or exposed hidden node)
    parent.build();
    parent.findConnections();
    parent.filter(model.filterLevel);
    if (parent.visible(model.filterLevel))
        model.nodeStructureChanged(parent);
    mep.panelEquations.updateOrder(nodePath);
    mep.panelEquations.updateVisibility(nodePath, index);
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) JTree(javax.swing.JTree) MPart(gov.sandia.n2a.eqset.MPart) TreeNode(javax.swing.tree.TreeNode) CannotUndoException(javax.swing.undo.CannotUndoException) NodePart(gov.sandia.n2a.ui.eq.tree.NodePart) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 23 with NodeBase

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

the class AddPart method create.

public static NodeBase create(List<String> path, int index, String name, MNode newPart, boolean nameIsGenerated) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    NodeBase n = parent.child(name);
    // Should be blocked by GUI constraints, but this defends against ill-formed model on clipboard.
    if (n != null && !(n instanceof NodePart))
        throw new CannotUndoException();
    NodePart createdNode = (NodePart) n;
    // Update database
    MPart createdPart = (MPart) parent.source.set(name, "");
    createdPart.merge(newPart);
    // Update GUI
    PanelModel mep = PanelModel.instance;
    JTree tree = mep.panelEquations.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    if (createdNode == null) {
        createdNode = new NodePart(createdPart);
        model.insertNodeIntoUnfiltered(createdNode, parent, index);
    }
    createdNode.build();
    createdNode.findConnections();
    createdNode.filter(model.filterLevel);
    TreeNode[] createdPath = createdNode.getPath();
    if (// 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.
    nameIsGenerated)
        // 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.
        createdNode.setUserObject("");
    else
        mep.panelEquations.updateOrder(createdPath);
    mep.panelEquations.updateVisibility(createdPath);
    return createdNode;
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) MPart(gov.sandia.n2a.eqset.MPart) JTree(javax.swing.JTree) TreeNode(javax.swing.tree.TreeNode) CannotRedoException(javax.swing.undo.CannotRedoException) CannotUndoException(javax.swing.undo.CannotUndoException) NodePart(gov.sandia.n2a.ui.eq.tree.NodePart) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 24 with NodeBase

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

the class AddVariable method create.

public static NodeBase create(List<String> path, int index, String name, MNode newPart, boolean nameIsGenerated) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    NodeBase n = parent.child(name);
    // Should be blocked by GUI constraints, but this defends against ill-formed model on clipboard.
    if (n != null && !(n instanceof NodeVariable))
        throw new CannotRedoException();
    NodeVariable createdNode = (NodeVariable) n;
    // Update database
    MPart createdPart = (MPart) parent.source.set(name, "");
    createdPart.merge(newPart);
    // Update GUI
    PanelModel mep = PanelModel.instance;
    JTree tree = mep.panelEquations.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    boolean alreadyExists = createdNode != null;
    if (!alreadyExists)
        createdNode = new NodeVariable(createdPart);
    // 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 (nameIsGenerated)
        createdNode.setUserObject("");
    FontMetrics fm = createdNode.getFontMetrics(tree);
    // preempt initialization
    createdNode.updateColumnWidths(fm);
    if (!alreadyExists)
        model.insertNodeIntoUnfiltered(createdNode, parent, index);
    TreeNode[] createdPath = createdNode.getPath();
    if (!nameIsGenerated) {
        createdNode.build();
        createdNode.findConnections();
        mep.panelEquations.updateOrder(createdPath);
        parent.updateTabStops(fm);
        parent.allNodesChanged(model);
    }
    mep.panelEquations.updateVisibility(createdPath);
    return createdNode;
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) MPart(gov.sandia.n2a.eqset.MPart) JTree(javax.swing.JTree) FontMetrics(java.awt.FontMetrics) TreeNode(javax.swing.tree.TreeNode) CannotRedoException(javax.swing.undo.CannotRedoException) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 25 with NodeBase

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

the class AddVariable 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();
    NodeVariable createdNode = (NodeVariable) 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--;
    MPart mparent = parent.source;
    mparent.clear(name);
    if (// Node is fully deleted
    mparent.child(name) == null) {
        model.removeNodeFromParent(createdNode);
        ((NodePart) parent).findConnections();
    } else // Just exposed an overridden node
    {
        createdNode.build();
        createdNode.findConnections();
        createdNode.updateColumnWidths(fm);
    }
    parent.updateTabStops(fm);
    parent.allNodesChanged(model);
    mep.panelEquations.updateOrder(createdPath);
    // includes nodeStructureChanged(), if necessary
    mep.panelEquations.updateVisibility(createdPath, index);
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) JTree(javax.swing.JTree) MPart(gov.sandia.n2a.eqset.MPart) FontMetrics(java.awt.FontMetrics) TreeNode(javax.swing.tree.TreeNode) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) CannotUndoException(javax.swing.undo.CannotUndoException) NodePart(gov.sandia.n2a.ui.eq.tree.NodePart) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Aggregations

NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)39 MPart (gov.sandia.n2a.eqset.MPart)20 JTree (javax.swing.JTree)19 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)18 PanelModel (gov.sandia.n2a.ui.eq.PanelModel)15 TreeNode (javax.swing.tree.TreeNode)15 FontMetrics (java.awt.FontMetrics)12 CannotRedoException (javax.swing.undo.CannotRedoException)12 NodePart (gov.sandia.n2a.ui.eq.tree.NodePart)11 CannotUndoException (javax.swing.undo.CannotUndoException)8 TreePath (javax.swing.tree.TreePath)7 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)5 ExtensionPoint (gov.sandia.n2a.plugins.ExtensionPoint)4 PanelEquationTree (gov.sandia.n2a.ui.eq.PanelEquationTree)4 NodeAnnotation (gov.sandia.n2a.ui.eq.tree.NodeAnnotation)4 NodeEquation (gov.sandia.n2a.ui.eq.tree.NodeEquation)3 NodeAnnotations (gov.sandia.n2a.ui.eq.tree.NodeAnnotations)2 NodeContainer (gov.sandia.n2a.ui.eq.tree.NodeContainer)2 NodeReference (gov.sandia.n2a.ui.eq.tree.NodeReference)2 Font (java.awt.Font)2