public static Image createImage(final JTree tree) { final TreeSelectionModel model = tree.getSelectionModel(); final TreePath[] paths = model.getSelectionPaths(); int count = 0; final List<ChangesBrowserNode> nodes = new ArrayList<ChangesBrowserNode>(); for (final TreePath path : paths) { final ChangesBrowserNode node = (ChangesBrowserNode) path.getLastPathComponent(); if (!node.isLeaf()) { nodes.add(node); count += node.getCount(); } } for (TreePath path : paths) { final ChangesBrowserNode element = (ChangesBrowserNode) path.getLastPathComponent(); boolean child = false; for (final ChangesBrowserNode node : nodes) { if (node.isNodeChild(element)) { child = true; break; } } if (!child) { if (element.isLeaf()) count++; } else if (!element.isLeaf()) { count -= element.getCount(); } } final JLabel label = new JLabel(VcsBundle.message("changes.view.dnd.label", count)); label.setOpaque(true); label.setForeground(tree.getForeground()); label.setBackground(tree.getBackground()); label.setFont(tree.getFont()); label.setSize(label.getPreferredSize()); final BufferedImage image = new BufferedImage(label.getWidth(), label.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D) image.getGraphics(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f)); label.paint(g2); g2.dispose(); return image; }
@Override public final Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { myTree = tree; clear(); mySelected = selected; myFocusedCalculated = false; // We paint background if and only if tree path is selected and tree has focus. // If path is selected and tree is not focused then we just paint focused border. if (UIUtil.isFullRowSelectionLAF()) { setBackground(selected ? UIUtil.getTreeSelectionBackground() : null); } else if (WideSelectionTreeUI.isWideSelection(tree)) { setPaintFocusBorder(false); if (selected) { setBackground( hasFocus ? UIUtil.getTreeSelectionBackground() : UIUtil.getTreeUnfocusedSelectionBackground()); } } else if (selected) { setPaintFocusBorder(true); if (isFocused()) { setBackground(UIUtil.getTreeSelectionBackground()); } else { setBackground(null); } } else { setBackground(null); } if (value instanceof LoadingNode) { setForeground(JBColor.GRAY); setIcon(LOADING_NODE_ICON); } else { setForeground(tree.getForeground()); setIcon(null); } if (UIUtil.isUnderGTKLookAndFeel()) { super.setOpaque(false); // avoid nasty background super.setIconOpaque(false); } else if (UIUtil.isUnderNimbusLookAndFeel() && selected && hasFocus) { super.setOpaque(false); // avoid erasing Nimbus focus frame super.setIconOpaque(false); } else if (WideSelectionTreeUI.isWideSelection(tree)) { super.setOpaque(false); // avoid erasing Nimbus focus frame super.setIconOpaque(false); } else { super.setOpaque( myOpaque || selected && hasFocus || selected && isFocused()); // draw selection background even for non-opaque tree } if (tree.getUI() instanceof WideSelectionTreeUI && UIUtil.isUnderAquaBasedLookAndFeel()) { setMyBorder(null); setIpad(new Insets(0, 2, 0, 2)); } customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus); return this; }