Example usage for javax.swing.tree DefaultMutableTreeNode getChildAt

List of usage examples for javax.swing.tree DefaultMutableTreeNode getChildAt

Introduction

In this page you can find the example usage for javax.swing.tree DefaultMutableTreeNode getChildAt.

Prototype

public TreeNode getChildAt(int index) 

Source Link

Document

Returns the child at the specified index in this node's child array.

Usage

From source file:org.apache.oodt.cas.workflow.gui.perspective.view.impl.DefaultTreeView.java

private TreePath getTreePath(TreePath currentPath, ViewState state) {
    String lookingForPath = state.getCurrentMetGroup();
    Stack<DefaultMutableTreeNode> stack = new Stack<DefaultMutableTreeNode>();
    DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) currentPath.getLastPathComponent();
    for (int i = 0; i < baseNode.getChildCount(); i++) {
        stack.push((DefaultMutableTreeNode) baseNode.getChildAt(i));
    }/*from w w  w  .j ava  2  s  .  co  m*/
    while (!stack.empty()) {
        DefaultMutableTreeNode node = stack.pop();
        if (node.getUserObject().equals("static-metadata")) {
            for (int i = 0; i < node.getChildCount(); i++) {
                stack.push((DefaultMutableTreeNode) node.getChildAt(i));
            }
        } else if (node.getUserObject() instanceof ConcurrentHashMap) {
            String key = (String) ((ConcurrentHashMap<String, String>) node.getUserObject()).keySet().iterator()
                    .next();
            if (lookingForPath.equals(key)) {
                return new TreePath(node.getPath());
            } else if (lookingForPath.startsWith(key + "/")) {
                lookingForPath = lookingForPath.substring(lookingForPath.indexOf("/") + 1);
                stack.clear();
                for (int i = 0; i < node.getChildCount(); i++) {
                    stack.add((DefaultMutableTreeNode) node.getChildAt(i));
                }
            }
        }
    }
    return currentPath;
}

From source file:org.apache.oodt.cas.workflow.gui.perspective.view.impl.DefaultTreeView.java

