source: josm/trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java@ 10932

Last change on this file since 10932 was 10714, checked in by simon04, 8 years ago

see #11390 - Use CompletableFuture for async image loading

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Dimension;
8import java.awt.GraphicsEnvironment;
9import java.awt.GridBagLayout;
10import java.awt.event.ActionEvent;
11import java.io.IOException;
12import java.net.MalformedURLException;
13import java.util.HashSet;
14import java.util.Set;
15
16import javax.swing.JComboBox;
17import javax.swing.JOptionPane;
18import javax.swing.JPanel;
19import javax.swing.JScrollPane;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.data.imagery.ImageryInfo;
23import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
24import org.openstreetmap.josm.gui.ExtendedDialog;
25import org.openstreetmap.josm.gui.layer.AlignImageryPanel;
26import org.openstreetmap.josm.gui.layer.ImageryLayer;
27import org.openstreetmap.josm.gui.preferences.imagery.WMSLayerTree;
28import org.openstreetmap.josm.gui.util.GuiHelper;
29import org.openstreetmap.josm.io.imagery.WMSImagery;
30import org.openstreetmap.josm.io.imagery.WMSImagery.LayerDetails;
31import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException;
32import org.openstreetmap.josm.tools.GBC;
33import org.openstreetmap.josm.tools.ImageProvider;
34
35/**
36 * Action displayed in imagery menu to add a new imagery layer.
37 * @since 3715
38 */
39public 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 String icon = info.getIcon();
56 if (icon != null) {
57 new ImageProvider(icon).setOptional(true).getResourceAsync().thenAccept(result -> {
58 if (result != null) {
59 GuiHelper.runInEDT(() -> result.attachImageIcon(this));
60 }
61 });
62 }
63 }
64
65 @Override
66 public void actionPerformed(ActionEvent e) {
67 if (!isEnabled()) return;
68 try {
69 final ImageryInfo infoToAdd = ImageryType.WMS_ENDPOINT.equals(info.getImageryType())
70 ? getWMSLayerInfo() : info;
71 if (infoToAdd != null) {
72 Main.getLayerManager().addLayer(ImageryLayer.create(infoToAdd));
73 AlignImageryPanel.addNagPanelIfNeeded(infoToAdd);
74 }
75 } catch (IllegalArgumentException ex) {
76 if (ex.getMessage() == null || ex.getMessage().isEmpty()) {
77 throw ex;
78 } else {
79 JOptionPane.showMessageDialog(Main.parent,
80 ex.getMessage(), tr("Error"),
81 JOptionPane.ERROR_MESSAGE);
82 }
83 }
84 }
85
86 protected ImageryInfo getWMSLayerInfo() {
87 try {
88 assert ImageryType.WMS_ENDPOINT.equals(info.getImageryType());
89 final WMSImagery wms = new WMSImagery();
90 wms.attemptGetCapabilities(info.getUrl());
91
92 final WMSLayerTree tree = new WMSLayerTree();
93 tree.updateTree(wms);
94 final JComboBox<String> formats = new JComboBox<>(wms.getFormats().toArray(new String[0]));
95 formats.setSelectedItem(wms.getPreferredFormats());
96 formats.setToolTipText(tr("Select image format for WMS layer"));
97
98 if (!GraphicsEnvironment.isHeadless()) {
99 if (1 != new ExtendedDialog(Main.parent, tr("Select WMS layers"), new String[]{tr("Add layers"), tr("Cancel")}) { {
100 final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
101 scrollPane.setPreferredSize(new Dimension(400, 400));
102 final JPanel panel = new JPanel(new GridBagLayout());
103 panel.add(scrollPane, GBC.eol().fill());
104 panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
105 setContent(panel);
106 } }.showDialog().getValue()) {
107 return null;
108 }
109 }
110
111 final String url = wms.buildGetMapUrl(
112 tree.getSelectedLayers(), (String) formats.getSelectedItem());
113 Set<String> supportedCrs = new HashSet<>();
114 boolean first = true;
115 StringBuilder layersString = new StringBuilder();
116 for (LayerDetails layer: tree.getSelectedLayers()) {
117 if (first) {
118 supportedCrs.addAll(layer.getProjections());
119 first = false;
120 }
121 layersString.append(layer.name);
122 layersString.append(", ");
123 supportedCrs.retainAll(layer.getProjections());
124 }
125
126 ImageryInfo ret = new ImageryInfo(info.getName(), url, "wms", info.getEulaAcceptanceRequired(), info.getCookies());
127 if (layersString.length() > 2) {
128 ret.setName(ret.getName() + ' ' + layersString.substring(0, layersString.length() - 2));
129 }
130 ret.setServerProjections(supportedCrs);
131 return ret;
132 } catch (MalformedURLException ex) {
133 if (!GraphicsEnvironment.isHeadless()) {
134 JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
135 tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
136 }
137 Main.error(ex, false);
138 } catch (IOException ex) {
139 if (!GraphicsEnvironment.isHeadless()) {
140 JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
141 tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
142 }
143 Main.error(ex, false);
144 } catch (WMSGetCapabilitiesException ex) {
145 if (!GraphicsEnvironment.isHeadless()) {
146 JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
147 tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
148 }
149 Main.error(ex, "Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData());
150 }
151 return null;
152 }
153
154 @Override
155 protected void updateEnabledState() {
156 ImageryType type = info.getImageryType();
157 // never enable blacklisted entries. Do not add same imagery layer twice (fix #2519)
158 if (info.isBlacklisted()) {
159 setEnabled(false);
160 } else if (type == ImageryType.TMS || type == ImageryType.BING || type == ImageryType.SCANEX) {
161 setEnabled(true);
162 } else {
163 setEnabled(!Main.getLayerManager().getLayers().isEmpty());
164 }
165 }
166}
Note: See TracBrowser for help on using the repository browser.