source: josm/trunk/src/org/openstreetmap/josm/actions/Map_Rectifier_WMSmenuAction.java@ 6084

Last change on this file since 6084 was 6069, checked in by stoecker, 11 years ago

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

  • Property svn:eol-style set to native
File size: 9.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.ArrayList;
11import java.util.regex.Matcher;
12import java.util.regex.Pattern;
13
14import javax.swing.ButtonGroup;
15import javax.swing.JLabel;
16import javax.swing.JOptionPane;
17import javax.swing.JPanel;
18import javax.swing.JRadioButton;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.imagery.ImageryInfo;
22import org.openstreetmap.josm.gui.ExtendedDialog;
23import org.openstreetmap.josm.gui.layer.WMSLayer;
24import org.openstreetmap.josm.tools.GBC;
25import org.openstreetmap.josm.tools.Shortcut;
26import org.openstreetmap.josm.tools.UrlLabel;
27import org.openstreetmap.josm.tools.Utils;
28import org.openstreetmap.josm.gui.widgets.JosmTextField;
29
30public class Map_Rectifier_WMSmenuAction extends JosmAction {
31 /**
32 * Class that bundles all required information of a rectifier service
33 */
34 public static class RectifierService {
35 private final String name;
36 private final String url;
37 private final String wmsUrl;
38 private final Pattern urlRegEx;
39 private final Pattern idValidator;
40 public JRadioButton btn;
41
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>();
66
67 public Map_Rectifier_WMSmenuAction() {
68 super(tr("Rectified Image..."),
69 "OLmarker",
70 tr("Download Rectified Images From Various Services"),
71 Shortcut.registerShortcut("imagery:rectimg",
72 tr("Imagery: {0}", tr("Rectified Image...")),
73 KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
74 true
75 );
76
77 // Add default services
78 services.add(
79 new RectifierService("Metacarta Map Rectifier",
80 "http://labs.metacarta.com/rectifier/",
81 "http://labs.metacarta.com/rectifier/wms.cgi?id=__s__&srs=EPSG:4326"
82 + "&Service=WMS&Version=1.1.0&Request=GetMap&format=image/png&",
83 // This matches more than the "classic" WMS link, so users can pretty much
84 // copy any link as long as it includes the ID
85 "labs\\.metacarta\\.com/(?:.*?)(?:/|=)([0-9]+)(?:\\?|/|\\.|$)",
86 "^[0-9]+$")
87 );
88 services.add(
89 new RectifierService("Map Warper",
90 "http://mapwarper.net/",
91 "http://mapwarper.net/maps/wms/__s__?request=GetMap&version=1.1.1"
92 + "&styles=&format=image/png&srs=epsg:4326&exceptions=application/vnd.ogc.se_inimage&",
93 // This matches more than the "classic" WMS link, so users can pretty much
94 // copy any link as long as it includes the ID
95 "(?:mapwarper\\.net|warper\\.geothings\\.net)/(?:.*?)/([0-9]+)(?:\\?|/|\\.|$)",
96 "^[0-9]+$")
97 );
98
99 // This service serves the purpose of "just this once" without forcing the user
100 // to commit the link to the preferences
101
102 // Clipboard content gets trimmed, so matching whitespace only ensures that this
103 // service will never be selected automatically.
104 services.add(new RectifierService(tr("Custom WMS Link"), "", "", "^\\s+$", ""));
105 }
106
107 @Override
108 public void actionPerformed(ActionEvent e) {
109 if (!isEnabled()) return;
110 JPanel panel = new JPanel(new GridBagLayout());
111 panel.add(new JLabel(tr("Supported Rectifier Services:")), GBC.eol());
112
113 JosmTextField tfWmsUrl = new JosmTextField(30);
114
115 String clip = Utils.getClipboardContent();
116 clip = clip == null ? "" : clip.trim();
117 ButtonGroup group = new ButtonGroup();
118
119 JRadioButton firstBtn = null;
120 for(RectifierService s : services) {
121 JRadioButton serviceBtn = new JRadioButton(s.name);
122 if(firstBtn == null) {
123 firstBtn = serviceBtn;
124 }
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(GridBagConstraints.EAST));
138 } else {
139 panel.add(serviceBtn, GBC.eol().anchor(GridBagConstraints.WEST));
140 }
141 }
142
143 // Fallback in case no match was found
144 if(tfWmsUrl.getText().equals("") && firstBtn != null) {
145 firstBtn.setSelected(true);
146 }
147
148 panel.add(new JLabel(tr("WMS URL or Image ID:")), GBC.eol());
149 panel.add(tfWmsUrl, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
150
151 ExtendedDialog diag = new ExtendedDialog(Main.parent,
152 tr("Add Rectified Image"),
153
154 new String[] {tr("Add Rectified Image"), tr("Cancel")});
155 diag.setContent(panel);
156 diag.setButtonIcons(new String[] {"OLmarker.png", "cancel.png"});
157
158 // This repeatedly shows the dialog in case there has been an error.
159 // The loop is break;-ed if the users cancels
160 outer: while(true) {
161 diag.showDialog();
162 int answer = diag.getValue();
163 // Break loop when the user cancels
164 if(answer != 1) {
165 break;
166 }
167
168 String text = tfWmsUrl.getText().trim();
169 // Loop all services until we find the selected one
170 for(RectifierService s : services) {
171 if(!s.isSelected()) {
172 continue;
173 }
174
175 // We've reached the custom WMS URL service
176 // Just set the URL and hope everything works out
177 if(s.wmsUrl.equals("")) {
178 addWMSLayer(s.name + " (" + text + ")", text);
179 break outer;
180 }
181
182 // First try to match if the entered string as an URL
183 Matcher m = s.urlRegEx.matcher(text);
184 if(m.find()) {
185 String id = m.group(1);
186 String newURL = s.wmsUrl.replaceAll("__s__", id);
187 String title = s.name + " (" + id + ")";
188 addWMSLayer(title, newURL);
189 break outer;
190 }
191 // If not, look if it's a valid ID for the selected service
192 if(s.idValidator.matcher(text).matches()) {
193 String newURL = s.wmsUrl.replaceAll("__s__", text);
194 String title = s.name + " (" + text + ")";
195 addWMSLayer(title, newURL);
196 break outer;
197 }
198
199 // We've found the selected service, but the entered string isn't suitable for
200 // it. So quit checking the other radio buttons
201 break;
202 }
203
204 // and display an error message. The while(true) ensures that the dialog pops up again
205 JOptionPane.showMessageDialog(Main.parent,
206 tr("Couldn''t match the entered link or id to the selected service. Please try again."),
207 tr("No valid WMS URL or id"),
208 JOptionPane.ERROR_MESSAGE);
209 diag.setVisible(true);
210 }
211 }
212
213 /**
214 * Adds a WMS Layer with given title and URL
215 * @param title Name of the layer as it will shop up in the layer manager
216 * @param url URL to the WMS server
217 */
218 private void addWMSLayer(String title, String url) {
219 Main.main.addLayer(new WMSLayer(new ImageryInfo(title, url)));
220 }
221
222 @Override
223 protected void updateEnabledState() {
224 setEnabled(Main.isDisplayingMapView() && !Main.map.mapView.getAllLayers().isEmpty());
225 }
226}
Note: See TracBrowser for help on using the repository browser.