Example usage for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode

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

Introduction

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

Prototype

public DefaultMutableTreeNode(Object userObject) 

Source Link

Document

Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.

Usage

From source file:Main.java

private void construct(MyObject obj1, DefaultMutableTreeNode root) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(obj1);
    root.add(node);/*ww  w .  java2s.  c  om*/
    for (MyObject o : obj1.childs) {
        construct(o, node);
    }
}

From source file:edu.ucla.stat.SOCR.chart.ChartTree.java

private TreeModel createTreeModel() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("SOCRCharts");
    root.add(createPieChartsNode());/*  w  w  w  . ja va  2s . c  o  m*/
    root.add(createBarChartsNode());
    root.add(createLineChartsNode());
    root.add(createAreaChartsNode());
    //  root.add(createTimeSeriesChartsNode());
    //  root.add(createFinancialChartsNode());
    //  root.add(createXYChartsNode());
    // root.add(createMeterChartsNode());
    // root.add(createMultipleAxisChartsNode());
    //  root.add(createCombinedAxisChartsNode());
    //  root.add(createGanttChartsNode());
    root.add(createMiscellaneousChartsNode());
    return new DefaultTreeModel(root);
}

From source file:Main.java

public void actionPerformed(ActionEvent ae) {
    DefaultMutableTreeNode dmtn, node;

    TreePath path = this.getSelectionPath();
    dmtn = (DefaultMutableTreeNode) path.getLastPathComponent();
    if (ae.getActionCommand().equals("insert")) {
        node = new DefaultMutableTreeNode("children");
        dmtn.add(node);//  w w  w  .j  a  va  2s .  c  o  m
        ((DefaultTreeModel) this.getModel()).nodeStructureChanged((TreeNode) dmtn);
    }
    if (ae.getActionCommand().equals("remove")) {
        node = (DefaultMutableTreeNode) dmtn.getParent();
        int nodeIndex = node.getIndex(dmtn);
        dmtn.removeAllChildren();
        node.remove(nodeIndex);
        ((DefaultTreeModel) this.getModel()).nodeStructureChanged((TreeNode) dmtn);
    }
}

From source file:ClassTree.java

public ClassTreeFrame() {
    setTitle("ClassTree");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // the root of the class tree is Object
    root = new DefaultMutableTreeNode(java.lang.Object.class);
    model = new DefaultTreeModel(root);
    tree = new JTree(model);

    // add this class to populate the tree with some data
    addClass(getClass());/*from  w  w w  .j ava 2  s  . co m*/

    // set up node icons
    ClassNameTreeCellRenderer renderer = new ClassNameTreeCellRenderer();
    renderer.setClosedIcon(new ImageIcon("red-ball.gif"));
    renderer.setOpenIcon(new ImageIcon("yellow-ball.gif"));
    renderer.setLeafIcon(new ImageIcon("blue-ball.gif"));
    tree.setCellRenderer(renderer);

    // set up selection mode
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent event) {
            // the user selected a different node--update description
            TreePath path = tree.getSelectionPath();
            if (path == null)
                return;
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            Class<?> c = (Class<?>) selectedNode.getUserObject();
            String description = getFieldDescription(c);
            textArea.setText(description);
        }
    });
    int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
    tree.getSelectionModel().setSelectionMode(mode);

    // this text area holds the class description
    textArea = new JTextArea();

    // add tree and text area
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 2));
    panel.add(new JScrollPane(tree));
    panel.add(new JScrollPane(textArea));

    add(panel, BorderLayout.CENTER);

    addTextField();
}

From source file:org.spf4j.ui.TSDBViewJInternalFrame.java

/**
 * Creates new form TSDBViewJInternalFrame
 *//*from w w w  . j  a  v a  2 s .  c o  m*/
