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

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

fix font problems with Khmer. Only tested on Java 8/Windows 7 right now.

File size: 5.2 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.net.URLConnection;
10import java.text.MessageFormat;
11
12import javax.swing.JEditorPane;
13import javax.swing.LookAndFeel;
14import javax.swing.UIDefaults;
15import javax.swing.UIManager;
16import javax.swing.text.html.StyleSheet;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20import org.openstreetmap.josm.tools.Utils;
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 {
28
29 /**
30 * Creates a new <code>JosmEditorPane</code>.
31 * The document model is set to <code>null</code>.
32 */
33 public JosmEditorPane() {
34 TextContextualPopupMenu.enableMenuFor(this);
35 }
36
37 /**
38 * Creates a <code>JosmEditorPane</code> based on a specified URL for input.
39 *
40 * @param initialPage the URL
41 * @exception IOException if the URL is <code>null</code> or cannot be accessed
42 */
43 public JosmEditorPane(URL initialPage) throws IOException {
44 this();
45 setPage(initialPage);
46 }
47
48 /**
49 * Creates a <code>JosmEditorPane</code> based on a string containing
50 * a URL specification.
51 *
52 * @param url the URL
53 * @exception IOException if the URL is <code>null</code> or cannot be accessed
54 */
55 public JosmEditorPane(String url) throws IOException {
56 this();
57 setPage(url);
58 }
59
60 /**
61 * Creates a <code>JosmEditorPane</code> that has been initialized
62 * to the given text. This is a convenience constructor that calls the
63 * <code>setContentType</code> and <code>setText</code> methods.
64 *
65 * @param type mime type of the given text
66 * @param text the text to initialize with; may be <code>null</code>
67 * @exception NullPointerException if the <code>type</code> parameter
68 * is <code>null</code>
69 */
70 public JosmEditorPane(String type, String text) {
71 this();
72 setContentType(type);
73 setText(text);
74 }
75
76 @Override
77 protected InputStream getStream(URL page) throws IOException {
78 URLConnection conn = Utils.setupURLConnection(page.openConnection());
79 InputStream result = conn.getInputStream();
80 String type = conn.getContentType();
81 if (type != null) {
82 setContentType(type);
83 }
84 return result;
85 }
86
87 /**
88 * Adapts a {@link JEditorPane} to be used as a powerful replacement of {@link javax.swing.JLabel}.
89 * @param pane The editor pane to adapt
90 * @param allBold If {@code true}, makes all text to be displayed in bold
91 */
92 public static void makeJLabelLike(JEditorPane pane, boolean allBold) {
93 pane.setContentType("text/html");
94 pane.setOpaque(false);
95 pane.setEditable(false);
96 adaptForNimbus(pane);
97
98 JosmHTMLEditorKit kit = new JosmHTMLEditorKit();
99 final Font f = UIManager.getFont("Label.font");
100 final StyleSheet ss = new StyleSheet();
101 ss.addRule((allBold ? "html" : "strong, b") + " {" + getFontRule(f) + "}");
102 ss.addRule("a {text-decoration: underline; color: blue}");
103 ss.addRule("h1 {" + getFontRule(GuiHelper.getTitleFont()) + "}");
104 ss.addRule("ol {margin-left: 1cm; margin-top: 0.1cm; margin-bottom: 0.2cm; list-style-type: decimal}");
105 ss.addRule("ul {margin-left: 1cm; margin-top: 0.1cm; margin-bottom: 0.2cm; list-style-type: disc}");
106 if ("km".equals(Main.pref.get("language"))) {
107 // Fix rendering problem for Khmer script
108 ss.addRule("p {" + getFontRule(UIManager.getFont("Label.font")) + "}");
109 }
110 kit.setStyleSheet(ss);
111 pane.setEditorKit(kit);
112 }
113
114 /**
115 * Adapts a {@link JEditorPane} for Nimbus look and feel.
116 * See <a href="https://stackoverflow.com/q/15228336/2257172">this StackOverflow question</a>.
117 * @param pane The editor pane to adapt
118 * @since 6935
119 */
120 public static void adaptForNimbus(JEditorPane pane) {
121 LookAndFeel currentLAF = UIManager.getLookAndFeel();
122 if (currentLAF != null && "Nimbus".equals(currentLAF.getName())) {
123 Color bgColor = UIManager.getColor("Label.background");
124 UIDefaults defaults = new UIDefaults();
125 defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
126 pane.putClientProperty("Nimbus.Overrides", defaults);
127 pane.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
128 pane.setBackground(bgColor);
129 }
130 }
131
132 private static String getFontRule(Font f) {
133 return MessageFormat.format(
134 "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
135 f.getName(),
136 f.getSize(),
137 "bold",
138 f.isItalic() ? "italic" : "normal"
139 );
140 }
141}
Note: See TracBrowser for help on using the repository browser.