1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 |
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.JPanel;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.tools.GBC;
|
---|
12 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * A small user interface component that consists of an image label and
|
---|
16 | * a fixed text content to the right of the image.
|
---|
17 | * @since 5965
|
---|
18 | */
|
---|
19 | public class ImageLabel extends JPanel {
|
---|
20 | private final JLabel imgLabel;
|
---|
21 | private final JLabel tf;
|
---|
22 | private final int charCount;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Constructs a new {@code ImageLabel}.
|
---|
26 | * @param img Image name (without extension) to find in {@code statusline} directory
|
---|
27 | * @param tooltip Tooltip text to display
|
---|
28 | * @param charCount Character count used to compute min/preferred size
|
---|
29 | * @param background The background color
|
---|
30 | */
|
---|
31 | public ImageLabel(String img, String tooltip, int charCount, Color background) {
|
---|
32 | setLayout(new GridBagLayout());
|
---|
33 | setBackground(background);
|
---|
34 | add(imgLabel = new JLabel(), GBC.std().anchor(GBC.WEST).insets(0, 1, 1, 0));
|
---|
35 | setIcon(img);
|
---|
36 | add(tf = new JLabel(), GBC.std().fill(GBC.BOTH).anchor(GBC.WEST).insets(2, 1, 1, 0));
|
---|
37 | setToolTipText(tooltip);
|
---|
38 | this.charCount = charCount;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Sets the text to display.
|
---|
43 | * @param t text to display
|
---|
44 | */
|
---|
45 | public void setText(String t) {
|
---|
46 | tf.setText(t);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Sets the image to display.
|
---|
51 | * @param img Image name (without extension) to find in {@code statusline} directory
|
---|
52 | */
|
---|
53 | public void setIcon(String img) {
|
---|
54 | imgLabel.setIcon(ImageProvider.get("statusline/" + img));
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public Dimension getPreferredSize() {
|
---|
59 | return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getPreferredSize().height);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public Dimension getMinimumSize() {
|
---|
64 | return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getMinimumSize().height);
|
---|
65 | }
|
---|
66 | }
|
---|