Search in sources :

Example 1 with FilteredTreeModel

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

the class NodeVariable method add.

@Override
public NodeBase add(String type, JTree tree, MNode data) {
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    if (type.isEmpty()) {
        if (model.getChildCount(this) == 0 || tree.isCollapsed(new TreePath(getPath())))
            return ((NodeBase) getParent()).add("Variable", tree, data);
        type = "Equation";
    }
    if (isBinding)
        return ((NodeBase) getParent()).add(type, tree, data);
    if (type.equals("Equation")) {
        // Determine if pasting over an existing equation
        if (data != null) {
            // includes @
            String key = data.key();
            NodeBase existingEquation = child(key);
            if (existingEquation != null) {
                // remove the @, since ChangeEquation expects strings from ParsedValue
                key = key.substring(1);
                String combiner = new Variable.ParsedValue(source.get()).combiner;
                PanelModel.instance.undoManager.add(new ChangeEquation(this, key, combiner, existingEquation.source.get(), key, combiner, data.get()));
                // Somewhat of a cheat, since we didn't really add it. OTOH, a paste operation should not be followed by edit mode.
                return existingEquation;
            }
        }
        // Determine index for new equation
        int index = 0;
        NodeBase child = (NodeBase) tree.getLastSelectedPathComponent();
        if (child != null && child.getParent() == this)
            index = getIndex(child);
        while (index > 0 && !(getChildAt(index) instanceof NodeEquation)) index--;
        if (index < getChildCount() && getChildAt(index) instanceof NodeEquation)
            index++;
        // Create an AddEquation action
        AddEquation ae = new AddEquation(this, index, data);
        PanelModel.instance.undoManager.add(ae);
        return ae.createdNode;
    } else if (type.equals("Annotation")) {
        // Determine index at which to insert new annotation
        int index = 0;
        int count = getChildCount();
        while (index < count && !(children.get(index) instanceof NodeReference)) index++;
        AddAnnotation aa = new AddAnnotation(this, index, data);
        PanelModel.instance.undoManager.add(aa);
        return aa.createdNode;
    } else if (type.equals("Reference")) {
        AddReference ar = new AddReference(this, getChildCount(), data);
        PanelModel.instance.undoManager.add(ar);
        return ar.createdNode;
    }
    // refer all other requests up the tree
    return ((NodeBase) getParent()).add(type, tree, data);
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) ChangeVariable(gov.sandia.n2a.ui.eq.undo.ChangeVariable) DeleteVariable(gov.sandia.n2a.ui.eq.undo.DeleteVariable) AddReference(gov.sandia.n2a.ui.eq.undo.AddReference) AddEquation(gov.sandia.n2a.ui.eq.undo.AddEquation) ChangeEquation(gov.sandia.n2a.ui.eq.undo.ChangeEquation) AddAnnotation(gov.sandia.n2a.ui.eq.undo.AddAnnotation) TreePath(javax.swing.tree.TreePath) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 2 with FilteredTreeModel

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

the class AddAnnotation method create.

