Search in sources :

Example 1 with AddVariable

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

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

the class NodePart method add.

public NodeBase add(String type, JTree tree, MNode data) {
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    if (// The node is deliberately closed to indicate user intent.
    tree.isCollapsed(new TreePath(getPath())) && model.getChildCount(this) > 0 && !isRoot()) {
        // The only thing that can contain a NodePart is another NodePart. (If that ever changes, the following code will break.)
        if (type.isEmpty())
            return ((NodePart) getParent()).add("Part", tree, data);
        return ((NodePart) getParent()).add(type, tree, data);
    }
    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;
    TreePath path = tree.getSelectionPath();
    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")) {
        AddAnnotation aa = new AddAnnotation(this, metadataIndex, data);
        // aa will automagically insert a $metadata block if needed
        PanelModel.instance.undoManager.add(aa);
        return aa.createdNode;
    } else if (type.equals("Annotations")) {
        // TODO: figure out how to handle this case
        return null;
    } else if (type.equals("Reference")) {
        AddReference ar = new AddReference(this, metadataIndex, data);
        PanelModel.instance.undoManager.add(ar);
        return ar.createdNode;
    } else if (type.equals("References")) {
        // TODO: figure out how to handle this case
        return null;
    } else if (type.equals("Part")) {
        AddPart ap = new AddPart(this, subpartIndex, data);
        PanelModel.instance.undoManager.add(ap);
        return ap.createdNode;
    } else if (type.equals("Inherit")) {
        Undoable un = null;
        NodeInherit inherit = (NodeInherit) child("$inherit");
        String value = "";
        if (data != null)
            value = data.get();
        if (inherit == null) {
            un = new AddInherit(this, value);
        } else if (!value.isEmpty()) {
            un = new ChangeInherit(inherit, value);
        }
        if (un != null)
            PanelModel.instance.undoManager.add(un);
        return child("$inherit");
    } 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());
        }
        AddVariable av = new AddVariable(this, variableIndex, data);
        PanelModel.instance.undoManager.add(av);
        return av.createdNode;
    }
}
Also used : Undoable(gov.sandia.n2a.ui.Undoable) AddInherit(gov.sandia.n2a.ui.eq.undo.AddInherit) AddVariable(gov.sandia.n2a.ui.eq.undo.AddVariable) AddReference(gov.sandia.n2a.ui.eq.undo.AddReference) 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) ChangeInherit(gov.sandia.n2a.ui.eq.undo.ChangeInherit) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Aggregations

MVolatile (gov.sandia.n2a.db.MVolatile)2 AddAnnotation (gov.sandia.n2a.ui.eq.undo.AddAnnotation)2 AddInherit (gov.sandia.n2a.ui.eq.undo.AddInherit)2 AddPart (gov.sandia.n2a.ui.eq.undo.AddPart)2 AddReference (gov.sandia.n2a.ui.eq.undo.AddReference)2 AddVariable (gov.sandia.n2a.ui.eq.undo.AddVariable)2 TreeNode (javax.swing.tree.TreeNode)2 TreePath (javax.swing.tree.TreePath)2 Undoable (gov.sandia.n2a.ui.Undoable)1 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)1 ChangeAnnotations (gov.sandia.n2a.ui.eq.undo.ChangeAnnotations)1 ChangeInherit (gov.sandia.n2a.ui.eq.undo.ChangeInherit)1 ChangeReferences (gov.sandia.n2a.ui.eq.undo.ChangeReferences)1 Point (java.awt.Point)1