Search in sources :

Example 1 with ExpandVetoException

use of javax.swing.tree.ExpandVetoException in project intellij-plugins by JetBrains.

the class DartConfigurable method createUIComponents.

private void createUIComponents() {
    mySdkPathComboWithBrowse = new ComboboxWithBrowseButton(new ComboBox<>());
    myDartiumPathComboWithBrowse = new ComboboxWithBrowseButton(new ComboBox<>());
    final CheckboxTree.CheckboxTreeCellRenderer checkboxTreeCellRenderer = new CheckboxTree.CheckboxTreeCellRenderer() {

        @Override
        public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            if (!(value instanceof CheckedTreeNode))
                return;
            final boolean dartSupportEnabled = myEnableDartSupportCheckBox.isSelected();
            final CheckedTreeNode node = (CheckedTreeNode) value;
            final Object userObject = node.getUserObject();
            if (userObject instanceof Project) {
                if (!dartSupportEnabled) {
                    //disabled state is also used as partially selected, that's why we do not change 'enabled' state if dartSupportEnabled
                    getCheckbox().setEnabled(false);
                }
                getTextRenderer().setEnabled(dartSupportEnabled);
                getTextRenderer().append(DartBundle.message("project.0", ((Project) userObject).getName()));
            } else if (userObject instanceof Module) {
                getCheckbox().setEnabled(dartSupportEnabled);
                getTextRenderer().setEnabled(dartSupportEnabled);
                final Icon moduleIcon = ModuleType.get((Module) userObject).getIcon();
                getTextRenderer().setIcon(dartSupportEnabled ? moduleIcon : IconLoader.getDisabledIcon(moduleIcon));
                getTextRenderer().append(((Module) userObject).getName());
            }
        }
    };
    myModulesCheckboxTreeTable = new CheckboxTreeTable(null, checkboxTreeCellRenderer, new ColumnInfo[] { new TreeColumnInfo("") });
    myModulesCheckboxTreeTable.addCheckboxTreeListener(new CheckboxTreeAdapter() {

        @Override
        public void nodeStateChanged(@NotNull CheckedTreeNode node) {
            updateErrorLabel();
        }
    });
    //myModulesCheckboxTreeTable.setRowHeight(myModulesCheckboxTreeTable.getRowHeight() + 2);
    myModulesCheckboxTreeTable.getTree().addTreeWillExpandListener(new TreeWillExpandListener() {

        public void treeWillExpand(final TreeExpansionEvent event) throws ExpandVetoException {
        }

        public void treeWillCollapse(final TreeExpansionEvent event) throws ExpandVetoException {
            throw new ExpandVetoException(event);
        }
    });
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) ComboBox(com.intellij.openapi.ui.ComboBox) ColumnInfo(com.intellij.util.ui.ColumnInfo) TreeColumnInfo(com.intellij.ui.treeStructure.treetable.TreeColumnInfo) ExpandVetoException(javax.swing.tree.ExpandVetoException) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) TreeColumnInfo(com.intellij.ui.treeStructure.treetable.TreeColumnInfo)

Example 2 with ExpandVetoException

use of javax.swing.tree.ExpandVetoException in project chatty by chatty.

the class Tree method createTree.

public static JTree createTree(Map<Page, List<Page>> nodes) {
    // Create nodes structure based on Map
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    for (Page parent : nodes.keySet()) {
        DefaultMutableTreeNode category = new DefaultMutableTreeNode(parent);
        root.add(category);
        List<Page> subNodes = nodes.get(parent);
        for (Page child : subNodes) {
            category.add(new DefaultMutableTreeNode(child));
        }
    }
    // Create and configure tree
    JTree tree = new JTree(root);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(false);
    // Disable icons and use default renderer
    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {

        // Border to give text some more space
        private final Border BORDER = BorderFactory.createEmptyBorder(0, 3, 0, 1);

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            Component c = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if (c instanceof JLabel) {
                JLabel label = (JLabel) c;
                label.setBorder(BORDER);
            }
            return c;
        }
    };
    renderer.setLeafIcon(null);
    renderer.setOpenIcon(null);
    tree.setCellRenderer(renderer);
    // Expand all branches
    for (int i = 0; i < tree.getRowCount(); i++) {
        tree.expandRow(i);
    }
    // Select closest node on click, to allow more leniency with selecting
    tree.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            int row = tree.getClosestRowForLocation(e.getX(), e.getY());
            if (row != -1) {
                tree.setSelectionRow(row);
            }
        }
    });
    // Prevent collapsing of nodes completely
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
            throw new ExpandVetoException(event);
        }
    });
    return tree;
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) MouseEvent(java.awt.event.MouseEvent) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) Page(chatty.gui.components.settings.SettingsDialog.Page) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) ExpandVetoException(javax.swing.tree.ExpandVetoException) JTree(javax.swing.JTree) Component(java.awt.Component) Border(javax.swing.border.Border) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Example 3 with ExpandVetoException

use of javax.swing.tree.ExpandVetoException in project sonarlint-intellij by SonarSource.

the class FlowsTree method init.

private void init() {
    setRootVisible(false);
    setShowsRootHandles(false);
    setCellRenderer(new TreeCellRenderer());
    this.selectionModel.addTreeSelectionListener(e -> {
        if (e.getSource() != null) {
            TreePath newPath = e.getNewLeadSelectionPath();
            if (newPath != null) {
                Object o = newPath.getLastPathComponent();
                if (!(o instanceof LocationNode)) {
                    FlowsTree.this.setSelectionPath(e.getOldLeadSelectionPath());
                } else {
                    navigateToSelected();
                }
            }
        }
    });
    TreeWillExpandListener l = new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
        // nothing to do
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
            throw new ExpandVetoException(event);
        }
    };
    addTreeWillExpandListener(l);
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) TreePath(javax.swing.tree.TreePath) LocationNode(org.sonarlint.intellij.ui.nodes.LocationNode) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) ExpandVetoException(javax.swing.tree.ExpandVetoException)

Example 4 with ExpandVetoException

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

the class FileTreeCellRenderer 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();
        if (file.isFile()) {
            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)

Example 5 with ExpandVetoException

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

the class EmptyIcon method makeTree.

private static JTree makeTree() {
    Icon emptyIcon = new EmptyIcon();
    UIManager.put("Tree.expandedIcon", new IconUIResource(emptyIcon));
    UIManager.put("Tree.collapsedIcon", new IconUIResource(emptyIcon));
    JTree tree = new JTree();
    tree.setEditable(true);
    tree.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    int row = 0;
    while (row < tree.getRowCount()) {
        tree.expandRow(row++);
    }
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {

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

        @Override
        public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
            throw new ExpandVetoException(e, "Tree collapse cancelled");
        }
    });
    return tree;
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) IconUIResource(javax.swing.plaf.IconUIResource) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) 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