Search in sources :

Example 1 with NodeReferences

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

the class ChangeReferences method apply.

public void apply(MNode add, MNode remove) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotUndoException();
    PanelEquationTree pet = parent.getTree();
    FilteredTreeModel model = null;
    StoredPath sp = null;
    if (pet != null) {
        model = (FilteredTreeModel) pet.tree.getModel();
        sp = new StoredPath(pet.tree);
    }
    // The immediate container of reference items in the tree.
    NodeContainer referenceNode;
    // The $reference node under which all changes are made.
    MNode referenceSource = null;
    if (parent instanceof NodeVariable) {
        referenceNode = (NodeContainer) parent;
        referenceSource = referenceNode.source.child("$reference");
    } else // NodePart is the default case
    {
        referenceNode = (NodeContainer) parent.child("$reference");
        if (referenceNode != null)
            referenceSource = referenceNode.source;
    }
    boolean needBuild = true;
    if (// This is an undo, and $reference did not exist before, so remove it.
    add == null) {
        // We can safely assume that referenceSource is non-null, since we only get here during an undo.
        referenceSource.parent().clear("$reference");
        if (parent instanceof NodePart) {
            if (model == null)
                FilteredTreeModel.removeNodeFromParentStatic(referenceNode);
            else
                model.removeNodeFromParent(referenceNode);
            needBuild = false;
        }
    } else // Update $reference node. Create if it doesn't exist.
    {
        if (referenceSource == null)
            referenceSource = parent.source.childOrCreate("$reference");
        if (// only happens when parent is NodePart
        referenceNode == null) {
            referenceNode = new NodeReferences((MPart) referenceSource);
            if (model == null)
                FilteredTreeModel.insertNodeIntoUnfilteredStatic(referenceNode, parent, index);
            else
                model.insertNodeIntoUnfiltered(referenceNode, parent, index);
        }
        if (remove != null)
            referenceSource.uniqueNodes(remove);
        referenceSource.merge(add);
    }
    if (needBuild) {
        referenceNode.build();
        referenceNode.filter();
        if (model != null && referenceNode.visible()) {
            model.nodeStructureChanged(referenceNode);
        }
    }
    PanelEquationTree.updateVisibility(pet, referenceNode.getPath(), -1, false);
    // This forces focus back to original location.
    if (!multi && sp != null)
        sp.restore(pet.tree, true);
    if (pet != null)
        pet.animate();
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) MPart(gov.sandia.n2a.eqset.MPart) StoredPath(gov.sandia.n2a.ui.eq.StoredPath) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) CannotUndoException(javax.swing.undo.CannotUndoException) NodeContainer(gov.sandia.n2a.ui.eq.tree.NodeContainer) NodeReferences(gov.sandia.n2a.ui.eq.tree.NodeReferences) NodePart(gov.sandia.n2a.ui.eq.tree.NodePart) PanelEquationTree(gov.sandia.n2a.ui.eq.PanelEquationTree) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel) MNode(gov.sandia.n2a.db.MNode)

Example 2 with NodeReferences

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

the class AddReference method create.

public static NodeBase create(List<String> path, int index, String name, String value, boolean multi) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    MPart block = (MPart) parent.source.childOrCreate("$reference");
    PanelEquationTree pet = parent.getTree();
    FilteredTreeModel model = (FilteredTreeModel) pet.tree.getModel();
    // If this is a variable, then mix metadata with equations and references
    NodeBase container = parent;
    if (// If this is a part, then display special block
    parent instanceof NodePart) {
        if (// empty implies the node is absent
        block.size() == 0) {
            container = new NodeReferences(block);
            model.insertNodeIntoUnfiltered(container, parent, index);
            index = 0;
        } else // the node is present, so retrieve it
        {
            container = parent.child("$reference");
        }
    }
    NodeBase createdNode = container.child(name);
    boolean alreadyExists = createdNode != null;
    MPart createdPart = (MPart) block.set(value, name);
    if (!alreadyExists)
        createdNode = new NodeReference(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 (value == null)
        createdNode.setUserObject("");
    if (!alreadyExists)
        model.insertNodeIntoUnfiltered(createdNode, container, index);
    if (// create was merged with change name/value
    value != null) {
        container.invalidateColumns(null);
        TreeNode[] createdPath = createdNode.getPath();
        pet.updateOrder(createdPath);
        pet.updateVisibility(createdPath, -2, !multi);
        if (multi)
            pet.tree.addSelectionPath(new TreePath(createdPath));
        container.allNodesChanged(model);
        pet.animate();
    }
    return createdNode;
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) MPart(gov.sandia.n2a.eqset.MPart) TreePath(javax.swing.tree.TreePath) NodeReference(gov.sandia.n2a.ui.eq.tree.NodeReference) TreeNode(javax.swing.tree.TreeNode) CannotRedoException(javax.swing.undo.CannotRedoException) NodeReferences(gov.sandia.n2a.ui.eq.tree.NodeReferences) NodePart(gov.sandia.n2a.ui.eq.tree.NodePart) PanelEquationTree(gov.sandia.n2a.ui.eq.PanelEquationTree) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Aggregations

MPart (gov.sandia.n2a.eqset.MPart)2 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)2 PanelEquationTree (gov.sandia.n2a.ui.eq.PanelEquationTree)2 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)2 NodePart (gov.sandia.n2a.ui.eq.tree.NodePart)2 NodeReferences (gov.sandia.n2a.ui.eq.tree.NodeReferences)2 MNode (gov.sandia.n2a.db.MNode)1 StoredPath (gov.sandia.n2a.ui.eq.StoredPath)1 NodeContainer (gov.sandia.n2a.ui.eq.tree.NodeContainer)1 NodeReference (gov.sandia.n2a.ui.eq.tree.NodeReference)1 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)1 TreeNode (javax.swing.tree.TreeNode)1 TreePath (javax.swing.tree.TreePath)1 CannotRedoException (javax.swing.undo.CannotRedoException)1 CannotUndoException (javax.swing.undo.CannotUndoException)1