Search in sources :

Example 1 with ChangeAnnotation

use of gov.sandia.n2a.ui.eq.undo.ChangeAnnotation in project n2a by frothga.

the class NodeAnnotation method applyEdit.

@Override
public void applyEdit(JTree tree) {
    String input = (String) getUserObject();
    if (input.isEmpty()) {
        delete(tree, true);
        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 = "";
    NodeBase parent = (NodeBase) getParent();
    String oldName = source.key();
    String oldValue = source.get();
    if (!name.equals(oldName)) {
        // Check if name change is forbidden
        if (name.isEmpty()) {
            name = oldName;
        } else {
            MPart mparent = source.getParent();
            MPart partAfter = (MPart) mparent.child(name);
            if (partAfter != null && partAfter.isFromTopDocument())
                name = oldName;
        }
    }
    if (name.equals(oldName) && value.equals(oldValue)) {
        FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
        FontMetrics fm = getFontMetrics(tree);
        parent.updateTabStops(fm);
        // Our siblings should not change, because we did not really change. Just repaint in non-edit mode.
        model.nodeChanged(this);
        return;
    }
    PanelModel.instance.undoManager.add(new ChangeAnnotation(parent, oldName, oldValue, name, value));
}
Also used : MPart(gov.sandia.n2a.eqset.MPart) ChangeAnnotation(gov.sandia.n2a.ui.eq.undo.ChangeAnnotation) FontMetrics(java.awt.FontMetrics) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Aggregations

MPart (gov.sandia.n2a.eqset.MPart)1 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)1 ChangeAnnotation (gov.sandia.n2a.ui.eq.undo.ChangeAnnotation)1 FontMetrics (java.awt.FontMetrics)1