1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.net.URL;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Iterator;
|
---|
11 | import java.util.regex.Pattern;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
18 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
19 |
|
---|
20 | public abstract class AbstractInfoAction extends JosmAction {
|
---|
21 |
|
---|
22 | public AbstractInfoAction() {
|
---|
23 | super();
|
---|
24 | }
|
---|
25 |
|
---|
26 | public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
|
---|
27 | super(name, iconName, tooltip, shortcut, register);
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * replies the base URL for browsing information about about a primitive
|
---|
32 | *
|
---|
33 | * @return the base URL, i.e. http://api.openstreetmap.org/browse
|
---|
34 | */
|
---|
35 | static public String getBaseBrowseUrl() {
|
---|
36 | String baseUrl = Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api");
|
---|
37 | Pattern pattern = Pattern.compile("/api/?$");
|
---|
38 | String ret = pattern.matcher(baseUrl).replaceAll("/browse");
|
---|
39 | if (ret.equals(baseUrl)) {
|
---|
40 | System.out.println(tr("WARNING: unexpected format of API base URL. Redirection to info or history page for OSM primitive will probably fail. API base URL is: ''{0}''",baseUrl));
|
---|
41 | }
|
---|
42 | if (ret.startsWith("http://api.openstreetmap.org/")) {
|
---|
43 | ret = ret.substring("http://api.openstreetmap.org/".length());
|
---|
44 | ret = "http://www.openstreetmap.org/" + ret;
|
---|
45 | }
|
---|
46 | return ret;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * replies the base URL for browsing information about a user
|
---|
51 | *
|
---|
52 | * @return the base URL, i.e. http://ww.openstreetmap.org/user
|
---|
53 | */
|
---|
54 | static public String getBaseUserUrl() {
|
---|
55 | String baseUrl = Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api");
|
---|
56 | Pattern pattern = Pattern.compile("/api/?$");
|
---|
57 | String ret = pattern.matcher(baseUrl).replaceAll("/user");
|
---|
58 | if (ret.equals(baseUrl)) {
|
---|
59 | System.out.println(tr("WARNING: unexpected format of API base URL. Redirection to user page for OSM user will probably fail. API base URL is: ''{0}''",baseUrl));
|
---|
60 | }
|
---|
61 | if (ret.startsWith("http://api.openstreetmap.org/")) {
|
---|
62 | ret = ret.substring("http://api.openstreetmap.org/".length());
|
---|
63 | ret = "http://www.openstreetmap.org/" + ret;
|
---|
64 | }
|
---|
65 | return ret;
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected void launchBrowser(URL url) {
|
---|
69 | OpenBrowser.displayUrl(
|
---|
70 | url.toString()
|
---|
71 | );
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected void launchBrowser(String url) {
|
---|
75 | OpenBrowser.displayUrl(
|
---|
76 | url
|
---|
77 | );
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected boolean confirmLaunchMultiple(int numPrimitives) {
|
---|
81 | String msg = tr(
|
---|
82 | "You''re about to launch {0} browser windows.<br>"
|
---|
83 | + "This may both clutter your screen with browser windows<br>"
|
---|
84 | + "and take some time to finish.", numPrimitives);
|
---|
85 | msg = "<html>" + msg + "</html>";
|
---|
86 | String [] options = new String [] {
|
---|
87 | tr("Continue"),
|
---|
88 | tr("Cancel")
|
---|
89 | };
|
---|
90 | int ret = JOptionPane.showOptionDialog(
|
---|
91 | Main.parent,
|
---|
92 | msg,
|
---|
93 | tr("Warning"),
|
---|
94 | JOptionPane.YES_NO_OPTION,
|
---|
95 | JOptionPane.WARNING_MESSAGE,
|
---|
96 | null,
|
---|
97 | options,
|
---|
98 | options[0]
|
---|
99 | );
|
---|
100 | return ret == JOptionPane.YES_OPTION;
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected void launchInfoBrowsersForSelectedPrimitives() {
|
---|
104 | ArrayList<OsmPrimitive> primitivesToShow = new ArrayList<OsmPrimitive>(getCurrentDataSet().getSelected());
|
---|
105 |
|
---|
106 | // filter out new primitives which are not yet uploaded to the server
|
---|
107 | //
|
---|
108 | Iterator<OsmPrimitive> it = primitivesToShow.iterator();
|
---|
109 | while(it.hasNext()) {
|
---|
110 | if (it.next().isNew()) {
|
---|
111 | it.remove();
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (primitivesToShow.isEmpty()) {
|
---|
116 | JOptionPane.showMessageDialog(
|
---|
117 | Main.parent,
|
---|
118 | tr("Please select at least one already uploaded node, way, or relation."),
|
---|
119 | tr("Warning"),
|
---|
120 | JOptionPane.WARNING_MESSAGE
|
---|
121 | );
|
---|
122 | return;
|
---|
123 | }
|
---|
124 |
|
---|
125 | // don't launch more than 10 browser instances / browser windows
|
---|
126 | //
|
---|
127 | int max = Math.min(10, primitivesToShow.size());
|
---|
128 | if (primitivesToShow.size() > max && ! confirmLaunchMultiple(primitivesToShow.size()))
|
---|
129 | return;
|
---|
130 | for(int i = 0; i < max; i++) {
|
---|
131 | launchBrowser(createInfoUrl(primitivesToShow.get(i)));
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public void actionPerformed(ActionEvent e) {
|
---|
136 | launchInfoBrowsersForSelectedPrimitives();
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected abstract String createInfoUrl(Object infoObject);
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | protected void updateEnabledState() {
|
---|
143 | setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
148 | setEnabled(selection != null && !selection.isEmpty());
|
---|
149 | }
|
---|
150 | }
|
---|