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:EditorPaneExample15.java

public EditorPaneExample15() {
    super("JEditorPane Example 15");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;/*www . j av a  2 s .  c om*/
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);
    c.gridy = 3;
    panel.add(new JLabel(LOAD_TIME), c);

    c.gridy = 4;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    onlineLoad = new JCheckBox("Online Load");
    panel.add(onlineLoad, c);
    onlineLoad.setSelected(true);
    onlineLoad.setForeground(typeLabel.getForeground());

    c.gridx = 1;
    c.gridy = 0;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;

    urlCombo = new JComboBox();
    panel.add(urlCombo, c);
    urlCombo.setEditable(true);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);
    timeLabel = new JLabel("");
    c.gridy = 3;
    panel.add(timeLabel, c);

    getContentPane().add(panel, "South");

    // Allocate the empty tree model
    DefaultMutableTreeNode emptyRootNode = new DefaultMutableTreeNode("Empty");
    emptyModel = new DefaultTreeModel(emptyRootNode);

    // Create and place the heading tree
    tree = new JTree(emptyModel);
    tree.setPreferredSize(new Dimension(200, 200));
    getContentPane().add(new JScrollPane(tree), "East");

    // Change page based on combo selection
    urlCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (populatingCombo == true) {
                return;
            }
            Object selection = urlCombo.getSelectedItem();
            loadNewPage(selection);
        }
    });

    // Listen for page load to complete
    pane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("page")) {
                loadComplete();
                displayLoadTime();
                populateCombo(findLinks(pane.getDocument(), null));
                TreeNode node = buildHeadingTree(pane.getDocument());
                tree.setModel(new DefaultTreeModel(node));
                enableInput();
                loadingPage = false;
            }
        }
    });

    // Listener for tree selection
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent evt) {
            TreePath path = evt.getNewLeadSelectionPath();
            if (path != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                Object userObject = node.getUserObject();
                if (userObject instanceof Heading) {
                    Heading heading = (Heading) userObject;
                    try {
                        Rectangle textRect = pane.modelToView(heading.getOffset());
                        textRect.y += 3 * textRect.height;
                        pane.scrollRectToVisible(textRect);
                    } catch (BadLocationException e) {
                    }
                }
            }
        }
    });

    // Listener for hypertext events
    pane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent evt) {
            // Ignore hyperlink events if the frame is busy
            if (loadingPage == true) {
                return;
            }
            if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane sp = (JEditorPane) evt.getSource();
                if (evt instanceof HTMLFrameHyperlinkEvent) {
                    HTMLDocument doc = (HTMLDocument) sp.getDocument();
                    doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) evt);
                } else {
                    loadNewPage(evt.getURL());
                }
            } else if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                pane.setCursor(handCursor);
            } else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
                pane.setCursor(defaultCursor);
            }
        }
    });
}

From source file:Main.java

public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) {
    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
    if (parent == null) {
        parent = rootNode;/*  w  w  w .j  a v  a2  s . com*/
    }
    treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
    if (shouldBeVisible) {
        tree.scrollPathToVisible(new TreePath(childNode.getPath()));
    }
    return childNode;
}

From source file:com.mindcognition.mindraider.ui.swing.explorer.LabelsTree.java

private DefaultMutableTreeNode addNodeToTree(String label, String labelUri) {
    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(
            new NotebookNodeUserObject(label, 0, labelUri));
    labelsTreeModel.insertNodeInto(childNode, labelsRootNode, labelsRootNode.getChildCount());
    return childNode;
}

From source file:EditorPaneExample16.java

