1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.GraphicsEnvironment;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.net.MalformedURLException;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Set;
|
---|
18 |
|
---|
19 | import javax.swing.JComboBox;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JScrollPane;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.data.imagery.DefaultLayer;
|
---|
26 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
27 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
28 | import org.openstreetmap.josm.data.imagery.WMTSTileSource;
|
---|
29 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
30 | import org.openstreetmap.josm.gui.layer.AlignImageryPanel;
|
---|
31 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
32 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
33 | import org.openstreetmap.josm.gui.preferences.imagery.WMSLayerTree;
|
---|
34 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
35 | import org.openstreetmap.josm.io.imagery.WMSImagery;
|
---|
36 | import org.openstreetmap.josm.io.imagery.WMSImagery.LayerDetails;
|
---|
37 | import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException;
|
---|
38 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
39 | import org.openstreetmap.josm.tools.GBC;
|
---|
40 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Action displayed in imagery menu to add a new imagery layer.
|
---|
44 | * @since 3715
|
---|
45 | */
|
---|
46 | public class AddImageryLayerAction extends JosmAction implements AdaptableAction {
|
---|
47 | private final transient ImageryInfo info;
|
---|
48 |
|
---|
49 | static class SelectWmsLayersDialog extends ExtendedDialog {
|
---|
50 | SelectWmsLayersDialog(WMSLayerTree tree, JComboBox<String> formats) {
|
---|
51 | super(Main.parent, tr("Select WMS layers"), tr("Add layers"), tr("Cancel"));
|
---|
52 | final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
|
---|
53 | scrollPane.setPreferredSize(new Dimension(400, 400));
|
---|
54 | final JPanel panel = new JPanel(new GridBagLayout());
|
---|
55 | panel.add(scrollPane, GBC.eol().fill());
|
---|
56 | panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
57 | setContent(panel);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Constructs a new {@code AddImageryLayerAction} for the given {@code ImageryInfo}.
|
---|
63 | * If an http:// icon is specified, it is fetched asynchronously.
|
---|
64 | * @param info The imagery info
|
---|
65 | */
|
---|
66 | public AddImageryLayerAction(ImageryInfo info) {
|
---|
67 | super(info.getMenuName(), /* ICON */"imagery_menu", tr("Add imagery layer {0}", info.getName()), null,
|
---|
68 | true, ToolbarPreferences.IMAGERY_PREFIX + info.getToolbarName(), false);
|
---|
69 | putValue("help", ht("/Preferences/Imagery"));
|
---|
70 | setTooltip(info.getToolTipText().replaceAll("</?html>", ""));
|
---|
71 | this.info = info;
|
---|
72 | installAdapters();
|
---|
73 |
|
---|
74 | // change toolbar icon from if specified
|
---|
75 | String icon = info.getIcon();
|
---|
76 | if (icon != null) {
|
---|
77 | new ImageProvider(icon).setOptional(true).getResourceAsync().thenAccept(result -> {
|
---|
78 | if (result != null) {
|
---|
79 | GuiHelper.runInEDT(() -> result.attachImageIcon(this));
|
---|
80 | }
|
---|
81 | });
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Converts general ImageryInfo to specific one, that does not need any user action to initialize
|
---|
87 | * see: https://josm.openstreetmap.de/ticket/13868
|
---|
88 | * @param info ImageryInfo that will be converted (or returned when no conversion needed)
|
---|
89 | * @return ImageryInfo object that's ready to be used to create TileSource
|
---|
90 | */
|
---|
91 | private ImageryInfo convertImagery(ImageryInfo info) {
|
---|
92 | try {
|
---|
93 | switch(info.getImageryType()) {
|
---|
94 | case WMS_ENDPOINT:
|
---|
95 | // convert to WMS type
|
---|
96 | return getWMSLayerInfo(info);
|
---|
97 | case WMTS:
|
---|
98 | // specify which layer to use
|
---|
99 | DefaultLayer layerId = new WMTSTileSource(info).userSelectLayer();
|
---|
100 | if (layerId != null) {
|
---|
101 | ImageryInfo copy = new ImageryInfo(info);
|
---|
102 | Collection<DefaultLayer> defaultLayers = new ArrayList<>(1);
|
---|
103 | defaultLayers.add(layerId);
|
---|
104 | copy.setDefaultLayers(defaultLayers);
|
---|
105 | return copy;
|
---|
106 | }
|
---|
107 | // layer not selected - refuse to add
|
---|
108 | return null;
|
---|
109 | default:
|
---|
110 | return info;
|
---|
111 | }
|
---|
112 | } catch (MalformedURLException ex) {
|
---|
113 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
114 | JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
|
---|
115 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
116 | }
|
---|
117 | Main.error(ex, false);
|
---|
118 | } catch (IOException ex) {
|
---|
119 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
120 | JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
|
---|
121 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
122 | }
|
---|
123 | Main.error(ex, false);
|
---|
124 | } catch (WMSGetCapabilitiesException ex) {
|
---|
125 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
126 | JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
|
---|
127 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
128 | }
|
---|
129 | Main.error(ex, "Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData());
|
---|
130 | }
|
---|
131 | return null;
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public void actionPerformed(ActionEvent e) {
|
---|
136 | if (!isEnabled()) return;
|
---|
137 | try {
|
---|
138 | final ImageryInfo infoToAdd = convertImagery(info);
|
---|
139 | if (infoToAdd != null) {
|
---|
140 | Main.getLayerManager().addLayer(ImageryLayer.create(infoToAdd));
|
---|
141 | AlignImageryPanel.addNagPanelIfNeeded(infoToAdd);
|
---|
142 | }
|
---|
143 | } catch (IllegalArgumentException ex) {
|
---|
144 | if (ex.getMessage() == null || ex.getMessage().isEmpty() || GraphicsEnvironment.isHeadless()) {
|
---|
145 | throw ex;
|
---|
146 | } else {
|
---|
147 | JOptionPane.showMessageDialog(Main.parent,
|
---|
148 | ex.getMessage(), tr("Error"),
|
---|
149 | JOptionPane.ERROR_MESSAGE);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Asks user to choose a WMS layer from a WMS endpoint.
|
---|
156 | * @param info the WMS endpoint.
|
---|
157 | * @return chosen WMS layer, or null
|
---|
158 | * @throws IOException if any I/O error occurs while contacting the WMS endpoint
|
---|
159 | * @throws WMSGetCapabilitiesException if the WMS getCapabilities request fails
|
---|
160 | */
|
---|
161 | protected static ImageryInfo getWMSLayerInfo(ImageryInfo info) throws IOException, WMSGetCapabilitiesException {
|
---|
162 | CheckParameterUtil.ensureThat(ImageryType.WMS_ENDPOINT.equals(info.getImageryType()), "wms_endpoint imagery type expected");
|
---|
163 |
|
---|
164 | final WMSImagery wms = new WMSImagery();
|
---|
165 | wms.attemptGetCapabilities(info.getUrl());
|
---|
166 |
|
---|
167 | final WMSLayerTree tree = new WMSLayerTree();
|
---|
168 | tree.updateTree(wms);
|
---|
169 | List<String> wmsFormats = wms.getFormats();
|
---|
170 | final JComboBox<String> formats = new JComboBox<>(wmsFormats.toArray(new String[wmsFormats.size()]));
|
---|
171 | formats.setSelectedItem(wms.getPreferredFormats());
|
---|
172 | formats.setToolTipText(tr("Select image format for WMS layer"));
|
---|
173 |
|
---|
174 | if (!GraphicsEnvironment.isHeadless() && 1 != new SelectWmsLayersDialog(tree, formats).showDialog().getValue()) {
|
---|
175 | return null;
|
---|
176 | }
|
---|
177 |
|
---|
178 | final String url = wms.buildGetMapUrl(
|
---|
179 | tree.getSelectedLayers(), (String) formats.getSelectedItem());
|
---|
180 | Set<String> supportedCrs = new HashSet<>();
|
---|
181 | boolean first = true;
|
---|
182 | StringBuilder layersString = new StringBuilder();
|
---|
183 | for (LayerDetails layer: tree.getSelectedLayers()) {
|
---|
184 | if (first) {
|
---|
185 | supportedCrs.addAll(layer.getProjections());
|
---|
186 | first = false;
|
---|
187 | }
|
---|
188 | layersString.append(layer.name);
|
---|
189 | layersString.append(", ");
|
---|
190 | supportedCrs.retainAll(layer.getProjections());
|
---|
191 | }
|
---|
192 |
|
---|
193 | // copy all information from WMS
|
---|
194 | ImageryInfo ret = new ImageryInfo(info);
|
---|
195 | // and update according to user choice
|
---|
196 | ret.setUrl(url);
|
---|
197 | ret.setImageryType(ImageryType.WMS);
|
---|
198 | if (layersString.length() > 2) {
|
---|
199 | ret.setName(ret.getName() + ' ' + layersString.substring(0, layersString.length() - 2));
|
---|
200 | }
|
---|
201 | ret.setServerProjections(supportedCrs);
|
---|
202 | return ret;
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | protected void updateEnabledState() {
|
---|
207 | if (info.isBlacklisted()) {
|
---|
208 | setEnabled(false);
|
---|
209 | } else {
|
---|
210 | setEnabled(true);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public String toString() {
|
---|
216 | return "AddImageryLayerAction [info=" + info + ']';
|
---|
217 | }
|
---|
218 | }
|
---|