public TSDBViewJInternalFrame(final String databaseFile) throws IOException {
    super(databaseFile);
    initComponents();
    tsDb = new TimeSeriesDatabase(databaseFile, null);
    Collection<TSTable> columnsInfo = tsDb.getTSTables();
    Map<String, DefaultMutableTreeNode> gNodes = new HashMap<>();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(databaseFile);
    long startDate = System.currentTimeMillis();
    for (TSTable info : columnsInfo) {
        String groupName = info.getTableName();
        long tableStart = tsDb.readStartDate(groupName);
        if (tableStart < startDate) {
            startDate = tableStart;
        }
        Pair<String, String> pair = Pair.from(groupName);
        if (pair == null) {
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(groupName);
            for (String colName : info.getColumnNames()) {
                child.add(new DefaultMutableTreeNode(colName));
            }
            root.add(child);
        } else {
            groupName = pair.getFirst();
            DefaultMutableTreeNode gNode = gNodes.get(groupName);
            if (gNode == null) {
                gNode = new DefaultMutableTreeNode(groupName);
                gNodes.put(groupName, gNode);
                root.add(gNode);
            }
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(pair.getSecond());
            for (String colName : info.getColumnNames()) {
                child.add(new DefaultMutableTreeNode(colName));
            }
            gNode.add(child);
        }
    }
    measurementTree.setModel(new DefaultTreeModel(root));
    measurementTree.setVisible(true);
    this.startDate.setValue(new DateTime().toDate());
}

From source file:gov.nih.nci.ncicb.cadsr.contexttree.CDEBrowserTree.java