public EditorPaneExample16() {
    super("JEditorPane Example 16");

    pane = new JEditorPane();
    pane.setEditable(true); // Editable
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;//from   w w w . ja  va2s  .  c  o  m
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);
    c.gridy = 3;
    panel.add(new JLabel(LOAD_TIME), c);

    c.gridy = 4;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    onlineLoad = new JCheckBox("Online Load");
    panel.add(onlineLoad, c);
    onlineLoad.setSelected(true);
    onlineLoad.setForeground(typeLabel.getForeground());

    c.gridy = 5;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    editableBox = new JCheckBox("Editable JEditorPane");
    panel.add(editableBox, c);
    editableBox.setSelected(true);
    editableBox.setForeground(typeLabel.getForeground());

    c.gridx = 1;
    c.gridy = 0;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;

    urlCombo = new JComboBox();
    panel.add(urlCombo, c);
    urlCombo.setEditable(true);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);
    timeLabel = new JLabel("");
    c.gridy = 3;
    panel.add(timeLabel, c);

    getContentPane().add(panel, "South");

    // Allocate the empty tree model
    DefaultMutableTreeNode emptyRootNode = new DefaultMutableTreeNode("Empty");
    emptyModel = new DefaultTreeModel(emptyRootNode);

    // Create and place the heading tree
    tree = new JTree(emptyModel);
    tree.setPreferredSize(new Dimension(200, 200));
    getContentPane().add(new JScrollPane(tree), "East");

    // Change page based on combo selection
    urlCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (populatingCombo == true) {
                return;
            }
            Object selection = urlCombo.getSelectedItem();
            loadNewPage(selection);
        }
    });

    // Change editability based on the checkbox
    editableBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            pane.setEditable(editableBox.isSelected());
            pane.revalidate();
            pane.repaint();
        }
    });

    // Listen for page load to complete
    pane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("page")) {
                loadComplete();
                displayLoadTime();
                populateCombo(findLinks(pane.getDocument(), null));
                TreeNode node = buildHeadingTree(pane.getDocument());
                tree.setModel(new DefaultTreeModel(node));
                enableInput();
                loadingPage = false;
            }
        }
    });

    // Listener for tree selection
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent evt) {
            TreePath path = evt.getNewLeadSelectionPath();
            if (path != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                Object userObject = node.getUserObject();
                if (userObject instanceof Heading) {
                    Heading heading = (Heading) userObject;
                    try {
                        Rectangle textRect = pane.modelToView(heading.getOffset());
                        textRect.y += 3 * textRect.height;
                        pane.scrollRectToVisible(textRect);
                    } catch (BadLocationException e) {
                    }
                }
            }
        }
    });

    // Listener for hypertext events
    pane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent evt) {
            // Ignore hyperlink events if the frame is busy
            if (loadingPage == true) {
                return;
            }
            if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane sp = (JEditorPane) evt.getSource();
                if (evt instanceof HTMLFrameHyperlinkEvent) {
                    HTMLDocument doc = (HTMLDocument) sp.getDocument();
                    doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) evt);
                } else {
                    loadNewPage(evt.getURL());
                }
            } else if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                pane.setCursor(handCursor);
            } else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
                pane.setCursor(defaultCursor);
            }
        }
    });
}

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

/**
 * Creates the tree node and its subnods
 * /* w ww .  j a v a  2  s.c om*/
 * @return A populated tree node.
 */
