Search in sources :

Example 11 with TreeCellRenderer

use of javax.swing.tree.TreeCellRenderer in project intellij-community by JetBrains.

the class ExternalSystemActionUtil method installCheckboxRenderer.

public static void installCheckboxRenderer(final SimpleTree tree, final CheckboxHandler handler) {
    final JCheckBox checkbox = new JCheckBox();
    final JPanel panel = new JPanel(new BorderLayout());
    panel.add(checkbox, BorderLayout.WEST);
    final TreeCellRenderer baseRenderer = tree.getCellRenderer();
    tree.setCellRenderer(new TreeCellRenderer() {

        public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean selected, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
            final Component baseComponent = baseRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
            final Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (!handler.isVisible(userObject)) {
                return baseComponent;
            }
            final Color foreground = selected ? UIUtil.getTreeSelectionForeground() : UIUtil.getTreeTextForeground();
            Color background = selected ? UIUtil.getTreeSelectionBackground(hasFocus) : UIUtil.getTreeTextBackground();
            panel.add(baseComponent, BorderLayout.CENTER);
            panel.setBackground(background);
            panel.setForeground(foreground);
            CheckBoxState state = handler.getState(userObject);
            checkbox.setSelected(state != CheckBoxState.UNCHECKED);
            checkbox.setEnabled(state != CheckBoxState.PARTIAL);
            checkbox.setBackground(background);
            checkbox.setForeground(foreground);
            return panel;
        }
    });
    tree.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            int row = tree.getRowForLocation(e.getX(), e.getY());
            if (row >= 0) {
                TreePath path = tree.getPathForRow(row);
                if (!isCheckboxEnabledFor(path, handler))
                    return;
                Rectangle checkBounds = checkbox.getBounds();
                checkBounds.setLocation(tree.getRowBounds(row).getLocation());
                if (checkBounds.contains(e.getPoint())) {
                    handler.toggle(path, e);
                    e.consume();
                    tree.setSelectionRow(row);
                }
            }
        }
    });
    tree.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                TreePath[] treePaths = tree.getSelectionPaths();
                if (treePaths != null) {
                    for (TreePath treePath : treePaths) {
                        if (!isCheckboxEnabledFor(treePath, handler))
                            continue;
                        handler.toggle(treePath, e);
                    }
                    e.consume();
                }
            }
        }
    });
}
Also used : TreeCellRenderer(javax.swing.tree.TreeCellRenderer) TreePath(javax.swing.tree.TreePath)

Example 12 with TreeCellRenderer

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

the class CompilerOptionsConfigurable method createTreeCellRenderer.

private TreeCellRenderer createTreeCellRenderer() {
    return new TreeCellRenderer() {

        private final JLabel myLabel = new JLabel();

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            final Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (!(userObject instanceof CompilerOptionInfo)) {
                // invisible root node
                return myLabel;
            }
            final CompilerOptionInfo info = (CompilerOptionInfo) userObject;
            myLabel.setText(info.DISPLAY_NAME);
            final ValueSource valueSource = getValueAndSource(info).second;
            renderAccordingToSource(myLabel, valueSource, selected);
            myLabel.setForeground(selected ? UIUtil.getTableSelectionForeground() : UIUtil.getTableForeground());
            return myLabel;
        }
    };
}
Also used : TreeCellRenderer(javax.swing.tree.TreeCellRenderer) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) CompilerOptionInfo(com.intellij.flex.model.bc.CompilerOptionInfo) ValueSource(com.intellij.flex.model.bc.ValueSource)

Example 13 with TreeCellRenderer

use of javax.swing.tree.TreeCellRenderer in project jdk8u_jdk by JetBrains.

the class SynthTreeUI method createDefaultCellEditor.

/**
     * {@inheritDoc}
     */
@Override
protected TreeCellEditor createDefaultCellEditor() {
    TreeCellRenderer renderer = tree.getCellRenderer();
    DefaultTreeCellEditor editor;
    if (renderer != null && (renderer instanceof DefaultTreeCellRenderer)) {
        editor = new SynthTreeCellEditor(tree, (DefaultTreeCellRenderer) renderer);
    } else {
        editor = new SynthTreeCellEditor(tree, null);
    }
    return editor;
}
Also used : TreeCellRenderer(javax.swing.tree.TreeCellRenderer) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) DefaultTreeCellEditor(javax.swing.tree.DefaultTreeCellEditor) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer)

Example 14 with TreeCellRenderer

use of javax.swing.tree.TreeCellRenderer in project jdk8u_jdk by JetBrains.

the class SynthTreeUI method paint.

/**
     * Paints the specified component.
     *
     * @param context context for the component being painted
     * @param g the {@code Graphics} object used for painting
     * @see #update(Graphics,JComponent)
     */
