Example usage for javax.swing.tree DefaultMutableTreeNode children

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

Introduction

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

Prototype

Vector children

To view the source code for javax.swing.tree DefaultMutableTreeNode children.

Click Source Link

Document

array of children, may be null if this node has no children

Usage

From source file:Main.java

/**
 * Method by Adrian: [//  www. ja  v a2  s . c  o m
 * <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

/** 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  ww  .  ja  v  a2s . co  m*/
    tree.expandPath(new TreePath(node.getPath()));
    final Enumeration e = node.children();
    while (e.hasMoreElements()) {
        expandTree(tree, (DefaultMutableTreeNode) e.nextElement());
    }
}

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

@edu.umd.cs.findbugs.annotations.SuppressWarnings("CLI_CONSTANT_LIST_INDEX")
private static List<String> getSelectedTables(@Nullable final TreePath[] selectionPaths) {
    if (selectionPaths == null) {
        return Collections.EMPTY_LIST;
    }/* w ww  .j  a  v  a 2  s. c o m*/
    List<String> result = new ArrayList<String>();
    for (TreePath path : selectionPaths) {
        Object[] pathArr = path.getPath();
        if (pathArr.length < 2) {
            continue;
        }
        DefaultMutableTreeNode colNode = (DefaultMutableTreeNode) pathArr[1];
        int depth = colNode.getDepth();
        String tableName;
        if (depth == 1) {
            result.add((String) colNode.getUserObject());
        } else {
            Enumeration childEnum = colNode.children();
            while (childEnum.hasMoreElements()) {
                DefaultMutableTreeNode child = (DefaultMutableTreeNode) childEnum.nextElement();
                tableName = Pair.of((String) colNode.getUserObject(), (String) child.getUserObject())
                        .toString();
                result.add(tableName);
            }
        }
    }
    return result;
}

From source file:Main.java

private int childIndex(final DefaultMutableTreeNode node, final String childValue) {
    Enumeration<DefaultMutableTreeNode> children = node.children();
    DefaultMutableTreeNode child = null;
    int index = -1;

    while (children.hasMoreElements() && index < 0) {
        child = children.nextElement();//  ww  w .jav a 2  s .c o m

        if (child.getUserObject() != null && childValue.equals(child.getUserObject())) {
            index = node.getIndex(child);
        }
    }
    return index;
}

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  w  w w.ja v a2s .  co m
        }
    }
}

From source file:fxts.stations.ui.help.ContentTree.java

/**
 * Fires expanding of top nodes./*  w  ww  . j  a v a 2 s. co  m*/
 */
private void expandTopNodes() {
    DefaultMutableTreeNode node;
    DefaultMutableTreeNode root;
    TreePath path;
    TreePath rootPath;
    root = (DefaultMutableTreeNode) mTree.getModel().getRoot();
    rootPath = new TreePath(root);
    for (Enumeration enumeration = root.children(); enumeration.hasMoreElements();) {
        node = (DefaultMutableTreeNode) enumeration.nextElement();

        //increase path
        path = rootPath.pathByAddingChild(node);
        //expands node
        mTree.expandPath(path);
    }
}

From source file:fxts.stations.ui.help.ContentTree.java

/**
 * Returns node containing the specified url.
 *
 * @param aParent parent node//from  www  .  j  a  va  2s . co m
 * @param aUrl    specified url
 *
 * @return node containing specified url
 */
public DefaultMutableTreeNode findNodeByUrl(DefaultMutableTreeNode aParent, String aUrl) {
    DefaultMutableTreeNode node;
    DefaultMutableTreeNode result;
    NodeInfo info;
    if (!aParent.getAllowsChildren()) {
        return null;
    }
    for (Enumeration enumeration = aParent.children(); enumeration.hasMoreElements();) {
        node = (DefaultMutableTreeNode) enumeration.nextElement();
        info = (NodeInfo) node.getUserObject();
        if (aUrl.equals(info.getUrl())) {
            return node;
        } else {
            result = findNodeByUrl(node, aUrl);
            if (result != null) {
                return result;
            }
        }
    }
    return null;
}

