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

Last change on this file since 13146 was 9175, checked in by simon04, 8 years ago

see #12231 - Use HttpClient instead of remaining Utils.setupURLConnection

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