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

Last change on this file since 6992 was 6935, checked in by Don-vip, 10 years ago

see #3764 - special handling for Nimbus

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.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.gui.util.GuiHelper;
19import org.openstreetmap.josm.tools.Utils;
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);
34 }
35
36 /**
37 * Creates a <code>JosmEditorPane</code> based on a specified URL for input.
38 *
39 * @param initialPage the URL
40 * @exception 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 * @exception 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 * @exception 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 URLConnection conn = Utils.setupURLConnection(page.openConnection());
78 InputStream result = conn.getInputStream();
79 String type = conn.getContentType();
80 if (type != null) {
81 setContentType(type);
82 }
83 return result;
84 }
85
86 /**
87 * Adapts a {@link JEditorPane} to be used as a powerful replacement of {@link javax.swing.JLabel}.
88 * @param pane The editor pane to adapt
89 * @param allBold If {@code true}, makes all text to be displayed in bold
90 */
91 public static void makeJLabelLike(JEditorPane pane, boolean allBold) {
92 pane.setContentType("text/html");
93 pane.setOpaque(false);
94 pane.setEditable(false);
95 adaptForNimbus(pane);
96
97 JosmHTMLEditorKit kit = new JosmHTMLEditorKit();
98 final Font f = UIManager.getFont("Label.font");
99 final StyleSheet ss = new StyleSheet();
100 ss.addRule((allBold ? "html" : "strong, b") + " {" + getFontRule(f) + "}");
101 ss.addRule("a {text-decoration: underline; color: blue}");
102 ss.addRule("h1 {" + getFontRule(GuiHelper.getTitleFont()) + "}");
103 ss.addRule("ol {margin-left: 1cm; margin-top: 0.1cm; margin-bottom: 0.2cm; list-style-type: decimal}");
104 ss.addRule("ul {margin-left: 1cm; margin-top: 0.1cm; margin-bottom: 0.2cm; list-style-type: disc}");
105 kit.setStyleSheet(ss);
106 pane.setEditorKit(kit);
107 }
108
109 /**
110 * Adapts a {@link JEditorPane} for Nimbus look and feel.
111 * See <a href="https://stackoverflow.com/q/15228336/2257172">this StackOverflow question</a>.
112 * @param pane The editor pane to adapt
113 * @since 6935
114 */
115 public static void adaptForNimbus(JEditorPane pane) {
116 LookAndFeel currentLAF = UIManager.getLookAndFeel();
117 if (currentLAF != null && "Nimbus".equals(currentLAF.getName())) {
118 Color bgColor = UIManager.getColor("Label.background");
119 UIDefaults defaults = new UIDefaults();
120 defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
121 pane.putClientProperty("Nimbus.Overrides", defaults);
122 pane.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
123 pane.setBackground(bgColor);
124 }
125 }
126
127 private static String getFontRule(Font f) {
128 return MessageFormat.format(
129 "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
130 f.getName(),
131 f.getSize(),
132 "bold",
133 f.isItalic() ? "italic" : "normal"
134 );
135 }
136}
Note: See TracBrowser for help on using the repository browser.