Example usage for javax.swing JTree isExpanded

List of usage examples for javax.swing JTree isExpanded

Introduction

In this page you can find the example usage for javax.swing JTree isExpanded.

Prototype

public boolean isExpanded(int row) 

Source Link

Document

Returns true if the node at the specified display row is currently expanded.

Usage

From source file:Main.java

/**
 * Saves the current expansion state of a JTree and returns it in an Enumeration for storing purposes. Example: A
 * JTree that has all its branches collapsed will return a TreeExpansionState of ",0".
 * //w w w.  j a v  a2  s  . com
 * @param tree
 *            Save the current expansion state of this tree
 * @return The current expansion state of the given tree as Enumeration<TreePath>
 */
public static String getTreeExpansionState(JTree tree, int row) {
    TreePath rowPath = tree.getPathForRow(row);
    StringBuilder sb = new StringBuilder();
    int rowCount = tree.getRowCount();

    for (int i = row; i < rowCount; i++) {
        TreePath path = tree.getPathForRow(i);

        if (i == row || isDescendant(path, rowPath)) {
            if (tree.isExpanded(path))
                sb.append("," + String.valueOf(i - row));
        } else {
            break;
        }
    }
    return sb.toString();
}

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

public Main(TaskTree taskTree) {
    super();/*from w w  w .  j  a v  a 2s.co  m*/

    this.taskTree = taskTree;

    this.setTitle("Task Tree");
    this.setSize(new Dimension(300, 500));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel quickInPanel = new JPanel(new BorderLayout());
    this.quickIn = new JTextArea();
    this.quickIn.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent arg0) {
            if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
                String newText = quickIn.getText().trim();
                if (newText != null && newText.length() > 0) {
                    addTask(Main.this.taskTree.getRoot(), 0, newText, true);
                    try {
                        Main.this.taskTree.changesMade();
                    } catch (Exception e) {
                        error(e.getMessage());
                    }
                }
                quickIn.setText("");
            }
        }
    });
    this.quickIn.setPreferredSize(new Dimension(300, 75));
    this.quickIn.setBorder(BorderFactory.createTitledBorder("Quick Input"));
    quickInPanel.add(this.quickIn, BorderLayout.CENTER);
    this.syncButton = new JButton("Sync");
    this.syncButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            new SyncThread(Main.this).start();
        }
    });
    quickInPanel.add(this.syncButton, BorderLayout.EAST);
    this.getContentPane().add(quickInPanel, BorderLayout.NORTH);

    this.tree = new JTree(taskTree.getTreeModel());
    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            Object newVal = htmlFilter(String.valueOf(node.getUserObject()));
            if (node.getChildCount() > 0 && !tree.isExpanded(new TreePath(node.getPath()))) {
                DefaultMutableTreeNode firstLeaf = (DefaultMutableTreeNode) node.getFirstLeaf();
                newVal = htmlFilter(String.valueOf(node.getUserObject()))
                        + " <span style='color:silver;font-style:italic'>" + "("
                        + String.valueOf(firstLeaf.getUserObject()) + ")</span>";
            }
            newVal = "<html>" + newVal + "</html>";

            return super.getTreeCellRendererComponent(tree, newVal, selected, expanded, leaf, row, hasFocus);
        }
    };
    ImageIcon bulletIcon = new ImageIcon(Main.class.getResource("bullet.gif"));
    renderer.setLeafIcon(bulletIcon);
    renderer.setOpenIcon(bulletIcon);
    renderer.setClosedIcon(bulletIcon);
    renderer.setBorder(BorderFactory.createEmptyBorder(4, 0, 4, 0));
    this.tree.setCellRenderer(renderer);
    this.tree.setRootVisible(false);
    this.tree.setShowsRootHandles(true);
    this.tree.addMouseListener(new MouseAdapter() {
        protected void doSelectRow(MouseEvent arg0) {
            int row = tree.getRowForLocation(arg0.getX(), arg0.getY());
            if (row != -1) {
                tree.setSelectionRow(row);
                if (arg0.isPopupTrigger()) {
                    popup.show(tree, arg0.getX(), arg0.getY());
                }
            }
        }

        public void mousePressed(MouseEvent arg0) {
            doSelectRow(arg0);
        }

        public void mouseReleased(MouseEvent arg0) {
            doSelectRow(arg0);
        }
    });
    JScrollPane treeScroll = new JScrollPane(tree);
    treeScroll.setBorder(BorderFactory.createTitledBorder("Task List"));
    this.getContentPane().add(treeScroll, BorderLayout.CENTER);

    this.popup = new JPopupMenu();
    JMenuItem addBefore = new JMenuItem("Add Before");
    addBefore.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            int pos = parent.getIndex(selected);
            promptAndInsert(parent, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(addBefore);
    JMenuItem addAfter = new JMenuItem("Add After");
    addAfter.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            int pos = parent.getIndex(selected) + 1;
            promptAndInsert(parent, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(addAfter);
    JMenuItem addNested = new JMenuItem("Add Nested");
    addNested.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            int pos = selected.getChildCount();
            promptAndInsert(selected, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                ex.getMessage();
            }
        }
    });
    this.popup.add(addNested);
    this.popup.add(new JSeparator());
    JMenuItem moveTop = new JMenuItem("Move to Top");
    moveTop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            moveTask(selected, parent, 0);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(moveTop);
    JMenuItem moveUp = new JMenuItem("Move Up");
    moveUp.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            int pos = Math.max(parent.getIndex(selected) - 1, 0);
            moveTask(selected, parent, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(moveUp);
    JMenuItem moveDown = new JMenuItem("Move Down");
    moveDown.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            int pos = Math.min(parent.getIndex(selected) + 1, parent.getChildCount() - 1);
            moveTask(selected, parent, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(moveDown);
    JMenuItem moveBottom = new JMenuItem("Move to Bottom");
    moveBottom.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            moveTask(selected, parent, parent.getChildCount() - 1);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(moveBottom);
    this.popup.add(new JSeparator());
    JMenuItem rename = new JMenuItem("Edit");
    rename.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            String newText = prompt((String) selected.getUserObject());
            if (newText != null && newText.length() > 0) {
                selected.setUserObject(newText);
                Main.this.taskTree.getTreeModel().reload(selected);
                try {
                    Main.this.taskTree.changesMade();
                } catch (Exception ex) {
                    error(ex.getMessage());
                }
            }
        }
    });
    this.popup.add(rename);
    JMenuItem delete = new JMenuItem("Delete");
    delete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            promptAndRemove(getSelectedNode());
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(delete);

    this.setVisible(true);

    loadConfig();
    load();

    syncButton.setVisible(this.taskTree.hasSyncCapability());
}

