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 |
|
---|
8 | import javax.swing.JEditorPane;
|
---|
9 | import javax.swing.JPanel;
|
---|
10 | import javax.swing.UIManager;
|
---|
11 | import javax.swing.text.html.HTMLEditorKit;
|
---|
12 | import javax.swing.text.html.StyleSheet;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * This panel can be used to display larger larger sections of formatted text in
|
---|
16 | * HTML.
|
---|
17 | *
|
---|
18 | * It displays HTML text in the same font as {@see 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 | *
|
---|
22 | */
|
---|
23 | public class HtmlPanel extends JPanel {
|
---|
24 | private JEditorPane jepMessage;
|
---|
25 |
|
---|
26 | protected void build() {
|
---|
27 | setLayout(new BorderLayout());
|
---|
28 | jepMessage = new JEditorPane("text/html", "");
|
---|
29 | jepMessage.setOpaque(false);
|
---|
30 | jepMessage.setEditable(false);
|
---|
31 | Font f = UIManager.getFont("Label.font");
|
---|
32 | StyleSheet ss = new StyleSheet();
|
---|
33 | String rule = 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 | rule = "body {" + rule + "}";
|
---|
41 | rule = MessageFormat.format(
|
---|
42 | "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
|
---|
43 | f.getName(),
|
---|
44 | f.getSize(),
|
---|
45 | "bold",
|
---|
46 | f.isItalic() ? "italic" : "normal"
|
---|
47 | );
|
---|
48 | rule = "strong {" + rule + "}";
|
---|
49 | ss.addRule(rule);
|
---|
50 | ss.addRule("a {text-decoration: underline; color: blue}");
|
---|
51 | ss.addRule("ul {margin-left: 1cm; list-style-type: disc}");
|
---|
52 | HTMLEditorKit kit = new HTMLEditorKit();
|
---|
53 | kit.setStyleSheet(ss);
|
---|
54 | jepMessage.setEditorKit(kit);
|
---|
55 |
|
---|
56 | add(jepMessage, BorderLayout.CENTER);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public HtmlPanel() {
|
---|
60 | build();
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Replies the editor pane used internally to render the HTML text.
|
---|
65 | *
|
---|
66 | * @return the editor pane used internally to render the HTML text.
|
---|
67 | */
|
---|
68 | public JEditorPane getEditorPane() {
|
---|
69 | return jepMessage;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Sets the current text to display. <code>text</code> is a html fragment.
|
---|
74 | * If null, empty string is assumed.
|
---|
75 | *
|
---|
76 | * @param text the text to display
|
---|
77 | */
|
---|
78 | public void setText(String text) {
|
---|
79 | if (text == null) {
|
---|
80 | text = "";
|
---|
81 | }
|
---|
82 | jepMessage.setText(text);
|
---|
83 | }
|
---|
84 | }
|
---|