[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[3715] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[7813] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[3715] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.GridBagConstraints;
|
---|
| 8 | import java.awt.GridBagLayout;
|
---|
| 9 | import java.awt.event.ActionEvent;
|
---|
| 10 | import java.awt.event.KeyEvent;
|
---|
| 11 | import java.util.ArrayList;
|
---|
[6316] | 12 | import java.util.List;
|
---|
[3715] | 13 | import java.util.regex.Matcher;
|
---|
| 14 | import java.util.regex.Pattern;
|
---|
| 15 |
|
---|
| 16 | import javax.swing.ButtonGroup;
|
---|
| 17 | import javax.swing.JLabel;
|
---|
| 18 | import javax.swing.JOptionPane;
|
---|
| 19 | import javax.swing.JPanel;
|
---|
| 20 | import javax.swing.JRadioButton;
|
---|
| 21 |
|
---|
| 22 | import org.openstreetmap.josm.Main;
|
---|
| 23 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
| 24 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
| 25 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
---|
[7859] | 26 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
| 27 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
[3715] | 28 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[4380] | 30 | import org.openstreetmap.josm.tools.Utils;
|
---|
[3715] | 31 |
|
---|
[7859] | 32 | /**
|
---|
| 33 | * Download rectified images from various services.
|
---|
| 34 | * @since 3715
|
---|
| 35 | */
|
---|
[6246] | 36 | public class MapRectifierWMSmenuAction extends JosmAction {
|
---|
[7859] | 37 |
|
---|
[3715] | 38 | /**
|
---|
| 39 | * Class that bundles all required information of a rectifier service
|
---|
| 40 | */
|
---|
| 41 | public static class RectifierService {
|
---|
| 42 | private final String name;
|
---|
| 43 | private final String url;
|
---|
| 44 | private final String wmsUrl;
|
---|
| 45 | private final Pattern urlRegEx;
|
---|
| 46 | private final Pattern idValidator;
|
---|
[7859] | 47 | private JRadioButton btn;
|
---|
[6069] | 48 |
|
---|
[3715] | 49 | /**
|
---|
[5562] | 50 | * @param name Name of the rectifing service
|
---|
| 51 | * @param url URL to the service where users can register, upload, etc.
|
---|
| 52 | * @param wmsUrl URL to the WMS server where JOSM will grab the images. Insert __s__ where the ID should be placed
|
---|
| 53 | * @param urlRegEx a regular expression that determines if a given URL is one of the service and returns the WMS id if so
|
---|
| 54 | * @param idValidator regular expression that checks if a given ID is syntactically valid
|
---|
[3715] | 55 | */
|
---|
| 56 | public RectifierService(String name, String url, String wmsUrl, String urlRegEx, String idValidator) {
|
---|
| 57 | this.name = name;
|
---|
| 58 | this.url = url;
|
---|
| 59 | this.wmsUrl = wmsUrl;
|
---|
| 60 | this.urlRegEx = Pattern.compile(urlRegEx);
|
---|
| 61 | this.idValidator = Pattern.compile(idValidator);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[7859] | 64 | private boolean isSelected() {
|
---|
[3715] | 65 | return btn.isSelected();
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
[7859] | 70 | * List of available rectifier services.
|
---|
[3715] | 71 | */
|
---|
[8308] | 72 | private final transient List<RectifierService> services = new ArrayList<>();
|
---|
[3715] | 73 |
|
---|
[7859] | 74 | /**
|
---|
| 75 | * Constructs a new {@code MapRectifierWMSmenuAction}.
|
---|
| 76 | */
|
---|
[6246] | 77 | public MapRectifierWMSmenuAction() {
|
---|
[3715] | 78 | super(tr("Rectified Image..."),
|
---|
| 79 | "OLmarker",
|
---|
| 80 | tr("Download Rectified Images From Various Services"),
|
---|
[4973] | 81 | Shortcut.registerShortcut("imagery:rectimg",
|
---|
| 82 | tr("Imagery: {0}", tr("Rectified Image...")),
|
---|
| 83 | KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
|
---|
[4851] | 84 | true
|
---|
[3715] | 85 | );
|
---|
[7813] | 86 | putValue("help", ht("/Menu/Imagery"));
|
---|
[3715] | 87 |
|
---|
| 88 | // Add default services
|
---|
| 89 | services.add(
|
---|
| 90 | new RectifierService("Metacarta Map Rectifier",
|
---|
| 91 | "http://labs.metacarta.com/rectifier/",
|
---|
| 92 | "http://labs.metacarta.com/rectifier/wms.cgi?id=__s__&srs=EPSG:4326"
|
---|
| 93 | + "&Service=WMS&Version=1.1.0&Request=GetMap&format=image/png&",
|
---|
| 94 | // This matches more than the "classic" WMS link, so users can pretty much
|
---|
| 95 | // copy any link as long as it includes the ID
|
---|
| 96 | "labs\\.metacarta\\.com/(?:.*?)(?:/|=)([0-9]+)(?:\\?|/|\\.|$)",
|
---|
| 97 | "^[0-9]+$")
|
---|
| 98 | );
|
---|
| 99 | services.add(
|
---|
[5562] | 100 | new RectifierService("Map Warper",
|
---|
| 101 | "http://mapwarper.net/",
|
---|
| 102 | "http://mapwarper.net/maps/wms/__s__?request=GetMap&version=1.1.1"
|
---|
[3715] | 103 | + "&styles=&format=image/png&srs=epsg:4326&exceptions=application/vnd.ogc.se_inimage&",
|
---|
| 104 | // This matches more than the "classic" WMS link, so users can pretty much
|
---|
| 105 | // copy any link as long as it includes the ID
|
---|
| 106 | "(?:mapwarper\\.net|warper\\.geothings\\.net)/(?:.*?)/([0-9]+)(?:\\?|/|\\.|$)",
|
---|
| 107 | "^[0-9]+$")
|
---|
| 108 | );
|
---|
| 109 |
|
---|
| 110 | // This service serves the purpose of "just this once" without forcing the user
|
---|
| 111 | // to commit the link to the preferences
|
---|
| 112 |
|
---|
| 113 | // Clipboard content gets trimmed, so matching whitespace only ensures that this
|
---|
| 114 | // service will never be selected automatically.
|
---|
| 115 | services.add(new RectifierService(tr("Custom WMS Link"), "", "", "^\\s+$", ""));
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | @Override
|
---|
| 119 | public void actionPerformed(ActionEvent e) {
|
---|
[3733] | 120 | if (!isEnabled()) return;
|
---|
[3715] | 121 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
| 122 | panel.add(new JLabel(tr("Supported Rectifier Services:")), GBC.eol());
|
---|
| 123 |
|
---|
[5886] | 124 | JosmTextField tfWmsUrl = new JosmTextField(30);
|
---|
[3715] | 125 |
|
---|
[4380] | 126 | String clip = Utils.getClipboardContent();
|
---|
| 127 | clip = clip == null ? "" : clip.trim();
|
---|
[3715] | 128 | ButtonGroup group = new ButtonGroup();
|
---|
| 129 |
|
---|
| 130 | JRadioButton firstBtn = null;
|
---|
[8510] | 131 | for (RectifierService s : services) {
|
---|
[3715] | 132 | JRadioButton serviceBtn = new JRadioButton(s.name);
|
---|
[8510] | 133 | if (firstBtn == null) {
|
---|
[3715] | 134 | firstBtn = serviceBtn;
|
---|
[3733] | 135 | }
|
---|
[3715] | 136 | // Checks clipboard contents against current service if no match has been found yet.
|
---|
| 137 | // If the contents match, they will be inserted into the text field and the corresponding
|
---|
| 138 | // service will be pre-selected.
|
---|
[8510] | 139 | if (!clip.isEmpty() && tfWmsUrl.getText().isEmpty()
|
---|
[3715] | 140 | && (s.urlRegEx.matcher(clip).find() || s.idValidator.matcher(clip).matches())) {
|
---|
| 141 | serviceBtn.setSelected(true);
|
---|
| 142 | tfWmsUrl.setText(clip);
|
---|
| 143 | }
|
---|
| 144 | s.btn = serviceBtn;
|
---|
| 145 | group.add(serviceBtn);
|
---|
[8510] | 146 | if (!s.url.isEmpty()) {
|
---|
[3715] | 147 | panel.add(serviceBtn, GBC.std());
|
---|
[5567] | 148 | panel.add(new UrlLabel(s.url, tr("Visit Homepage")), GBC.eol().anchor(GridBagConstraints.EAST));
|
---|
[3733] | 149 | } else {
|
---|
[3715] | 150 | panel.add(serviceBtn, GBC.eol().anchor(GridBagConstraints.WEST));
|
---|
[3733] | 151 | }
|
---|
[3715] | 152 | }
|
---|
| 153 |
|
---|
| 154 | // Fallback in case no match was found
|
---|
[8510] | 155 | if (tfWmsUrl.getText().isEmpty() && firstBtn != null) {
|
---|
[3715] | 156 | firstBtn.setSelected(true);
|
---|
[3733] | 157 | }
|
---|
[3715] | 158 |
|
---|
| 159 | panel.add(new JLabel(tr("WMS URL or Image ID:")), GBC.eol());
|
---|
| 160 | panel.add(tfWmsUrl, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
---|
| 161 |
|
---|
| 162 | ExtendedDialog diag = new ExtendedDialog(Main.parent,
|
---|
| 163 | tr("Add Rectified Image"),
|
---|
| 164 |
|
---|
| 165 | new String[] {tr("Add Rectified Image"), tr("Cancel")});
|
---|
| 166 | diag.setContent(panel);
|
---|
[8061] | 167 | diag.setButtonIcons(new String[] {"OLmarker", "cancel"});
|
---|
[3715] | 168 |
|
---|
| 169 | // This repeatedly shows the dialog in case there has been an error.
|
---|
| 170 | // The loop is break;-ed if the users cancels
|
---|
[8510] | 171 | outer: while (true) {
|
---|
[3715] | 172 | diag.showDialog();
|
---|
| 173 | int answer = diag.getValue();
|
---|
| 174 | // Break loop when the user cancels
|
---|
[8510] | 175 | if (answer != 1) {
|
---|
[3715] | 176 | break;
|
---|
[3733] | 177 | }
|
---|
[3715] | 178 |
|
---|
| 179 | String text = tfWmsUrl.getText().trim();
|
---|
| 180 | // Loop all services until we find the selected one
|
---|
[8510] | 181 | for (RectifierService s : services) {
|
---|
| 182 | if (!s.isSelected()) {
|
---|
[3715] | 183 | continue;
|
---|
[3733] | 184 | }
|
---|
[3715] | 185 |
|
---|
| 186 | // We've reached the custom WMS URL service
|
---|
| 187 | // Just set the URL and hope everything works out
|
---|
[8510] | 188 | if (s.wmsUrl.isEmpty()) {
|
---|
[8068] | 189 | try {
|
---|
[8846] | 190 | addWMSLayer(s.name + " (" + text + ')', text);
|
---|
[8068] | 191 | break outer;
|
---|
| 192 | } catch (IllegalStateException ex) {
|
---|
| 193 | Main.error(ex.getMessage());
|
---|
| 194 | }
|
---|
[3715] | 195 | }
|
---|
| 196 |
|
---|
| 197 | // First try to match if the entered string as an URL
|
---|
| 198 | Matcher m = s.urlRegEx.matcher(text);
|
---|
[8510] | 199 | if (m.find()) {
|
---|
[3715] | 200 | String id = m.group(1);
|
---|
| 201 | String newURL = s.wmsUrl.replaceAll("__s__", id);
|
---|
[8846] | 202 | String title = s.name + " (" + id + ')';
|
---|
[3715] | 203 | addWMSLayer(title, newURL);
|
---|
| 204 | break outer;
|
---|
| 205 | }
|
---|
| 206 | // If not, look if it's a valid ID for the selected service
|
---|
[8510] | 207 | if (s.idValidator.matcher(text).matches()) {
|
---|
[3715] | 208 | String newURL = s.wmsUrl.replaceAll("__s__", text);
|
---|
[8846] | 209 | String title = s.name + " (" + text + ')';
|
---|
[3715] | 210 | addWMSLayer(title, newURL);
|
---|
| 211 | break outer;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | // We've found the selected service, but the entered string isn't suitable for
|
---|
| 215 | // it. So quit checking the other radio buttons
|
---|
| 216 | break;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[8855] | 219 | // and display an error message. The while loop ensures that the dialog pops up again
|
---|
[3715] | 220 | JOptionPane.showMessageDialog(Main.parent,
|
---|
[4253] | 221 | tr("Couldn''t match the entered link or id to the selected service. Please try again."),
|
---|
[3715] | 222 | tr("No valid WMS URL or id"),
|
---|
| 223 | JOptionPane.ERROR_MESSAGE);
|
---|
| 224 | diag.setVisible(true);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * Adds a WMS Layer with given title and URL
|
---|
[5562] | 230 | * @param title Name of the layer as it will shop up in the layer manager
|
---|
| 231 | * @param url URL to the WMS server
|
---|
[8068] | 232 | * @throws IllegalStateException if imagery time is neither HTML nor WMS
|
---|
[3715] | 233 | */
|
---|
[8870] | 234 | private static void addWMSLayer(String title, String url) {
|
---|
[8068] | 235 | WMSLayer layer = new WMSLayer(new ImageryInfo(title, url));
|
---|
| 236 | Main.main.addLayer(layer);
|
---|
[3715] | 237 | }
|
---|
| 238 |
|
---|
[3733] | 239 | @Override
|
---|
| 240 | protected void updateEnabledState() {
|
---|
[5460] | 241 | setEnabled(Main.isDisplayingMapView() && !Main.map.mapView.getAllLayers().isEmpty());
|
---|
[3733] | 242 | }
|
---|
[3715] | 243 | }
|
---|