public static NodeBase create(List<String> path, int index, String name, String value, String blockName, NodeFactory factory, NodeFactory factoryBlock) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    MPart block = (MPart) parent.source.childOrCreate(blockName);
    PanelEquationTree pet = PanelModel.instance.panelEquations;
    JTree tree = pet.tree;
    FilteredTreeModel model = (FilteredTreeModel) 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 = factoryBlock.create(block);
            model.insertNodeIntoUnfiltered(container, parent, index);
            index = 0;
        } else // the node is present, so retrieve it
        {
            container = parent.child(blockName);
        }
    }
    NodeBase createdNode = container.child(name);
    boolean alreadyExists = createdNode != null;
    MPart createdPart = (MPart) block.set(name, value);
    if (!alreadyExists)
        createdNode = factory.create(createdPart);
    FontMetrics fm = createdNode.getFontMetrics(tree);
    if (container.getChildCount() > 0) {
        NodeBase firstChild = (NodeBase) container.getChildAt(0);
        if (firstChild.needsInitTabs())
            firstChild.initTabs(fm);
    }
    // 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("");
    // preempt initialization; uses actual name, not user value
    createdNode.updateColumnWidths(fm);
    if (!alreadyExists)
        model.insertNodeIntoUnfiltered(createdNode, container, index);
    if (// create was merged with change name/value
    value != null) {
        container.updateTabStops(fm);
        container.allNodesChanged(model);
        TreeNode[] createdPath = createdNode.getPath();
        pet.updateOrder(createdPath);
        pet.updateVisibility(createdPath);
        if (path.size() == 1 && name.equals("lock"))
            pet.updateLock();
    }
    return createdNode;
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) MPart(gov.sandia.n2a.eqset.MPart) JTree(javax.swing.JTree) FontMetrics(java.awt.FontMetrics) TreeNode(javax.swing.tree.TreeNode) CannotRedoException(javax.swing.undo.CannotRedoException) NodePart(gov.sandia.n2a.ui.eq.tree.NodePart) PanelEquationTree(gov.sandia.n2a.ui.eq.PanelEquationTree) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 3 with FilteredTreeModel

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

the class ChangeAnnotation method apply.

public static void apply(List<String> path, String nameBefore, String nameAfter, String valueAfter, String blockName, NodeFactory factory) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    NodeBase nodeBefore = parent.child(nameBefore);
    if (nodeBefore == null)
        throw new CannotRedoException();
    PanelEquationTree pet = PanelModel.instance.panelEquations;
    JTree tree = pet.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    FontMetrics fm = nodeBefore.getFontMetrics(tree);
    NodeBase nodeAfter;
    if (nameBefore.equals(nameAfter)) {
        nodeAfter = nodeBefore;
        nodeAfter.source.set(valueAfter);
    } else {
        // Update database
        MPart mparent;
        if (parent instanceof NodeVariable)
            mparent = (MPart) parent.source.child(blockName);
        else
            mparent = parent.source;
        // should directly change destinationNode if it exists
        MPart newPart = (MPart) mparent.set(nameAfter, valueAfter);
        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 = factory.create(newPart);
                model.insertNodeIntoUnfiltered(nodeAfter, parent, index);
            }
        }
    }
    nodeAfter.updateColumnWidths(fm);
    parent.updateTabStops(fm);
    parent.allNodesChanged(model);
    TreeNode[] nodePath = nodeAfter.getPath();
    pet.updateOrder(nodePath);
    pet.updateVisibility(nodePath);
    // Only an inherited lock node can be touched by editing. It is possible to activate (make local) if the user assigns a specific value to it.
    if (path.size() == 2 && path.get(1).equals("$metadata") && (nameBefore.equals("lock") || nameAfter.equals("lock")))
        pet.updateLock();
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) 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) PanelEquationTree(gov.sandia.n2a.ui.eq.PanelEquationTree) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 4 with FilteredTreeModel

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

the class ChangeEquation method apply.

public static void apply(List<String> path, String nameBefore, String nameAfter, String combinerAfter, String valueAfter) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    NodeBase nodeBefore = 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);
    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(nameAfter, valueAfter);
        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.filterLevel))
                model.nodeChanged(nodeBefore);
            else
                parent.hide(nodeBefore, model, true);
        }
    }
    if (parent.getChildCount() > 0) {
        NodeBase firstChild = (NodeBase) parent.getChildAt(0);
        if (firstChild.needsInitTabs())
            firstChild.initTabs(fm);
    }
    if (!parent.source.get().equals(combinerAfter)) {
        parent.source.set(combinerAfter);
        parent.updateColumnWidths(fm);
        NodeBase grandparent = (NodeBase) parent.getParent();
        grandparent.updateTabStops(fm);
        grandparent.allNodesChanged(model);
    }
    nodeAfter.updateColumnWidths(fm);
    parent.updateTabStops(fm);
    parent.allNodesChanged(model);
    mep.panelEquations.updateVisibility(nodeAfter.getPath());
}
Also used : 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) MPart(gov.sandia.n2a.eqset.MPart) FontMetrics(java.awt.FontMetrics) CannotRedoException(javax.swing.undo.CannotRedoException) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 5 with FilteredTreeModel

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

