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