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 Pair<Image, Point> createDraggedImage(DnDAction action, Point dragOrigin) { final TreePath[] paths = getSelectionPaths(); if (paths == null) return null; final int count = paths.length; final JLabel label = new JLabel(String.format("%s item%s", count, count == 1 ? "" : "s")); label.setOpaque(true); label.setForeground(myTree.getForeground()); label.setBackground(myTree.getBackground()); label.setFont(myTree.getFont()); label.setSize(label.getPreferredSize()); final BufferedImage image = UIUtil.createImage(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 new Pair<Image, Point>( image, new Point(-image.getWidth(null), -image.getHeight(null))); }