Search in sources :

Example 1 with StoredPath

use of gov.sandia.n2a.ui.eq.StoredPath 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 StoredPath

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

the class ChangeAnnotations 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 metadata items in the tree.
    NodeContainer metadataNode;
    // The $metadata node under which all changes are made.
    MNode metadataSource = null;
    if (parent instanceof NodeVariable) {
        metadataNode = (NodeContainer) parent;
        metadataSource = metadataNode.source.child("$metadata");
    } else // NodePart is the default case
    {
        metadataNode = (NodeContainer) parent.child("$metadata");
        if (metadataNode != null)
            metadataSource = metadataNode.source;
    }
    boolean needBuild = true;
    if (// This is an undo, and $metadata did not exist before, so remove it.
    add == null) {
        // We can safely assume that metadataSource is non-null, since we only get here during an undo.
        metadataSource.parent().clear("$metadata");
        if (parent instanceof NodePart) {
            if (model == null)
                FilteredTreeModel.removeNodeFromParentStatic(metadataNode);
            else
                model.removeNodeFromParent(metadataNode);
            needBuild = false;
        }
    } else // Update $metadata node. Create if it doesn't exist.
    {
        if (metadataSource == null)
            metadataSource = parent.source.childOrCreate("$metadata");
        if (// only happens when parent is NodePart
        metadataNode == null) {
            metadataNode = new NodeAnnotations((MPart) metadataSource);
            if (model == null)
                FilteredTreeModel.insertNodeIntoUnfilteredStatic(metadataNode, parent, index);
            else
                model.insertNodeIntoUnfiltered(metadataNode, parent, index);
        }
        if (remove != null)
            metadataSource.uniqueNodes(remove);
        metadataSource.merge(add);
    }
    if (needBuild) {
        List<String> expanded = null;
        if (model != null)
            expanded = AddAnnotation.saveExpandedNodes(pet.tree, metadataNode);
        metadataNode.build();
        metadataNode.filter();
        if (model != null && metadataNode.visible()) {
            model.nodeStructureChanged(metadataNode);
            AddAnnotation.restoreExpandedNodes(pet.tree, metadataNode, expanded);
        }
    }
    if (pet != null) {
        TreeNode[] path = metadataNode.getPath();
        PanelEquationTree.updateVisibility(pet, path, -2, !multi && !graph);
        if (// We only care about viewing metadata tree if edit happened in the tree, not graphically.
        !graph) {
            if (multi)
                pet.tree.addSelectionPath(new TreePath(path));
            else
                // This forces focus back to original location.
                sp.restore(pet.tree, true);
        }
        pet.animate();
    }
    if (multi && parent instanceof NodePart) {
        NodePart np = (NodePart) parent;
        if (np.graph != null)
            np.graph.setSelected(true);
    }
    AddAnnotation.update(parent, touchesPin, touchesCategory);
}
Also used : NodeAnnotations(gov.sandia.n2a.ui.eq.tree.NodeAnnotations) MPart(gov.sandia.n2a.eqset.MPart) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) NodeContainer(gov.sandia.n2a.ui.eq.tree.NodeContainer) MNode(gov.sandia.n2a.db.MNode) NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) StoredPath(gov.sandia.n2a.ui.eq.StoredPath) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) CannotUndoException(javax.swing.undo.CannotUndoException) NodePart(gov.sandia.n2a.ui.eq.tree.NodePart) PanelEquationTree(gov.sandia.n2a.ui.eq.PanelEquationTree) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Aggregations

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