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;
024    private Dimension oldPreferred;
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        setFocusable(false);
054        super.setText(html);
055    }
056
057    /**
058     * Set the maximum width. Use this method instead of setMaximumSize because
059     * this saves a little bit of overhead and is actually taken into account.
060     *
061     * @param width the maximum width
062     */
063    public void setMaxWidth(int width) {
064        this.maxWidth = width;
065    }
066
067    /**
068     * Tries to determine a suitable height for the given contents and return
069     * that dimension.
070     */
071    @Override
072    public Dimension getPreferredSize() {
073        // Without this check it will result in an infinite loop calling
074        // getPreferredSize. Remember the old bounds and only recalculate if
075        // the size actually changed.
076        if (this.getBounds().equals(oldbounds) && oldPreferred != null) {
077            return oldPreferred;
078        }
079        oldbounds = this.getBounds();
080
081        Dimension superPreferred = super.getPreferredSize();
082        // Make it not larger than required
083        int width = Math.min(superPreferred.width, maxWidth);
084
085        // Calculate suitable width and height
086        final View v = (View) super.getClientProperty(BasicHTML.propertyKey);
087
088        if (v == null) {
089            return superPreferred;
090        }
091
092        v.setSize(width, 0);
093        int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS));
094        int h = (int) Math.ceil(v.getPreferredSpan(View.Y_AXIS));
095
096        oldPreferred = new Dimension(w, h);
097        return oldPreferred;
098    }
099}