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