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.Arrays;
|
---|
8 | import java.util.Optional;
|
---|
9 |
|
---|
10 | import javax.swing.JEditorPane;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 | import javax.swing.UIManager;
|
---|
13 | import javax.swing.event.HyperlinkEvent;
|
---|
14 | import javax.swing.event.HyperlinkListener;
|
---|
15 | import javax.swing.text.html.StyleSheet;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This panel can be used to display larger sections of formatted text in
|
---|
21 | * HTML.
|
---|
22 | *
|
---|
23 | * It displays HTML text in the same font as {@link javax.swing.JLabel}. Hyperlinks are rendered in
|
---|
24 | * blue and they are underlined. There is also a CSS rule for the HTML tag <strong>
|
---|
25 | * configured.
|
---|
26 | * @since 2688
|
---|
27 | */
|
---|
28 | public class HtmlPanel extends JPanel {
|
---|
29 |
|
---|
30 | private static final HyperlinkListener defaultHyperlinkListener = e -> {
|
---|
31 | if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType()) && e.getURL() != null) {
|
---|
32 | OpenBrowser.displayUrl(e.getURL().toString());
|
---|
33 | }
|
---|
34 | };
|
---|
35 |
|
---|
36 | private JosmEditorPane jepMessage;
|
---|
37 |
|
---|
38 | protected final void build() {
|
---|
39 | setLayout(new BorderLayout());
|
---|
40 | jepMessage = new JosmEditorPane("text/html", "");
|
---|
41 | jepMessage.setOpaque(false);
|
---|
42 | jepMessage.setEditable(false);
|
---|
43 | Font f = UIManager.getFont("Label.font");
|
---|
44 | StyleSheet ss = new StyleSheet();
|
---|
45 | ss.addRule("body {" + MessageFormat.format(
|
---|
46 | "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
|
---|
47 | f.getName(),
|
---|
48 | f.getSize(),
|
---|
49 | f.isBold() ? "bold" : "normal",
|
---|
50 | f.isItalic() ? "italic" : "normal"
|
---|
51 | ) + '}');
|
---|
52 | ss.addRule("strong {" + MessageFormat.format(
|
---|
53 | "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
|
---|
54 | f.getName(),
|
---|
55 | f.getSize(),
|
---|
56 | "bold",
|
---|
57 | f.isItalic() ? "italic" : "normal"
|
---|
58 | ) + '}');
|
---|
59 | ss.addRule("a {text-decoration: underline; color: blue}");
|
---|
60 | ss.addRule("ul {margin-left: 1cm; list-style-type: disc}");
|
---|
61 | JosmHTMLEditorKit kit = new JosmHTMLEditorKit();
|
---|
62 | kit.setStyleSheet(ss);
|
---|
63 | jepMessage.setEditorKit(kit);
|
---|
64 |
|
---|
65 | add(jepMessage, BorderLayout.CENTER);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Constructs a new {@code HtmlPanel}.
|
---|
70 | */
|
---|
71 | public HtmlPanel() {
|
---|
72 | build();
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Constructs a new {@code HtmlPanel} with the given HTML text.
|
---|
77 | * @param text the text to display
|
---|
78 | */
|
---|
79 | public HtmlPanel(String text) {
|
---|
80 | this();
|
---|
81 | setText(text);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Replies the editor pane used internally to render the HTML text.
|
---|
86 | *
|
---|
87 | * @return the editor pane used internally to render the HTML text.
|
---|
88 | */
|
---|
89 | public JEditorPane getEditorPane() {
|
---|
90 | return jepMessage;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Sets the current text to display. <code>text</code> is a html fragment.
|
---|
95 | * If null, empty string is assumed.
|
---|
96 | *
|
---|
97 | * @param text the text to display
|
---|
98 | */
|
---|
99 | public final void setText(String text) {
|
---|
100 | jepMessage.setText(Optional.ofNullable(text).orElse(""));
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Opens hyperlinks on click.
|
---|
105 | * @since 13111
|
---|
106 | */
|
---|
107 | public final void enableClickableHyperlinks() {
|
---|
108 | if (!Arrays.asList(jepMessage.getHyperlinkListeners()).contains(defaultHyperlinkListener)) {
|
---|
109 | jepMessage.addHyperlinkListener(defaultHyperlinkListener);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|