source: josm/trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java@ 10568

Last change on this file since 10568 was 10568, checked in by Don-vip, 8 years ago

fix #13169 - Extract imagery layer settings to new class (patch by michael2402, modified) - gsoc-core

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.List;
10import java.util.Map;
11import java.util.Set;
12import java.util.TreeSet;
13
14import javax.swing.AbstractAction;
15import javax.swing.Action;
16import javax.swing.JOptionPane;
17
18import org.apache.commons.jcs.access.CacheAccess;
19import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
22import org.openstreetmap.josm.data.imagery.ImageryInfo;
23import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
24import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
25import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
26import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
27import org.openstreetmap.josm.data.preferences.BooleanProperty;
28import org.openstreetmap.josm.data.preferences.IntegerProperty;
29import org.openstreetmap.josm.data.projection.Projection;
30import org.openstreetmap.josm.gui.ExtendedDialog;
31import org.openstreetmap.josm.gui.layer.imagery.TileSourceDisplaySettings;
32
33/**
34 * This is a layer that grabs the current screen from an WMS server. The data
35 * fetched this way is tiled and managed to the disc to reduce server load.
36 *
37 */
38public class WMSLayer extends AbstractCachedTileSourceLayer<TemplatedWMSTileSource> {
39 private static final String PREFERENCE_PREFIX = "imagery.wms";
40 /**
41 * Registers all setting properties
42 */
43 static {
44 new TileSourceDisplaySettings(PREFERENCE_PREFIX);
45 }
46
47 /** default tile size for WMS Layer */
48 public static final IntegerProperty PROP_IMAGE_SIZE = new IntegerProperty(PREFERENCE_PREFIX + ".imageSize", 512);
49
50 /** should WMS layer autozoom in default mode */
51 public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
52
53 private static final String CACHE_REGION_NAME = "WMS";
54
55 private final Set<String> supportedProjections;
56
57 /**
58 * Constructs a new {@code WMSLayer}.
59 * @param info ImageryInfo description of the layer
60 */
61 public WMSLayer(ImageryInfo info) {
62 super(info);
63 this.supportedProjections = new TreeSet<>(info.getServerProjections());
64 }
65
66 @Override
67 protected TileSourceDisplaySettings createDisplaySettings() {
68 return new TileSourceDisplaySettings(PREFERENCE_PREFIX);
69 }
70
71 @Override
72 public Action[] getMenuEntries() {
73 List<Action> ret = new ArrayList<>();
74 ret.addAll(Arrays.asList(super.getMenuEntries()));
75 ret.add(SeparatorLayerAction.INSTANCE);
76 ret.add(new LayerSaveAction(this));
77 ret.add(new LayerSaveAsAction(this));
78 ret.add(new BookmarkWmsAction());
79 return ret.toArray(new Action[]{});
80 }
81
82 @Override
83 protected TemplatedWMSTileSource getTileSource(ImageryInfo info) {
84 if (info.getImageryType() == ImageryType.WMS && info.getUrl() != null) {
85 TemplatedWMSTileSource.checkUrl(info.getUrl());
86 TemplatedWMSTileSource tileSource = new TemplatedWMSTileSource(info);
87 info.setAttribution(tileSource);
88 return tileSource;
89 }
90 return null;
91 }
92
93 /**
94 * This action will add a WMS layer menu entry with the current WMS layer
95 * URL and name extended by the current resolution.
96 * When using the menu entry again, the WMS cache will be used properly.
97 */
98 public class BookmarkWmsAction extends AbstractAction {
99 /**
100 * Constructs a new {@code BookmarkWmsAction}.
101 */
102 public BookmarkWmsAction() {
103 super(tr("Set WMS Bookmark"));
104 }
105
106 @Override
107 public void actionPerformed(ActionEvent ev) {
108 ImageryLayerInfo.addLayer(new ImageryInfo(info));
109 }
110 }
111
112 @Override
113 protected Map<String, String> getHeaders(TemplatedWMSTileSource tileSource) {
114 return tileSource.getHeaders();
115 }
116
117 @Override
118 public boolean isProjectionSupported(Projection proj) {
119 return supportedProjections == null || supportedProjections.isEmpty() || supportedProjections.contains(proj.toCode()) ||
120 (info.isEpsg4326To3857Supported() && supportedProjections.contains("EPSG:4326")
121 && "EPSG:3857".equals(Main.getProjection().toCode()));
122 }
123
124 @Override
125 public String nameSupportedProjections() {
126 StringBuilder ret = new StringBuilder();
127 for (String e: supportedProjections) {
128 ret.append(e).append(", ");
129 }
130 String appendix = "";
131
132 if (isReprojectionPossible()) {
133 appendix = ". <p>" + tr("JOSM will use EPSG:4326 to query the server, but results may vary "
134 + "depending on the WMS server") + "</p>";
135 }
136 return ret.substring(0, ret.length()-2) + appendix;
137 }
138
139 @Override
140 public void projectionChanged(Projection oldValue, Projection newValue) {
141 // do not call super - we need custom warning dialog
142
143 if (!isProjectionSupported(newValue)) {
144 String message =
145 "<html><body><p>" + tr("The layer {0} does not support the new projection {1}.", getName(), newValue.toCode()) +
146 "<p style='width: 450px; position: absolute; margin: 0px;'>" +
147 tr("Supported projections are: {0}", nameSupportedProjections()) + "</p>" +
148 "<p>" + tr("Change the projection again or remove the layer.");
149
150 ExtendedDialog warningDialog = new ExtendedDialog(Main.parent, tr("Warning"), new String[]{tr("OK")}).
151 setContent(message).
152 setIcon(JOptionPane.WARNING_MESSAGE);
153
154 if (isReprojectionPossible()) {
155 warningDialog.toggleEnable("imagery.wms.projectionSupportWarnings." + tileSource.getBaseUrl());
156 }
157 warningDialog.showDialog();
158 }
159
160 if (!newValue.equals(oldValue)) {
161 tileSource.initProjection(newValue);
162 }
163 }
164
165 @Override
166 protected Class<? extends TileLoader> getTileLoaderClass() {
167 return WMSCachedTileLoader.class;
168 }
169
170 @Override
171 protected String getCacheName() {
172 return CACHE_REGION_NAME;
173 }
174
175 /**
176 * @return cache region for WMS layer
177 */
178 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
179 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
180 }
181
182 private boolean isReprojectionPossible() {
183 return supportedProjections.contains("EPSG:4326") && "EPSG:3857".equals(Main.getProjection().toCode());
184 }
185}
Note: See TracBrowser for help on using the repository browser.