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.util.ArrayList;
|
---|
8 | import java.util.Iterator;
|
---|
9 | import java.util.regex.Pattern;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
16 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
17 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
18 |
|
---|
19 | public abstract class AbstractInfoAction extends JosmAction {
|
---|
20 |
|
---|
21 | public AbstractInfoAction() {
|
---|
22 | super();
|
---|
23 | }
|
---|
24 |
|
---|
25 | public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
|
---|
26 | super(name, iconName, tooltip, shortcut, register);
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * replies the base URL for browsing information about about a primitive
|
---|
31 | *
|
---|
32 | * @return the base URL, i.e. http://api.openstreetmap.org/browse
|
---|
33 | */
|
---|
34 | protected String getBaseURL() {
|
---|
35 | String baseUrl = Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api");
|
---|
36 | Pattern pattern = Pattern.compile("/api/?$");
|
---|
37 | String ret = pattern.matcher(baseUrl).replaceAll("/browse");
|
---|
38 | if (ret.equals(baseUrl)) {
|
---|
39 | System.out.println(tr("WARNING: unexpected format of API base URL. Redirection to history page for OSM primitive will probably fail. API base URL is: ''{0}''",baseUrl));
|
---|
40 | }
|
---|
41 | if (ret.startsWith("http://api.openstreetmap.org/")) {
|
---|
42 | ret = ret.substring("http://api.openstreetmap.org/".length());
|
---|
43 | ret = "http://www.openstreetmap.org/" + ret;
|
---|
44 | }
|
---|
45 | return ret;
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected void launchBrowser() {
|
---|
49 | ArrayList<OsmPrimitive> primitivesToShow = new ArrayList<OsmPrimitive>(getCurrentDataSet().getSelected());
|
---|
50 |
|
---|
51 | // filter out new primitives which are not yet uploaded to the server
|
---|
52 | //
|
---|
53 | Iterator<OsmPrimitive> it = primitivesToShow.iterator();
|
---|
54 | while(it.hasNext()) {
|
---|
55 | if (it.next().id == 0) {
|
---|
56 | it.remove();
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (primitivesToShow.isEmpty()) {
|
---|
61 | OptionPaneUtil.showMessageDialog(
|
---|
62 | Main.parent,
|
---|
63 | tr("Please select at least one already uploaded node, way, or relation."),
|
---|
64 | tr("Warning"),
|
---|
65 | JOptionPane.WARNING_MESSAGE
|
---|
66 | );
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | // don't launch more than 10 browser instances / browser windows
|
---|
71 | //
|
---|
72 | int max = Math.min(10, primitivesToShow.size());
|
---|
73 | if (max < primitivesToShow.size()) {
|
---|
74 | System.out.println(tr("WARNING: launching browser windows for the first {0} of {1} selected primitives only", 10, primitivesToShow.size()));
|
---|
75 | }
|
---|
76 | for(int i = 0; i < max; i++) {
|
---|
77 | OpenBrowser.displayUrl(
|
---|
78 | createInfoUrl(primitivesToShow.get(i))
|
---|
79 | );
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void actionPerformed(ActionEvent e) {
|
---|
84 | launchBrowser();
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected abstract String createInfoUrl(OsmPrimitive primitive);
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | protected void updateEnabledState() {
|
---|
91 | setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
|
---|
92 | }
|
---|
93 | }
|
---|