Search in sources :

Example 61 with TreeNode

use of javax.swing.tree.TreeNode in project processdash by dtuma.

the class TreeListSelector method expandAll.

private void expandAll(TreePath parentPath, boolean expand) {
    TreeNode node = (TreeNode) parentPath.getLastPathComponent();
    for (Enumeration e = node.children(); e.hasMoreElements(); ) {
        TreeNode child = (TreeNode) e.nextElement();
        TreePath childPath = parentPath.pathByAddingChild(child);
        expandAll(childPath, expand);
    }
    if (expand)
        treeComponent.expandPath(parentPath);
    else
        treeComponent.collapsePath(parentPath);
}
Also used : Enumeration(java.util.Enumeration) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode)

Example 62 with TreeNode

use of javax.swing.tree.TreeNode in project processdash by dtuma.

the class TreeListSelector method setSelectedList.

public void setSelectedList(List selectedList) {
    // reset tree expansion to only show first level
    TreeNode root = (TreeNode) treeModel.getRoot();
    TreePath rootPath = new TreePath(root);
    for (Enumeration e = root.children(); e.hasMoreElements(); ) expandAll(rootPath.pathByAddingChild(e.nextElement()), false);
    treeComponent.getSelectionModel().clearSelection();
    listModel.clear();
    for (Iterator iter = selectedList.iterator(); iter.hasNext(); ) {
        listModel.addElement(iter.next());
    }
}
Also used : Enumeration(java.util.Enumeration) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) Iterator(java.util.Iterator)

Example 63 with TreeNode

use of javax.swing.tree.TreeNode in project n2a by frothga.

the class NodeBase method getKeyPath.

// Structure maintenance -------------------------------------------------
public List<String> getKeyPath() {
    TreeNode[] path = getPath();
    List<String> result = new ArrayList<String>(path.length);
    for (TreeNode n : path) result.add(((NodeBase) n).source.key());
    return result;
}
Also used : TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArrayList(java.util.ArrayList)

Example 64 with TreeNode

use of javax.swing.tree.TreeNode 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 65 with TreeNode

use of javax.swing.tree.TreeNode in project n2a by frothga.

the class ChangeAnnotation method apply.

public static void apply(List<String> path, String nameBefore, String nameAfter, String valueAfter, String blockName, NodeFactory factory) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    NodeBase nodeBefore = parent.child(nameBefore);
    if (nodeBefore == null)
        throw new CannotRedoException();
    PanelEquationTree pet = PanelModel.instance.panelEquations;
    JTree tree = pet.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    FontMetrics fm = nodeBefore.getFontMetrics(tree);
    NodeBase nodeAfter;
    if (nameBefore.equals(nameAfter)) {
        nodeAfter = nodeBefore;
        nodeAfter.source.set(valueAfter);
    } else {
        // Update database
        MPart mparent;
        if (parent instanceof NodeVariable)
            mparent = (MPart) parent.source.child(blockName);
        else
            mparent = parent.source;
        // should directly change destinationNode if it exists
        MPart newPart = (MPart) mparent.set(nameAfter, valueAfter);
        mparent.clear(nameBefore);
        MPart oldPart = (MPart) mparent.child(nameBefore);
        // Update GUI
        nodeAfter = parent.child(nameAfter);
        if (oldPart == null) {
            if (nodeAfter == null) {
                nodeAfter = nodeBefore;
                nodeAfter.source = newPart;
            } else {
                model.removeNodeFromParent(nodeBefore);
            }
        } else {
            if (nodeAfter == null) {
                int index = parent.getIndex(nodeBefore);
                nodeAfter = factory.create(newPart);
                model.insertNodeIntoUnfiltered(nodeAfter, parent, index);
            }
        }
    }
    nodeAfter.updateColumnWidths(fm);
    parent.updateTabStops(fm);
    parent.allNodesChanged(model);
    TreeNode[] nodePath = nodeAfter.getPath();
    pet.updateOrder(nodePath);
    pet.updateVisibility(nodePath);
    // Only an inherited lock node can be touched by editing. It is possible to activate (make local) if the user assigns a specific value to it.
    if (path.size() == 2 && path.get(1).equals("$metadata") && (nameBefore.equals("lock") || nameAfter.equals("lock")))
        pet.updateLock();
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) JTree(javax.swing.JTree) MPart(gov.sandia.n2a.eqset.MPart) FontMetrics(java.awt.FontMetrics) TreeNode(javax.swing.tree.TreeNode) CannotRedoException(javax.swing.undo.CannotRedoException) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) PanelEquationTree(gov.sandia.n2a.ui.eq.PanelEquationTree) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Aggregations

TreeNode (javax.swing.tree.TreeNode)313 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)146 TreePath (javax.swing.tree.TreePath)125 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)31 Enumeration (java.util.Enumeration)30 PanelEquationTree (gov.sandia.n2a.ui.eq.PanelEquationTree)28 MPart (gov.sandia.n2a.eqset.MPart)27 ArrayList (java.util.ArrayList)27 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)25 JTree (javax.swing.JTree)23 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)23 Nullable (org.jetbrains.annotations.Nullable)23 MutableTreeNode (javax.swing.tree.MutableTreeNode)19 CannotRedoException (javax.swing.undo.CannotRedoException)18 NodePart (gov.sandia.n2a.ui.eq.tree.NodePart)16 CannotUndoException (javax.swing.undo.CannotUndoException)14 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)12 MouseEvent (java.awt.event.MouseEvent)11 NotNull (org.jetbrains.annotations.NotNull)11 PanelEquations (gov.sandia.n2a.ui.eq.PanelEquations)10