public DefaultMutableTreeNode buildTree(Hashtable treeParams) throws Exception {

    DefaultMutableTreeNode tree = null;

    BaseTreeNode baseNode = null;//from w ww .ja v  a  2s  . c  om

    //TimeUtils.recordStartTime("Tree");
    try {
        log.info("Tree Start " + TimeUtils.getEasternTime());

        baseNode = new BaseTreeNode(treeParams);
        CDEBrowserTreeCache cache = CDEBrowserTreeCache.getAnInstance();
        cache.init(baseNode, treeParams);
        WebNode contexts = new WebNode(cache.getIdGen().getNewId(), "caDSR Contexts",
                "javascript:" + baseNode.getJsFunctionName() + "('P_PARAM_TYPE=P_PARAM_TYPE&P_IDSEQ=P_IDSEQ&"
                        + baseNode.getExtraURLParameters() + "')");
        tree = new DefaultMutableTreeNode(contexts);
        List allContexts = cache.getAllContextHolders();

        if (allContexts == null)
            return tree;

        ListIterator contextIt = allContexts.listIterator();

        while (contextIt.hasNext()) {
            ContextHolder currContextHolder = (ContextHolder) contextIt.next();

            Context currContext = currContextHolder.getContext();
            DefaultMutableTreeNode contextNode = currContextHolder.getNode();

            //Adding data template nodes

            DefaultMutableTreeNode tmpLabelNode;
            DefaultMutableTreeNode otherTempNodes;

            if (Context.CTEP.equals(currContext.getName())) {

                cache.initCtepInfo(baseNode, currContext);

                tmpLabelNode = new DefaultMutableTreeNode(
                        new WebNode(cache.getIdGen().getNewId(), "Protocol Form Templates"));
                List ctepNodes = cache.getAllTemplatesForCtep();
                tmpLabelNode.add((DefaultMutableTreeNode) ctepNodes.get(0));
                tmpLabelNode.add((DefaultMutableTreeNode) ctepNodes.get(1));
                contextNode.add(tmpLabelNode);

                log.info("CTEP Templates End " + TimeUtils.getEasternTime());
            } else {
                log.info("Other Templates Start " + TimeUtils.getEasternTime());

                otherTempNodes = cache.getTemplateNodes(currContext.getConteIdseq());

                if (otherTempNodes != null) {
                    contextNode.add(otherTempNodes);
                }

                log.info("Other Templates End " + TimeUtils.getEasternTime());
            }

            //Adding classification nodes
            long startingTime = System.currentTimeMillis();
            log.info("Classification Start " + TimeUtils.getEasternTime());
            DefaultMutableTreeNode csNode = cache.getClassificationNodes(currContext.getConteIdseq());

            if (csNode != null)
                contextNode.add(csNode);

            long timeElsp = System.currentTimeMillis() - startingTime;
            log.info("Classification Took " + timeElsp);
            //End Adding Classification Node

            //Adding protocols nodes
            //Filtering CTEP context in data element search tree
            log.info("Proto forms Start " + TimeUtils.getEasternTime());

            /** Remove to TT 1892
             if ((!currContext.getName().equals(Context.CTEP) && treeType.equals(TreeConstants.DE_SEARCH_TREE))
             //Publish Change order
               || (baseNode.isCTEPUser().equals("Yes") && treeType.equals(TreeConstants.DE_SEARCH_TREE))
               || (treeType.equals(TreeConstants.FORM_SEARCH_TREE))) {
              if ((currContext.getName().equals(
                      Context.CTEP) && baseNode.isCTEPUser().equals("Yes")) || (!currContext.getName().equals(Context.CTEP)))
                      {
                              
             **/
            List protoNodes = cache.getProtocolNodes(currContext.getConteIdseq());

            /** for release 3.0.1, forms without protocol is not displayed, uncomment this
             * code to display them
               DefaultMutableTreeNode noProtocolFormNode =
               cache.getProtocolFormNodeWithNoProtocol(currContext.getConteIdseq());
            */
            DefaultMutableTreeNode protocolFormsLabelNode = null;

            /** for release 3.0.1, forms without protocol is not displayed, uncomment this
             * code to display them
               if ((protoNodes != null && !protoNodes.isEmpty()) || noProtocolFormNode != null ) {
            */
            if ((protoNodes != null && !protoNodes.isEmpty())) {
                protocolFormsLabelNode = new DefaultMutableTreeNode(
                        new WebNode(cache.getIdGen().getNewId(), "Protocol Forms"));
                /** for release 3.0.1, forms without protocol is not displayed, uncomment this
                 * code to display them
                        
                    // Add form with no protocol
                    if (noProtocolFormNode != null ) {
                      protocolFormsLabelNode.add(noProtocolFormNode);
                    }
                */
                // Add form with protocol
                if (protoNodes != null && !protoNodes.isEmpty()) {
                    Iterator tmpIter = protoNodes.iterator();

                    while (tmpIter.hasNext()) {
                        protocolFormsLabelNode.add((DefaultMutableTreeNode) tmpIter.next());
                    }
                }

                contextNode.add(protocolFormsLabelNode);
            }
            /** } TT 1892 
            }**/

            log.info("Proto forms End " + TimeUtils.getEasternTime());
            //End Add Protocol Nodes

            //Display Catalog

            //Get Publishing Node info
            log.info("Publish strat " + TimeUtils.getEasternTime());
            DefaultMutableTreeNode publishNode = cache.getPublishNode(currContext);

            if (publishNode != null)
                contextNode.add(publishNode);

            log.info("Publish end " + TimeUtils.getEasternTime());
            //End Catalog

            tree.add(contextNode);
        }

        log.info("Tree End " + TimeUtils.getEasternTime());
    } catch (Exception ex) {
        ex.printStackTrace();

        throw ex;
    }

    return tree;
}

From source file:Main.java

private static DefaultTreeModel getTreeModel() {
    MutableTreeNode root = new DefaultMutableTreeNode("1");

    DefaultMutableTreeNode cover = new DefaultMutableTreeNode("A");
    cover.insert(new DefaultMutableTreeNode("P"), 0);
    cover.insert(new DefaultMutableTreeNode("S"), 0);
    root.insert(cover, 0);//from w  w  w .  j  av a2 s. com

    DefaultMutableTreeNode base = new DefaultMutableTreeNode("B");
    base.insert(new DefaultMutableTreeNode("S"), 0);
    base.insert(new DefaultMutableTreeNode("H"), 0);
    root.insert(base, 0);

    DefaultTreeModel model = new DefaultTreeModel(root);
    return model;
}

From source file:TreeUtil.java

