source: josm/trunk/src/org/openstreetmap/josm/gui/JMultilineLabel.java @ 6267

Last change on this file since 6267 was 6267, checked in by Don-vip, 5 years ago

Sonar/FindBugs - Replace singular fields by local variables

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2
3package org.openstreetmap.josm.gui;
4
5import java.awt.Dimension;
6import java.awt.Rectangle;
7
8import javax.swing.JLabel;
9import javax.swing.plaf.basic.BasicHTML;
10import javax.swing.text.View;
11
12/**
13 * Creates a normal label that will wrap its contents if there less width than
14 * required to print it in one line. Additionally the maximum width of the text
15 * can be set using <code>setMaxWidth</code>.
16 *
17 * Note that this won't work if JMultilineLabel is put into a JScrollBox or
18 * similar as the bounds will never change. Instead scrollbars will be displayed.
19 */
20public class JMultilineLabel extends JLabel {
21    private int maxWidth = Integer.MAX_VALUE;
22    private Rectangle oldbounds = null;
23    private Dimension oldPreferred = null;
24
25    /**
26     * Constructs a normal label but adds HTML tags if not already done so.
27     * Supports both newline characters (<code>\n</code>) as well as the HTML
28     * <code>&lt;br&gt;</code> to insert new lines.
29     *
30     * Use setMaxWidth to limit the width of the label.
31     * @param text
32     */
33    public JMultilineLabel(String text)
34    {
35        super();
36        text = text.trim().replaceAll("\n", "<br>");
37        if(!text.startsWith("<html>")) {
38            text = "<html>" + text + "</html>";
39        }
40        super.setText(text);
41    }
42
43    /**
44     * Set the maximum width. Use this method instead of setMaximumSize because
45     * this saves a little bit of overhead and is actually taken into account.
46     *
47     * @param width
48     */
49    public void setMaxWidth(int width) {
50        this.maxWidth = width;
51    }
52
53    /**
54     * Tries to determine a suitable height for the given contents and return
55     * that dimension.
56     */
57    @Override
58    public Dimension getPreferredSize() {
59        // Without this check it will result in an infinite loop calling
60        // getPreferredSize. Remember the old bounds and only recalculate if
61        // the size actually changed.
62        if (this.getBounds().equals(oldbounds) && oldPreferred != null) {
63            return oldPreferred;
64        }
65        oldbounds = this.getBounds();
66
67        Dimension superPreferred = super.getPreferredSize();
68        // Make it not larger than required
69        int width = Math.min(superPreferred.width, maxWidth);
70
71        // Calculate suitable width and height
72        final View v = (View) super.getClientProperty(BasicHTML.propertyKey);
73
74        if (v == null) {
75            return superPreferred;
76        }
77
78        v.setSize(width, 0);
79        int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS));
80        int h = (int) Math.ceil(v.getPreferredSpan(View.Y_AXIS));
81
82        oldPreferred = new Dimension(w, h);
83        return oldPreferred;
84    }
85}
Note: See TracBrowser for help on using the repository browser.