1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.awt.Font;
|
---|
6 | import java.text.MessageFormat;
|
---|
7 | import java.util.Optional;
|
---|
8 |
|
---|
9 | import javax.swing.JEditorPane;
|
---|
10 | import javax.swing.JPanel;
|
---|
11 | import javax.swing.UIManager;
|
---|
12 | import javax.swing.text.html.StyleSheet;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * This panel can be used to display larger sections of formatted text in
|
---|
16 | * HTML.
|
---|
17 | *
|
---|
18 | * It displays HTML text in the same font as {@link javax.swing.JLabel}. Hyperlinks are rendered in
|
---|
19 | * blue and they are underlined. There is also a CSS rule for the HTML tag <strong>
|
---|
20 | * configured.
|
---|
21 | * @since 2688
|
---|
22 | */
|
---|
23 | public class HtmlPanel extends JPanel {
|
---|
24 | private JosmEditorPane jepMessage;
|
---|
25 |
|
---|
26 | protected final void build() {
|
---|
27 | setLayout(new BorderLayout());
|
---|
28 | jepMessage = new JosmEditorPane("text/html", "");
|
---|
29 | jepMessage.setOpaque(false);
|
---|
30 | jepMessage.setEditable(false);
|
---|
31 | Font f = UIManager.getFont("Label.font");
|
---|
32 | StyleSheet ss = new StyleSheet();
|
---|
33 | ss.addRule("body {" + MessageFormat.format(
|
---|
34 | "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
|
---|
35 | f.getName(),
|
---|
36 | f.getSize(),
|
---|
37 | f.isBold() ? "bold" : "normal",
|
---|
38 | f.isItalic() ? "italic" : "normal"
|
---|
39 | ) + '}');
|
---|
40 | ss.addRule("strong {" + MessageFormat.format(
|
---|
41 | "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
|
---|
42 | f.getName(),
|
---|
43 | f.getSize(),
|
---|
44 | "bold",
|
---|
45 | f.isItalic() ? "italic" : "normal"
|
---|
46 | ) + '}');
|
---|
47 | ss.addRule("a {text-decoration: underline; color: blue}");
|
---|
48 | ss.addRule("ul {margin-left: 1cm; list-style-type: disc}");
|
---|
49 | JosmHTMLEditorKit kit = new JosmHTMLEditorKit();
|
---|
50 | kit.setStyleSheet(ss);
|
---|
51 | jepMessage.setEditorKit(kit);
|
---|
52 |
|
---|
53 | add(jepMessage, BorderLayout.CENTER);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Constructs a new {@code HtmlPanel}.
|
---|
58 | */
|
---|
59 | public HtmlPanel() {
|
---|
60 | build();
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Constructs a new {@code HtmlPanel} with the given HTML text.
|
---|
65 | * @param text the text to display
|
---|
66 | */
|
---|
67 | public HtmlPanel(String text) {
|
---|
68 | this();
|
---|
69 | setText(text);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Replies the editor pane used internally to render the HTML text.
|
---|
74 | *
|
---|
75 | * @return the editor pane used internally to render the HTML text.
|
---|
76 | */
|
---|
77 | public JEditorPane getEditorPane() {
|
---|
78 | return jepMessage;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Sets the current text to display. <code>text</code> is a html fragment.
|
---|
83 | * If null, empty string is assumed.
|
---|
84 | *
|
---|
85 | * @param text the text to display
|
---|
86 | */
|
---|
87 | public final void setText(String text) {
|
---|
88 | jepMessage.setText(Optional.ofNullable(text).orElse(""));
|
---|
89 | }
|
---|
90 | }
|
---|