Example usage for javax.swing.tree TreePath getLastPathComponent

List of usage examples for javax.swing.tree TreePath getLastPathComponent

Introduction

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

Prototype

public Object getLastPathComponent() 

Source Link

Document

Returns the last element of this path.

Usage

From source file:Main.java

static private void expandAll(JTree tree, TreePath parent, boolean expand) {
    // Traverse children
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            TreeNode n = (TreeNode) e.nextElement();
            TreePath path = parent.pathByAddingChild(n);
            expandAll(tree, path, expand);
        }//from w ww .  j  a v a2  s .  c om
    }
    // Expansion or collapse must be done bottom-up
    if (expand) {
        tree.expandPath(parent);
    } else {
        tree.collapsePath(parent);
    }
}

From source file:TreeUtils.java

public static void expandAll(JTree tree, TreePath parent, boolean expand) {
    // Traverse children
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            TreeNode n = (TreeNode) e.nextElement();
            TreePath path = parent.pathByAddingChild(n);
            expandAll(tree, path, expand);
        }/*from   w  w w .  j a  va 2s  .c  om*/
    }

    // Expansion or collapse must be done bottom-up
    if (expand) {
        tree.expandPath(parent);
    } else {
        tree.collapsePath(parent);
    }
}

From source file:Main.java

/** 
 * Expand (or collapse) the children nodes of specified node of a JTree for a certain depth.
 * @param tree Tree of which we want to expand the nodes.
 * @param parent Parent node./*from  w  w  w  . ja  v a  2s.c o  m*/
 * @param expand <code>true</code> to expand the nodes.  <code>false</code> to collapse the nodes. 
 * @param depth Depth of children to expand or collapse.
 */
public static void expandNodes(JTree tree, TreePath parent, boolean expand, int depth) {
    if (parent == null || depth == 0)
        return;

    Object node = parent.getLastPathComponent();
    if (node != null) {
        for (int i = 0; i < tree.getModel().getChildCount(node); i++) {
            Object child = tree.getModel().getChild(node, i);
            TreePath path = parent.pathByAddingChild(child);
            expandNodes(tree, path, expand, depth - 1);
        }
    }

    if (expand)
        tree.expandPath(parent);
    else
        tree.collapsePath(parent);
}

From source file:jshm.gui.GuiUtil.java

private static void expandTreeBelowNode(JXTreeTable tree, TreeTableModel model, TreePath path) {
    Object parent = path.getLastPathComponent();

    int childCount = model.getChildCount(parent);

    for (int i = 0; i < childCount; i++) {
        TreePath childPath = path.pathByAddingChild(model.getChild(parent, i));

        // using childCount is faster and plain better than isLeaf
        // since a non-leaf might have 0 children which would be
        // useless to expand
        if (0 < model.getChildCount(childPath.getLastPathComponent())) {
            tree.expandPath(childPath);//from  w  w  w .  j  av a  2 s.c o m
            expandTreeBelowNode(tree, model, childPath);
        }
    }
}

From source file:jshm.gui.GuiUtil.java

private static void getExpandedPaths(JXTreeTable tree, TreeTableModel model, TreePath path,
        List<TreePath> paths) {
    //      System.out.println("Checking: " + path);

    Object parent = path.getLastPathComponent();
    final int childCount = model.getChildCount(parent);

    for (int i = 0; i < childCount; i++) {
        Object curNode = model.getChild(parent, i);
        TreePath curPath = path.pathByAddingChild(curNode);

        if (model.isLeaf(curNode))
            continue;

        if (tree.isExpanded(curPath))
            paths.add(curPath);/*from w  ww. j a  v a2 s .c  om*/

        getExpandedPaths(tree, model, curPath, paths);
    }
}

From source file:Main.java

private void expandAll(JTree tree, TreePath parent) {
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            TreeNode n = (TreeNode) e.nextElement();
            TreePath path = parent.pathByAddingChild(n);
            expandAll(tree, path);// w  w w  . j av  a  2 s .co m
        }
    }
    tree.expandPath(parent);
    // tree.collapsePath(parent);
}

From source file:Main.java

protected void showMenu(int x, int y) {
    JPopupMenu popup = new JPopupMenu();
    JMenuItem mi = new JMenuItem("Delete");
    TreePath path = tree.getSelectionPath();
    Object node = path.getLastPathComponent();
    if (node == tree.getModel().getRoot()) {
        mi.setEnabled(false);/*  w ww  . j ava 2  s. c  o m*/
    }
    popup.add(mi);
    mi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            deleteSelectedItems();
        }
    });
    popup.show(tree, x, y);
}

From source file:com.peter.mavenrunner.MavenRunnerTopComponent.java

private static void expandAll(JTree tree, TreePath treePath, boolean expand, int maxLevel, int currentLevel) {
    if (maxLevel != -1 && currentLevel >= maxLevel - 1) {
        return;//w  w w  .  ja v a2 s . com
    }

    TreeNode node = (TreeNode) treePath.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration<TreeNode> e = node.children(); e.hasMoreElements();) {
            TreeNode n = e.nextElement();

            TreePath path = treePath.pathByAddingChild(n);
            expandAll(tree, path, expand, maxLevel, currentLevel + 1);
        }
    }

    // Expansion or collapse must be done bottom-up
    if (expand) {
        tree.expandPath(treePath);
    } else {
        tree.collapsePath(treePath);
    }
}

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);/*from w  w  w . j  a v  a  2  s .co 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:TreeIt.java

public TreeIt() {
    JFrame f = new JFrame();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar");
    DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months");
    root.add(months);/*from ww  w  .  ja  v  a2  s  .c  o  m*/
    String monthLabels[] = { "January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December" };
    for (int i = 0, n = monthLabels.length; i < n; i++)
        months.add(new DefaultMutableTreeNode(monthLabels[i]));
    DefaultMutableTreeNode weeks = new DefaultMutableTreeNode("Weeks");
    root.add(weeks);
    String weekLabels[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    for (int i = 0, n = weekLabels.length; i < n; i++)
        weeks.add(new DefaultMutableTreeNode(weekLabels[i]));
    JTree jt = new JTree(root);
    jt.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            TreePath path = e.getPath();
            System.out.println("Picked: " + path.getLastPathComponent());
            Object elements[] = path.getPath();
            for (int i = 0, n = elements.length; i < n; i++) {
                System.out.print("->" + elements[i]);
            }
            System.out.println();
        }
    });

    DefaultMutableTreeNode lastLeaf = root.getLastLeaf();
    TreePath path = new TreePath(lastLeaf.getPath());
    jt.setSelectionPath(path);

    jt.setCellRenderer(new MyCellRenderer());

    JScrollPane jsp = new JScrollPane(jt);
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.CENTER);
    f.setSize(250, 250);
    f.show();
}