@Override
public void refreshView(final ViewState state) {
    Rectangle visibleRect = null;
    if (this.tree != null) {
        visibleRect = this.tree.getVisibleRect();
    }//from  w  w  w .  j  av  a2s.  c o m

    this.removeAll();

    this.actionsMenu = this.createPopupMenu(state);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("WORKFLOWS");
    for (ModelGraph graph : state.getGraphs()) {
        root.add(this.buildTree(graph, state));
    }
    tree = new JTree(root);
    tree.setShowsRootHandles(true);
    tree.setRootVisible(false);
    tree.add(this.actionsMenu);

    if (state.getSelected() != null) {
        // System.out.println("SELECTED: " + state.getSelected());
        TreePath treePath = this.getTreePath(root, state.getSelected());
        if (state.getCurrentMetGroup() != null) {
            treePath = this.getTreePath(treePath, state);
        } else if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_STATIC_METADATA))) {
            DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int i = 0; i < baseNode.getChildCount(); i++) {
                if (((DefaultMutableTreeNode) baseNode.getChildAt(i)).getUserObject()
                        .equals("static-metadata")) {
                    treePath = new TreePath(((DefaultMutableTreeNode) baseNode.getChildAt(i)).getPath());
                    break;
                }
            }
        } else if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_PRECONDITIONS))) {
            if (treePath == null) {
                treePath = this.getTreePath(root, state.getSelected().getPreConditions());
            }
            DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int i = 0; i < baseNode.getChildCount(); i++) {
                if (((DefaultMutableTreeNode) baseNode.getChildAt(i)).getUserObject()
                        .equals("pre-conditions")) {
                    treePath = new TreePath(((DefaultMutableTreeNode) baseNode.getChildAt(i)).getPath());
                    break;
                }
            }
        } else if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_POSTCONDITIONS))) {
            if (treePath == null) {
                treePath = this.getTreePath(root, state.getSelected().getPostConditions());
            }
            DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int i = 0; i < baseNode.getChildCount(); i++) {
                if (((DefaultMutableTreeNode) baseNode.getChildAt(i)).getUserObject()
                        .equals("post-conditions")) {
                    treePath = new TreePath(((DefaultMutableTreeNode) baseNode.getChildAt(i)).getPath());
                    break;
                }
            }
        }
        this.tree.expandPath(treePath);
        this.tree.setSelectionPath(treePath);
    }

    tree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            if (e.getPath().getLastPathComponent() instanceof DefaultMutableTreeNode) {
                DefaultTreeView.this.resetProperties(state);
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                if (node.getUserObject() instanceof ModelGraph) {
                    state.setSelected((ModelGraph) node.getUserObject());
                    state.setCurrentMetGroup(null);
                    DefaultTreeView.this.notifyListeners();
                } else if (node.getUserObject().equals("static-metadata")
                        || node.getUserObject().equals("pre-conditions")
                        || node.getUserObject().equals("post-conditions")) {
                    state.setSelected((ModelGraph) ((DefaultMutableTreeNode) node.getParent()).getUserObject());
                    state.setCurrentMetGroup(null);
                    state.setProperty(EXPAND_STATIC_METADATA,
                            Boolean.toString(node.getUserObject().equals("static-metadata")));
                    state.setProperty(EXPAND_PRECONDITIONS,
                            Boolean.toString(node.getUserObject().equals("pre-conditions")));
                    state.setProperty(EXPAND_POSTCONDITIONS,
                            Boolean.toString(node.getUserObject().equals("post-conditions")));
                    DefaultTreeView.this.notifyListeners();
                } else if (node.getUserObject() instanceof ConcurrentHashMap) {
                    DefaultMutableTreeNode metNode = null;
                    String group = null;
                    Object[] path = e.getPath().getPath();
                    for (int i = path.length - 1; i >= 0; i--) {
                        if (((DefaultMutableTreeNode) path[i]).getUserObject() instanceof ModelGraph) {
                            metNode = (DefaultMutableTreeNode) path[i];
                            break;
                        } else if (((DefaultMutableTreeNode) path[i])
                                .getUserObject() instanceof ConcurrentHashMap) {
                            if (group == null) {
                                group = (String) ((ConcurrentHashMap<String, String>) ((DefaultMutableTreeNode) path[i])
                                        .getUserObject()).keySet().iterator().next();
                            } else {
                                group = (String) ((ConcurrentHashMap<String, String>) ((DefaultMutableTreeNode) path[i])
                                        .getUserObject()).keySet().iterator().next() + "/" + group;
                            }
                        }
                    }
                    ModelGraph graph = (ModelGraph) metNode.getUserObject();
                    state.setSelected(graph);
                    state.setCurrentMetGroup(group);
                    DefaultTreeView.this.notifyListeners();
                } else {
                    state.setSelected(null);
                    DefaultTreeView.this.notifyListeners();
                }
            }
        }

    });
    tree.setCellRenderer(new TreeCellRenderer() {

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            if (node.getUserObject() instanceof String) {
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                JLabel label = new JLabel((String) node.getUserObject());
                label.setForeground(Color.blue);
                panel.add(label, BorderLayout.CENTER);
                panel.setBackground(selected ? Color.lightGray : Color.white);
                return panel;
            } else if (node.getUserObject() instanceof ModelGraph) {
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                JLabel iconLabel = new JLabel(
                        ((ModelGraph) node.getUserObject()).getModel().getExecutionType() + ": ");
                iconLabel.setForeground(((ModelGraph) node.getUserObject()).getModel().getColor());
                iconLabel.setBackground(Color.white);
                JLabel idLabel = new JLabel(((ModelGraph) node.getUserObject()).getModel().getModelName());
                idLabel.setBackground(Color.white);
                panel.add(iconLabel, BorderLayout.WEST);
                panel.add(idLabel, BorderLayout.CENTER);
                panel.setBackground(selected ? Color.lightGray : Color.white);
                return panel;
            } else if (node.getUserObject() instanceof ConcurrentHashMap) {
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                String group = (String) ((ConcurrentHashMap<String, String>) node.getUserObject()).keySet()
                        .iterator().next();
                JLabel nameLabel = new JLabel(group + " : ");
                nameLabel.setForeground(Color.blue);
                nameLabel.setBackground(Color.white);
                JLabel valueLabel = new JLabel(
                        ((ConcurrentHashMap<String, String>) node.getUserObject()).get(group));
                valueLabel.setForeground(Color.darkGray);
                valueLabel.setBackground(Color.white);
                panel.add(nameLabel, BorderLayout.WEST);
                panel.add(valueLabel, BorderLayout.EAST);
                panel.setBackground(selected ? Color.lightGray : Color.white);
                return panel;
            } else {
                return new JLabel();
            }
        }

    });
    tree.addMouseListener(new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3 && DefaultTreeView.this.tree.getSelectionPath() != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) DefaultTreeView.this.tree
                        .getSelectionPath().getLastPathComponent();
                if (node.getUserObject() instanceof String && !(node.getUserObject().equals("pre-conditions")
                        || node.getUserObject().equals("post-conditions"))) {
                    return;
                }
                orderSubMenu.setEnabled(node.getUserObject() instanceof ModelGraph
                        && !((ModelGraph) node.getUserObject()).isCondition()
                        && ((ModelGraph) node.getUserObject()).getParent() != null);
                DefaultTreeView.this.actionsMenu.show(DefaultTreeView.this.tree, e.getX(), e.getY());
            }
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }
    });
    this.scrollPane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    this.add(this.scrollPane, BorderLayout.CENTER);
    if (visibleRect != null) {
        this.tree.scrollRectToVisible(visibleRect);
    }

    this.revalidate();
}

