Search in sources :

Example 11 with PanelModel

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;
}
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 12 with PanelModel

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);
}
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) CannotRedoException(javax.swing.undo.CannotRedoException) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 13 with PanelModel

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();
    }
}
Also used : PanelModel(gov.sandia.n2a.ui.eq.PanelModel) MPart(gov.sandia.n2a.eqset.MPart) MDoc(gov.sandia.n2a.db.MDoc)

Aggregations

PanelModel (gov.sandia.n2a.ui.eq.PanelModel)13 MPart (gov.sandia.n2a.eqset.MPart)8 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)6 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)6 JTree (javax.swing.JTree)6 MNode (gov.sandia.n2a.db.MNode)4 FontMetrics (java.awt.FontMetrics)4 CannotRedoException (javax.swing.undo.CannotRedoException)4 MDoc (gov.sandia.n2a.db.MDoc)3 TreeNode (javax.swing.tree.TreeNode)3 NodeContainer (gov.sandia.n2a.ui.eq.tree.NodeContainer)2 NodeEquation (gov.sandia.n2a.ui.eq.tree.NodeEquation)2 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)2 CannotUndoException (javax.swing.undo.CannotUndoException)2 Variable (gov.sandia.n2a.eqset.Variable)1 PanelEquations (gov.sandia.n2a.ui.eq.PanelEquations)1 NodeAnnotation (gov.sandia.n2a.ui.eq.tree.NodeAnnotation)1 NodeAnnotations (gov.sandia.n2a.ui.eq.tree.NodeAnnotations)1