the class Move method apply.

public static void apply(List<String> path, int indexBefore, int indexAfter, int indexMetadata, boolean createOrder, boolean destroyOrder) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotUndoException();
    PanelModel mep = PanelModel.instance;
    JTree tree = mep.panelEquations.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    NodeBase moveNode = (NodeBase) parent.getChildAt(indexBefore);
    model.removeNodeFromParent(moveNode);
    NodeBase metadataNode = parent.child("$metadata");
    if (createOrder) {
        if (metadataNode == null) {
            metadataNode = new NodeAnnotations((MPart) parent.source.set("$metadata", ""));
            model.insertNodeIntoUnfiltered(metadataNode, parent, indexMetadata);
        }
        NodeBase orderNode = new NodeAnnotation((MPart) metadataNode.source.set("gui.order", ""));
        model.insertNodeIntoUnfiltered(orderNode, metadataNode, metadataNode.getChildCount());
    }
    if (destroyOrder) {
        NodeBase orderNode = metadataNode.child("gui.order");
        FontMetrics fm = orderNode.getFontMetrics(tree);
        metadataNode.source.clear("gui.order");
        model.removeNodeFromParent(metadataNode.child("gui.order"));
        if (metadataNode.getChildCount() == 0) {
            parent.source.clear("$metadata");
            model.removeNodeFromParent(metadataNode);
        } else {
            metadataNode.updateTabStops(fm);
            metadataNode.allNodesChanged(model);
        }
    }
    model.insertNodeIntoUnfiltered(moveNode, parent, indexAfter);
    TreeNode[] movePath = moveNode.getPath();
    if (!destroyOrder)
        mep.panelEquations.updateOrder(movePath);
    mep.panelEquations.updateVisibility(movePath);
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) JTree(javax.swing.JTree) NodeAnnotations(gov.sandia.n2a.ui.eq.tree.NodeAnnotations) MPart(gov.sandia.n2a.eqset.MPart) NodeAnnotation(gov.sandia.n2a.ui.eq.tree.NodeAnnotation) FontMetrics(java.awt.FontMetrics) TreeNode(javax.swing.tree.TreeNode) CannotUndoException(javax.swing.undo.CannotUndoException) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Aggregations

FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)46 MPart (gov.sandia.n2a.eqset.MPart)34 TreeNode (javax.swing.tree.TreeNode)31 PanelEquationTree (gov.sandia.n2a.ui.eq.PanelEquationTree)29 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)28 CannotRedoException (javax.swing.undo.CannotRedoException)21 NodePart (gov.sandia.n2a.ui.eq.tree.NodePart)18 TreePath (javax.swing.tree.TreePath)18 CannotUndoException (javax.swing.undo.CannotUndoException)15 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)14 PanelEquations (gov.sandia.n2a.ui.eq.PanelEquations)11 JTree (javax.swing.JTree)10 PanelEquationGraph (gov.sandia.n2a.ui.eq.PanelEquationGraph)9 NodeContainer (gov.sandia.n2a.ui.eq.tree.NodeContainer)9 Variable (gov.sandia.n2a.eqset.Variable)8 MNode (gov.sandia.n2a.db.MNode)7 PanelModel (gov.sandia.n2a.ui.eq.PanelModel)6 NodeAnnotations (gov.sandia.n2a.ui.eq.tree.NodeAnnotations)6 FontMetrics (java.awt.FontMetrics)6 Point (java.awt.Point)6