Search in sources :

Example 56 with TreeNode

use of javax.swing.tree.TreeNode in project JMRI by JMRI.

the class WarrantRoute method showRoute.

/**
     * Callback from RouteFinder - exactly one route found
     *
     * @param destNode destination block
     * @param tree     possible routes
     */
protected void showRoute(DefaultMutableTreeNode destNode, DefaultTreeModel tree) {
    TreeNode[] nodes = tree.getPathToRoot(destNode);
    _orders.clear();
    for (TreeNode node : nodes) {
        _orders.add((BlockOrder) ((DefaultMutableTreeNode) node).getUserObject());
    }
    _routeModel.fireTableDataChanged();
    if (log.isDebugEnabled()) {
        log.debug("showRoute: Route has " + _orders.size() + " orders.");
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Example 57 with TreeNode

use of javax.swing.tree.TreeNode in project JMRI by JMRI.

the class CombinedLocoSelTreePane method updateForDecoderTypeID.

/**
     * Decoder identify has matched one or more specific types
     */
@Override
void updateForDecoderTypeID(List<DecoderFile> pList) {
    // find and select the first item
    if (log.isDebugEnabled()) {
        StringBuffer buf = new StringBuffer("Identified " + pList.size() + " matches: ");
        for (int i = 0; i < pList.size(); i++) {
            buf.append(pList.get(i).getModel() + ":");
        }
        log.debug(buf.toString());
    }
    if (pList.size() <= 0) {
        log.error("Found empty list in updateForDecoderTypeID, should not happen");
        return;
    }
    dTree.clearSelection();
    // and issue a warning instruction in the status bar
    if (pList.size() > 1) {
        dTree.getSelectionModel().setSelectionMode(DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
        _statusLabel.setText(Bundle.getMessage("StateMultipleMatch"));
    } else {
        dTree.getSelectionModel().setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
    }
    // set everybody not identified 
    Enumeration<DecoderTreeNode> e = dRoot.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        // loop over the tree
        DecoderTreeNode node = e.nextElement();
        node.setIdentified(false);
    }
    selectedPath = new ArrayList<TreePath>();
    // Find decoder nodes in tree and set selected
    for (int i = 0; i < pList.size(); i++) {
        // loop over selected decoders
        e = dRoot.breadthFirstEnumeration();
        DecoderFile f = pList.get(i);
        String findMfg = f.getMfg();
        String findFamily = f.getFamily();
        String findModel = f.getModel();
        while (e.hasMoreElements()) {
            // loop over the tree & find node
            DecoderTreeNode node = e.nextElement();
            // never match show=NO nodes
            if (node.getShowable() == DecoderFile.Showable.NO) {
                continue;
            }
            // convert path to comparison string
            TreeNode[] list = node.getPath();
            if (list.length == 3) {
                // check for match to mfg, model (as family)
                if (list[1].toString().equals(findMfg) && list[2].toString().equals(findModel)) {
                    log.debug("match length 3");
                    node.setIdentified(true);
                    dModel.reload();
                    ((DecoderTreeNode) list[1]).setIdentified(true);
                    ((DecoderTreeNode) list[2]).setIdentified(true);
                    TreePath path = new TreePath(node.getPath());
                    selectedPath.add(path);
                    break;
                }
            } else if (list.length == 4) {
                // check for match to mfg, family, model
                if (list[1].toString().equals(findMfg) && list[2].toString().equals(findFamily) && list[3].toString().equals(findModel)) {
                    log.debug("match length 4");
                    node.setIdentified(true);
                    dModel.reload();
                    ((DecoderTreeNode) list[1]).setIdentified(true);
                    ((DecoderTreeNode) list[2]).setIdentified(true);
                    ((DecoderTreeNode) list[3]).setIdentified(true);
                    TreePath path = new TreePath(node.getPath());
                    selectedPath.add(path);
                    break;
                }
            }
        }
    }
    // now select and show paths in tree
    for (TreePath path : selectedPath) {
        dTree.addSelectionPath(path);
        dTree.expandPath(path);
        dTree.scrollPathToVisible(path);
    }
}
Also used : DecoderFile(jmri.jmrit.decoderdefn.DecoderFile) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Example 58 with TreeNode

use of javax.swing.tree.TreeNode in project jdk8u_jdk by JetBrains.

the class bug6505523 method createAndShowGUI.

private static void createAndShowGUI() {
    final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Problem with NPE under JDK 1.6");
    final DefaultMutableTreeNode problematic = new DefaultMutableTreeNode("Expand me and behold a NPE in stderr");
    problematic.add(new DefaultMutableTreeNode("some content"));
    root.add(new DefaultMutableTreeNode("irrelevant..."));
    root.add(problematic);
    final DefaultTreeModel model = new DefaultTreeModel(root);
    tree = new JTree(model);
    tree.setRootVisible(true);
    tree.setShowsRootHandles(true);
    tree.expandRow(0);
    tree.collapseRow(2);
    // this is critical - without dragEnabled everything works
    tree.setDragEnabled(true);
    tree.addTreeExpansionListener(new TreeExpansionListener() {

        @Override
        public void treeExpanded(TreeExpansionEvent event) {
            TreeNode parent = problematic.getParent();
            if (parent instanceof DefaultMutableTreeNode) {
                model.removeNodeFromParent(problematic);
            }
        }

        @Override
        public void treeCollapsed(TreeExpansionEvent event) {
        }
    });
    JFrame frame = new JFrame("JTree Problem");
    frame.add(new JScrollPane(tree));
    frame.setSize(500, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
Also used : JScrollPane(javax.swing.JScrollPane) JTree(javax.swing.JTree) TreeExpansionListener(javax.swing.event.TreeExpansionListener) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) JFrame(javax.swing.JFrame) TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Example 59 with TreeNode

use of javax.swing.tree.TreeNode in project jdk8u_jdk by JetBrains.

the class Test4631471 method main.

private static void main() throws Exception {
    // the DefaultMutableTreeNode will archive correctly
    new Test4631471() {

        protected Object getObject() {
            return getRoot();
        }
    }.test(false);
    // the DefaultTreeModel will also archive correctly
    new Test4631471() {

        protected Object getObject() {
            return getModel();
        }
    }.test(false);
    // create a new model from the root node
    // this simulates the the MetaData ctor:
    // registerConstructor("javax.swing.tree.DefaultTreeModel", new String[]{"root"});
    new Test4631471() {

        protected Object getObject() {
            return new DefaultTreeModel((TreeNode) getModel().getRoot());
        }
    }.test(false);
    // the JTree will archive correctly too
    new Test4631471() {

        protected Object getObject() {
            return getTree();
        }
    }.test(false);
}
Also used : TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel)

Example 60 with TreeNode

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

the class PropTreeModel method getNodeForKey.

public TreeNode getNodeForKey(DashHierarchy props, PropertyKey key) {
    if (key == null)
        return null;
    if (rootKey.equals(key))
        return (TreeNode) getRoot();
    PropertyKey parentKey = key.getParent();
    int numChildren = props.getNumChildren(parentKey);
    TreeNode parent = getNodeForKey(props, parentKey);
    if (parent == null || parent.getChildCount() != numChildren)
        return null;
    PropertyKey childKey;
    for (int i = numChildren; i-- > 0; ) {
        childKey = props.getChildKey(parentKey, i);
        if (childKey.equals(key))
            return parent.getChildAt(i);
    }
    return null;
}
Also used : TreeNode(javax.swing.tree.TreeNode) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) PropertyKey(net.sourceforge.processdash.hier.PropertyKey)

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