Search in sources :

Example 1 with MPart

use of gov.sandia.n2a.eqset.MPart in project n2a by frothga.

the class NodePart method build.

@Override
public void build() {
    setUserObject();
    removeAllChildren();
    String order = source.get("$metadata", "gui.order");
    Set<String> sorted = new HashSet<String>();
    // comma-separated list
    String[] subkeys = order.split(",");
    for (String k : subkeys) {
        MNode c = source.child(k);
        if (c != null) {
            buildTriage((MPart) c);
            sorted.add(k);
        }
    }
    // Build everything else. Sort all subparts to the end.
    ArrayList<MNode> subparts = new ArrayList<MNode>();
    for (MNode c : source) {
        if (sorted.contains(c.key()))
            continue;
        if (MPart.isPart(c))
            subparts.add(c);
        else
            buildTriage((MPart) c);
    }
    for (MNode c : subparts) buildTriage((MPart) c);
}
Also used : MPart(gov.sandia.n2a.eqset.MPart) ArrayList(java.util.ArrayList) MNode(gov.sandia.n2a.db.MNode) HashSet(java.util.HashSet)

Example 2 with MPart

use of gov.sandia.n2a.eqset.MPart in project n2a by frothga.

the class NodeReference 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 ChangeReference(parent, oldName, oldValue, name, value));
}
Also used : MPart(gov.sandia.n2a.eqset.MPart) FontMetrics(java.awt.FontMetrics) ChangeReference(gov.sandia.n2a.ui.eq.undo.ChangeReference) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 3 with MPart

use of gov.sandia.n2a.eqset.MPart in project n2a by frothga.

the class NodeVariable method build.

@Override
public void build() {
    removeAllChildren();
    // but should do little harm.
    if (source.isFromTopDocument())
        enforceOneLine(source);
    setUserObject(source.key() + "=" + getValue());
    for (MNode n : source) {
        if (n.key().startsWith("@"))
            add(new NodeEquation((MPart) n));
    }
    MPart metadata = (MPart) source.child("$metadata");
    if (metadata != null) {
        for (MNode m : metadata) add(new NodeAnnotation((MPart) m));
    }
    MPart references = (MPart) source.child("$reference");
    if (references != null) {
        for (MNode r : references) add(new NodeReference((MPart) r));
    }
}
Also used : MPart(gov.sandia.n2a.eqset.MPart) MNode(gov.sandia.n2a.db.MNode)

Example 4 with MPart

use of gov.sandia.n2a.eqset.MPart in project n2a by frothga.

the class AddAnnotation method create.

public static NodeBase create(List<String> path, int index, String name, String value, String blockName, NodeFactory factory, NodeFactory factoryBlock) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    MPart block = (MPart) parent.source.childOrCreate(blockName);
    PanelEquationTree pet = PanelModel.instance.panelEquations;
    JTree tree = pet.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    // If this is a variable, then mix metadata with equations and references
    NodeBase container = parent;
    if (// If this is a part, then display special block
    parent instanceof NodePart) {
        if (// empty implies the node is absent
        block.size() == 0) {
            container = factoryBlock.create(block);
            model.insertNodeIntoUnfiltered(container, parent, index);
            index = 0;
        } else // the node is present, so retrieve it
        {
            container = parent.child(blockName);
        }
    }
    NodeBase createdNode = container.child(name);
    boolean alreadyExists = createdNode != null;
    MPart createdPart = (MPart) block.set(name, value);
    if (!alreadyExists)
        createdNode = factory.create(createdPart);
    FontMetrics fm = createdNode.getFontMetrics(tree);
    if (container.getChildCount() > 0) {
        NodeBase firstChild = (NodeBase) container.getChildAt(0);
        if (firstChild.needsInitTabs())
            firstChild.initTabs(fm);
    }
    // pure create, so about to go into edit mode. This should only happen on first application of the create action, and should only be possible if visibility is already correct.
    if (value == null)
        createdNode.setUserObject("");
    // preempt initialization; uses actual name, not user value
    createdNode.updateColumnWidths(fm);
    if (!alreadyExists)
        model.insertNodeIntoUnfiltered(createdNode, container, index);
    if (// create was merged with change name/value
    value != null) {
        container.updateTabStops(fm);
        container.allNodesChanged(model);
        TreeNode[] createdPath = createdNode.getPath();
        pet.updateOrder(createdPath);
        pet.updateVisibility(createdPath);
        if (path.size() == 1 && name.equals("lock"))
            pet.updateLock();
    }
    return createdNode;
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) MPart(gov.sandia.n2a.eqset.MPart) JTree(javax.swing.JTree) FontMetrics(java.awt.FontMetrics) TreeNode(javax.swing.tree.TreeNode) CannotRedoException(javax.swing.undo.CannotRedoException) NodePart(gov.sandia.n2a.ui.eq.tree.NodePart) PanelEquationTree(gov.sandia.n2a.ui.eq.PanelEquationTree) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 5 with MPart

use of gov.sandia.n2a.eqset.MPart in project n2a by frothga.

the class AddAnnotation method uniqueName.

public static String uniqueName(NodeBase parent, String blockName, String prefix, boolean allowEmptySuffix) {
    MPart block = (MPart) parent.source.child(blockName);
    if (allowEmptySuffix && (block == null || block.child(prefix) == null))
        return prefix;
    int suffix = 1;
    if (block != null) {
        while (block.child(prefix + suffix) != null) suffix++;
    }
    return prefix + suffix;
}
Also used : MPart(gov.sandia.n2a.eqset.MPart)

Aggregations

MPart (gov.sandia.n2a.eqset.MPart)37 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)21 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)20 JTree (javax.swing.JTree)18 PanelModel (gov.sandia.n2a.ui.eq.PanelModel)16 TreeNode (javax.swing.tree.TreeNode)15 FontMetrics (java.awt.FontMetrics)13 NodePart (gov.sandia.n2a.ui.eq.tree.NodePart)10 CannotRedoException (javax.swing.undo.CannotRedoException)10 CannotUndoException (javax.swing.undo.CannotUndoException)9 MNode (gov.sandia.n2a.db.MNode)5 EquationSet (gov.sandia.n2a.eqset.EquationSet)5 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)5 Variable (gov.sandia.n2a.eqset.Variable)4 ArrayList (java.util.ArrayList)4 ParseException (gov.sandia.n2a.language.ParseException)3 PanelEquationTree (gov.sandia.n2a.ui.eq.PanelEquationTree)3 NodeAnnotation (gov.sandia.n2a.ui.eq.tree.NodeAnnotation)3 NodeEquation (gov.sandia.n2a.ui.eq.tree.NodeEquation)3 IncommensurableException (javax.measure.IncommensurableException)3