use of gov.sandia.n2a.ui.eq.undo.AddAnnotation in project n2a by frothga.
the class NodeAnnotations method add.
@Override
public NodeBase add(String type, JTree tree, MNode data) {
if (type.isEmpty() || type.equals("Annotation")) {
// Add a new annotation to our children
int index = getChildCount() - 1;
TreePath path = tree.getSelectionPath();
if (path != null) {
NodeBase selected = (NodeBase) path.getLastPathComponent();
// unfiltered index
if (isNodeChild(selected))
index = getIndex(selected);
}
index++;
AddAnnotation aa = new AddAnnotation((NodeBase) getParent(), index, data);
PanelModel.instance.undoManager.add(aa);
return aa.createdNode;
}
return ((NodeBase) getParent()).add(type, tree, data);
}
use of gov.sandia.n2a.ui.eq.undo.AddAnnotation in project n2a by frothga.
the class NodeVariable method add.
@Override
public NodeBase add(String type, JTree tree, MNode data) {
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
if (type.isEmpty()) {
if (model.getChildCount(this) == 0 || tree.isCollapsed(new TreePath(getPath())))
return ((NodeBase) getParent()).add("Variable", tree, data);
type = "Equation";
}
if (isBinding)
return ((NodeBase) getParent()).add(type, tree, data);
if (type.equals("Equation")) {
// Determine if pasting over an existing equation
if (data != null) {
// includes @
String key = data.key();
NodeBase existingEquation = child(key);
if (existingEquation != null) {
// remove the @, since ChangeEquation expects strings from ParsedValue
key = key.substring(1);
String combiner = new Variable.ParsedValue(source.get()).combiner;
PanelModel.instance.undoManager.add(new ChangeEquation(this, key, combiner, existingEquation.source.get(), key, combiner, data.get()));
// Somewhat of a cheat, since we didn't really add it. OTOH, a paste operation should not be followed by edit mode.
return existingEquation;
}
}
// Determine index for new equation
int index = 0;
NodeBase child = (NodeBase) tree.getLastSelectedPathComponent();
if (child != null && child.getParent() == this)
index = getIndex(child);
while (index > 0 && !(getChildAt(index) instanceof NodeEquation)) index--;
if (index < getChildCount() && getChildAt(index) instanceof NodeEquation)
index++;
// Create an AddEquation action
AddEquation ae = new AddEquation(this, index, data);
PanelModel.instance.undoManager.add(ae);
return ae.createdNode;
} else if (type.equals("Annotation")) {
// Determine index at which to insert new annotation
int index = 0;
int count = getChildCount();
while (index < count && !(children.get(index) instanceof NodeReference)) index++;
AddAnnotation aa = new AddAnnotation(this, index, data);
PanelModel.instance.undoManager.add(aa);
return aa.createdNode;
} else if (type.equals("Reference")) {
AddReference ar = new AddReference(this, getChildCount(), data);
PanelModel.instance.undoManager.add(ar);
return ar.createdNode;
}
// refer all other requests up the tree
return ((NodeBase) getParent()).add(type, tree, data);
}
use of gov.sandia.n2a.ui.eq.undo.AddAnnotation 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;
}
}
Aggregations