source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/HtmlPanel.java@ 12892

Last change on this file since 12892 was 11553, checked in by Don-vip, 7 years ago

refactor handling of null values - use Java 8 Optional where possible

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.BorderLayout;
5import java.awt.Font;
6import java.text.MessageFormat;
7import java.util.Optional;
8
9import javax.swing.JEditorPane;
10import javax.swing.JPanel;
11import javax.swing.UIManager;
12import 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 */
23public 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}
Note: See TracBrowser for help on using the repository browser.