[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[4968] | 2 | package org.openstreetmap.josm.gui.preferences.imagery;
|
---|
[3715] | 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.net.MalformedURLException;
|
---|
[6016] | 8 | import java.net.URL;
|
---|
[11017] | 9 | import java.util.List;
|
---|
[6016] | 10 |
|
---|
[6000] | 11 | import javax.swing.DefaultComboBoxModel;
|
---|
[3715] | 12 | import javax.swing.JButton;
|
---|
[5617] | 13 | import javax.swing.JCheckBox;
|
---|
[6000] | 14 | import javax.swing.JComboBox;
|
---|
[3715] | 15 | import javax.swing.JLabel;
|
---|
| 16 | import javax.swing.JOptionPane;
|
---|
| 17 | import javax.swing.JScrollPane;
|
---|
[5731] | 18 |
|
---|
[4551] | 19 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
[11289] | 20 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
[3715] | 21 | import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
|
---|
[7312] | 22 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[5886] | 23 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
[5617] | 24 | import org.openstreetmap.josm.io.imagery.WMSImagery;
|
---|
[3715] | 25 | import org.openstreetmap.josm.tools.GBC;
|
---|
[12620] | 26 | import org.openstreetmap.josm.tools.Logging;
|
---|
[5650] | 27 | import org.openstreetmap.josm.tools.Utils;
|
---|
[3715] | 28 |
|
---|
[6733] | 29 | /**
|
---|
[8426] | 30 | * An imagery panel used to add WMS imagery sources.
|
---|
| 31 | * @since 2599
|
---|
[6733] | 32 | */
|
---|
[5617] | 33 | public class AddWMSLayerPanel extends AddImageryPanel {
|
---|
[3715] | 34 |
|
---|
[8308] | 35 | private final transient WMSImagery wms = new WMSImagery();
|
---|
[5617] | 36 | private final JCheckBox endpoint = new JCheckBox(tr("Store WMS endpoint only, select layers at usage"));
|
---|
[8308] | 37 | private final transient WMSLayerTree tree = new WMSLayerTree();
|
---|
[7015] | 38 | private final JComboBox<String> formats = new JComboBox<>();
|
---|
[5731] | 39 | private final JLabel wmsInstruction;
|
---|
[11308] | 40 | private final JosmTextArea wmsUrl = new JosmTextArea(3, 40).transferFocusOnTab();
|
---|
[5731] | 41 | private final JButton showBounds = new JButton(tr("Show bounds"));
|
---|
[3715] | 42 |
|
---|
[5886] | 43 | /**
|
---|
| 44 | * Constructs a new {@code AddWMSLayerPanel}.
|
---|
| 45 | */
|
---|
[3715] | 46 | public AddWMSLayerPanel() {
|
---|
[4015] | 47 |
|
---|
[13591] | 48 | add(new JLabel(tr("{0} Make sure OSM has the permission to use this service", "1.")), GBC.eol());
|
---|
| 49 | add(new JLabel(tr("{0} Enter GetCapabilities URL", "2.")), GBC.eol());
|
---|
[5617] | 50 | add(rawUrl, GBC.eol().fill());
|
---|
| 51 | rawUrl.setLineWrap(true);
|
---|
[13591] | 52 | JButton getLayers = new JButton(tr("{0} Get layers", "3."));
|
---|
[5617] | 53 | add(getLayers, GBC.eop().fill());
|
---|
[4015] | 54 |
|
---|
[13591] | 55 | add(new JLabel(tr("{0} Select layers", "4.")), GBC.eol());
|
---|
[5617] | 56 | add(endpoint, GBC.eol().fill());
|
---|
[5649] | 57 | add(new JScrollPane(tree.getLayerTree()), GBC.eol().fill().weight(1, 100));
|
---|
[5731] | 58 |
|
---|
[5617] | 59 | showBounds.setEnabled(false);
|
---|
[9778] | 60 | add(showBounds, GBC.eop().fill());
|
---|
[4015] | 61 |
|
---|
[13591] | 62 | add(new JLabel(tr("{0} Select image format", "5.")), GBC.eol());
|
---|
[6000] | 63 | add(formats, GBC.eol().fill());
|
---|
| 64 |
|
---|
[13591] | 65 | wmsInstruction = new JLabel(tr("{0} Edit generated {1} URL (optional)", "6.", "WMS"));
|
---|
[10179] | 66 | add(wmsInstruction, GBC.eol());
|
---|
[8426] | 67 | wmsInstruction.setLabelFor(wmsUrl);
|
---|
[5617] | 68 | add(wmsUrl, GBC.eop().fill());
|
---|
| 69 | wmsUrl.setLineWrap(true);
|
---|
| 70 |
|
---|
[13591] | 71 | add(new JLabel(tr("{0} Enter name for this layer", "7.")), GBC.eol());
|
---|
[5617] | 72 | add(name, GBC.eop().fill());
|
---|
| 73 |
|
---|
[10611] | 74 | getLayers.addActionListener(e -> {
|
---|
| 75 | try {
|
---|
| 76 | wms.attemptGetCapabilities(rawUrl.getText());
|
---|
| 77 | tree.updateTree(wms);
|
---|
[11017] | 78 | List<String> wmsFormats = wms.getFormats();
|
---|
[13206] | 79 | formats.setModel(new DefaultComboBoxModel<>(wmsFormats.toArray(new String[0])));
|
---|
[10611] | 80 | formats.setSelectedItem(wms.getPreferredFormats());
|
---|
| 81 | } catch (MalformedURLException ex1) {
|
---|
[12620] | 82 | Logging.log(Logging.LEVEL_ERROR, ex1);
|
---|
[10611] | 83 | JOptionPane.showMessageDialog(getParent(), tr("Invalid service URL."),
|
---|
| 84 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 85 | } catch (IOException ex2) {
|
---|
[12620] | 86 | Logging.log(Logging.LEVEL_ERROR, ex2);
|
---|
[10611] | 87 | JOptionPane.showMessageDialog(getParent(), tr("Could not retrieve WMS layer list."),
|
---|
| 88 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 89 | } catch (WMSImagery.WMSGetCapabilitiesException ex3) {
|
---|
[13415] | 90 | String incomingData = ex3.getIncomingData() != null ? ex3.getIncomingData().trim() : "";
|
---|
[10611] | 91 | String title = tr("WMS Error");
|
---|
[11649] | 92 | StringBuilder message = new StringBuilder(tr("Could not parse WMS layer list."));
|
---|
[12620] | 93 | Logging.log(Logging.LEVEL_ERROR, "Could not parse WMS layer list. Incoming data:\n"+incomingData, ex3);
|
---|
[10611] | 94 | if ((incomingData.startsWith("<html>") || incomingData.startsWith("<HTML>"))
|
---|
| 95 | && (incomingData.endsWith("</html>") || incomingData.endsWith("</HTML>"))) {
|
---|
[11649] | 96 | GuiHelper.notifyUserHtmlError(this, title, message.toString(), incomingData);
|
---|
[10611] | 97 | } else {
|
---|
| 98 | if (ex3.getMessage() != null) {
|
---|
[11649] | 99 | message.append('\n').append(ex3.getMessage());
|
---|
[7312] | 100 | }
|
---|
[11649] | 101 | JOptionPane.showMessageDialog(getParent(), message.toString(), title, JOptionPane.ERROR_MESSAGE);
|
---|
[3715] | 102 | }
|
---|
| 103 | }
|
---|
| 104 | });
|
---|
| 105 |
|
---|
[10611] | 106 | endpoint.addItemListener(e -> {
|
---|
| 107 | tree.getLayerTree().setEnabled(!endpoint.isSelected());
|
---|
| 108 | showBounds.setEnabled(!endpoint.isSelected());
|
---|
| 109 | wmsInstruction.setEnabled(!endpoint.isSelected());
|
---|
| 110 | formats.setEnabled(!endpoint.isSelected());
|
---|
| 111 | wmsUrl.setEnabled(!endpoint.isSelected());
|
---|
| 112 | if (endpoint.isSelected()) {
|
---|
| 113 | URL url = wms.getServiceUrl();
|
---|
| 114 | if (url != null) {
|
---|
| 115 | name.setText(url.getHost());
|
---|
[5731] | 116 | }
|
---|
[10611] | 117 | } else {
|
---|
[5731] | 118 | onLayerSelectionChanged();
|
---|
[3715] | 119 | }
|
---|
| 120 | });
|
---|
| 121 |
|
---|
[10611] | 122 | tree.getLayerTree().addPropertyChangeListener("selectedLayers", evt -> onLayerSelectionChanged());
|
---|
[6000] | 123 |
|
---|
[10611] | 124 | formats.addActionListener(e -> onLayerSelectionChanged());
|
---|
| 125 |
|
---|
| 126 | showBounds.addActionListener(e -> {
|
---|
| 127 | if (tree.getSelectedLayers().get(0).bounds != null) {
|
---|
| 128 | SlippyMapBBoxChooser mapPanel = new SlippyMapBBoxChooser();
|
---|
| 129 | mapPanel.setBoundingBox(tree.getSelectedLayers().get(0).bounds);
|
---|
| 130 | JOptionPane.showMessageDialog(null, mapPanel, tr("Show Bounds"), JOptionPane.PLAIN_MESSAGE);
|
---|
| 131 | } else {
|
---|
| 132 | JOptionPane.showMessageDialog(null, tr("No bounding box was found for this layer."),
|
---|
| 133 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
[3715] | 134 | }
|
---|
| 135 | });
|
---|
[6070] | 136 |
|
---|
[5731] | 137 | registerValidableComponent(endpoint);
|
---|
| 138 | registerValidableComponent(rawUrl);
|
---|
| 139 | registerValidableComponent(wmsUrl);
|
---|
[3715] | 140 | }
|
---|
[6070] | 141 |
|
---|
[5731] | 142 | protected final void onLayerSelectionChanged() {
|
---|
| 143 | if (wms.getServiceUrl() != null) {
|
---|
[6000] | 144 | wmsUrl.setText(wms.buildGetMapUrl(tree.getSelectedLayers(), (String) formats.getSelectedItem()));
|
---|
[5731] | 145 | name.setText(wms.getServiceUrl().getHost() + ": " + Utils.join(", ", tree.getSelectedLayers()));
|
---|
| 146 | }
|
---|
| 147 | showBounds.setEnabled(tree.getSelectedLayers().size() == 1);
|
---|
| 148 | }
|
---|
[3715] | 149 |
|
---|
[5617] | 150 | @Override
|
---|
| 151 | public ImageryInfo getImageryInfo() {
|
---|
| 152 | final ImageryInfo info;
|
---|
| 153 | if (endpoint.isSelected()) {
|
---|
[5731] | 154 | info = new ImageryInfo(getImageryName(), getImageryRawUrl());
|
---|
[5617] | 155 | info.setImageryType(ImageryInfo.ImageryType.WMS_ENDPOINT);
|
---|
[3715] | 156 | } else {
|
---|
[5731] | 157 | info = wms.toImageryInfo(getImageryName(), tree.getSelectedLayers());
|
---|
| 158 | info.setUrl(getWmsUrl());
|
---|
[11289] | 159 | info.setImageryType(ImageryType.WMS);
|
---|
[3715] | 160 | }
|
---|
[4551] | 161 | return info;
|
---|
[3715] | 162 | }
|
---|
[6070] | 163 |
|
---|
[5731] | 164 | protected final String getWmsUrl() {
|
---|
[8568] | 165 | return sanitize(wmsUrl.getText(), ImageryInfo.ImageryType.WMS);
|
---|
[5731] | 166 | }
|
---|
[6070] | 167 |
|
---|
[6084] | 168 | @Override
|
---|
[5731] | 169 | protected boolean isImageryValid() {
|
---|
| 170 | if (getImageryName().isEmpty()) {
|
---|
| 171 | return false;
|
---|
| 172 | }
|
---|
| 173 | if (endpoint.isSelected()) {
|
---|
| 174 | return !getImageryRawUrl().isEmpty();
|
---|
| 175 | } else {
|
---|
| 176 | return !getWmsUrl().isEmpty();
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
[3715] | 179 | }
|
---|