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

Last change on this file since 13661 was 13111, checked in by Don-vip, 6 years ago

fix #11217, fix #12623 - major rework of notes tooltips:

  • display clickable links
  • allow to copy text from notes, including comments
  • Property svn:eol-style set to native
File size: 3.4 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.Arrays;
8import java.util.Optional;
9
10import javax.swing.JEditorPane;
11import javax.swing.JPanel;
12import javax.swing.UIManager;
13import javax.swing.event.HyperlinkEvent;
14import javax.swing.event.HyperlinkListener;
15import javax.swing.text.html.StyleSheet;
16
17import org.openstreetmap.josm.tools.OpenBrowser;
18
19/**
20 * This panel can be used to display larger sections of formatted text in
21 * HTML.
22 *
23 * It displays HTML text in the same font as {@link javax.swing.JLabel}. Hyperlinks are rendered in
24 * blue and they are underlined. There is also a CSS rule for the HTML tag <strong>
25 * configured.
26 * @since 2688
27 */
28public class HtmlPanel extends JPanel {
29
30 private static final HyperlinkListener defaultHyperlinkListener = e -> {
31 if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
32 OpenBrowser.displayUrl(e.getURL().toString());
33 }
34 };
35
36 private JosmEditorPane jepMessage;
37
38 protected final void build() {
39 setLayout(new BorderLayout());
40 jepMessage = new JosmEditorPane("text/html", "");
41 jepMessage.setOpaque(false);
42 jepMessage.setEditable(false);
43 Font f = UIManager.getFont("Label.font");
44 StyleSheet ss = new StyleSheet();
45 ss.addRule("body {" + MessageFormat.format(
46 "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
47 f.getName(),
48 f.getSize(),
49 f.isBold() ? "bold" : "normal",
50 f.isItalic() ? "italic" : "normal"
51 ) + '}');
52 ss.addRule("strong {" + MessageFormat.format(
53 "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
54 f.getName(),
55 f.getSize(),
56 "bold",
57 f.isItalic() ? "italic" : "normal"
58 ) + '}');
59 ss.addRule("a {text-decoration: underline; color: blue}");
60 ss.addRule("ul {margin-left: 1cm; list-style-type: disc}");
61 JosmHTMLEditorKit kit = new JosmHTMLEditorKit();
62 kit.setStyleSheet(ss);
63 jepMessage.setEditorKit(kit);
64
65 add(jepMessage, BorderLayout.CENTER);
66 }
67
68 /**
69 * Constructs a new {@code HtmlPanel}.
70 */
71 public HtmlPanel() {
72 build();
73 }
74
75 /**
76 * Constructs a new {@code HtmlPanel} with the given HTML text.
77 * @param text the text to display
78 */
79 public HtmlPanel(String text) {
80 this();
81 setText(text);
82 }
83
84 /**
85 * Replies the editor pane used internally to render the HTML text.
86 *
87 * @return the editor pane used internally to render the HTML text.
88 */
89 public JEditorPane getEditorPane() {
90 return jepMessage;
91 }
92
93 /**
94 * Sets the current text to display. <code>text</code> is a html fragment.
95 * If null, empty string is assumed.
96 *
97 * @param text the text to display
98 */
99 public final void setText(String text) {
100 jepMessage.setText(Optional.ofNullable(text).orElse(""));
101 }
102
103 /**
104 * Opens hyperlinks on click.
105 * @since 13111
106 */
107 public final void enableClickableHyperlinks() {
108 if (!Arrays.asList(jepMessage.getHyperlinkListeners()).contains(defaultHyperlinkListener)) {
109 jepMessage.addHyperlinkListener(defaultHyperlinkListener);
110 }
111 }
112}
Note: See TracBrowser for help on using the repository browser.