Changeset 15827 in osm for applications/editors


Ignore:
Timestamp:
2009-06-10T16:27:18+02:00 (15 years ago)
Author:
stoecker
Message:

fix #2713 - patch by xeen - Improve WMS Rectified Image-dialog

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Map_Rectifier_WMSmenuAction.java

    r13497 r15827  
    44
    55import java.awt.event.ActionEvent;
     6import java.awt.event.KeyEvent;
     7import java.awt.datatransfer.DataFlavor;
     8import java.awt.datatransfer.Transferable;
     9import java.awt.GridBagLayout;
     10import java.awt.Toolkit;
     11
     12import java.util.ArrayList;
     13import java.util.regex.Matcher;
     14import java.util.regex.Pattern;
     15
     16import javax.swing.ButtonGroup;
     17import javax.swing.JLabel;
    618import javax.swing.JOptionPane;
    7 
     19import javax.swing.JPanel;
     20import javax.swing.JRadioButton;
     21import javax.swing.JTextField;
     22
     23import org.openstreetmap.josm.actions.JosmAction;
     24import org.openstreetmap.josm.gui.ExtendedDialog;
     25import org.openstreetmap.josm.gui.MapView;
    826import org.openstreetmap.josm.Main;
    9 import org.openstreetmap.josm.actions.JosmAction;
    10 import org.openstreetmap.josm.gui.MapView;
     27import org.openstreetmap.josm.tools.GBC;
     28import org.openstreetmap.josm.tools.Shortcut;
     29import org.openstreetmap.josm.tools.UrlLabel;
    1130
    1231public class Map_Rectifier_WMSmenuAction extends JosmAction {
    13 
    14     /**
    15      * tim waters "chippy"
     32    /**
     33     * Class that bundles all required information of a rectifier service
    1634     */
    17     private static final long serialVersionUID = 1L;
     35    public class rectifierService {
     36        private String name;
     37        private String url;
     38        private String wmsUrl;
     39        private Pattern urlRegEx;
     40        private Pattern idValidator;
     41        public JRadioButton btn;
     42        /**
     43          * @param name: Name of the rectifing service
     44          * @param url: URL to the service where users can register, upload, etc.
     45          * @param wmsUrl: URL to the WMS server where JOSM will grab the images. Insert __s__ where the ID should be placed
     46          * @param urlRegEx: a regular expression that determines if a given URL is one of the service and returns the WMS id if so
     47          * @param idValidator: regular expression that checks if a given ID is syntactically valid
     48          */
     49        public rectifierService(String name, String url, String wmsUrl, String urlRegEx, String idValidator) {
     50            this.name = name;
     51            this.url = url;
     52            this.wmsUrl = wmsUrl;
     53            this.urlRegEx = Pattern.compile(urlRegEx);
     54            this.idValidator = Pattern.compile(idValidator);
     55        }
     56
     57        public boolean isSelected() {
     58            return btn.isSelected();
     59        }
     60    }
     61
     62    /**
     63     * List of available rectifier services. May be extended from the outside
     64    */
     65    public ArrayList<rectifierService> services = new ArrayList<rectifierService>();
    1866
    1967    public Map_Rectifier_WMSmenuAction() {
    20         super(tr("Rectified Image..."), "OLmarker", tr("Download Rectified Image from Metacarta's Map Rectifier WMS"), null, false);
     68        super(tr("Rectified Image..."),
     69            "OLmarker",
     70            tr("Download Rectified Images From Various Services"),
     71            Shortcut.registerShortcut("wms:rectimg",
     72                tr("WMS: {0}", tr("Rectified Image...")),
     73                KeyEvent.VK_R,
     74                Shortcut.GROUP_NONE),
     75            true
     76        );
     77
     78        // Add default services
     79        services.add(
     80            new rectifierService("Metacarta Map Rectifier",
     81                "http://labs.metacarta.com/rectifier/",
     82                "http://labs.metacarta.com/rectifier/wms.cgi?id=__s__&srs=EPSG:4326"
     83                    + "&Service=WMS&Version=1.1.0&Request=GetMap&format=image/png",
     84                // This matches more than the "classic" WMS link, so users can pretty much
     85                // copy any link as long as it includes the ID
     86                "labs\\.metacarta\\.com/(?:.*?)(?:/|=)([0-9]+)(?:\\?|/|\\.|$)",
     87                "^[0-9]+$")
     88        );
     89        services.add(
     90            // TODO: Change all links to mapwarper.net once the project has moved.
     91            // The RegEx already matches the new URL and old URLs will be forwarded
     92            // to make the transition as smooth as possible for the users
     93            new rectifierService("Geothings Map Warper",
     94                "http://warper.geothings.net/",
     95                "http://warper.geothings.net/maps/wms/__s__?request=GetMap&version=1.1.1"
     96                    + "&styles=&format=image/png&srs=epsg:4326&exceptions=application/vnd.ogc.se_inimage",
     97                // This matches more than the "classic" WMS link, so users can pretty much
     98                // copy any link as long as it includes the ID
     99                "(?:mapwarper\\.net|warper\\.geothings\\.net/(?:.*?)/([0-9]+)(?:\\?|/|\\.|$)",
     100                "^[0-9]+$")
     101        );
     102
     103        // This service serves the purpose of "just this once" without forcing the user
     104        // to commit the link to the preferences
     105
     106        // Clipboard content gets trimmed, so matching whitespace only ensures that this
     107        // service will never be selected automatically.
     108        services.add(new rectifierService(tr("Custom WMS Link"), "", "", "^\\s+$", ""));
    21109    }
    22110
    23111    public void actionPerformed(ActionEvent e) {
    24         String newid = JOptionPane.showInputDialog(Main.parent, tr("Metacarta Map Rectifier image id"),
    25         Main.pref.get("wmsplugin.rectifier_id"));
    26 
    27         if (newid != null && !newid.equals("")) {
    28             String newURL = "http://labs.metacarta.com/rectifier/wms.cgi?id="+newid+
    29             "&srs=EPSG:4326&Service=WMS&Version=1.1.0&Request=GetMap&format=image/png";
    30 
    31             Main.pref.put("wmsplugin.rectifier_id", newid);
    32             WMSLayer wmsLayer = new WMSLayer(tr("rectifier id={0}",newid), newURL);
    33             Main.main.addLayer(wmsLayer);
    34         }
     112        JPanel panel = new JPanel(new GridBagLayout());
     113        panel.add(new JLabel(tr("Supported Rectifier Services:")), GBC.eol());
     114
     115        JTextField tfWmsUrl = new JTextField(30);
     116
     117        String clip = getClipboardContents();
     118        ButtonGroup group = new ButtonGroup();
     119
     120        JRadioButton firstBtn = null;
     121        for(rectifierService s : services) {
     122            JRadioButton serviceBtn = new JRadioButton(s.name);
     123            if(firstBtn == null)
     124                firstBtn = serviceBtn;
     125            // Checks clipboard contents against current service if no match has been found yet.
     126            // If the contents match, they will be inserted into the text field and the corresponding
     127            // service will be pre-selected.
     128            if(!clip.equals("") && tfWmsUrl.getText().equals("")
     129                && (s.urlRegEx.matcher(clip).find() || s.idValidator.matcher(clip).matches())) {
     130                serviceBtn.setSelected(true);
     131                tfWmsUrl.setText(clip);
     132            }
     133            s.btn = serviceBtn;
     134            group.add(serviceBtn);
     135            if(!s.url.equals("")) {
     136                panel.add(serviceBtn, GBC.std());
     137                panel.add(new UrlLabel(s.url, tr("Visit Homepage")), GBC.eol().anchor(GBC.EAST));
     138            } else
     139                panel.add(serviceBtn, GBC.eol().anchor(GBC.WEST));
     140        }
     141
     142        // Fallback in case no match was found
     143        if(tfWmsUrl.getText().equals("") && firstBtn != null)
     144            firstBtn.setSelected(true);
     145
     146        panel.add(new JLabel(tr("WMS URL or Image ID:")), GBC.eol());
     147        panel.add(tfWmsUrl, GBC.eol().fill(GBC.HORIZONTAL));
     148
     149        ExtendedDialog diag = new ExtendedDialog(Main.parent,
     150                            tr("Add Rectified Image"),
     151                            panel,
     152                            new String[] {tr("Add Rectified Image"), tr("Cancel")},
     153                            new String[] {"OLmarker.png", "cancel.png"});
     154
     155        // This repeatedly shows the dialog in case there has been an error.
     156        // The loop is break;-ed if the users cancels
     157        outer: while(true) {
     158            int answer = diag.getValue();
     159            // Break loop when the user cancels
     160            if(answer != 1)
     161                break;
     162
     163            String text = tfWmsUrl.getText().trim();
     164            // Loop all services until we find the selected one
     165            for(rectifierService s : services) {
     166                if(!s.isSelected())
     167                    continue;
     168
     169                // We've reached the custom WMS URL service
     170                // Just set the URL and hope everything works out
     171                if(s.wmsUrl.equals("")) {
     172                    addWMSLayer(s.name + " (" + text + ")", text);
     173                    break outer;
     174                }
     175
     176                // First try to match if the entered string as an URL
     177                Matcher m = s.urlRegEx.matcher(text);
     178                if(m.find()) {
     179                    String id = m.group(1);
     180                    String newURL = s.wmsUrl.replaceAll("__s__", id);
     181                    String title = s.name + " (" + id + ")";
     182                    addWMSLayer(title, newURL);
     183                    break outer;
     184                }
     185                // If not, look if it's a valid ID for the selected service
     186                if(s.idValidator.matcher(text).matches()) {
     187                    String newURL = s.wmsUrl.replaceAll("__s__", text);
     188                    String title = s.name + " (" + text + ")";
     189                    addWMSLayer(title, newURL);
     190                    break outer;
     191                }
     192
     193                // We've found the selected service, but the entered string isn't suitable for
     194                // it. So quit checking the other radio buttons
     195                break;
     196            }
     197
     198            // and display an error message. The while(true) ensures that the dialog pops up again
     199            JOptionPane.showMessageDialog(Main.parent,
     200                tr("Couldn't match the entered link or id to the selected service. Please try again."),
     201                tr("No valid WMS URL or id"),
     202                JOptionPane.ERROR_MESSAGE);
     203            diag.setVisible(true);
     204        }
     205    }
     206
     207    /**
     208     * Adds a WMS Layer with given title and UR:
     209     * @param title: Name of the layer as it will shop up in the layer manager
     210     * @param url: URL to the WMS server
     211     */
     212    private void addWMSLayer(String title, String url) {
     213        WMSLayer wmsLayer = new WMSLayer(title, url);
     214        Main.main.addLayer(wmsLayer);
     215    }
     216
     217    /**
     218     * Helper function that extracts a String from the Clipboard if available.
     219     * Returns an empty String otherwise
     220     * @return String Clipboard contents if available
     221     */
     222    private String getClipboardContents() {
     223        String result = "";
     224        Transferable contents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
     225
     226        if(contents == null || !contents.isDataFlavorSupported(DataFlavor.stringFlavor))
     227            return "";
     228
     229        try {
     230            result = (String)contents.getTransferData(DataFlavor.stringFlavor);
     231        } catch(Exception ex) {
     232            return "";
     233        }
     234        return result.trim();
    35235    }
    36236}
Note: See TracChangeset for help on using the changeset viewer.