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

Last change on this file since 2711 was 2711, checked in by stoecker, 14 years ago

fix bad line endings

File size: 2.6 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.HTMLEditorKit;
12import 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 */
23public 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}
Note: See TracBrowser for help on using the repository browser.