From source file:org.codinjutsu.tools.mongo.view.MongoResultPanel.java

private String stringifyResult(DefaultMutableTreeNode selectedResultNode) {
    List<Object> stringifiedObjects = new LinkedList<Object>();
    for (int i = 0; i < selectedResultNode.getChildCount(); i++) {
        DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) selectedResultNode.getChildAt(i);
        stringifiedObjects.add(childNode.getUserObject());
    }// ww w.j  a  v  a2  s  .c  o m

    return String.format("[ %s ]", StringUtils.join(stringifiedObjects, " , "));
}

From source file:org.codinjutsu.tools.nosql.mongo.view.MongoResultPanel.java

private String stringifyResult(DefaultMutableTreeNode selectedResultNode) {
    List<Object> stringifiedObjects = new LinkedList<>();
    for (int i = 0; i < selectedResultNode.getChildCount(); i++) {
        DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) selectedResultNode.getChildAt(i);
        stringifiedObjects.add(childNode.getUserObject());
    }/*from   w  w  w. j a  v  a 2  s  . c o  m*/

    return String.format("[ %s ]", StringUtils.join(stringifiedObjects, " , "));
}

From source file:org.codinjutsu.tools.nosql.redis.view.RedisFragmentedKeyTreeModel.java

public static DefaultMutableTreeNode wrapNodes(DefaultMutableTreeNode source, String separator) {
    if (isEmpty(separator)) {
        return source;
    }/*from ww w.  j a v a 2 s.  co m*/
    DefaultMutableTreeNode targetRootNode = (DefaultMutableTreeNode) source.clone();
    for (int i = 0; i < source.getChildCount(); i++) {
        DefaultMutableTreeNode originalChildNode = (DefaultMutableTreeNode) source.getChildAt(i);
        NoSqlTreeNode clonedChildNode = (NoSqlTreeNode) originalChildNode.clone();
        RedisKeyValueDescriptor descriptor = (RedisKeyValueDescriptor) clonedChildNode.getDescriptor();
        String[] explodedKey = StringUtils.explode(descriptor.getKey(), separator);
        if (explodedKey.length == 1) {
            addChildren(clonedChildNode, originalChildNode);
            targetRootNode.add(clonedChildNode);
        } else {
            updateTree(targetRootNode, originalChildNode, explodedKey, descriptor);
        }
    }
    return targetRootNode;
}

From source file:org.codinjutsu.tools.nosql.redis.view.RedisFragmentedKeyTreeModel.java