From source file:org.eclipse.jubula.rc.swing.tester.util.TreeOperationContext.java

/**
 * {@inheritDoc}// ww  w  .  ja  v  a 2s  . c  o m
 */
public String getRenderedText(final Object node) {
    return (String) getQueuer().invokeAndWait("getRenderedText", new IRunnable() { //$NON-NLS-1$
        public Object run() {
            int row = getRowForTreeNode(node);
            JTree tree = (JTree) getTree();
            Component cellRendererComponent = tree.getCellRenderer().getTreeCellRendererComponent(tree, node,
                    false, tree.isExpanded(row), m_model.isLeaf(node), row, false);
            try {
                return TesterUtil.getRenderedText(cellRendererComponent);
            } catch (StepExecutionException e) {
                // This is a valid case in JTrees since if there is no text
                // there is also no renderer 
                log.warn("Renderer not supported: " + //$NON-NLS-1$
                cellRendererComponent.getClass(), e);
                return null;
            }
        }
    });
}

From source file:org.executequery.gui.browser.TreeFindAction.java

protected boolean changed(JComponent comp, String searchString, Position.Bias bias) {

    if (StringUtils.isBlank(searchString)) {

        return false;
    }/* w  w  w.  j  a  v a  2 s  .  c o  m*/

    JTree tree = (JTree) comp;
    String prefix = searchString;

    if (ignoreCase()) {

        prefix = prefix.toUpperCase();
    }

    boolean wildcardStart = prefix.startsWith("*");
    if (wildcardStart) {

        prefix = prefix.substring(1);

    } else {

        prefix = "^" + prefix;
    }
    prefix = prefix.replaceAll("\\*", ".*");

    Matcher matcher = Pattern.compile(prefix).matcher("");
    List<TreePath> matchedPaths = new ArrayList<TreePath>();
    for (int i = 1; i < tree.getRowCount(); i++) {

        TreePath path = tree.getPathForRow(i);
        String text = tree.convertValueToText(path.getLastPathComponent(), tree.isRowSelected(i),
                tree.isExpanded(i), true, i, false);

        if (ignoreCase()) {

            text = text.toUpperCase();
        }

        //            if ((wildcardStart && text.contains(prefix)) || text.startsWith(prefix, 0)) {
        //
        //                matchedPaths.add(path);
        //            }

        matcher.reset(text);
        if (matcher.find()) {

            matchedPaths.add(path);
        }

    }

    foundValues(matchedPaths);

    return !(matchedPaths.isEmpty());
}

