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

Last change on this file since 4936 was 3461, checked in by bastiK, 14 years ago

added gui preference for autosave; fixed #5359 - Button to delete autosaved data

  • Property svn:eol-style set to native
File size: 2.7 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 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 public HtmlPanel(String text) {
64 this();
65 setText(text);
66 }
67
68 /**
69 * Replies the editor pane used internally to render the HTML text.
70 *
71 * @return the editor pane used internally to render the HTML text.
72 */
73 public JEditorPane getEditorPane() {
74 return jepMessage;
75 }
76
77 /**
78 * Sets the current text to display. <code>text</code> is a html fragment.
79 * If null, empty string is assumed.
80 *
81 * @param text the text to display
82 */
83 public void setText(String text) {
84 if (text == null) {
85 text = "";
86 }
87 jepMessage.setText(text);
88 }
89}
Note: See TracBrowser for help on using the repository browser.