public void addNode(String name, Object obj) {
    itsMap.put(name, obj);/*ww  w . ja v a2s.  c  o  m*/
    DefaultMutableTreeNode tempNode = itsRootNode;
    StringTokenizer tok = new StringTokenizer(name, ".");
    String currentName = null;
    while (tok.hasMoreTokens()) {
        String myTok = tok.nextToken();
        currentName = (currentName == null) ? myTok : currentName + "." + myTok;
        boolean createNew = true;
        for (int j = 0; j < tempNode.getChildCount(); j++) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) tempNode.getChildAt(j);
            if (childNode.toString().equals(myTok)) {
                tempNode = childNode;
                createNew = false;
                break;
            }
        }
        if (createNew) {
            DefaultMutableTreeNode aNode = new DefaultMutableTreeNode(myTok);
            itsTreeMap.put(currentName, aNode);
            // Let's give some consideration to where in the tree we place
            // the new node.
            // We want any nodes with children to be listed first, in
            // alphabetical order.
            // Then come nodes with no children, in alphabetical order.
            if (tok.hasMoreTokens()) {
                // This node is not a leaf node
                int targeti;
                for (targeti = 0; targeti < tempNode.getChildCount(); targeti++) {
                    TreeNode bNode = tempNode.getChildAt(targeti);
                    if (bNode.isLeaf() || bNode.toString().compareToIgnoreCase(myTok) > 0) {
                        break;
                    }
                }
                tempNode.insert(aNode, targeti);
            } else {
                // This node is a leaf node
                int targeti;
                for (targeti = 0; targeti < tempNode.getChildCount(); targeti++) {
                    TreeNode bNode = tempNode.getChildAt(targeti);
                    if (bNode.isLeaf() && bNode.toString().compareToIgnoreCase(myTok) > 0) {
                        break;
                    }
                }
                tempNode.insert(aNode, targeti);
            }
            tempNode = aNode;
        }
    }
}

From source file:OAT.ui.util.UiUtil.java

public static DefaultMutableTreeNode addChildNode(DefaultMutableTreeNode parent, Object child) {
    HashSet hashSet = new HashSet();
    DefaultMutableTreeNode childNode;

    for (int i = 0; i < parent.getChildCount(); i++) {
        hashSet.add(parent.getChildAt(i).toString());
    }/*from   ww  w .  j a  v  a  2 s .  co  m*/

    if (hashSet.add(child.toString())) {
        childNode = new DefaultMutableTreeNode(child);
        parent.insert(childNode, parent.getChildCount());
    } else {
        int i = 0;
        do {
            childNode = (DefaultMutableTreeNode) parent.getChildAt(i++);
            //System.out.println("childNode: " + childNode);
        } while (!childNode.toString().equals(child.toString()));
    }

    return childNode;
}

From source file:StAXStreamTreeViewer.java

private void parseRestOfDocument(XMLStreamReader reader, DefaultMutableTreeNode current)
        throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:

            DefaultMutableTreeNode element = new DefaultMutableTreeNode(reader.getLocalName());
            current.add(element);/* w  ww  . ja v a 2s .  c om*/
            current = element;

            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }
                DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                        "prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
                current.add(namespace);
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    DefaultMutableTreeNode attribute = new DefaultMutableTreeNode(
                            "Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                                    + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode(
                                "prefix=" + attPrefix + ",URI=" + attURI);
                        attribute.add(attNamespace);
                    }
                    current.add(attribute);
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            current = (DefaultMutableTreeNode) current.getParent();
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                DefaultMutableTreeNode data = new DefaultMutableTreeNode("CD:" + reader.getText());
                current.add(data);
            }
            break;
        case XMLStreamConstants.DTD:
            DefaultMutableTreeNode dtd = new DefaultMutableTreeNode("DTD:" + reader.getText());
            current.add(dtd);
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            DefaultMutableTreeNode comment = new DefaultMutableTreeNode(reader.getText());
            current.add(comment);
            break;
        default:
            System.out.println(type);
        }
    }
}