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;
013import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
014
015/**
016 * A small user interface component that consists of an image label and
017 * a fixed text content to the right of the image.
018 * @since 5965
019 */
020public class ImageLabel extends JPanel {
021    private final JLabel imgLabel = new JLabel();
022    private final JLabel tf = new JLabel();
023    private int charCount;
024
025    /**
026     * Constructs a new {@code ImageLabel}.
027     * @param img Image name (without extension) to find in {@code statusline} directory
028     * @param tooltip Tooltip text to display
029     * @param charCount Character count used to compute min/preferred size
030     * @param background The background color
031     */
032    public ImageLabel(String img, String tooltip, int charCount, Color background) {
033        setLayout(new GridBagLayout());
034        setBackground(background);
035        add(imgLabel, GBC.std().anchor(GBC.WEST).insets(0, 1, 1, 0));
036        setIcon(img);
037        add(tf, GBC.std().fill(GBC.BOTH).anchor(GBC.WEST).insets(2, 1, 1, 0));
038        setToolTipText(tooltip);
039        setCharCount(charCount);
040    }
041
042    /**
043     * Sets the text to display.
044     * @param t text to display
045     */
046    public void setText(String t) {
047        tf.setText(t);
048    }
049
050    /**
051     * Sets the image to display.
052     * @param img Image name (without extension) to find in {@code statusline} directory
053     */
054    public void setIcon(String img) {
055        imgLabel.setIcon(ImageProvider.get("statusline/", img, ImageSizes.STATUSLINE));
056    }
057
058    /**
059     * Sets the foreground color of the text.
060     * @param fg text color
061     */
062    @Override
063    public void setForeground(Color fg) {
064        super.setForeground(fg);
065        if (tf != null) {
066            tf.setForeground(fg);
067        }
068    }
069
070    @Override
071    public Dimension getPreferredSize() {
072        return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getPreferredSize().height);
073    }
074
075    @Override
076    public Dimension getMinimumSize() {
077        return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getMinimumSize().height);
078    }
079
080    /**
081     * Returns the preferred char count.
082     * @return the preferred char count
083     * @since 10191
084     */
085    public final int getCharCount() {
086        return charCount;
087    }
088
089    /**
090     * Sets the preferred char count.
091     * @param charCount the preferred char count
092     * @since 10191
093     */
094    public final void setCharCount(int charCount) {
095        this.charCount = charCount;
096    }
097}