private static NoSqlTreeNode findNodeByKey(DefaultMutableTreeNode parentTargetNode, String keyFragment) {
    for (int i = 0; i < parentTargetNode.getChildCount(); i++) {
        NoSqlTreeNode currentChild = (NoSqlTreeNode) parentTargetNode.getChildAt(i);
        NodeDescriptor descriptor = currentChild.getDescriptor();
        String nodeKey;//from w  w  w. j  av  a 2 s .c o  m
        if (descriptor instanceof FragmentedKeyNodeDescriptor) {
            nodeKey = ((FragmentedKeyNodeDescriptor) descriptor).getKeyFragment();
        } else if (descriptor instanceof RedisKeyValueDescriptor) {
            nodeKey = ((RedisKeyValueDescriptor) descriptor).getKey();
        } else {
            return null;
        }
        if (keyFragment.equals(nodeKey)) {
            return currentChild;
        }
    }
    return null;
}

From source file:org.eclipse.wb.tests.designer.swing.model.component.JTreeTest.java

public void test_JTree_parsing() throws Exception {
    ContainerInfo panel = parseContainer("import javax.swing.tree.*;", "public class Test extends JPanel {",
            "  public Test() {", "    JTree tree = new JTree();", "    add(tree);",
            "    tree.setModel(new DefaultTreeModel(", "      new DefaultMutableTreeNode('(root)') {",
            "        {", "          DefaultMutableTreeNode node1 = new DefaultMutableTreeNode('1');",
            "            DefaultMutableTreeNode node2 = new DefaultMutableTreeNode('11');",
            "            node1.add(node2);", "          add(node1);",
            "          node1 = new DefaultMutableTreeNode('2');",
            "            node1.add(new DefaultMutableTreeNode('21'));", "          add(node1);", "        }",
            "      }", "    ));", "  }", "}");
    panel.refresh();/*  w ww.j  a  v  a  2s.c o  m*/
    //
    ComponentInfo treeInfo = panel.getChildrenComponents().get(0);
    JTree treeObject = (JTree) treeInfo.getObject();
    // validate model
    {
        TreeModel model = treeObject.getModel();
        assertNotNull(model);
        DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) model.getRoot();
        {
            assertEquals("(root)", rootNode.getUserObject());
            assertEquals(2, rootNode.getChildCount());
        }
        {
            DefaultMutableTreeNode node_1 = (DefaultMutableTreeNode) rootNode.getChildAt(0);
            assertEquals("1", node_1.getUserObject());
            assertEquals(1, node_1.getChildCount());
            {
                DefaultMutableTreeNode node_2 = (DefaultMutableTreeNode) node_1.getChildAt(0);
                assertEquals("11", node_2.getUserObject());
                assertEquals(0, node_2.getChildCount());
            }
        }
        {
            DefaultMutableTreeNode node_1 = (DefaultMutableTreeNode) rootNode.getChildAt(1);
            assertEquals("2", node_1.getUserObject());
            assertEquals(1, node_1.getChildCount());
            {
                DefaultMutableTreeNode node_2 = (DefaultMutableTreeNode) node_1.getChildAt(0);
                assertEquals("21", node_2.getUserObject());
                assertEquals(0, node_2.getChildCount());
            }
        }
    }
    // check "model" property
    {
        Property modelProperty = treeInfo.getPropertyByTitle("model");
        PropertyEditor modelEditor = modelProperty.getEditor();
        // text
        {
            String text = (String) ReflectionUtils.invokeMethod2(modelEditor, "getText", Property.class,
                    modelProperty);
            assertEquals("(root), +1, ++11, +2, ++21", text);
        }
        // tooltip
        {
            String tooltip = getPropertyTooltipText(modelEditor, modelProperty);
            assertEquals(StringUtils.join(
                    new String[] { "(root)", "    1", "        11", "    2", "        21" }, "\n"), tooltip);
            // position
            PropertyTooltipProvider provider = modelEditor.getAdapter(PropertyTooltipProvider.class);
            assertSame(PropertyTooltipProvider.BELOW, provider.getTooltipPosition());
        }
    }
}

From source file:org.kuali.test.ui.components.sqlquerypanel.DatabasePanel.java

private void loadSelectedDbObjects(DefaultMutableTreeNode node, List<TableData> selectedDbObjects) {
    if (node.getUserObject() instanceof TableData) {
        TableData td = (TableData) node.getUserObject();

        if (hasSelectedColumns(td)) {
            selectedDbObjects.add(td);/*ww  w. j  a  v  a  2s  . com*/
        }
    }

    for (int i = 0; i < node.getChildCount(); ++i) {
        loadSelectedDbObjects((DefaultMutableTreeNode) node.getChildAt(i), selectedDbObjects);
    }
}