From source file:org.executequery.gui.browser.TreeFindAction.java

public TreePath getNextMatch(JTree tree, String prefix, int startingRow, Position.Bias bias) {

    int max = tree.getRowCount();
    if (prefix == null) {
        throw new IllegalArgumentException();
    }// w w w .  j a v  a2  s  .  co m
    if (startingRow < 0 || startingRow >= max) {
        throw new IllegalArgumentException();
    }

    if (ignoreCase()) {
        prefix = prefix.toUpperCase();
    }

    // start search from the next/previous element froom the
    // selected element
    int increment = (bias == null || bias == Position.Bias.Forward) ? 1 : -1;

    int row = startingRow;
    do {

        TreePath path = tree.getPathForRow(row);
        String text = tree.convertValueToText(path.getLastPathComponent(), tree.isRowSelected(row),
                tree.isExpanded(row), true, row, false);

        if (ignoreCase()) {

            text = text.toUpperCase();
        }

        if (text.startsWith(prefix)) {

            return path;
        }

        row = (row + increment + max) % max;

    } while (row != startingRow);

    return null;
}

From source file:org.nuclos.client.explorer.ExplorerNode.java

/**
 * expand all children of this node/*w  w  w.j a  v a 2  s . c  o m*/
 * @param tree
 */
public void expandAllChildren(final JTree tree) {
    for (int i = getChildCount() - 1; i >= 0; i--) {
        final TreePath treePath = new TreePath(
                (((DefaultMutableTreeNode) ExplorerNode.this.getChildAt(i))).getPath());
        if (!tree.isExpanded(treePath)) {
            tree.expandPath(treePath);
        }
    }
}

From source file:org.nuclos.client.explorer.ExplorerNode.java

static void createExpandendPathsForTree(TreePath path, JTree tree, List<String> lstExpandedPathsResult) {
    final ExplorerNode<?> explorernode = (ExplorerNode<?>) path.getLastPathComponent();
    boolean isExpanded = tree.isExpanded(path);
    if (isExpanded) {
        lstExpandedPathsResult.add(explorernode.getIdentifierPath());

        for (int i = 0; i < explorernode.getChildCount(); i++) {
            final ExplorerNode<?> explorernodeChild = (ExplorerNode<?>) explorernode.getChildAt(i);
            createExpandendPathsForTree(path.pathByAddingChild(explorernodeChild), tree,
                    lstExpandedPathsResult);
        }//  ww  w.  j a  v  a  2  s . co m
    }
}