001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.widgets;
003
004import java.awt.Color;
005import java.awt.Dimension;
006import java.awt.GridBagLayout;
007
008import javax.swing.JLabel;
009import javax.swing.JPanel;
010
011import org.openstreetmap.josm.tools.GBC;
012import org.openstreetmap.josm.tools.ImageProvider;
013
014/**
015 * A small user interface component that consists of an image label and
016 * a fixed text content to the right of the image.
017 * @since 5965
018 */
019public class ImageLabel extends JPanel {
020    private final JLabel tf;
021    private final int charCount;
022
023    /**
024     * Constructs a new {@code ImageLabel}.
025     * @param img Image name (without .png extension) to find in {@code statusline} directory
026     * @param tooltip Tooltip text to display
027     * @param charCount Character count used to compute min/preferred size
028     * @param background The background color
029     */
030    public ImageLabel(String img, String tooltip, int charCount, Color background) {
031        setLayout(new GridBagLayout());
032        setBackground(background);
033        add(new JLabel(ImageProvider.get("statusline/"+img+".png")), GBC.std().anchor(GBC.WEST).insets(0, 1, 1, 0));
034        add(tf = new JLabel(), GBC.std().fill(GBC.BOTH).anchor(GBC.WEST).insets(2, 1, 1, 0));
035        setToolTipText(tooltip);
036        this.charCount = charCount;
037    }
038
039    /**
040     * Sets the text to display.
041     * @param t text to display
042     */
043    public void setText(String t) {
044        tf.setText(t);
045    }
046
047    @Override
048    public Dimension getPreferredSize() {
049        return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getPreferredSize().height);
050    }
051
052    @Override
053    public Dimension getMinimumSize() {
054        return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getMinimumSize().height);
055    }
056}