private MutableTreeNode createChartsNode(String name) {
    //System.out.println("adding-"+name+"-");

    BufferedReader rder = startReaderBuffer();

    DefaultMutableTreeNode node_root = new DefaultMutableTreeNode(name);
    String line, className, chartName;
    StringBuffer sb = new StringBuffer();

    while ((line = readLine(rder)) != null) {
        if (line.toLowerCase().equalsIgnoreCase("[" + name + "]")) {
            while ((line = readLine(rder)) != null) {

                if (line.toLowerCase().startsWith("item")) {
                    line = line.substring(line.indexOf("=") + 1);
                    StringTokenizer tk = new StringTokenizer(line, "=,; ");

                    chartName = tk.nextToken().trim();
                    className = tk.nextToken().trim();
                    //   System.out.println("className =["+className+"]");
                    DefaultMutableTreeNode n = new DefaultMutableTreeNode(
                            new DemoDescription(className, chartName));
                    node_root.add(n);
                } //item

                else if (line.toLowerCase().startsWith("subcategory")) {
                    line = line.substring(line.indexOf("=") + 1);
                    try {
                        rder.mark(100);
                        node_root.add(createChartsNode(line.trim()));
                        rder.reset();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } //subCategory

                else if (line.toLowerCase().startsWith("[")) {
                    //System.out.println("end of "+name);
                    return node_root;
                }

            }
        }

    }
    return node_root;
}

From source file:uk.co.markfrimston.tasktree.TaskTree.java

public DefaultMutableTreeNode addTask(DefaultMutableTreeNode parent, int childPos, String name) {
    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(name);
    treeModel.insertNodeInto(newNode, parent, childPos);
    return newNode;
}

From source file:com.mindcognition.mindraider.ui.swing.explorer.NotebooksTree.java

private DefaultMutableTreeNode addNodeToTree(String notebookLabel, String notebookUri) {
    if (notebookUris.contains(notebookUri)) {
        return null;
    } else {//from  www  .j  a  v  a  2  s  .com
        notebookUris.add(notebookUri);
        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(
                new NotebookNodeUserObject(notebookLabel, 0, notebookUri));
        notebooksTreeModel.insertNodeInto(childNode, notebooksRootNode, notebooksRootNode.getChildCount());
        return childNode;
    }
}

From source file:com.imaginea.betterdocs.BetterDocsAction.java

private static MutableTreeNode getNodes(String projectName, Iterable<CodeInfo> codeInfoCollection) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(projectName);
    Collection<String> fileNameSet = new HashSet<String>();
    for (CodeInfo codeInfo : codeInfoCollection) {
        if (!fileNameSet.contains(codeInfo.getFileName())) {
            node.add(new DefaultMutableTreeNode(codeInfo));
            fileNameSet.add(codeInfo.getFileName());
        }/*from   w ww.j  av  a 2s  .  com*/
    }
    return node;
}

From source file:EditorPaneExample14.java

public EditorPaneExample14() {
    super("JEditorPane Example 14");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;//from ww  w .  j a va2s .com
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);
    c.gridy = 3;
    panel.add(new JLabel(LOAD_TIME), c);

    c.gridy = 4;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    onlineLoad = new JCheckBox("Online Load");
    panel.add(onlineLoad, c);
    onlineLoad.setSelected(true);
    onlineLoad.setForeground(typeLabel.getForeground());

    c.gridx = 1;
    c.gridy = 0;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;

    urlCombo = new JComboBox();
    panel.add(urlCombo, c);
    urlCombo.setEditable(true);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);
    timeLabel = new JLabel("");
    c.gridy = 3;
    panel.add(timeLabel, c);

    getContentPane().add(panel, "South");

    // Modify the default style sheet
    InputStream is = EditorPaneExample14.class.getResourceAsStream("changedDefault.css");
    if (is != null) {
        try {
            addToStyleSheet(editorKit.getStyleSheet(), is);
        } catch (IOException e) {
            System.out.println("Failed to modify default style sheet");
        }
    }

    // Allocate the empty tree model
    DefaultMutableTreeNode emptyRootNode = new DefaultMutableTreeNode("Empty");
    emptyModel = new DefaultTreeModel(emptyRootNode);

    // Create and place the heading tree
    tree = new JTree(emptyModel);
    tree.setPreferredSize(new Dimension(200, 200));
    getContentPane().add(new JScrollPane(tree), "East");

    // Change page based on combo selection
    urlCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (populatingCombo == true) {
                return;
            }
            Object selection = urlCombo.getSelectedItem();
            loadNewPage(selection);
        }
    });

    // Listen for page load to complete
    pane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("page")) {
                loadComplete();
                displayLoadTime();
                populateCombo(findLinks(pane.getDocument(), null));
                TreeNode node = buildHeadingTree(pane.getDocument());
                tree.setModel(new DefaultTreeModel(node));
                enableInput();
                loadingPage = false;
            }
        }
    });

    // Listener for tree selection
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent evt) {
            TreePath path = evt.getNewLeadSelectionPath();
            if (path != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                Object userObject = node.getUserObject();
                if (userObject instanceof Heading) {
                    Heading heading = (Heading) userObject;
                    try {
                        Rectangle textRect = pane.modelToView(heading.getOffset());
                        textRect.y += 3 * textRect.height;
                        pane.scrollRectToVisible(textRect);
                    } catch (BadLocationException e) {
                    }
                }
            }
        }
    });

    // Listener for hypertext events
    pane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent evt) {
            // Ignore hyperlink events if the frame is busy
            if (loadingPage == true) {
                return;
            }
            if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane sp = (JEditorPane) evt.getSource();
                if (evt instanceof HTMLFrameHyperlinkEvent) {
                    HTMLDocument doc = (HTMLDocument) sp.getDocument();
                    doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) evt);
                } else {
                    loadNewPage(evt.getURL());
                }
            } else if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                pane.setCursor(handCursor);
            } else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
                pane.setCursor(defaultCursor);
            }
        }
    });
}

