Search in sources :

Example 1 with Undoable

use of gov.sandia.n2a.ui.Undoable in project n2a by frothga.

the class PanelEquationTree method deleteSelected.

public void deleteSelected() {
    if (container.locked)
        return;
    // Collect and convert selection.
    TreePath[] paths = tree.getSelectionPaths();
    if (paths == null)
        return;
    TreePath leadPath = tree.getLeadSelectionPath();
    List<NodeBase> selection = new ArrayList<NodeBase>();
    for (TreePath path : paths) selection.add((NodeBase) path.getLastPathComponent());
    NodeBase leadNode = null;
    if (leadPath != null)
        leadNode = (NodeBase) leadPath.getLastPathComponent();
    // Pre-process selection to enforce constraints.
    // Detect if all equations under a variable are deleted. If so, ensure that the variable is marked for deletion.
    Map<NodeVariable, List<NodeEquation>> equations = new HashMap<NodeVariable, List<NodeEquation>>();
    for (NodeBase n : selection) {
        if (!(n instanceof NodeEquation))
            continue;
        NodeVariable v = (NodeVariable) n.getParent();
        List<NodeEquation> e = equations.get(v);
        if (e == null) {
            e = new ArrayList<NodeEquation>();
            equations.put(v, e);
        }
        e.add((NodeEquation) n);
    }
    for (NodeVariable v : equations.keySet()) {
        int count = 0;
        Enumeration<?> children = v.childrenFiltered();
        while (children.hasMoreElements()) {
            if (children.nextElement() instanceof NodeEquation)
                count++;
        }
        if (count == equations.get(v).size() && !selection.contains(v)) {
            // Delete containing variable instead. Equations will be removed from selection later.
            selection.add(v);
            // Needs to actually be in tree selection for parent detection.
            tree.addSelectionPath(new TreePath(v.getPath()));
        }
    }
    // Eliminate any selected node that is beneath some other selected node.
    NodeInherit inherit = null;
    List<NodeBase> filteredSelection = new ArrayList<NodeBase>();
    for (NodeBase n : selection) {
        if (n.hasSelectedAncestor(tree))
            continue;
        filteredSelection.add(n);
        if (n instanceof NodeInherit) {
            if (inherit == null || inherit.getLevel() > n.getLevel())
                inherit = (NodeInherit) n;
        }
    }
    selection = filteredSelection;
    if (// Since deleting $inherit rebuilds whole tree, don't allow any other deletes.
    inherit != null) {
        selection.clear();
        selection.add(inherit);
    } else if (leadNode != null) {
        if (!selection.contains(leadNode) && leadNode instanceof NodeEquation)
            leadNode = (NodeBase) leadNode.getParent();
        if (selection.contains(leadNode)) {
            // Ensure that leadNode is the final edit.
            selection.remove(leadNode);
            selection.add(leadNode);
        } else {
            leadNode = null;
        }
    }
    // Create transaction
    UndoManager um = MainFrame.instance.undoManager;
    CompoundEditView compound = null;
    int count = selection.size();
    boolean multi = count > 1;
    if (multi)
        um.addEdit(compound = new CompoundEditView(CompoundEditView.CLEAR_TREE));
    int i = 0;
    for (NodeBase n : selection) {
        i++;
        Undoable u = n.makeDelete(false);
        if (u == null)
            continue;
        if (u instanceof UndoableView) {
            UndoableView uv = (UndoableView) u;
            uv.setMulti(multi);
            if (multi && i == count)
                uv.setMultiLast(true);
        }
        if (multi && i == count)
            compound.leadPath = n.getKeyPath();
        um.apply(u);
    }
    um.endCompoundEdit();
}
Also used : Undoable(gov.sandia.n2a.ui.Undoable) NodeInherit(gov.sandia.n2a.ui.eq.tree.NodeInherit) HashMap(java.util.HashMap) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) ArrayList(java.util.ArrayList) NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) NodeEquation(gov.sandia.n2a.ui.eq.tree.NodeEquation) TreePath(javax.swing.tree.TreePath) UndoManager(gov.sandia.n2a.ui.UndoManager) UndoableView(gov.sandia.n2a.ui.eq.undo.UndoableView) List(java.util.List) ArrayList(java.util.ArrayList) CompoundEditView(gov.sandia.n2a.ui.eq.undo.CompoundEditView)

Example 2 with Undoable

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

Undoable (gov.sandia.n2a.ui.Undoable)2 TreePath (javax.swing.tree.TreePath)2 MVolatile (gov.sandia.n2a.db.MVolatile)1 UndoManager (gov.sandia.n2a.ui.UndoManager)1 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)1 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)1 NodeEquation (gov.sandia.n2a.ui.eq.tree.NodeEquation)1 NodeInherit (gov.sandia.n2a.ui.eq.tree.NodeInherit)1 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)1 AddAnnotation (gov.sandia.n2a.ui.eq.undo.AddAnnotation)1 AddInherit (gov.sandia.n2a.ui.eq.undo.AddInherit)1 AddPart (gov.sandia.n2a.ui.eq.undo.AddPart)1 AddReference (gov.sandia.n2a.ui.eq.undo.AddReference)1 AddVariable (gov.sandia.n2a.ui.eq.undo.AddVariable)1 ChangeInherit (gov.sandia.n2a.ui.eq.undo.ChangeInherit)1 CompoundEditView (gov.sandia.n2a.ui.eq.undo.CompoundEditView)1 UndoableView (gov.sandia.n2a.ui.eq.undo.UndoableView)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1