Search in sources :

Example 1 with ChangePart

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

the class NodePart method applyEdit.

@Override
public void applyEdit(JTree tree) {
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    String input = (String) getUserObject();
    String[] pieces = input.split("=", 2);
    String name = pieces[0].trim();
    String oldKey = source.key();
    if (name.equals(oldKey)) {
        setUserObject();
        model.nodeChanged(this);
        return;
    }
    PanelModel mep = PanelModel.instance;
    if (// Edits to root cause a rename of the document on disk
    isRoot()) {
        if (name.isEmpty()) {
            setUserObject();
            model.nodeChanged(this);
            return;
        }
        name = MDir.validFilenameFrom(name);
        // In addition to filename constraints, we also forbid comma, because these names are used in list expressions.
        name = name.replace(",", "-");
        String stem = name;
        int suffix = 0;
        MNode models = AppData.models;
        MNode existingDocument = models.child(name);
        while (existingDocument != null) {
            suffix++;
            name = stem + " " + suffix;
            existingDocument = models.child(name);
        }
        mep.undoManager.add(new ChangeDoc(oldKey, name));
        // MDir promises to maintain object identity during the move, so "source" is still valid.
        return;
    }
    if (input.isEmpty()) {
        delete(tree, true);
        return;
    }
    name = validIdentifierFrom(name);
    NodeBase parent = (NodeBase) getParent();
    NodeBase sibling = parent.child(name);
    if (// the name already exists in top document, so reject rename
    sibling != null && (sibling.source.isFromTopDocument() || !(sibling instanceof NodePart))) {
        setUserObject();
        model.nodeChanged(this);
        return;
    }
    mep.undoManager.add(new ChangePart(this, oldKey, name));
}
Also used : PanelModel(gov.sandia.n2a.ui.eq.PanelModel) ChangeDoc(gov.sandia.n2a.ui.eq.undo.ChangeDoc) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel) MNode(gov.sandia.n2a.db.MNode) ChangePart(gov.sandia.n2a.ui.eq.undo.ChangePart)

Aggregations

MNode (gov.sandia.n2a.db.MNode)1 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)1 PanelModel (gov.sandia.n2a.ui.eq.PanelModel)1 ChangeDoc (gov.sandia.n2a.ui.eq.undo.ChangeDoc)1 ChangePart (gov.sandia.n2a.ui.eq.undo.ChangePart)1