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

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

see #3764 - make UI messages copy-able (patch by simon04)

File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Font;
5import java.io.IOException;
6import java.io.InputStream;
7import java.net.URL;
8import java.net.URLConnection;
9import java.text.MessageFormat;
10
11import javax.swing.JEditorPane;
12import javax.swing.UIManager;
13import javax.swing.text.html.StyleSheet;
14
15import org.openstreetmap.josm.gui.util.GuiHelper;
16import org.openstreetmap.josm.tools.Utils;
17
18/**
19 * Subclass of {@link JEditorPane} that adds a "native" context menu (cut/copy/paste/select all)
20 * and effectively uses JOSM user agent when performing HTTP request in {@link #setPage(URL)} method.
21 * @since 5886
22 */
23public class JosmEditorPane extends JEditorPane {
24
25 /**
26 * Creates a new <code>JosmEditorPane</code>.
27 * The document model is set to <code>null</code>.
28 */
29 public JosmEditorPane() {
30 TextContextualPopupMenu.enableMenuFor(this);
31 }
32
33 /**
34 * Creates a <code>JosmEditorPane</code> based on a specified URL for input.
35 *
36 * @param initialPage the URL
37 * @exception IOException if the URL is <code>null</code> or cannot be accessed
38 */
39 public JosmEditorPane(URL initialPage) throws IOException {
40 this();
41 setPage(initialPage);
42 }
43
44 /**
45 * Creates a <code>JosmEditorPane</code> based on a string containing
46 * a URL specification.
47 *
48 * @param url the URL
49 * @exception IOException if the URL is <code>null</code> or cannot be accessed
50 */
51 public JosmEditorPane(String url) throws IOException {
52 this();
53 setPage(url);
54 }
55
56 /**
57 * Creates a <code>JosmEditorPane</code> that has been initialized
58 * to the given text. This is a convenience constructor that calls the
59 * <code>setContentType</code> and <code>setText</code> methods.
60 *
61 * @param type mime type of the given text
62 * @param text the text to initialize with; may be <code>null</code>
63 * @exception NullPointerException if the <code>type</code> parameter
64 * is <code>null</code>
65 */
66 public JosmEditorPane(String type, String text) {
67 this();
68 setContentType(type);
69 setText(text);
70 }
71
72 @Override
73 protected InputStream getStream(URL page) throws IOException {
74 URLConnection conn = Utils.setupURLConnection(page.openConnection());
75 InputStream result = conn.getInputStream();
76 String type = conn.getContentType();
77 if (type != null) {
78 setContentType(type);
79 }
80 return result;
81 }
82
83 /**
84 * Adapts an {@link JEditorPane} to be used as a powerful replacement of {@link javax.swing.JLabel}.
85 */
86 public static void makeJLabelLike(JEditorPane pane, boolean allBold) {
87 pane.setContentType("text/html");
88 pane.setOpaque(false);
89 pane.setEditable(false);
90
91 JosmHTMLEditorKit kit = new JosmHTMLEditorKit();
92 final Font f = UIManager.getFont("Label.font");
93 final StyleSheet ss = new StyleSheet();
94 ss.addRule((allBold ? "html" : "strong, b") + " {" + getFontRule(f) + "}");
95 ss.addRule("a {text-decoration: underline; color: blue}");
96 ss.addRule("h1 {" + getFontRule(GuiHelper.getTitleFont()) + "}");
97 kit.setStyleSheet(ss);
98 pane.setEditorKit(kit);
99 }
100
101 private static String getFontRule(Font f) {
102 return MessageFormat.format(
103 "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
104 f.getName(),
105 f.getSize(),
106 "bold",
107 f.isItalic() ? "italic" : "normal"
108 );
109 }
110}
Note: See TracBrowser for help on using the repository browser.