From source file:org.languagetool.gui.ConfigurationDialog.java

@NotNull
private DefaultTreeModel getTreeModel(DefaultMutableTreeNode rootNode) {
    DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    treeModel.addTreeModelListener(new TreeModelListener() {
        @Override/*w ww. j  a  v  a 2 s . c  om*/
        public void treeNodesChanged(TreeModelEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getTreePath().getLastPathComponent();
            int index = e.getChildIndices()[0];
            node = (DefaultMutableTreeNode) node.getChildAt(index);
            if (node instanceof RuleNode) {
                RuleNode o = (RuleNode) node;
                if (o.getRule().isDefaultOff() || o.getRule().getCategory().isDefaultOff()) {
                    if (o.isEnabled()) {
                        config.getEnabledRuleIds().add(o.getRule().getId());
                        config.getDisabledRuleIds().remove(o.getRule().getId());
                    } else {
                        config.getEnabledRuleIds().remove(o.getRule().getId());
                        config.getDisabledRuleIds().add(o.getRule().getId());
                    }
                } else {
                    if (o.isEnabled()) {
                        config.getDisabledRuleIds().remove(o.getRule().getId());
                    } else {
                        config.getDisabledRuleIds().add(o.getRule().getId());
                    }
                }
            }
            if (node instanceof CategoryNode) {
                CategoryNode o = (CategoryNode) node;
                if (o.getCategory().isDefaultOff()) {
                    if (o.isEnabled()) {
                        config.getDisabledCategoryNames().remove(o.getCategory().getName());
                        config.getEnabledCategoryNames().add(o.getCategory().getName());
                    } else {
                        config.getDisabledCategoryNames().add(o.getCategory().getName());
                        config.getEnabledCategoryNames().remove(o.getCategory().getName());
                    }
                } else {
                    if (o.isEnabled()) {
                        config.getDisabledCategoryNames().remove(o.getCategory().getName());
                    } else {
                        config.getDisabledCategoryNames().add(o.getCategory().getName());
                    }
                }
            }
        }

        @Override
        public void treeNodesInserted(TreeModelEvent e) {
        }

        @Override
        public void treeNodesRemoved(TreeModelEvent e) {
        }

        @Override
        public void treeStructureChanged(TreeModelEvent e) {
        }
    });
    return treeModel;
}

From source file:org.objectstyle.cayenne.modeler.ProjectTreeView.java

public void dataNodeChanged(DataNodeEvent e) {

    DefaultMutableTreeNode node = getProjectModel()
            .getNodeForObjectPath(new Object[] { mediator.getCurrentDataDomain(), e.getDataNode() });

    if (node != null) {

        if (e.isNameChange()) {
            positionNode((DefaultMutableTreeNode) node.getParent(), node,
                    Comparators.getDataDomainChildrenComparator());
            showNode(node);//from  w  w w.  j a va 2  s.c o m
        } else {

            getProjectModel().nodeChanged(node);

            // check for DataMap additions/removals...

            Object[] maps = e.getDataNode().getDataMaps().toArray();
            int mapCount = maps.length;

            // DataMap was linked
            if (mapCount > node.getChildCount()) {

                for (int i = 0; i < mapCount; i++) {
                    boolean found = false;
                    for (int j = 0; j < node.getChildCount(); j++) {
                        DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(j);
                        if (maps[i] == child.getUserObject()) {
                            found = true;
                            break;
                        }
                    }

                    if (!found) {
                        DefaultMutableTreeNode newMapNode = new DefaultMutableTreeNode(maps[i], false);
                        positionNode(node, newMapNode, Comparators.getNamedObjectComparator());
                        break;
                    }
                }
            }
            // DataMap was unlinked
            else if (mapCount < node.getChildCount()) {
                for (int j = 0; j < node.getChildCount(); j++) {
                    boolean found = false;
                    DefaultMutableTreeNode child;
                    child = (DefaultMutableTreeNode) node.getChildAt(j);
                    Object obj = child.getUserObject();
                    for (int i = 0; i < mapCount; i++) {
                        if (maps[i] == obj) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        removeNode(child);
                        break;
                    }
                }
            }
        }
    }
}