use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class NodeAnnotation method applyEdit.
@Override
public void applyEdit(JTree tree) {
String input = (String) getUserObject();
if (input.isEmpty()) {
boolean canceled = MainFrame.instance.undoManager.getPresentationName().equals("AddAnnotation");
delete(canceled);
return;
}
String[] parts = input.split("=", 2);
String name = parts[0].trim().replaceAll("[ \\n\\t]", "");
String value;
if (parts.length > 1)
value = parts[1].trim();
else
value = "";
String oldName = key();
String oldValue = folded.get();
if (!name.equals(oldName)) {
// Check if name change is forbidden
if (name.isEmpty()) {
name = oldName;
} else {
// If the first name along the new path matches the first name along the old path,
// then this is merely a rename internal the current node, which is allowed.
String[] names = name.split("\\.");
String[] oldNames = oldName.split("\\.");
if (// If first name does not match, we need to check more carefully.
!names[0].equals(oldNames[0])) {
// We don't want to overwrite a node that has already been defined in the top document.
// However, if it is part of folded path, we can define a new value for it.
MPart mparent = source.parent();
MPart partAfter = mparent;
for (String n : names) {
partAfter = (MPart) partAfter.child(n);
if (partAfter == null)
break;
}
if (partAfter != null && partAfter.isFromTopDocument() && source.size() > 0) {
name = oldName;
}
}
}
}
if (name.equals(oldName) && value.equals(oldValue)) {
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
setUserObject();
// Our siblings should not change, because we did not really change. Just repaint in non-edit mode.
model.nodeChanged(this);
return;
}
MainFrame.instance.undoManager.apply(new ChangeAnnotation(this, name, value));
}
use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class NodeEquation method applyEdit.
@Override
public void applyEdit(JTree tree) {
String input = (String) getUserObject();
if (input.isEmpty()) {
boolean canceled = MainFrame.instance.undoManager.getPresentationName().equals("AddEquation");
delete(canceled);
return;
}
// There are three possible outcomes of the edit:
// 1) Nothing changed
// 2) The name was not allowed to change
// 3) Arbitrary change
Variable.ParsedValue piecesBefore = new Variable.ParsedValue(source.get() + source.key());
Variable.ParsedValue piecesAfter = new Variable.ParsedValue(input);
NodeVariable parent = (NodeVariable) getParent();
if (!piecesBefore.condition.equals(piecesAfter.condition)) {
MPart partAfter = (MPart) parent.source.child("@" + piecesAfter.condition);
if (// Can't overwrite another top-document node, unless it is a revocation ...
partAfter != null && partAfter.isFromTopDocument()) {
String value = partAfter.get();
boolean revoked = value.isEmpty() || value.startsWith("$kill");
// reject key change
if (!revoked)
piecesAfter.condition = piecesBefore.condition;
}
}
if (piecesBefore.equals(piecesAfter)) {
setUserObject();
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
model.nodeChanged(this);
return;
}
// The fact that we are modifying an existing equation node indicates that the variable (parent) should only contain a combiner.
piecesBefore.combiner = parent.source.get();
if (piecesAfter.combiner.isEmpty())
piecesAfter.combiner = piecesBefore.combiner;
MainFrame.instance.undoManager.apply(new ChangeEquation(parent, piecesBefore.condition, piecesBefore.combiner, piecesBefore.expression, piecesAfter.condition, piecesAfter.combiner, piecesAfter.expression));
}
use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class AddAnnotations method create.
public static NodeBase create(List<String> path, int index, MNode saved, NodeFactory factory, boolean multi, boolean touchesPin, boolean touchesCategory) {
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);
PanelEquationTree pet = parent.getTree();
FilteredTreeModel model = (FilteredTreeModel) pet.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();
TreeNode[] nodePath = node.getPath();
pet.updateVisibility(nodePath, -2, !multi);
if (multi)
pet.tree.addSelectionPath(new TreePath(nodePath));
pet.animate();
if (blockName.equals("$metadata"))
AddAnnotation.update(parent, touchesPin, touchesCategory);
return node;
}
use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class AddInherit method destroy.
public static void destroy(List<String> path, boolean canceled) {
NodePart parent = (NodePart) NodeBase.locateNode(path);
if (parent == null)
throw new CannotUndoException();
NodePart grandparent = (NodePart) parent.getTrueParent();
NodeBase node = parent.child("$inherit");
TreeNode[] nodePath = node.getPath();
int index = parent.getIndexFiltered(node);
if (canceled)
index--;
PanelEquations pe = PanelModel.instance.panelEquations;
PanelEquationTree pet = parent.getTree();
FilteredTreeModel model = (FilteredTreeModel) pet.tree.getModel();
PanelEquationGraph peg = pe.panelEquationGraph;
MPart mparent = parent.source;
// Complex restructuring happens here.
mparent.clear("$inherit");
// Handles all cases (complete deletion or exposed hidden node)
parent.build();
if (grandparent == null)
parent.findConnections();
else
grandparent.findConnections();
parent.rebuildPins();
parent.filter();
if (parent == pe.part) {
// safely disconnects old nodes, even though parent has been rebuilt with new nodes
peg.reloadPart();
// Ensure that parts are not visible in parent panel.
parent.filter();
}
// Presumably, part node is still visible. Is there any harm in doing this if it is not?
model.nodeStructureChanged(parent);
pet.updateOrder(nodePath);
pet.updateVisibility(nodePath, index);
pet.animate();
if (parent != pe.part) {
peg.updatePins();
peg.reconnect();
peg.repaint();
}
if (// root node, so update categories in search list
parent.getTrueParent() == null) {
PanelModel.instance.panelSearch.search();
}
}
use of gov.sandia.n2a.ui.eq.FilteredTreeModel in project n2a by frothga.
the class AddReference method create.
public static NodeBase create(List<String> path, int index, String name, String value, boolean multi) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotRedoException();
MPart block = (MPart) parent.source.childOrCreate("$reference");
PanelEquationTree pet = parent.getTree();
FilteredTreeModel model = (FilteredTreeModel) pet.tree.getModel();
// If this is a variable, then mix metadata with equations and references
NodeBase container = parent;
if (// If this is a part, then display special block
parent instanceof NodePart) {
if (// empty implies the node is absent
block.size() == 0) {
container = new NodeReferences(block);
model.insertNodeIntoUnfiltered(container, parent, index);
index = 0;
} else // the node is present, so retrieve it
{
container = parent.child("$reference");
}
}
NodeBase createdNode = container.child(name);
boolean alreadyExists = createdNode != null;
MPart createdPart = (MPart) block.set(value, name);
if (!alreadyExists)
createdNode = new NodeReference(createdPart);
// pure create, so about to go into edit mode. This should only happen on first application of the create action, and should only be possible if visibility is already correct.
if (value == null)
createdNode.setUserObject("");
if (!alreadyExists)
model.insertNodeIntoUnfiltered(createdNode, container, index);
if (// create was merged with change name/value
value != null) {
container.invalidateColumns(null);
TreeNode[] createdPath = createdNode.getPath();
pet.updateOrder(createdPath);
pet.updateVisibility(createdPath, -2, !multi);
if (multi)
pet.tree.addSelectionPath(new TreePath(createdPath));
container.allNodesChanged(model);
pet.animate();
}
return createdNode;
}
Aggregations