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.JEditorPane;
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 JEditorPane {
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        this(text, false);
036    }
037
038    /**
039     * Constructs a normal label but adds HTML tags if not already done so.
040     * Supports both newline characters (<code>\n</code>) as well as the HTML
041     * <code>&lt;br&gt;</code> to insert new lines.
042     *
043     * Use setMaxWidth to limit the width of the label.
044     * @param text The text to display
045     * @param allBold If {@code true}, makes all text to be displayed in bold
046     */
047    public JMultilineLabel(String text, boolean allBold) {
048        JosmEditorPane.makeJLabelLike(this, allBold);
049        String html = text.trim().replaceAll("\n", "<br>");
050        if (!html.startsWith("<html>")) {
051            html = "<html>" + html + "</html>";
052        }
053        super.setText(html);
054    }
055
056    /**
057     * Set the maximum width. Use this method instead of setMaximumSize because
058     * this saves a little bit of overhead and is actually taken into account.
059     *
060     * @param width the maximum width
061     */
062    public void setMaxWidth(int width) {
063        this.maxWidth = width;
064    }
065
066    /**
067     * Tries to determine a suitable height for the given contents and return
068     * that dimension.
069     */
070    @Override
071    public Dimension getPreferredSize() {
072        // Without this check it will result in an infinite loop calling
073        // getPreferredSize. Remember the old bounds and only recalculate if
074        // the size actually changed.
075        if (this.getBounds().equals(oldbounds) && oldPreferred != null) {
076            return oldPreferred;
077        }
078        oldbounds = this.getBounds();
079
080        Dimension superPreferred = super.getPreferredSize();
081        // Make it not larger than required
082        int width = Math.min(superPreferred.width, maxWidth);
083
084        // Calculate suitable width and height
085        final View v = (View) super.getClientProperty(BasicHTML.propertyKey);
086
087        if (v == null) {
088            return superPreferred;
089        }
090
091        v.setSize(width, 0);
092        int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS));
093        int h = (int) Math.ceil(v.getPreferredSpan(View.Y_AXIS));
094
095        oldPreferred = new Dimension(w, h);
096        return oldPreferred;
097    }
098}