1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.net.URLConnection;
|
---|
8 |
|
---|
9 | import javax.swing.JEditorPane;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.tools.Utils;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Subclass of {@link JEditorPane} that adds a "native" context menu (cut/copy/paste/select all)
|
---|
15 | * and effectively uses JOSM user agent when performing HTTP request in {@link #setPage(URL)} method.
|
---|
16 | * @since 5886
|
---|
17 | */
|
---|
18 | public class JosmEditorPane extends JEditorPane {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Creates a new <code>JosmEditorPane</code>.
|
---|
22 | * The document model is set to <code>null</code>.
|
---|
23 | */
|
---|
24 | public JosmEditorPane() {
|
---|
25 | TextContextualPopupMenu.enableMenuFor(this);
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Creates a <code>JosmEditorPane</code> based on a specified URL for input.
|
---|
30 | *
|
---|
31 | * @param initialPage the URL
|
---|
32 | * @exception IOException if the URL is <code>null</code> or cannot be accessed
|
---|
33 | */
|
---|
34 | public JosmEditorPane(URL initialPage) throws IOException {
|
---|
35 | this();
|
---|
36 | setPage(initialPage);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Creates a <code>JosmEditorPane</code> based on a string containing
|
---|
41 | * a URL specification.
|
---|
42 | *
|
---|
43 | * @param url the URL
|
---|
44 | * @exception IOException if the URL is <code>null</code> or cannot be accessed
|
---|
45 | */
|
---|
46 | public JosmEditorPane(String url) throws IOException {
|
---|
47 | this();
|
---|
48 | setPage(url);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Creates a <code>JosmEditorPane</code> that has been initialized
|
---|
53 | * to the given text. This is a convenience constructor that calls the
|
---|
54 | * <code>setContentType</code> and <code>setText</code> methods.
|
---|
55 | *
|
---|
56 | * @param type mime type of the given text
|
---|
57 | * @param text the text to initialize with; may be <code>null</code>
|
---|
58 | * @exception NullPointerException if the <code>type</code> parameter
|
---|
59 | * is <code>null</code>
|
---|
60 | */
|
---|
61 | public JosmEditorPane(String type, String text) {
|
---|
62 | this();
|
---|
63 | setContentType(type);
|
---|
64 | setText(text);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | protected InputStream getStream(URL page) throws IOException {
|
---|
69 | URLConnection conn = Utils.setupURLConnection(page.openConnection());
|
---|
70 | InputStream result = conn.getInputStream();
|
---|
71 | String type = conn.getContentType();
|
---|
72 | if (type != null) {
|
---|
73 | setContentType(type);
|
---|
74 | }
|
---|
75 | return result;
|
---|
76 | }
|
---|
77 | }
|
---|