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

Last change on this file since 6040 was 6040, checked in by Don-vip, 11 years ago

fix #8827 - HTML rendering differ if run before or after having launched Help Browser

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