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

Last change on this file since 6654 was 6654, checked in by simon04, 10 years ago

fix #9514 fix #9484 fix #9502 - Upload dialog: make source field behave like comment field, provide link "obtain from current layers" to insert current layers in field, display Bing layer as "Bing"

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