source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/ImageLabel.java@ 13146

Last change on this file since 13146 was 10191, checked in by Don-vip, 8 years ago

fix #7302 - Field showing name of object under cursor is often too short. Adapt it dynamically so it always displays at least 20 characters for width of 1280 pixels or less, and at most 80 characters for width of 1920 pixels or more

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Color;
5import java.awt.Dimension;
6import java.awt.GridBagLayout;
7
8import javax.swing.JLabel;
9import javax.swing.JPanel;
10
11import org.openstreetmap.josm.tools.GBC;
12import 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 */
19public class ImageLabel extends JPanel {
20 private final JLabel imgLabel = new JLabel();
21 private final JLabel tf = new JLabel();
22 private 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, GBC.std().anchor(GBC.WEST).insets(0, 1, 1, 0));
35 setIcon(img);
36 add(tf, GBC.std().fill(GBC.BOTH).anchor(GBC.WEST).insets(2, 1, 1, 0));
37 setToolTipText(tooltip);
38 setCharCount(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
67 /**
68 * Returns the preferred char count.
69 * @return the preferred char count
70 * @since 10191
71 */
72 public final int getCharCount() {
73 return charCount;
74 }
75
76 /**
77 * Sets the preferred char count.
78 * @param charCount the preferred char count
79 * @since 10191
80 */
81 public final void setCharCount(int charCount) {
82 this.charCount = charCount;
83 }
84}
Note: See TracBrowser for help on using the repository browser.