| package org.jetbrains.plugins.gradle.util; |
| |
| import com.intellij.util.ui.GraphicsUtil; |
| import com.intellij.util.ui.UIUtil; |
| import org.jetbrains.annotations.NotNull; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| |
| /** |
| * @author Denis Zhdanov |
| * @since 11/22/12 7:52 PM |
| */ |
| public class TextIcon implements Icon { |
| |
| @NotNull private final String myText; |
| |
| private final int myControlWidth; |
| private final int myControlHeight; |
| |
| private int myTextHeight; |
| |
| public TextIcon(@NotNull String text) { |
| myText = text; |
| JLabel label = new JLabel(""); |
| Font font = label.getFont(); |
| FontMetrics metrics = label.getFontMetrics(font); |
| myControlWidth = metrics.stringWidth(text) + 4; |
| myControlHeight = font.getSize(); |
| } |
| |
| @Override |
| public void paintIcon(Component c, Graphics g, int x, int y) { |
| GraphicsUtil.setupAntialiasing(g, true, false); |
| if (myTextHeight <= 0) { |
| myTextHeight = g.getFont().createGlyphVector(((Graphics2D)g).getFontRenderContext(), myText).getPixelBounds(null, 0, 0).height; |
| } |
| |
| g.setColor(UIUtil.getLabelForeground()); |
| g.drawString(myText, x + 2, y + myControlHeight - ((myControlHeight - myTextHeight) / 2)); |
| } |
| |
| @Override |
| public int getIconWidth() { |
| return myControlWidth; |
| } |
| |
| @Override |
| public int getIconHeight() { |
| return myControlHeight; |
| } |
| } |