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.List;
|
---|
12 | import java.util.regex.Pattern;
|
---|
13 |
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
19 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
20 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
21 | import org.openstreetmap.josm.io.OsmApi;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
25 |
|
---|
26 | public abstract class AbstractInfoAction extends JosmAction {
|
---|
27 |
|
---|
28 | public AbstractInfoAction(boolean installAdapters) {
|
---|
29 | super(installAdapters);
|
---|
30 | }
|
---|
31 |
|
---|
32 | public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
|
---|
33 | super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * replies the base URL for browsing information about about a primitive
|
---|
38 | *
|
---|
39 | * @return the base URL, i.e. http://api.openstreetmap.org/browse
|
---|
40 | */
|
---|
41 | static public String getBaseBrowseUrl() {
|
---|
42 | String baseUrl = Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL);
|
---|
43 | Pattern pattern = Pattern.compile("/api/?$");
|
---|
44 | String ret = pattern.matcher(baseUrl).replaceAll("/browse");
|
---|
45 | if (ret.equals(baseUrl)) {
|
---|
46 | Main.warn(tr("Unexpected format of API base URL. Redirection to info or history page for OSM object will probably fail. API base URL is: ''{0}''",baseUrl));
|
---|
47 | }
|
---|
48 | if (ret.startsWith("http://api.openstreetmap.org/")) {
|
---|
49 | ret = ret.substring("http://api.openstreetmap.org/".length());
|
---|
50 | ret = "http://www.openstreetmap.org/" + ret;
|
---|
51 | }
|
---|
52 | return ret;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * replies the base URL for browsing information about a user
|
---|
57 | *
|
---|
58 | * @return the base URL, i.e. http://www.openstreetmap.org/user
|
---|
59 | */
|
---|
60 | static public String getBaseUserUrl() {
|
---|
61 | String baseUrl = Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL);
|
---|
62 | Pattern pattern = Pattern.compile("/api/?$");
|
---|
63 | String ret = pattern.matcher(baseUrl).replaceAll("/user");
|
---|
64 | if (ret.equals(baseUrl)) {
|
---|
65 | Main.warn(tr("Unexpected format of API base URL. Redirection to user page for OSM user will probably fail. API base URL is: ''{0}''",baseUrl));
|
---|
66 | }
|
---|
67 | if (ret.startsWith("http://api.openstreetmap.org/")) {
|
---|
68 | ret = ret.substring("http://api.openstreetmap.org/".length());
|
---|
69 | ret = "http://www.openstreetmap.org/" + ret;
|
---|
70 | }
|
---|
71 | return ret;
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected void launchBrowser(URL url) {
|
---|
75 | OpenBrowser.displayUrl(
|
---|
76 | url.toString()
|
---|
77 | );
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected void launchBrowser(String url) {
|
---|
81 | OpenBrowser.displayUrl(
|
---|
82 | url
|
---|
83 | );
|
---|
84 | }
|
---|
85 |
|
---|
86 | public static boolean confirmLaunchMultiple(int numBrowsers) {
|
---|
87 | String msg = tr(
|
---|
88 | "You are about to launch {0} browser windows.<br>"
|
---|
89 | + "This may both clutter your screen with browser windows<br>"
|
---|
90 | + "and take some time to finish.", numBrowsers);
|
---|
91 | msg = "<html>" + msg + "</html>";
|
---|
92 | ButtonSpec[] spec = new ButtonSpec[] {
|
---|
93 | new ButtonSpec(
|
---|
94 | tr("Continue"),
|
---|
95 | ImageProvider.get("ok"),
|
---|
96 | tr("Click to continue and to open {0} browsers", numBrowsers),
|
---|
97 | null // no specific help topic
|
---|
98 | ),
|
---|
99 | new ButtonSpec(
|
---|
100 | tr("Cancel"),
|
---|
101 | ImageProvider.get("cancel"),
|
---|
102 | tr("Click to abort launching external browsers"),
|
---|
103 | null // no specific help topic
|
---|
104 | )
|
---|
105 | };
|
---|
106 | int ret = HelpAwareOptionPane.showOptionDialog(
|
---|
107 | Main.parent,
|
---|
108 | msg,
|
---|
109 | tr("Warning"),
|
---|
110 | JOptionPane.WARNING_MESSAGE,
|
---|
111 | null,
|
---|
112 | spec,
|
---|
113 | spec[0],
|
---|
114 | HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
|
---|
115 | );
|
---|
116 | return ret == 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected void launchInfoBrowsersForSelectedPrimitives() {
|
---|
120 | List<OsmPrimitive> primitivesToShow = new ArrayList<OsmPrimitive>(getCurrentDataSet().getAllSelected());
|
---|
121 |
|
---|
122 | // filter out new primitives which are not yet uploaded to the server
|
---|
123 | //
|
---|
124 | Iterator<OsmPrimitive> it = primitivesToShow.iterator();
|
---|
125 | while(it.hasNext()) {
|
---|
126 | if (it.next().isNew()) {
|
---|
127 | it.remove();
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (primitivesToShow.isEmpty()) {
|
---|
132 | JOptionPane.showMessageDialog(
|
---|
133 | Main.parent,
|
---|
134 | tr("Please select at least one already uploaded node, way, or relation."),
|
---|
135 | tr("Warning"),
|
---|
136 | JOptionPane.WARNING_MESSAGE
|
---|
137 | );
|
---|
138 | return;
|
---|
139 | }
|
---|
140 |
|
---|
141 | // don't launch more than 10 browser instances / browser windows
|
---|
142 | //
|
---|
143 | int max = Math.min(10, primitivesToShow.size());
|
---|
144 | if (primitivesToShow.size() > max && ! confirmLaunchMultiple(primitivesToShow.size()))
|
---|
145 | return;
|
---|
146 | for(int i = 0; i < max; i++) {
|
---|
147 | launchBrowser(createInfoUrl(primitivesToShow.get(i)));
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Override
|
---|
152 | public void actionPerformed(ActionEvent e) {
|
---|
153 | launchInfoBrowsersForSelectedPrimitives();
|
---|
154 | }
|
---|
155 |
|
---|
156 | protected abstract String createInfoUrl(Object infoObject);
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | protected void updateEnabledState() {
|
---|
160 | setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
165 | setEnabled(selection != null && !selection.isEmpty());
|
---|
166 | }
|
---|
167 | }
|
---|