Search in sources :

Example 1 with DeleteAnnotation

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

the class PanelEquationTree method watchSelected.

public void watchSelected(boolean clearOthers) {
    if (container.locked)
        return;
    TreePath[] paths = tree.getSelectionPaths();
    if (paths == null)
        return;
    TreePath leadPath = tree.getLeadSelectionPath();
    NodeVariable leadVariable = null;
    List<NodeVariable> variables = new ArrayList<NodeVariable>(paths.length);
    for (TreePath path : paths) {
        NodeBase n = (NodeBase) path.getLastPathComponent();
        // Make one attempt to walk up tree, in case an equation or metadata item is selected under a variable.
        if (!(n instanceof NodeVariable))
            n = (NodeBase) n.getParent();
        if (!(n instanceof NodeVariable))
            continue;
        NodeVariable v = (NodeVariable) n;
        if (v.isBinding)
            continue;
        variables.add(v);
        if (path.equals(leadPath))
            leadVariable = v;
    }
    if (clearOthers)
        findExistingWatches(container.root, variables);
    UndoManager um = MainFrame.instance.undoManager;
    CompoundEditView compound = null;
    boolean multi = variables.size() > 1;
    if (multi)
        um.addEdit(compound = new CompoundEditView(CompoundEditView.CLEAR_TREE));
    for (NodeVariable v : variables) {
        // Toggle watch on the selected variable
        boolean selectVariable = multi || tree.isCollapsed(new TreePath(v.getPath()));
        MPart watch = (MPart) v.source.child("$metadata", "watch");
        if (watch != null) {
            NodeAnnotation watchNode = (NodeAnnotation) v.child("watch");
            if (// currently on, so turn it off
            watch.isFromTopDocument()) {
                DeleteAnnotation d = new DeleteAnnotation(watchNode, false);
                d.selectVariable = selectVariable;
                if (multi)
                    compound.addEdit(d);
                else
                    um.apply(d);
            } else // Currently off, because it is not explicitly set in this document. Turn it on by overriding it locally.
            {
                ChangeAnnotation c = new ChangeAnnotation(watchNode, "watch", "");
                c.selectVariable = selectVariable;
                if (multi)
                    compound.addEdit(c);
                else
                    um.apply(c);
            }
        } else // currently off, so turn it on
        {
            AddAnnotation a = new AddAnnotation(v, v.getChildCount(), new MVolatile("", "watch"));
            a.selectVariable = selectVariable;
            if (multi)
                compound.addEdit(a);
            else
                um.apply(a);
        }
    }
    if (multi) {
        um.endCompoundEdit();
        if (leadVariable != null)
            compound.leadPath = leadVariable.getKeyPath();
        compound.redo();
    }
}
Also used : MPart(gov.sandia.n2a.eqset.MPart) ChangeAnnotation(gov.sandia.n2a.ui.eq.undo.ChangeAnnotation) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) ArrayList(java.util.ArrayList) MVolatile(gov.sandia.n2a.db.MVolatile) AddAnnotation(gov.sandia.n2a.ui.eq.undo.AddAnnotation) NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) TreePath(javax.swing.tree.TreePath) UndoManager(gov.sandia.n2a.ui.UndoManager) NodeAnnotation(gov.sandia.n2a.ui.eq.tree.NodeAnnotation) DeleteAnnotation(gov.sandia.n2a.ui.eq.undo.DeleteAnnotation) CompoundEditView(gov.sandia.n2a.ui.eq.undo.CompoundEditView)

Aggregations

MVolatile (gov.sandia.n2a.db.MVolatile)1 MPart (gov.sandia.n2a.eqset.MPart)1 UndoManager (gov.sandia.n2a.ui.UndoManager)1 NodeAnnotation (gov.sandia.n2a.ui.eq.tree.NodeAnnotation)1 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)1 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)1 AddAnnotation (gov.sandia.n2a.ui.eq.undo.AddAnnotation)1 ChangeAnnotation (gov.sandia.n2a.ui.eq.undo.ChangeAnnotation)1 CompoundEditView (gov.sandia.n2a.ui.eq.undo.CompoundEditView)1 DeleteAnnotation (gov.sandia.n2a.ui.eq.undo.DeleteAnnotation)1 ArrayList (java.util.ArrayList)1 TreePath (javax.swing.tree.TreePath)1