From source file:net.sf.xmm.moviemanager.commands.importexport.IMDbInfoUpdater.java

@SuppressWarnings("unchecked")
public void execute() {

    /* Setting the priority of the thread to 4 to give the GUI room to update more often */
    Thread.currentThread().setPriority(4);

    DefaultMutableTreeNode root = (DefaultMutableTreeNode) ((DefaultTreeModel) MovieManager.getDialog()
            .getMoviesList().getModel()).getRoot();
    final Enumeration<DefaultMutableTreeNode> enumeration = root.children();

    lengthOfTask = root.getChildCount();

    try {//from   ww  w  .  j a  v a  2 s.  c  om

        Runnable threadRunner = new Runnable() {

            public void run() {

                DefaultMutableTreeNode node;
                ModelEntry model;

                ModelMovieInfo modelInfo = new ModelMovieInfo();
                IMDb imdb;
                try {
                    imdb = IMDbLib.newIMDb(MovieManager.getConfig().getHttpSettings());

                    while (enumeration.hasMoreElements()) {

                        if (canceled)
                            break;

                        // Will start only threadCount number of threads
                        while (threadHandler.getThreadCount() > threadCount - 1) {
                            threadHandler.waitForNextDecrease();
                        }

                        node = enumeration.nextElement();
                        model = (ModelEntry) node.getUserObject();

                        if (!model.getHasGeneralInfoData()) {
                            model.updateGeneralInfoData();
                        }

                        if (!model.getHasAdditionalInfoData()) {
                            model.updateAdditionalInfoData();
                        }

                        /* wrapping each movie in a thread */
                        Thread t = new Thread(new GetInfo(modelInfo, model, imdb));
                        t.start();

                        // Wait till the new thread has started and increased the thread count.
                        threadHandler.waitForNextIncrease();
                    }

                    do {
                        threadHandler.waitForNextDecrease();

                        if (threadHandler.getNoAction()) {
                            log.debug("No threads have finished within timeout of " + threadHandler.getTimeout()
                                    + "ms.");

                            ArrayList<GetInfo> active = threadHandler.getActiveThreads();

                            log.debug("Active threads:");

                            for (GetInfo t : active) {
                                log.debug(t.getTitle());
                            }
                        }

                    } while (threadHandler.getThreadCount() > 0);

                    setDone();

                    log.debug("Done updating list!");

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Thread t = new Thread(threadRunner);
        t.start();

    } catch (Exception e) {
        log.warn("Exception:" + e.getMessage());
    }
}

From source file:fxts.stations.ui.help.ContentTree.java

/**
 * Returns path to node containing the specified url.
 *
 * @param aParentPath parent node/* www .  j  a v a  2  s  . c o m*/
 * @param aUrl        specified url
 *
 * @return path to node containing specified url
 */
public TreePath findPathByUrl(TreePath aParentPath, String aUrl) {
    DefaultMutableTreeNode parentNode;
    DefaultMutableTreeNode node;
    TreePath resultPath;
    NodeInfo info;
    parentNode = (DefaultMutableTreeNode) aParentPath.getLastPathComponent();
    if (!parentNode.getAllowsChildren()) {
        return null;
    }
    for (Enumeration enumeration = parentNode.children(); enumeration.hasMoreElements();) {
        node = (DefaultMutableTreeNode) enumeration.nextElement();
        info = (NodeInfo) node.getUserObject();
        if (aUrl.equals(info.getUrl())) {
            return aParentPath.pathByAddingChild(node);
        } else {
            resultPath = findPathByUrl(aParentPath.pathByAddingChild(node), aUrl);
            if (resultPath != null) {
                return resultPath;
            }
        }
    }
    return null;
}

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

private void renormalizeThreadDepth(DefaultMutableTreeNode threadNode1) {
    for (Enumeration e = threadNode1.children(); e.hasMoreElements();) {
        DefaultMutableTreeNode monitorNode2 = (DefaultMutableTreeNode) e.nextElement();
        for (int ii = 0; ii < monitorNode2.getChildCount(); ii++) {
            renormalizeMonitorDepth(monitorNode2, ii);
        }/*from   w  w w  .ja v a2s.  c o  m*/
    }
}