protected void paint(SynthContext context, Graphics g) {
    paintContext = context;
    updateLeadSelectionRow();
    Rectangle paintBounds = g.getClipBounds();
    Insets insets = tree.getInsets();
    TreePath initialPath = getClosestPathForLocation(tree, 0, paintBounds.y);
    Enumeration paintingEnumerator = treeState.getVisiblePathsFrom(initialPath);
    int row = treeState.getRowForPath(initialPath);
    int endY = paintBounds.y + paintBounds.height;
    TreeModel treeModel = tree.getModel();
    SynthContext cellContext = getContext(tree, Region.TREE_CELL);
    drawingCache.clear();
    setHashColor(context.getStyle().getColor(context, ColorType.FOREGROUND));
    if (paintingEnumerator != null) {
        // First pass, draw the rows
        boolean done = false;
        boolean isExpanded;
        boolean hasBeenExpanded;
        boolean isLeaf;
        Rectangle rowBounds = new Rectangle(0, 0, tree.getWidth(), 0);
        Rectangle bounds;
        TreePath path;
        TreeCellRenderer renderer = tree.getCellRenderer();
        DefaultTreeCellRenderer dtcr = (renderer instanceof DefaultTreeCellRenderer) ? (DefaultTreeCellRenderer) renderer : null;
        configureRenderer(cellContext);
        while (!done && paintingEnumerator.hasMoreElements()) {
            path = (TreePath) paintingEnumerator.nextElement();
            bounds = getPathBounds(tree, path);
            if ((path != null) && (bounds != null)) {
                isLeaf = treeModel.isLeaf(path.getLastPathComponent());
                if (isLeaf) {
                    isExpanded = hasBeenExpanded = false;
                } else {
                    isExpanded = treeState.getExpandedState(path);
                    hasBeenExpanded = tree.hasBeenExpanded(path);
                }
                rowBounds.y = bounds.y;
                rowBounds.height = bounds.height;
                paintRow(renderer, dtcr, context, cellContext, g, paintBounds, insets, bounds, rowBounds, path, row, isExpanded, hasBeenExpanded, isLeaf);
                if ((bounds.y + bounds.height) >= endY) {
                    done = true;
                }
            } else {
                done = true;
            }
            row++;
        }
        // Draw the connecting lines and controls.
        // Find each parent and have them draw a line to their last child
        boolean rootVisible = tree.isRootVisible();
        TreePath parentPath = initialPath;
        parentPath = parentPath.getParentPath();
        while (parentPath != null) {
            paintVerticalPartOfLeg(g, paintBounds, insets, parentPath);
            drawingCache.put(parentPath, Boolean.TRUE);
            parentPath = parentPath.getParentPath();
        }
        done = false;
        paintingEnumerator = treeState.getVisiblePathsFrom(initialPath);
        while (!done && paintingEnumerator.hasMoreElements()) {
            path = (TreePath) paintingEnumerator.nextElement();
            bounds = getPathBounds(tree, path);
            if ((path != null) && (bounds != null)) {
                isLeaf = treeModel.isLeaf(path.getLastPathComponent());
                if (isLeaf) {
                    isExpanded = hasBeenExpanded = false;
                } else {
                    isExpanded = treeState.getExpandedState(path);
                    hasBeenExpanded = tree.hasBeenExpanded(path);
                }
                // See if the vertical line to the parent has been drawn.
                parentPath = path.getParentPath();
                if (parentPath != null) {
                    if (drawingCache.get(parentPath) == null) {
                        paintVerticalPartOfLeg(g, paintBounds, insets, parentPath);
                        drawingCache.put(parentPath, Boolean.TRUE);
                    }
                    paintHorizontalPartOfLeg(g, paintBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf);
                } else if (rootVisible && row == 0) {
                    paintHorizontalPartOfLeg(g, paintBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf);
                }
                if (shouldPaintExpandControl(path, row, isExpanded, hasBeenExpanded, isLeaf)) {
                    paintExpandControl(g, paintBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf);
                }
                if ((bounds.y + bounds.height) >= endY) {
                    done = true;
                }
            } else {
                done = true;
            }
            row++;
        }
    }
    cellContext.dispose();
    paintDropLine(g);
    // Empty out the renderer pane, allowing renderers to be gc'ed.
    rendererPane.removeAll();
    paintContext = null;
}
Also used : TreeCellRenderer(javax.swing.tree.TreeCellRenderer) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) TreeModel(javax.swing.tree.TreeModel) Insets(java.awt.Insets) Enumeration(java.util.Enumeration) TreePath(javax.swing.tree.TreePath) Rectangle(java.awt.Rectangle) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer)

Aggregations

TreeCellRenderer (javax.swing.tree.TreeCellRenderer)14 DefaultTreeCellRenderer (javax.swing.tree.DefaultTreeCellRenderer)7 TreePath (javax.swing.tree.TreePath)4 ComponentAdapter (java.awt.event.ComponentAdapter)2 ComponentEvent (java.awt.event.ComponentEvent)2 TableCellRenderer (javax.swing.table.TableCellRenderer)2 CompilerOptionInfo (com.intellij.flex.model.bc.CompilerOptionInfo)1 ValueSource (com.intellij.flex.model.bc.ValueSource)1 DetailsComponent (com.intellij.openapi.ui.DetailsComponent)1 JBScrollPane (com.intellij.ui.components.JBScrollPane)1 TreeTableView (com.intellij.ui.dualView.TreeTableView)1 TableView (com.intellij.ui.table.TableView)1 TableViewModel (com.intellij.util.ui.TableViewModel)1 Color (java.awt.Color)1 Insets (java.awt.Insets)1 Rectangle (java.awt.Rectangle)1 Enumeration (java.util.Enumeration)1 UIResource (javax.swing.plaf.UIResource)1 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)1 DefaultTreeCellEditor (javax.swing.tree.DefaultTreeCellEditor)1