001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.widgets;
003
004import java.awt.BorderLayout;
005import java.awt.Font;
006import java.text.MessageFormat;
007
008import javax.swing.JEditorPane;
009import javax.swing.JPanel;
010import javax.swing.UIManager;
011import javax.swing.text.html.StyleSheet;
012
013/**
014 * This panel can be used to display larger sections of formatted text in
015 * HTML.
016 *
017 * It displays HTML text in the same font as {@link javax.swing.JLabel}. Hyperlinks are rendered in
018 * blue and they are underlined. There is also a CSS rule for the HTML tag <strong>
019 * configured.
020 * @since 2688
021 */
022public class HtmlPanel extends JPanel {
023    private JosmEditorPane jepMessage;
024
025    protected final void build() {
026        setLayout(new BorderLayout());
027        jepMessage = new JosmEditorPane("text/html", "");
028        jepMessage.setOpaque(false);
029        jepMessage.setEditable(false);
030        Font f = UIManager.getFont("Label.font");
031        StyleSheet ss = new StyleSheet();
032        String rule = MessageFormat.format(
033                "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
034                f.getName(),
035                f.getSize(),
036                f.isBold() ? "bold" : "normal",
037                        f.isItalic() ? "italic" : "normal"
038        );
039        rule = "body {" + rule + '}';
040        rule = MessageFormat.format(
041                "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
042                f.getName(),
043                f.getSize(),
044                "bold",
045                f.isItalic() ? "italic" : "normal"
046        );
047        rule = "strong {" + rule + '}';
048        ss.addRule(rule);
049        ss.addRule("a {text-decoration: underline; color: blue}");
050        ss.addRule("ul {margin-left: 1cm; list-style-type: disc}");
051        JosmHTMLEditorKit kit = new JosmHTMLEditorKit();
052        kit.setStyleSheet(ss);
053        jepMessage.setEditorKit(kit);
054
055        add(jepMessage, BorderLayout.CENTER);
056    }
057
058    /**
059     * Constructs a new {@code HtmlPanel}.
060     */
061    public HtmlPanel() {
062        build();
063    }
064
065    /**
066     * Constructs a new {@code HtmlPanel} with the given HTML text.
067     * @param text the text to display
068     */
069    public HtmlPanel(String text) {
070        this();
071        setText(text);
072    }
073
074    /**
075     * Replies the editor pane used internally to render the HTML text.
076     *
077     * @return the editor pane used internally to render the HTML text.
078     */
079    public JEditorPane getEditorPane() {
080        return jepMessage;
081    }
082
083    /**
084     * Sets the current text to display. <code>text</code> is a html fragment.
085     * If null, empty string is assumed.
086     *
087     * @param text the text to display
088     */
089    public final void setText(String text) {
090        if (text == null) {
091            text = "";
092        }
093        jepMessage.setText(text);
094    }
095}