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