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.HashSet;
|
---|
14 | import java.util.Set;
|
---|
15 |
|
---|
16 | import javax.swing.JComboBox;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JScrollPane;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
23 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
24 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
25 | import org.openstreetmap.josm.gui.layer.AlignImageryPanel;
|
---|
26 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
27 | import org.openstreetmap.josm.gui.preferences.imagery.WMSLayerTree;
|
---|
28 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
29 | import org.openstreetmap.josm.io.imagery.WMSImagery;
|
---|
30 | import org.openstreetmap.josm.io.imagery.WMSImagery.LayerDetails;
|
---|
31 | import org.openstreetmap.josm.tools.GBC;
|
---|
32 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
33 | import org.openstreetmap.josm.tools.ImageProvider.ImageResourceCallback;
|
---|
34 | import org.openstreetmap.josm.tools.ImageResource;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Action displayed in imagery menu to add a new imagery layer.
|
---|
38 | * @since 3715
|
---|
39 | */
|
---|
40 | public class AddImageryLayerAction extends JosmAction implements AdaptableAction {
|
---|
41 | private final transient ImageryInfo info;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code AddImageryLayerAction} for the given {@code ImageryInfo}.
|
---|
45 | * If an http:// icon is specified, it is fetched asynchronously.
|
---|
46 | * @param info The imagery info
|
---|
47 | */
|
---|
48 | public AddImageryLayerAction(ImageryInfo info) {
|
---|
49 | super(info.getMenuName(), /* ICON */"imagery_menu", tr("Add imagery layer {0}", info.getName()), null, false, false);
|
---|
50 | putValue("toolbar", "imagery_" + info.getToolbarName());
|
---|
51 | putValue("help", ht("/Preferences/Imagery"));
|
---|
52 | this.info = info;
|
---|
53 | installAdapters();
|
---|
54 |
|
---|
55 | // change toolbar icon from if specified
|
---|
56 | String icon = info.getIcon();
|
---|
57 | if (icon != null) {
|
---|
58 | new ImageProvider(icon).setOptional(true).getInBackground(new ImageResourceCallback() {
|
---|
59 | @Override
|
---|
60 | public void finished(final ImageResource result) {
|
---|
61 | if (result != null) {
|
---|
62 | GuiHelper.runInEDT(new Runnable() {
|
---|
63 | @Override
|
---|
64 | public void run() {
|
---|
65 | result.getImageIcon(AddImageryLayerAction.this);
|
---|
66 | }
|
---|
67 | });
|
---|
68 | }
|
---|
69 | }
|
---|
70 | });
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void actionPerformed(ActionEvent e) {
|
---|
76 | if (!isEnabled()) return;
|
---|
77 | try {
|
---|
78 | final ImageryInfo infoToAdd = ImageryType.WMS_ENDPOINT.equals(info.getImageryType())
|
---|
79 | ? getWMSLayerInfo() : info;
|
---|
80 | if (infoToAdd != null) {
|
---|
81 | Main.main.addLayer(ImageryLayer.create(infoToAdd));
|
---|
82 | AlignImageryPanel.addNagPanelIfNeeded(infoToAdd);
|
---|
83 | }
|
---|
84 | } catch (IllegalArgumentException ex) {
|
---|
85 | if (ex.getMessage() == null || ex.getMessage().isEmpty()) {
|
---|
86 | throw ex;
|
---|
87 | } else {
|
---|
88 | JOptionPane.showMessageDialog(Main.parent,
|
---|
89 | ex.getMessage(), tr("Error"),
|
---|
90 | JOptionPane.ERROR_MESSAGE);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected ImageryInfo getWMSLayerInfo() {
|
---|
96 | try {
|
---|
97 | assert ImageryType.WMS_ENDPOINT.equals(info.getImageryType());
|
---|
98 | final WMSImagery wms = new WMSImagery();
|
---|
99 | wms.attemptGetCapabilities(info.getUrl());
|
---|
100 |
|
---|
101 | final WMSLayerTree tree = new WMSLayerTree();
|
---|
102 | tree.updateTree(wms);
|
---|
103 | final JComboBox<String> formats = new JComboBox<>(wms.getFormats().toArray(new String[0]));
|
---|
104 | formats.setSelectedItem(wms.getPreferredFormats());
|
---|
105 | formats.setToolTipText(tr("Select image format for WMS layer"));
|
---|
106 |
|
---|
107 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
108 | if (1 != new ExtendedDialog(Main.parent, tr("Select WMS layers"), new String[]{tr("Add layers"), tr("Cancel")}) { {
|
---|
109 | final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
|
---|
110 | scrollPane.setPreferredSize(new Dimension(400, 400));
|
---|
111 | final JPanel panel = new JPanel(new GridBagLayout());
|
---|
112 | panel.add(scrollPane, GBC.eol().fill());
|
---|
113 | panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
114 | setContent(panel);
|
---|
115 | } }.showDialog().getValue()) {
|
---|
116 | return null;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | final String url = wms.buildGetMapUrl(
|
---|
121 | tree.getSelectedLayers(), (String) formats.getSelectedItem());
|
---|
122 | Set<String> supportedCrs = new HashSet<>();
|
---|
123 | boolean first = true;
|
---|
124 | StringBuilder layersString = new StringBuilder();
|
---|
125 | for (LayerDetails layer: tree.getSelectedLayers()) {
|
---|
126 | if (first) {
|
---|
127 | supportedCrs.addAll(layer.getProjections());
|
---|
128 | first = false;
|
---|
129 | }
|
---|
130 | layersString.append(layer.name);
|
---|
131 | layersString.append(", ");
|
---|
132 | supportedCrs.retainAll(layer.getProjections());
|
---|
133 | }
|
---|
134 |
|
---|
135 | ImageryInfo ret = new ImageryInfo(info.getName(), url, "wms", info.getEulaAcceptanceRequired(), info.getCookies());
|
---|
136 | if (layersString.length() > 2) {
|
---|
137 | ret.setName(ret.getName() + ' ' + layersString.substring(0, layersString.length() - 2));
|
---|
138 | }
|
---|
139 | ret.setServerProjections(supportedCrs);
|
---|
140 | return ret;
|
---|
141 | } catch (MalformedURLException ex) {
|
---|
142 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
143 | JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
|
---|
144 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
145 | }
|
---|
146 | Main.error(ex, false);
|
---|
147 | } catch (IOException ex) {
|
---|
148 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
149 | JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
|
---|
150 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
151 | }
|
---|
152 | Main.error(ex, false);
|
---|
153 | } catch (WMSImagery.WMSGetCapabilitiesException ex) {
|
---|
154 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
155 | JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
|
---|
156 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
157 | }
|
---|
158 | Main.error("Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData());
|
---|
159 | }
|
---|
160 | return null;
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | protected void updateEnabledState() {
|
---|
165 | ImageryType type = info.getImageryType();
|
---|
166 | // never enable blacklisted entries. Do not add same imagery layer twice (fix #2519)
|
---|
167 | if (info.isBlacklisted()) {
|
---|
168 | setEnabled(false);
|
---|
169 | } else if (type == ImageryType.TMS || type == ImageryType.BING || type == ImageryType.SCANEX) {
|
---|
170 | setEnabled(true);
|
---|
171 | } else if (Main.isDisplayingMapView() && !Main.map.mapView.getAllLayers().isEmpty()) {
|
---|
172 | setEnabled(true);
|
---|
173 | } else {
|
---|
174 | setEnabled(false);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|