1 | // License: GPL. See LICENSE file for details.
|
---|
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 JLabel tf;
|
---|
21 | private int charCount;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Constructs a new {@code ImageLabel}.
|
---|
25 | * @param img Image name (without .png extension) to find in {@code statusline} directory
|
---|
26 | * @param tooltip Tooltip text to display
|
---|
27 | * @param charCount Character count used to compute min/preferred size
|
---|
28 | * @param background The background color
|
---|
29 | */
|
---|
30 | public ImageLabel(String img, String tooltip, int charCount, Color background) {
|
---|
31 | setLayout(new GridBagLayout());
|
---|
32 | setBackground(background);
|
---|
33 | add(new JLabel(ImageProvider.get("statusline/"+img+".png")), GBC.std().anchor(GBC.WEST).insets(0,1,1,0));
|
---|
34 | add(tf = new JLabel(), GBC.std().fill(GBC.BOTH).anchor(GBC.WEST).insets(2,1,1,0));
|
---|
35 | setToolTipText(tooltip);
|
---|
36 | this.charCount = charCount;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Sets the text to display.
|
---|
41 | * @param t text to display
|
---|
42 | */
|
---|
43 | public void setText(String t) {
|
---|
44 | tf.setText(t);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public Dimension getPreferredSize() {
|
---|
49 | return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getPreferredSize().height);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public Dimension getMinimumSize() {
|
---|
54 | return new Dimension(25 + charCount*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getMinimumSize().height);
|
---|
55 | }
|
---|
56 | }
|
---|