Search in sources :

Example 1 with AddReference

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

the class NodeReferences method add.

@Override
public NodeBase add(String type, JTree tree, MNode data) {
    if (type.isEmpty() || type.equals("Reference")) {
        // Add a new reference to our children
        int index = getChildCount() - 1;
        TreePath path = tree.getSelectionPath();
        if (path != null) {
            NodeBase selected = (NodeBase) path.getLastPathComponent();
            // unfiltered index
            if (isNodeChild(selected))
                index = getIndex(selected);
        }
        index++;
        AddReference ar = new AddReference((NodeBase) getParent(), index, data);
        PanelModel.instance.undoManager.add(ar);
        return ar.createdNode;
    }
    return ((NodeBase) getParent()).add(type, tree, data);
}
Also used : TreePath(javax.swing.tree.TreePath) AddReference(gov.sandia.n2a.ui.eq.undo.AddReference)

Example 2 with AddReference

use of gov.sandia.n2a.ui.eq.undo.AddReference 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 3 with AddReference

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

the class NodeVariable method makeAdd.

@Override
public Undoable makeAdd(String type, JTree tree, MNode data, Point location) {
    if (type.isEmpty()) {
        FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
        if (model.getChildCount(this) == 0 || tree.isCollapsed(new TreePath(getPath())))
            return ((NodeBase) parent).makeAdd("Variable", tree, data, location);
        type = "Equation";
    }
    if (isBinding && !type.equals("Annotation"))
        return ((NodeBase) parent).makeAdd(type, tree, data, location);
    if (type.equals("Equation")) {
        if (data != null) {
            // includes @
            String key = data.key();
            // Determine if pasting over empty variable (no equations of any type except a naked combiner)
            Variable.ParsedValue existing = new Variable.ParsedValue(source.get());
            boolean hasEquations = !existing.condition.isEmpty() || !existing.expression.isEmpty();
            if (!hasEquations) {
                // unfiltered
                Enumeration<?> children = children();
                while (children.hasMoreElements()) {
                    Object c = children.nextElement();
                    if (c instanceof NodeEquation) {
                        hasEquations = true;
                        break;
                    }
                }
            }
            if (// no equations, or possibly a naked combiner
            !hasEquations) {
                String value = existing.combiner + data.get() + key;
                if (value.endsWith("@"))
                    value = value.substring(0, value.length() - 1);
                return new ChangeVariable(this, source.key(), value);
            }
            // Determine if pasting over an existing equation
            NodeBase existingEquation = child(key);
            if (existingEquation != null) {
                // remove the @, since ChangeEquation expects strings from ParsedValue
                key = key.substring(1);
                String combiner = existing.combiner;
                String newValue = data.get();
                String existingValue = existingEquation.source.get();
                if (!newValue.equals(existingValue))
                    return new ChangeEquation(this, key, combiner, existingValue, key, combiner, newValue);
                // else the user intent is to duplicate the equation for convenience before editing it.
                // In this case, we need to create a new equation with alternate key.
                data = new MVolatile(existingValue, "@" + key + "&&");
            }
        }
        // Determine index for new equation
        int index = 0;
        NodeBase child = null;
        TreePath path = tree.getLeadSelectionPath();
        if (path != null)
            child = (NodeBase) path.getLastPathComponent();
        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
        return new AddEquation(this, index, data);
    } 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++;
        return new AddAnnotation(this, index, data);
    } else if (type.equals("Annotations")) {
        // In this case, everything under this node will be rebuilt, so no need to worry about insertion index.
        return new ChangeAnnotations(this, data);
    } else if (type.equals("Reference")) {
        return new AddReference(this, getChildCount(), data);
    } else if (type.equals("References")) {
        return new ChangeReferences(this, data);
    }
    // refer all other requests up the tree
    return ((NodeBase) parent).makeAdd(type, tree, data, location);
}
Also used : ChangeReferences(gov.sandia.n2a.ui.eq.undo.ChangeReferences) DeleteVariable(gov.sandia.n2a.ui.eq.undo.DeleteVariable) Variable(gov.sandia.n2a.eqset.Variable) ChangeVariable(gov.sandia.n2a.ui.eq.undo.ChangeVariable) AddReference(gov.sandia.n2a.ui.eq.undo.AddReference) ChangeVariable(gov.sandia.n2a.ui.eq.undo.ChangeVariable) Point(java.awt.Point) MVolatile(gov.sandia.n2a.db.MVolatile) 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) ChangeAnnotations(gov.sandia.n2a.ui.eq.undo.ChangeAnnotations) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 4 with AddReference

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

the class NodePart method makeAdd.