From source file:com.pironet.tda.AbstractDumpParser.java

protected void diffDumps(String prefix, DefaultMutableTreeNode root, Map dumpStore, TreePath[] dumps,
        int minOccurence, String regex) {
    List<String> keys = new Vector<>(dumps.length);

    for (final TreePath dump : dumps) {
        String dumpName = getDumpStringFromTreePath(dump);
        if (dumpName.indexOf(" at") > 0) {
            dumpName = dumpName.substring(0, dumpName.indexOf(" at"));
        } else if (dumpName.indexOf(" around") > 0) {
            dumpName = dumpName.substring(0, dumpName.indexOf(" around"));
        }//w  ww .  j a  v  a2 s  . c o  m
        keys.add(dumpName);
    }

    String info = prefix + " between " + keys.get(0) + " and " + keys.get(keys.size() - 1);
    DefaultMutableTreeNode catMerge = new DefaultMutableTreeNode(
            new TableCategory(info, IconFactory.DIFF_DUMPS));
    root.add(catMerge);
    int threadCount = 0;

    if (dumpStore.get(keys.get(0)) != null) {

        for (final Object o : ((Map) dumpStore.get(keys.get(0))).keySet()) {
            String threadKey = ((String) o).trim();

            if (Strings.isNullOrEmpty(regex) || threadKey.matches(regex)) {
                int occurence = 0;
                for (int i = 1; i < dumps.length; i++) {
                    Map threads = (Map) dumpStore.get(keys.get(i));
                    if (threads.containsKey(threadKey)) {
                        occurence++;
                    }
                }

                if (occurence >= (minOccurence - 1)) {
                    threadCount++;
                    StringBuilder content = new StringBuilder("<body bgcolor=\"ffffff\"><b><font size=")
                            .append(TDA.getFontSizeModifier(-1)).append('>').append(keys.get(0))
                            .append("</b></font><hr><pre><font size=").append(TDA.getFontSizeModifier(-1))
                            .append('>').append(fixMonitorLinks(
                                    (String) ((Map) dumpStore.get(keys.get(0))).get(threadKey), keys.get(0)));

                    int maxLines = 0;
                    for (int i = 1; i < dumps.length; i++) {
                        if (((Map) dumpStore.get(keys.get(i))).containsKey(threadKey)) {
                            content.append("\n\n</pre><b><font size=");
                            content.append(TDA.getFontSizeModifier(-1));
                            content.append('>');
                            content.append(keys.get(i));
                            content.append("</font></b><hr><pre><font size=");
                            content.append(TDA.getFontSizeModifier(-1));
                            content.append('>');
                            content.append(fixMonitorLinks(
                                    (String) ((Map) dumpStore.get(keys.get(i))).get(threadKey), keys.get(i)));
                            int countLines = countLines(
                                    ((String) ((Map) dumpStore.get(keys.get(i))).get(threadKey)));
                            maxLines = maxLines > countLines ? maxLines : countLines;
                        }
                    }
                    addToCategory(catMerge, threadKey, null, content.toString(), maxLines, true);
                }
            }
        }
    }

    ((Category) catMerge.getUserObject()).setInfo(getStatInfo(keys, prefix, minOccurence, threadCount));

}