Search in sources :

Example 6 with ChangeEquation

use of gov.sandia.n2a.ui.eq.undo.ChangeEquation 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));
}
Also used : ChangeEquation(gov.sandia.n2a.ui.eq.undo.ChangeEquation) MPart(gov.sandia.n2a.eqset.MPart) Variable(gov.sandia.n2a.eqset.Variable) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Aggregations

ChangeEquation (gov.sandia.n2a.ui.eq.undo.ChangeEquation)6 Variable (gov.sandia.n2a.eqset.Variable)4 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)4 AddEquation (gov.sandia.n2a.ui.eq.undo.AddEquation)3 ChangeVariable (gov.sandia.n2a.ui.eq.undo.ChangeVariable)3 DeleteVariable (gov.sandia.n2a.ui.eq.undo.DeleteVariable)3 AddAnnotation (gov.sandia.n2a.ui.eq.undo.AddAnnotation)2 AddReference (gov.sandia.n2a.ui.eq.undo.AddReference)2 DeleteEquation (gov.sandia.n2a.ui.eq.undo.DeleteEquation)2 Point (java.awt.Point)2 TreePath (javax.swing.tree.TreePath)2 MVolatile (gov.sandia.n2a.db.MVolatile)1 MPart (gov.sandia.n2a.eqset.MPart)1 CompoundEdit (gov.sandia.n2a.ui.CompoundEdit)1 UndoManager (gov.sandia.n2a.ui.UndoManager)1 AddInherit (gov.sandia.n2a.ui.eq.undo.AddInherit)1 ChangeAnnotations (gov.sandia.n2a.ui.eq.undo.ChangeAnnotations)1 ChangeReferences (gov.sandia.n2a.ui.eq.undo.ChangeReferences)1 ChangeVariableToInherit (gov.sandia.n2a.ui.eq.undo.ChangeVariableToInherit)1