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