Search in sources :

Example 6 with ExpandVetoException

use of javax.swing.tree.ExpandVetoException in project java-swing-tips by aterai.

the class EmptyIcon method updateUI.

@Override
public void updateUI() {
    setCellRenderer(null);
    removeTreeWillExpandListener(listener);
    super.updateUI();
    setUI(new BasicTreeUI() {

        @Override
        public Rectangle getPathBounds(JTree tree, TreePath path) {
            if (Objects.nonNull(tree) && Objects.nonNull(treeState)) {
                return getTreePathBounds(path, tree.getInsets(), new Rectangle());
            }
            return null;
        }

        private Rectangle getTreePathBounds(TreePath path, Insets insets, Rectangle bounds) {
            Rectangle rect = treeState.getBounds(path, bounds);
            if (Objects.nonNull(rect)) {
                rect.width = tree.getWidth();
                rect.y += insets.top;
            }
            return rect;
        }
    });
    UIManager.put("Tree.repaintWholeRow", Boolean.TRUE);
    TreeCellRenderer r = getCellRenderer();
    setCellRenderer((tree, value, selected, expanded, leaf, row, hasFocus) -> {
        Component c = r.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        c.setBackground(selected ? SELECTED_COLOR : tree.getBackground());
        if (c instanceof JComponent) {
            ((JComponent) c).setOpaque(true);
        }
        return c;
    });
    setOpaque(false);
    setRootVisible(false);
    // https://ateraimemo.com/Swing/TreeNodeCollapseVeto.html
    listener = new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(TreeExpansionEvent e) {
        // throw new ExpandVetoException(e, "Tree expansion cancelled");
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
            throw new ExpandVetoException(e, "Tree collapse cancelled");
        }
    };
    addTreeWillExpandListener(listener);
}
Also used : TreeCellRenderer(javax.swing.tree.TreeCellRenderer) TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) TreePath(javax.swing.tree.TreePath) BasicTreeUI(javax.swing.plaf.basic.BasicTreeUI) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) ExpandVetoException(javax.swing.tree.ExpandVetoException)

Example 7 with ExpandVetoException

use of javax.swing.tree.ExpandVetoException in project java-swing-tips by aterai.

the class BackgroundTask method treeWillExpand.

@Override
public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException {
    TreePath path = e.getPath();
    Object o = path.getLastPathComponent();
    if (o instanceof DefaultMutableTreeNode) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) o;
        File file = (File) node.getUserObject();
        String name = file.getName();
        // System.out.println(name);
        if (!name.isEmpty() && name.codePointAt(0) == '.') {
            throw new ExpandVetoException(e, "Tree expansion cancelled");
        }
    }
}
Also used : TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) File(java.io.File) ExpandVetoException(javax.swing.tree.ExpandVetoException)

Aggregations

ExpandVetoException (javax.swing.tree.ExpandVetoException)7 TreeExpansionEvent (javax.swing.event.TreeExpansionEvent)5 TreeWillExpandListener (javax.swing.event.TreeWillExpandListener)5 TreePath (javax.swing.tree.TreePath)4 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)3 File (java.io.File)2 Page (chatty.gui.components.settings.SettingsDialog.Page)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 ComboBox (com.intellij.openapi.ui.ComboBox)1 TreeColumnInfo (com.intellij.ui.treeStructure.treetable.TreeColumnInfo)1 ColumnInfo (com.intellij.util.ui.ColumnInfo)1 Component (java.awt.Component)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 JLabel (javax.swing.JLabel)1 JTree (javax.swing.JTree)1 Border (javax.swing.border.Border)1 IconUIResource (javax.swing.plaf.IconUIResource)1 BasicTreeUI (javax.swing.plaf.basic.BasicTreeUI)1