use of gov.sandia.n2a.ui.eq.undo.CompoundEditView 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();
}
use of gov.sandia.n2a.ui.eq.undo.CompoundEditView in project n2a by frothga.
the class GraphNode method nudge.
public void nudge(ActionEvent e, int dx, int dy) {
if (container.locked)
return;
if ((e.getModifiers() & ActionEvent.CTRL_MASK) != 0) {
dx *= 10;
dy *= 10;
}
List<GraphNode> selection = container.panelEquationGraph.getSelection();
selection.remove(this);
MNode metadata = new MVolatile();
NodePart np;
MNode bounds;
GraphPanel gp = container.panelEquationGraph.graphPanel;
if (this == gp.pinIn) {
np = container.part;
bounds = metadata.childOrCreate("gui", "pin", "bounds", "in");
} else if (this == gp.pinOut) {
np = container.part;
bounds = metadata.childOrCreate("gui", "pin", "bounds", "out");
} else {
np = node;
bounds = metadata.childOrCreate("gui", "bounds");
}
Rectangle now = getBounds();
if (dx != 0 || np != node)
metadata.set(now.x - parent.offset.x + dx, "gui", "bounds", "x");
if (dy != 0 || np != node)
metadata.set(now.y - parent.offset.y + dy, "gui", "bounds", "y");
ChangeAnnotations ca = new ChangeAnnotations(np, metadata);
ca.graph = true;
UndoManager um = MainFrame.instance.undoManager;
if (selection.isEmpty()) {
um.apply(ca);
} else {
CompoundEditView compound = new CompoundEditView(CompoundEditView.CLEAR_GRAPH);
um.addEdit(compound);
// delayed execution
compound.addEdit(ca);
if (np == node)
compound.leadPath = np.getKeyPath();
for (GraphNode g : selection) {
metadata = new MVolatile();
if (g == gp.pinIn) {
np = container.part;
bounds = metadata.childOrCreate("gui", "pin", "bounds", "in");
} else if (g == gp.pinOut) {
np = container.part;
bounds = metadata.childOrCreate("gui", "pin", "bounds", "out");
} else {
np = g.node;
bounds = metadata.childOrCreate("gui", "bounds");
if (compound.leadPath == null)
compound.leadPath = np.getKeyPath();
}
now = g.getBounds();
if (dx != 0 || np != node)
bounds.set(now.x - parent.offset.x + dx, "x");
if (dy != 0 || np != node)
bounds.set(now.y - parent.offset.y + dy, "y");
ca = new ChangeAnnotations(np, metadata);
ca.graph = true;
compound.addEdit(ca);
}
um.endCompoundEdit();
compound.redo();
}
}
use of gov.sandia.n2a.ui.eq.undo.CompoundEditView in project n2a by frothga.
the class PanelEquationTree method watchSelected.
public void watchSelected(boolean clearOthers) {
if (container.locked)
return;
TreePath[] paths = tree.getSelectionPaths();
if (paths == null)
return;
TreePath leadPath = tree.getLeadSelectionPath();
NodeVariable leadVariable = null;
List<NodeVariable> variables = new ArrayList<NodeVariable>(paths.length);
for (TreePath path : paths) {
NodeBase n = (NodeBase) path.getLastPathComponent();
// Make one attempt to walk up tree, in case an equation or metadata item is selected under a variable.
if (!(n instanceof NodeVariable))
n = (NodeBase) n.getParent();
if (!(n instanceof NodeVariable))
continue;
NodeVariable v = (NodeVariable) n;
if (v.isBinding)
continue;
variables.add(v);
if (path.equals(leadPath))
leadVariable = v;
}
if (clearOthers)
findExistingWatches(container.root, variables);
UndoManager um = MainFrame.instance.undoManager;
CompoundEditView compound = null;
boolean multi = variables.size() > 1;
if (multi)
um.addEdit(compound = new CompoundEditView(CompoundEditView.CLEAR_TREE));
for (NodeVariable v : variables) {
// Toggle watch on the selected variable
boolean selectVariable = multi || tree.isCollapsed(new TreePath(v.getPath()));
MPart watch = (MPart) v.source.child("$metadata", "watch");
if (watch != null) {
NodeAnnotation watchNode = (NodeAnnotation) v.child("watch");
if (// currently on, so turn it off
watch.isFromTopDocument()) {
DeleteAnnotation d = new DeleteAnnotation(watchNode, false);
d.selectVariable = selectVariable;
if (multi)
compound.addEdit(d);
else
um.apply(d);
} else // Currently off, because it is not explicitly set in this document. Turn it on by overriding it locally.
{
ChangeAnnotation c = new ChangeAnnotation(watchNode, "watch", "");
c.selectVariable = selectVariable;
if (multi)
compound.addEdit(c);
else
um.apply(c);
}
} else // currently off, so turn it on
{
AddAnnotation a = new AddAnnotation(v, v.getChildCount(), new MVolatile("", "watch"));
a.selectVariable = selectVariable;
if (multi)
compound.addEdit(a);
else
um.apply(a);
}
}
if (multi) {
um.endCompoundEdit();
if (leadVariable != null)
compound.leadPath = leadVariable.getKeyPath();
compound.redo();
}
}
Aggregations