[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[3715] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[7665] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[3715] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
[5617] | 7 | import java.awt.Dimension;
|
---|
[6000] | 8 | import java.awt.GridBagLayout;
|
---|
[3715] | 9 | import java.awt.event.ActionEvent;
|
---|
[5617] | 10 | import java.io.IOException;
|
---|
| 11 | import java.net.MalformedURLException;
|
---|
[6248] | 12 |
|
---|
[6000] | 13 | import javax.swing.JComboBox;
|
---|
[4531] | 14 | import javax.swing.JOptionPane;
|
---|
[6000] | 15 | import javax.swing.JPanel;
|
---|
[5617] | 16 | import javax.swing.JScrollPane;
|
---|
[6248] | 17 |
|
---|
[3715] | 18 | import org.openstreetmap.josm.Main;
|
---|
| 19 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
[3733] | 20 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
[5617] | 21 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[5091] | 22 | import org.openstreetmap.josm.gui.actionsupport.AlignImageryPanel;
|
---|
[3715] | 23 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
[6248] | 24 | import org.openstreetmap.josm.gui.preferences.imagery.WMSLayerTree;
|
---|
[6707] | 25 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[5617] | 26 | import org.openstreetmap.josm.io.imagery.WMSImagery;
|
---|
[6000] | 27 | import org.openstreetmap.josm.tools.GBC;
|
---|
[4721] | 28 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[7693] | 29 | import org.openstreetmap.josm.tools.ImageProvider.ImageResourceCallback;
|
---|
| 30 | import org.openstreetmap.josm.tools.ImageResource;
|
---|
[3715] | 31 |
|
---|
[6707] | 32 | /**
|
---|
[7509] | 33 | * Action displayed in imagery menu to add a new imagery layer.
|
---|
[6707] | 34 | * @since 3715
|
---|
| 35 | */
|
---|
[4032] | 36 | public class AddImageryLayerAction extends JosmAction implements AdaptableAction {
|
---|
[8308] | 37 | private final transient ImageryInfo info;
|
---|
[3715] | 38 |
|
---|
[6707] | 39 | /**
|
---|
| 40 | * Constructs a new {@code AddImageryLayerAction} for the given {@code ImageryInfo}.
|
---|
| 41 | * If an http:// icon is specified, it is fetched asynchronously.
|
---|
| 42 | * @param info The imagery info
|
---|
| 43 | */
|
---|
[3715] | 44 | public AddImageryLayerAction(ImageryInfo info) {
|
---|
[4172] | 45 | super(info.getMenuName(), /* ICON */"imagery_menu", tr("Add imagery layer {0}",info.getName()), null, false, false);
|
---|
[3715] | 46 | putValue("toolbar", "imagery_" + info.getToolbarName());
|
---|
[7665] | 47 | putValue("help", ht("/Preferences/Imagery"));
|
---|
[3715] | 48 | this.info = info;
|
---|
[3733] | 49 | installAdapters();
|
---|
[4713] | 50 |
|
---|
| 51 | // change toolbar icon from if specified
|
---|
| 52 | try {
|
---|
[7693] | 53 | String icon = info.getIcon();
|
---|
| 54 | if (icon != null) {
|
---|
| 55 | new ImageProvider(icon).setOptional(true).getInBackground(new ImageResourceCallback() {
|
---|
[6707] | 56 | @Override
|
---|
[7693] | 57 | public void finished(final ImageResource result) {
|
---|
[6707] | 58 | if (result != null) {
|
---|
| 59 | GuiHelper.runInEDT(new Runnable() {
|
---|
| 60 | @Override
|
---|
| 61 | public void run() {
|
---|
[7693] | 62 | result.getImageIcon(AddImageryLayerAction.this);
|
---|
[6707] | 63 | }
|
---|
| 64 | });
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | });
|
---|
[4713] | 68 | }
|
---|
| 69 | } catch (Exception ex) {
|
---|
| 70 | throw new RuntimeException(ex.getMessage(), ex);
|
---|
| 71 | }
|
---|
[3715] | 72 | }
|
---|
| 73 |
|
---|
| 74 | @Override
|
---|
| 75 | public void actionPerformed(ActionEvent e) {
|
---|
[3733] | 76 | if (!isEnabled()) return;
|
---|
[4531] | 77 | try {
|
---|
[5617] | 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();
|
---|
| 83 | }
|
---|
[4531] | 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 | }
|
---|
[3715] | 93 | }
|
---|
[5617] | 94 |
|
---|
| 95 | protected ImageryInfo getWMSLayerInfo() {
|
---|
| 96 | try {
|
---|
[8345] | 97 | assert ImageryType.WMS_ENDPOINT.equals(info.getImageryType());
|
---|
[5617] | 98 | final WMSImagery wms = new WMSImagery();
|
---|
| 99 | wms.attemptGetCapabilities(info.getUrl());
|
---|
| 100 |
|
---|
| 101 | final WMSLayerTree tree = new WMSLayerTree();
|
---|
| 102 | tree.updateTree(wms);
|
---|
[7017] | 103 | final JComboBox<String> formats = new JComboBox<>(wms.getFormats().toArray(new String[0]));
|
---|
[6930] | 104 | formats.setSelectedItem(wms.getPreferredFormats());
|
---|
[6000] | 105 | formats.setToolTipText(tr("Select image format for WMS layer"));
|
---|
[5617] | 106 |
|
---|
| 107 | if (1 != new ExtendedDialog(Main.parent, tr("Select WMS layers"), new String[]{tr("Add layers"), tr("Cancel")}) {{
|
---|
| 108 | final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
|
---|
| 109 | scrollPane.setPreferredSize(new Dimension(400, 400));
|
---|
[6000] | 110 | final JPanel panel = new JPanel(new GridBagLayout());
|
---|
| 111 | panel.add(scrollPane, GBC.eol().fill());
|
---|
| 112 | panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
| 113 | setContent(panel);
|
---|
[5617] | 114 | }}.showDialog().getValue()) {
|
---|
| 115 | return null;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[6000] | 118 | final String url = wms.buildGetMapUrl(
|
---|
| 119 | tree.getSelectedLayers(), (String) formats.getSelectedItem());
|
---|
[5617] | 120 | return new ImageryInfo(info.getName(), url, "wms", info.getEulaAcceptanceRequired(), info.getCookies());
|
---|
[8342] | 121 | } catch (MalformedURLException ex) {
|
---|
[5617] | 122 | JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
|
---|
| 123 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 124 | } catch (IOException ex) {
|
---|
| 125 | JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
|
---|
| 126 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 127 | } catch (WMSImagery.WMSGetCapabilitiesException ex) {
|
---|
| 128 | JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
|
---|
| 129 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
[6248] | 130 | Main.error("Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData());
|
---|
[5617] | 131 | }
|
---|
| 132 | return null;
|
---|
| 133 | }
|
---|
[6069] | 134 |
|
---|
[5458] | 135 | protected boolean isLayerAlreadyPresent() {
|
---|
[5460] | 136 | if (Main.isDisplayingMapView()) {
|
---|
[5458] | 137 | for (ImageryLayer layer : Main.map.mapView.getLayersOfType(ImageryLayer.class)) {
|
---|
| 138 | if (info.equals(layer.getInfo())) {
|
---|
| 139 | return true;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | return false;
|
---|
| 144 | }
|
---|
[3733] | 145 |
|
---|
| 146 | @Override
|
---|
| 147 | protected void updateEnabledState() {
|
---|
[5458] | 148 | // never enable blacklisted entries. Do not add same imagery layer twice (fix #2519)
|
---|
[5466] | 149 | if (info.isBlacklisted() /*|| isLayerAlreadyPresent()*/) { // FIXME check disabled to allow several instances with different settings (see #7981)
|
---|
[3826] | 150 | setEnabled(false);
|
---|
[3878] | 151 | } else if (info.getImageryType() == ImageryType.TMS || info.getImageryType() == ImageryType.BING || info.getImageryType() == ImageryType.SCANEX) {
|
---|
[3826] | 152 | setEnabled(true);
|
---|
[5460] | 153 | } else if (Main.isDisplayingMapView() && !Main.map.mapView.getAllLayers().isEmpty()) {
|
---|
[3826] | 154 | setEnabled(true);
|
---|
| 155 | } else {
|
---|
| 156 | setEnabled(false);
|
---|
| 157 | }
|
---|
[3733] | 158 | }
|
---|
| 159 | }
|
---|