Search in sources :

Example 21 with FilteredTreeModel

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

the class NodeAnnotation method makeAdd.

@Override
public Undoable makeAdd(String type, JTree tree, MNode data, Point location) {
    // By context, we assume the user wants to add another annotation.
    if (type.isEmpty())
        type = "Annotation";
    if (type.equals("Annotation")) {
        FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
        if (// Open node
        model.getChildCount(this) > 0 && !tree.isCollapsed(new TreePath(getPath()))) {
            // Add a new annotation 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 AddAnnotation(this, index, data);
        }
    // else let the request travel up to our parent
    }
    return ((NodeBase) parent).makeAdd(type, tree, data, location);
}
Also used : AddAnnotation(gov.sandia.n2a.ui.eq.undo.AddAnnotation) TreePath(javax.swing.tree.TreePath) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel) Point(java.awt.Point)

Example 22 with FilteredTreeModel

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

the class NodeInherit method applyEdit.

@Override
public void applyEdit(JTree tree) {
    String input = (String) getUserObject();
    if (input.isEmpty()) {
        boolean canceled = MainFrame.instance.undoManager.getPresentationName().equals("AddInherit");
        delete(canceled);
        return;
    }
    String[] parts = input.split("=", 2);
    String value;
    if (parts.length > 1)
        value = parts[1].trim();
    else
        value = "";
    String oldValue = source.get();
    if (// Nothing to do
    value.equals(oldValue)) {
        if (// name change not allowed
        !parts[0].equals("$inherit")) {
            // Repaint the original value
            setUserObject(source.key() + "=" + source.get());
            FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
            model.nodeChanged(this);
            Rectangle bounds = tree.getPathBounds(new TreePath(getPath()));
            if (bounds != null)
                tree.repaint(bounds);
        }
        return;
    }
    MainFrame.instance.undoManager.apply(new ChangeInherit(this, value));
}
Also used : TreePath(javax.swing.tree.TreePath) Rectangle(java.awt.Rectangle) ChangeInherit(gov.sandia.n2a.ui.eq.undo.ChangeInherit) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 23 with FilteredTreeModel

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

Example 24 with FilteredTreeModel

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

the class AddAnnotations method create.

public static void create(List<String> path, int index, MNode saved, NodeFactory factory) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    String blockName = saved.key();
    NodeBase n = parent.child(blockName);
    if (n != null && !(n instanceof NodeContainer))
        throw new CannotRedoException();
    NodeContainer node = (NodeContainer) n;
    MPart block = (MPart) parent.source.childOrCreate(blockName);
    block.merge(saved);
    PanelModel mep = PanelModel.instance;
    JTree tree = mep.panelEquations.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    if (node == null) {
        node = (NodeContainer) factory.create(block);
        model.insertNodeIntoUnfiltered(node, parent, index);
    }
    // Replaces all nodes, so they are set to require tab initialization.
    node.build();
    node.filter(model.filterLevel);
    mep.panelEquations.updateVisibility(node.getPath());
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) MPart(gov.sandia.n2a.eqset.MPart) JTree(javax.swing.JTree) CannotRedoException(javax.swing.undo.CannotRedoException) NodeContainer(gov.sandia.n2a.ui.eq.tree.NodeContainer) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 25 with FilteredTreeModel

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

the class AddAnnotations method destroy.

public static void destroy(List<String> path, String blockName) {
    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();
    NodeContainer node = (NodeContainer) parent.child(blockName);
    TreeNode[] nodePath = node.getPath();
    int index = parent.getIndexFiltered(node);
    MPart mparent = parent.source;
    mparent.clear(blockName);
    if (mparent.child(blockName) == null) {
        model.removeNodeFromParent(node);
    } else // Just exposed an overridden node
    {
        // Necessary to remove all overridden nodes
        node.build();
        node.filter(model.filterLevel);
    }
    mep.panelEquations.updateVisibility(nodePath, index);
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) JTree(javax.swing.JTree) MPart(gov.sandia.n2a.eqset.MPart) TreeNode(javax.swing.tree.TreeNode) CannotUndoException(javax.swing.undo.CannotUndoException) NodeContainer(gov.sandia.n2a.ui.eq.tree.NodeContainer) 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