@Override
public Undoable makeAdd(String type, JTree tree, MNode data, Point location) {
    if (tree == null) {
        // The only thing we can add is a part in the current graph view.
        if (!type.equals("Part"))
            return null;
    } else {
        boolean collapsed = tree.isCollapsed(new TreePath(getPath()));
        boolean hasChildren = ((FilteredTreeModel) tree.getModel()).getChildCount(this) > 0;
        if (// The node is deliberately closed to indicate user intent.
        collapsed && hasChildren) {
            if (type.isEmpty())
                type = "Part";
            return ((NodePart) parent).makeAdd(type, tree, data, location);
        }
    // else this is an open node, so anything can be inserted under it.
    }
    int variableIndex = -1;
    int subpartIndex = -1;
    int metadataIndex = 0;
    // unfiltered, so we can insert at the correct place in the underlying collection
    int count = getChildCount();
    for (int i = 0; i < count; i++) {
        TreeNode t = getChildAt(i);
        if (t instanceof NodeInherit) {
            metadataIndex = i + 1;
        }
        if (t instanceof NodePart) {
            if (variableIndex < 0)
                variableIndex = i;
            subpartIndex = i + 1;
        }
    }
    if (variableIndex < 0)
        variableIndex = count;
    if (subpartIndex < 0)
        subpartIndex = count;
    if (tree != null) {
        TreePath path = tree.getLeadSelectionPath();
        if (path != null) {
            NodeBase selected = (NodeBase) path.getLastPathComponent();
            if (selected.getParent() == this) {
                // When we have a specific item selected, the user expects the new item to appear directly below it.
                // unfiltered
                int selectedIndex = getIndex(selected);
                variableIndex = selectedIndex + 1;
                subpartIndex = selectedIndex + 1;
            }
        }
    }
    if (type.equals("Annotation")) {
        // will automagically insert a $metadata block if needed
        return new AddAnnotation(this, metadataIndex, data);
    } else if (type.equals("Annotations")) {
        return new ChangeAnnotations(this, data);
    } else if (type.equals("Reference")) {
        return new AddReference(this, metadataIndex, data);
    } else if (type.equals("References")) {
        return new ChangeReferences(this, data);
    } else if (type.equals("Part")) {
        return new AddPart(this, subpartIndex, data, location);
    } else if (type.equals("Inherit")) {
        return new AddInherit(this, data.get());
    } else // treat all other requests as "Variable"
    {
        if (data != null && type.equals("Equation")) {
            // convert equation into nameless variable
            data = new MVolatile(data.get() + data.key(), "");
        }
        return new AddVariable(this, variableIndex, data);
    }
}
Also used : ChangeReferences(gov.sandia.n2a.ui.eq.undo.ChangeReferences) AddInherit(gov.sandia.n2a.ui.eq.undo.AddInherit) AddVariable(gov.sandia.n2a.ui.eq.undo.AddVariable) AddReference(gov.sandia.n2a.ui.eq.undo.AddReference) Point(java.awt.Point) MVolatile(gov.sandia.n2a.db.MVolatile) AddAnnotation(gov.sandia.n2a.ui.eq.undo.AddAnnotation) TreePath(javax.swing.tree.TreePath) AddPart(gov.sandia.n2a.ui.eq.undo.AddPart) TreeNode(javax.swing.tree.TreeNode) ChangeAnnotations(gov.sandia.n2a.ui.eq.undo.ChangeAnnotations)

Example 5 with AddReference

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

the class NodeReferences method makeAdd.

@Override
public Undoable makeAdd(String type, JTree tree, MNode data, Point location) {
    if (type.isEmpty() || type.equals("Reference")) {
        // Add a new reference to our children
        int index = getChildCount() - 1;
        TreePath path = tree.getLeadSelectionPath();
        if (path != null) {
            NodeBase selected = (NodeBase) path.getLastPathComponent();
            // unfiltered index
            if (isNodeChild(selected))
                index = getIndex(selected);
        }
        index++;
        return new AddReference((NodeBase) getParent(), index, data);
    }
    return ((NodeBase) parent).makeAdd(type, tree, data, location);
}
Also used : TreePath(javax.swing.tree.TreePath) AddReference(gov.sandia.n2a.ui.eq.undo.AddReference) Point(java.awt.Point)

Aggregations

AddReference (gov.sandia.n2a.ui.eq.undo.AddReference)6 TreePath (javax.swing.tree.TreePath)6 AddAnnotation (gov.sandia.n2a.ui.eq.undo.AddAnnotation)4 MVolatile (gov.sandia.n2a.db.MVolatile)3 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)3 Point (java.awt.Point)3 Variable (gov.sandia.n2a.eqset.Variable)2 AddEquation (gov.sandia.n2a.ui.eq.undo.AddEquation)2 AddInherit (gov.sandia.n2a.ui.eq.undo.AddInherit)2 AddPart (gov.sandia.n2a.ui.eq.undo.AddPart)2 AddVariable (gov.sandia.n2a.ui.eq.undo.AddVariable)2 ChangeAnnotations (gov.sandia.n2a.ui.eq.undo.ChangeAnnotations)2 ChangeEquation (gov.sandia.n2a.ui.eq.undo.ChangeEquation)2 ChangeReferences (gov.sandia.n2a.ui.eq.undo.ChangeReferences)2 ChangeVariable (gov.sandia.n2a.ui.eq.undo.ChangeVariable)2 DeleteVariable (gov.sandia.n2a.ui.eq.undo.DeleteVariable)2 TreeNode (javax.swing.tree.TreeNode)2 Undoable (gov.sandia.n2a.ui.Undoable)1 ChangeInherit (gov.sandia.n2a.ui.eq.undo.ChangeInherit)1