1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Font;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.net.URL;
|
---|
9 | import java.text.MessageFormat;
|
---|
10 |
|
---|
11 | import javax.swing.JEditorPane;
|
---|
12 | import javax.swing.LookAndFeel;
|
---|
13 | import javax.swing.UIDefaults;
|
---|
14 | import javax.swing.UIManager;
|
---|
15 | import javax.swing.text.html.StyleSheet;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
18 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
19 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
20 | import 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 | */
|
---|
27 | public 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 | }
|
---|