Example usage for javax.swing.tree DefaultMutableTreeNode isLeaf

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

Introduction

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

Prototype

public boolean isLeaf() 

Source Link

Document

Returns true if this node has no children.

Usage

From source file:Main.java

/** Fully expands the given JTree from the specified node. */
public static void expandTree(final JTree tree, final DefaultMutableTreeNode node) {
    if (node.isLeaf())
        return;//from   w w  w  . j av  a 2 s .c  om
    tree.expandPath(new TreePath(node.getPath()));
    final Enumeration e = node.children();
    while (e.hasMoreElements()) {
        expandTree(tree, (DefaultMutableTreeNode) e.nextElement());
    }
}

From source file:Main.java

public static void expandTree(JTree tree) {
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
    @SuppressWarnings("unchecked")
    Enumeration<DefaultMutableTreeNode> e = root.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        if (node.isLeaf())
            continue;
        int row = tree.getRowForPath(new TreePath(node.getPath()));
        tree.expandRow(row);/*from  ww  w  . j  a v a  2s.  c o  m*/
    }
}

From source file:Main.java

/**
 * Method by Adrian: [/*from  ww  w. j a  v a2  s .  c om*/
 * <a href="http://stackoverflow.com/a/15704264/5620200">StackOverflow</a> ]
 * & Mike: [ <a href=
 * "http://stackoverflow.com/questions/1542170/arranging-nodes-in-a-jtree">
 * StackOverflow</a> ]
 * 
 * @param node
 * @return
 */
@SuppressWarnings("unchecked")
public static DefaultMutableTreeNode sort(DefaultMutableTreeNode node) {
    List<DefaultMutableTreeNode> children = Collections.list(node.children());
    List<String> orgCnames = new ArrayList<String>();
    List<String> cNames = new ArrayList<String>();
    DefaultMutableTreeNode temParent = new DefaultMutableTreeNode();
    for (DefaultMutableTreeNode child : children) {
        DefaultMutableTreeNode ch = (DefaultMutableTreeNode) child;
        temParent.insert(ch, 0);
        String uppser = ch.toString().toUpperCase();
        // Not dependent on package name, so if duplicates are found
        // they will later on be confused. Adding this is of
        // very little consequence and fixes the issue.
        if (cNames.contains(uppser)) {
            uppser += "$COPY";
        }
        cNames.add(uppser);
        orgCnames.add(uppser);
        if (!child.isLeaf()) {
            sort(child);
        }
    }
    Collections.sort(cNames);
    for (String name : cNames) {
        int indx = orgCnames.indexOf(name);
        int insertIndex = node.getChildCount();
        node.insert(children.get(indx), insertIndex);
    }
    // Fixing folder placement
    for (int i = 0; i < node.getChildCount() - 1; i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
        for (int j = i + 1; j <= node.getChildCount() - 1; j++) {
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) node.getChildAt(j);
            if (!prevNode.isLeaf() && child.isLeaf()) {
                node.insert(child, j);
                node.insert(prevNode, i);
            }
        }
    }
    return node;
}

From source file:Main.java

public static DefaultMutableTreeNode sortTreeNode(DefaultMutableTreeNode root) {
    if (root == null)
        return root;
    int count = root.getChildCount();
    if (count <= 0)
        return root;
    for (int i = 0; i < count; i++) {
        for (int j = count - 1; j > i; j--) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j);
            String nt = node.getUserObject().toString();
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j - 1);
            String np = prevNode.getUserObject().toString();
            if (nt.compareToIgnoreCase(np) < 0) {
                root.insert(node, j - 1);
                root.insert(prevNode, j);
            }//from   w  w  w. jav  a  2  s .c om
        }
        sortTreeNode((DefaultMutableTreeNode) root.getChildAt(i));
    }

    for (int i = 0; i < count; i++) {
        for (int j = count - 1; j > i; j--) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j);
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j - 1);
            if (prevNode.isLeaf() && !node.isLeaf()) {
                root.insert(node, j - 1);
                root.insert(prevNode, j);
            }
        }
    }

    return root;
}

From source file:UsingTreeSelectionListener.java

