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