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