Search in sources :

Example 1 with UndoableView

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

Aggregations

UndoManager (gov.sandia.n2a.ui.UndoManager)1 Undoable (gov.sandia.n2a.ui.Undoable)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 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 TreePath (javax.swing.tree.TreePath)1