1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.net.MalformedURLException;
|
---|
11 |
|
---|
12 | import javax.swing.Action;
|
---|
13 | import javax.swing.ImageIcon;
|
---|
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.ImageCallback;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Action displayed in imagery menu to add a new imagery layer.
|
---|
34 | * @since 3715
|
---|
35 | */
|
---|
36 | public class AddImageryLayerAction extends JosmAction implements AdaptableAction {
|
---|
37 |
|
---|
38 | private static final int MAX_ICON_SIZE = 24;
|
---|
39 | private final ImageryInfo info;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Constructs a new {@code AddImageryLayerAction} for the given {@code ImageryInfo}.
|
---|
43 | * If an http:// icon is specified, it is fetched asynchronously.
|
---|
44 | * @param info The imagery info
|
---|
45 | */
|
---|
46 | public AddImageryLayerAction(ImageryInfo info) {
|
---|
47 | super(info.getMenuName(), /* ICON */"imagery_menu", tr("Add imagery layer {0}",info.getName()), null, false, false);
|
---|
48 | putValue("toolbar", "imagery_" + info.getToolbarName());
|
---|
49 | this.info = info;
|
---|
50 | installAdapters();
|
---|
51 |
|
---|
52 | // change toolbar icon from if specified
|
---|
53 | try {
|
---|
54 | if (info.getIcon() != null) {
|
---|
55 | new ImageProvider(info.getIcon()).setOptional(true).
|
---|
56 | setMaxHeight(MAX_ICON_SIZE).setMaxWidth(MAX_ICON_SIZE).getInBackground(new ImageCallback() {
|
---|
57 | @Override
|
---|
58 | public void finished(final ImageIcon result) {
|
---|
59 | if (result != null) {
|
---|
60 | GuiHelper.runInEDT(new Runnable() {
|
---|
61 | @Override
|
---|
62 | public void run() {
|
---|
63 | putValue(Action.SMALL_ICON, result);
|
---|
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 formats = new JComboBox(wms.getFormats().toArray());
|
---|
105 | formats.setToolTipText(tr("Select image format for WMS layer"));
|
---|
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));
|
---|
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);
|
---|
114 | }}.showDialog().getValue()) {
|
---|
115 | return null;
|
---|
116 | }
|
---|
117 |
|
---|
118 | final String url = wms.buildGetMapUrl(
|
---|
119 | tree.getSelectedLayers(), (String) formats.getSelectedItem());
|
---|
120 | return new ImageryInfo(info.getName(), url, "wms", info.getEulaAcceptanceRequired(), info.getCookies());
|
---|
121 | } // exception handling from AddWMSLayerPanel.java
|
---|
122 | catch (MalformedURLException ex) {
|
---|
123 | JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
|
---|
124 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
125 | } catch (IOException ex) {
|
---|
126 | JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
|
---|
127 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
128 | } catch (WMSImagery.WMSGetCapabilitiesException ex) {
|
---|
129 | JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
|
---|
130 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
131 | Main.error("Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData());
|
---|
132 | }
|
---|
133 | return null;
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected boolean isLayerAlreadyPresent() {
|
---|
137 | if (Main.isDisplayingMapView()) {
|
---|
138 | for (ImageryLayer layer : Main.map.mapView.getLayersOfType(ImageryLayer.class)) {
|
---|
139 | if (info.equals(layer.getInfo())) {
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | return false;
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | protected void updateEnabledState() {
|
---|
149 | // never enable blacklisted entries. Do not add same imagery layer twice (fix #2519)
|
---|
150 | if (info.isBlacklisted() /*|| isLayerAlreadyPresent()*/) { // FIXME check disabled to allow several instances with different settings (see #7981)
|
---|
151 | setEnabled(false);
|
---|
152 | } else if (info.getImageryType() == ImageryType.TMS || info.getImageryType() == ImageryType.BING || info.getImageryType() == ImageryType.SCANEX) {
|
---|
153 | setEnabled(true);
|
---|
154 | } else if (Main.isDisplayingMapView() && !Main.map.mapView.getAllLayers().isEmpty()) {
|
---|
155 | setEnabled(true);
|
---|
156 | } else {
|
---|
157 | setEnabled(false);
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|