use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class ChangeVariable method apply.
public static void apply(List<String> path, String nameBefore, String nameAfter, MNode savedTree) {
NodeBase parent = NodeBase.locateNode(path);
if (parent == null)
throw new CannotRedoException();
NodeVariable nodeBefore = (NodeVariable) parent.child(nameBefore);
if (nodeBefore == null)
throw new CannotRedoException();
PanelModel mep = PanelModel.instance;
JTree tree = mep.panelEquations.tree;
FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
FontMetrics fm = nodeBefore.getFontMetrics(tree);
NodeVariable nodeAfter;
if (nameBefore.equals(nameAfter)) {
nodeAfter = nodeBefore;
// Same as valueAfter. Sub-tree is not relevant here.
nodeAfter.source.set(savedTree.get());
} else {
// Update database
MPart mparent = parent.source;
mparent.clear(nameBefore);
mparent.set(nameAfter, "").merge(savedTree);
MPart newPart = (MPart) mparent.child(nameAfter);
MPart oldPart = (MPart) mparent.child(nameBefore);
// Update GUI
nodeAfter = (NodeVariable) parent.child(nameAfter);
if (oldPart == null) {
if (nodeAfter == null) {
nodeAfter = nodeBefore;
nodeAfter.source = newPart;
} else {
model.removeNodeFromParent(nodeBefore);
}
} else {
if (nodeAfter == null) {
int index = parent.getIndex(nodeBefore);
nodeAfter = new NodeVariable(newPart);
model.insertNodeIntoUnfiltered(nodeAfter, parent, index);
}
nodeBefore.build();
nodeBefore.findConnections();
if (nodeBefore.visible(model.filterLevel))
model.nodeStructureChanged(nodeBefore);
else
parent.hide(nodeBefore, model, true);
}
}
nodeAfter.build();
nodeAfter.findConnections();
nodeAfter.updateColumnWidths(fm);
parent.updateTabStops(fm);
parent.allNodesChanged(model);
TreeNode[] nodePath = nodeAfter.getPath();
mep.panelEquations.updateOrder(nodePath);
mep.panelEquations.updateVisibility(nodePath);
}
use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class EquationTreeCellEditor method getTreeCellEditorComponent.
@Override
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
editingIcon = renderer.getIconFor((NodeBase) value, expanded, leaf);
offset = renderer.getIconTextGap() + editingIcon.getIconWidth();
Font font = renderer.getFontFor(editingNode);
String text = editingNode.getText(expanded, true);
FontMetrics fm = tree.getFontMetrics(font);
int textWidth = fm.stringWidth(text);
int width = tree.getWidth();
if (editingComponent != null)
editingContainer.remove(editingComponent);
if (text.contains("\n") || textWidth > width || multiLineRequested) {
editingComponent = multiLinePane;
multiLineEditor.setText(text);
multiLineEditor.setFont(font);
int equals = text.indexOf('=');
if (equals >= 0)
multiLineEditor.setCaretPosition(equals);
multiLineRequested = false;
} else {
editingComponent = oneLineEditor;
oneLineEditor.setText(text);
oneLineEditor.setFont(font);
}
editingContainer.add(editingComponent);
undoManager.discardAllEdits();
return editingContainer;
}
use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class EquationTreeCellEditor method isCellEditable.
/**
* Indicate whether the current cell may be edited.
* Apparently, this method is called in only two situations:
* 1) The left or right (but not center or scroll wheel) mouse button has been clicked.
* In this case, event is the MouseEvent. For a double-click, the first and second presses
* are delivered as separate events (with click count set to 2 on the second press).
* 2) startEditingAtPath() was called, and JTree wants to verify that editing is permitted.
* In this case, event is null.
*/
@Override
public boolean isCellEditable(EventObject event) {
if (event != null) {
Object source = event.getSource();
if (!(source instanceof JTree))
return false;
if (source != tree) {
// Allow us to change trees,
setTree((JTree) source);
// but still avoid immediate edit.
return false;
}
if (event instanceof MouseEvent) {
MouseEvent me = (MouseEvent) event;
final TreePath path = tree.getPathForLocation(me.getX(), me.getY());
if (path != null) {
if (me.getClickCount() == 1 && SwingUtilities.isLeftMouseButton(me) && path.equals(lastPath)) {
EventQueue.invokeLater(new Runnable() {
public void run() {
tree.startEditingAtPath(path);
}
});
}
}
}
return false;
}
if (lastPath == null)
return false;
Object o = lastPath.getLastPathComponent();
if (!(o instanceof NodeBase))
return false;
editingNode = (NodeBase) o;
if (!editingNode.allowEdit())
return false;
return true;
}
use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class FilteredTreeModel method isLeaf.
public boolean isLeaf(Object node) {
// Don't bother with getAllowsChildren()
NodeBase p = (NodeBase) node;
List<Integer> filtered = p.getFiltered();
if (filtered == null)
return p.isLeaf();
return filtered.size() == 0;
}
use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.
the class FilteredTreeModel method getChildCount.
public int getChildCount(Object parent) {
NodeBase p = (NodeBase) parent;
List<Integer> filtered = p.getFiltered();
if (filtered == null)
return p.getChildCount();
return filtered.size();
}
Aggregations