public void valueChanged(TreeSelectionEvent se) {
    JTree tree = (JTree) se.getSource();
    DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    String selectedNodeName = selectedNode.toString();
    if (selectedNode.isLeaf()) {

        System.out.println(selectedNodeName);

    }//from www .  j av  a2  s  .  c  o m
}

From source file:com.autentia.wuija.widget.TreeComponent.java

private void expand(DefaultMutableTreeNode node, boolean expand) {
    if (!node.isLeaf()) {
        ((IceUserObject) node.getUserObject()).setExpanded(expand);
        final Enumeration nodes = node.children();
        while (nodes.hasMoreElements()) {
            final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) nodes.nextElement();
            expand(childNode, expand);/*from   www  .  j  a v  a 2  s .  com*/
        }
    }
}

From source file:CheckBoxNodeTreeSample.java

public boolean isCellEditable(EventObject event) {
    boolean returnValue = false;
    if (event instanceof MouseEvent) {
        MouseEvent mouseEvent = (MouseEvent) event;
        TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
        if (path != null) {
            Object node = path.getLastPathComponent();
            if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
                DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
                Object userObject = treeNode.getUserObject();
                returnValue = ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
            }//w w  w .  jav a  2 s. com
        }
    }
    return returnValue;
}

From source file:TreeDragTest.java

public void drop(DropTargetDropEvent dtde) {
    Point pt = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) parentpath.getLastPathComponent();
    if (parent.isLeaf()) {
        dtde.rejectDrop();/*from w  w w . ja va  2s.c om*/
        return;
    }

    try {
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            if (tr.isDataFlavorSupported(flavors[i])) {
                dtde.acceptDrop(dtde.getDropAction());
                TreePath p = (TreePath) tr.getTransferData(flavors[i]);
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) p.getLastPathComponent();
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                model.insertNodeInto(node, parent, 0);
                dtde.dropComplete(true);
                return;
            }
        }
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}

From source file:TreeIconDemo2.java

/** Required by TreeSelectionListener interface. */
public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

    if (node == null)
        return;/*from  w ww. j av a  2  s  .c  o  m*/

    Object nodeInfo = node.getUserObject();
    if (node.isLeaf()) {
        BookInfo book = (BookInfo) nodeInfo;
        displayURL(book.bookURL);
        if (DEBUG) {
            System.out.print(book.bookURL + ":  \n    ");
        }
    } else {
        displayURL(helpURL);
    }
    if (DEBUG) {
        System.out.println(nodeInfo.toString());
    }
}

From source file:com.ssn.listener.SSNFacebookAlbumSelectionListener.java

@Override
public void valueChanged(TreeSelectionEvent event) {
    DefaultMutableTreeNode node = treeHelper.getTreeNode(event.getPath());

    if (this.form.getHiveTree() != null) {
        this.form.getHiveTree().clearSelection();
    }/* w  w  w .jav a2  s .  co m*/
    if (this.form.getInstagramTree() != null) {
        this.form.getInstagramTree().clearSelection();
    }

    if (node.isLeaf()) {
        this.form.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        SSNAlbumNode fnode = null;
        try {
            if (((SSNIconData) node.getUserObject()).getObject() instanceof SSNAlbumNode)
                fnode = (SSNAlbumNode) ((SSNIconData) node.getUserObject()).getObject();
        } catch (ClassCastException ee) {
            ee.printStackTrace();
            this.form.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
        String facebookDirPath = SSNHelper.getFacebookPhotosDirPath();
        if (fnode != null)
            fileTree.m_display.setText(facebookDirPath + File.separator + fnode);

        SSNIconData iconData = (SSNIconData) node.getUserObject();
        SSNAlbumNode albumNode = (SSNAlbumNode) iconData.getObject();

        MediaOperations mediaOperations = facebook.mediaOperations();

        List<Photo> listPhoto, completePhotoList = new ArrayList<Photo>();
        do {
            PagingParameters pagingParameters = new PagingParameters(100, completePhotoList.size(), null,
                    Calendar.getInstance().getTimeInMillis());
            listPhoto = mediaOperations.getPhotos(albumNode.getAlbum().getId(), pagingParameters);
            completePhotoList.addAll(listPhoto);

        } while (listPhoto.size() > 0);

        createComponents(this.form, completePhotoList, albumNode);
    }

}