001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.widgets;
003
004import java.awt.Dimension;
005import java.awt.Rectangle;
006
007import javax.swing.JLabel;
008import javax.swing.plaf.basic.BasicHTML;
009import javax.swing.text.View;
010
011/**
012 * Creates a normal label that will wrap its contents if there less width than
013 * required to print it in one line. Additionally the maximum width of the text
014 * can be set using <code>setMaxWidth</code>.
015 *
016 * Note that this won't work if JMultilineLabel is put into a JScrollBox or
017 * similar as the bounds will never change. Instead scrollbars will be displayed.
018 * 
019 * @since 6340
020 */
021public class JMultilineLabel extends JLabel {
022    private int maxWidth = Integer.MAX_VALUE;
023    private Rectangle oldbounds = null;
024    private Dimension oldPreferred = null;
025
026    /**
027     * Constructs a normal label but adds HTML tags if not already done so.
028     * Supports both newline characters (<code>\n</code>) as well as the HTML
029     * <code>&lt;br&gt;</code> to insert new lines.
030     *
031     * Use setMaxWidth to limit the width of the label.
032     * @param text The text to display
033     */
034    public JMultilineLabel(String text) {
035        super();
036        String html = text.trim().replaceAll("\n", "<br>");
037        if (!html.startsWith("<html>")) {
038            html = "<html>" + html + "</html>";
039        }
040        super.setText(html);
041    }
042
043    /**
044     * Set the maximum width. Use this method instead of setMaximumSize because
045     * this saves a little bit of overhead and is actually taken into account.
046     *
047     * @param width
048     */
049    public void setMaxWidth(int width) {
050        this.maxWidth = width;
051    }
052
053    /**
054     * Tries to determine a suitable height for the given contents and return
055     * that dimension.
056     */
057    @Override
058    public Dimension getPreferredSize() {
059        // Without this check it will result in an infinite loop calling
060        // getPreferredSize. Remember the old bounds and only recalculate if
061        // the size actually changed.
062        if (this.getBounds().equals(oldbounds) && oldPreferred != null) {
063            return oldPreferred;
064        }
065        oldbounds = this.getBounds();
066
067        Dimension superPreferred = super.getPreferredSize();
068        // Make it not larger than required
069        int width = Math.min(superPreferred.width, maxWidth);
070
071        // Calculate suitable width and height
072        final View v = (View) super.getClientProperty(BasicHTML.propertyKey);
073
074        if (v == null) {
075            return superPreferred;
076        }
077
078        v.setSize(width, 0);
079        int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS));
080        int h = (int) Math.ceil(v.getPreferredSpan(View.Y_AXIS));
081
082        oldPreferred = new Dimension(w, h);
083        return oldPreferred;
084    }
085}