001// License: GPL. See LICENSE file for details.
002package org.openstreetmap.josm.gui.widgets;
003
004import java.awt.Color;
005import java.awt.Dimension;
006import java.awt.GridBagLayout;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009import org.openstreetmap.josm.tools.GBC;
010import org.openstreetmap.josm.tools.ImageProvider;
011
012
013/**
014 * A small user interface component that consists of an image label and
015 * a fixed text content to the right of the image.
016 * Moved from @link org.openstreetmap.josm.gui.MapStatus @since 5965
017 */
018public class ImageLabel extends JPanel {
019    public static final Color backColor = Color.decode("#b8cfe5");
020    public static final Color backColorActive = Color.decode("#aaff5e");
021
022    private JLabel tf;
023    private int charCount;
024    
025    public ImageLabel(String img, String tooltip, int charCount) {
026        super();
027        setLayout(new GridBagLayout());
028        setBackground(backColor);
029        add(new JLabel(ImageProvider.get("statusline/"+img+".png")), GBC.std().anchor(GBC.WEST).insets(0,1,1,0));
030        add(tf = new JLabel(), GBC.std().fill(GBC.BOTH).anchor(GBC.WEST).insets(2,1,1,0));
031        setToolTipText(tooltip);
032        this.charCount = charCount;
033    }
034    
035    public void setText(String t) {
036        tf.setText(t);
037    }
038    @Override public Dimension getPreferredSize() {
039        return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getPreferredSize().height);
040    }
041    @Override public Dimension getMinimumSize() {
042        return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getMinimumSize().height);
043    }
044}