source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/JosmEditorPane.java@ 14463

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

fix #17040 - fix memory leaks when calling history dialog

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Color;
5import java.awt.Font;
6import java.io.IOException;
7import java.io.InputStream;
8import java.net.URL;
9import java.text.MessageFormat;
10
11import javax.swing.JEditorPane;
12import javax.swing.LookAndFeel;
13import javax.swing.UIDefaults;
14import javax.swing.UIManager;
15import javax.swing.text.html.StyleSheet;
16
17import org.openstreetmap.josm.gui.util.GuiHelper;
18import org.openstreetmap.josm.tools.Destroyable;
19import org.openstreetmap.josm.tools.HttpClient;
20import org.openstreetmap.josm.tools.LanguageInfo;
21
22/**
23 * Subclass of {@link JEditorPane} that adds a "native" context menu (cut/copy/paste/select all)
24 * and effectively uses JOSM user agent when performing HTTP request in {@link #setPage(URL)} method.
25 * @since 5886
26 */
27public class JosmEditorPane extends JEditorPane implements Destroyable {
28
29 private final PopupMenuLauncher launcher;
30
31 /**
32 * Creates a new <code>JosmEditorPane</code>.
33 * The document model is set to <code>null</code>.
34 */
35 public JosmEditorPane() {
36 launcher = TextContextualPopupMenu.enableMenuFor(this, true);
37 }
38
39 /**
40 * Creates a <code>JosmEditorPane</code> based on a specified URL for input.
41 *
42 * @param initialPage the URL
43 * @throws IOException if the URL is <code>null</code> or cannot be accessed
44 */
45 public JosmEditorPane(URL initialPage) throws IOException {
46 this();
47 setPage(initialPage);
48 }
49
50 /**
51 * Creates a <code>JosmEditorPane</code> based on a string containing
52 * a URL specification.
53 *
54 * @param url the URL
55 * @throws IOException if the URL is <code>null</code> or cannot be accessed
56 */
57 public JosmEditorPane(String url) throws IOException {
58 this();
59 setPage(url);
60 }
61
62 /**
63 * Creates a <code>JosmEditorPane</code> that has been initialized
64 * to the given text. This is a convenience constructor that calls the
65 * <code>setContentType</code> and <code>setText</code> methods.
66 *
67 * @param type mime type of the given text
68 * @param text the text to initialize with; may be <code>null</code>
69 * @throws NullPointerException if the <code>type</code> parameter
70 * is <code>null</code>
71 */
72 public JosmEditorPane(String type, String text) {
73 this();
74 setContentType(type);
75 setText(text);
76 }
77
78 @Override
79 protected InputStream getStream(URL page) throws IOException {
80 final HttpClient.Response conn = HttpClient.create(page).connect();
81 String type = conn.getContentType();
82 if (type != null) {
83 setContentType(type);
84 }
85 return conn.getContent();
86 }
87
88 /**
89 * Adapts a {@link JEditorPane} to be used as a powerful replacement of {@link javax.swing.JLabel}.
90 * @param pane The editor pane to adapt
91 * @param allBold If {@code true}, makes all text to be displayed in bold
92 */
93 public static void makeJLabelLike(JEditorPane pane, boolean allBold) {
94 pane.setContentType("text/html");
95 pane.setOpaque(false);
96 pane.setEditable(false);
97 adaptForNimbus(pane);
98
99 JosmHTMLEditorKit kit = new JosmHTMLEditorKit();
100 final Font f = UIManager.getFont("Label.font");
101 final StyleSheet ss = new StyleSheet();
102 ss.addRule((allBold ? "html" : "strong, b") + " {" + getFontRule(f) + '}');
103 ss.addRule("a {text-decoration: underline; color: blue}");
104 ss.addRule("h1 {" + getFontRule(GuiHelper.getTitleFont()) + '}');
105 ss.addRule("ol {margin-left: 1cm; margin-top: 0.1cm; margin-bottom: 0.2cm; list-style-type: decimal}");
106 ss.addRule("ul {margin-left: 1cm; margin-top: 0.1cm; margin-bottom: 0.2cm; list-style-type: disc}");
107 if ("km".equals(LanguageInfo.getJOSMLocaleCode())) {
108 // Fix rendering problem for Khmer script
109 ss.addRule("p {" + getFontRule(UIManager.getFont("Label.font")) + '}');
110 }
111 kit.setStyleSheet(ss);
112 pane.setEditorKit(kit);
113 }
114
115 /**
116 * Adapts a {@link JEditorPane} for Nimbus look and feel.
117 * See <a href="https://stackoverflow.com/q/15228336/2257172">this StackOverflow question</a>.
118 * @param pane The editor pane to adapt
119 * @since 6935
120 */
121 public static void adaptForNimbus(JEditorPane pane) {
122 LookAndFeel currentLAF = UIManager.getLookAndFeel();
123 if (currentLAF != null && "Nimbus".equals(currentLAF.getName())) {
124 Color bgColor = UIManager.getColor("Label.background");
125 UIDefaults defaults = new UIDefaults();
126 defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
127 pane.putClientProperty("Nimbus.Overrides", defaults);
128 pane.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
129 pane.setBackground(bgColor);
130 }
131 }
132
133 private static String getFontRule(Font f) {
134 return MessageFormat.format(
135 "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
136 f.getName(),
137 f.getSize(),
138 "bold",
139 f.isItalic() ? "italic" : "normal"
140 );
141 }
142
143 @Override
144 public void destroy() {
145 TextContextualPopupMenu.disableMenuFor(this, launcher);
146 }
147}
Note: See TracBrowser for help on using the repository browser.