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();
}
Aggregations