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