1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.Action;
|
---|
15 |
|
---|
16 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
17 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
|
---|
18 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
|
---|
21 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
22 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
23 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
24 | import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
|
---|
25 | import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
|
---|
26 | import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
|
---|
27 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
28 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
29 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
30 | import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
|
---|
31 | import org.openstreetmap.josm.gui.MapView;
|
---|
32 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * This is a layer that grabs the current screen from an WMS server. The data
|
---|
36 | * fetched this way is tiled and managed to the disc to reduce server load.
|
---|
37 | *
|
---|
38 | */
|
---|
39 | public class WMSLayer extends AbstractTileSourceLayer {
|
---|
40 | /** default tile size for WMS Layer */
|
---|
41 | public static final IntegerProperty PROP_IMAGE_SIZE = new IntegerProperty("imagery.wms.imageSize", 512);
|
---|
42 | /** should WMS layer autozoom in default mode */
|
---|
43 | public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty("imagery.wms.default_autozoom", true);
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Constructs a new {@code WMSLayer}.
|
---|
47 | * @param info ImageryInfo description of the layer
|
---|
48 | */
|
---|
49 | public WMSLayer(ImageryInfo info) {
|
---|
50 | super(info);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void hookUpMapView() {
|
---|
55 | super.hookUpMapView();
|
---|
56 | final ProjectionChangeListener listener = new ProjectionChangeListener() {
|
---|
57 | @Override
|
---|
58 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
59 | if (!oldValue.equals(newValue) && tileSource instanceof TemplatedWMSTileSource) {
|
---|
60 | ((TemplatedWMSTileSource)tileSource).initProjection(newValue);
|
---|
61 | }
|
---|
62 |
|
---|
63 | }
|
---|
64 | };
|
---|
65 | Main.addProjectionChangeListener(listener);
|
---|
66 |
|
---|
67 | MapView.addLayerChangeListener(new LayerChangeListener() {
|
---|
68 | @Override
|
---|
69 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
70 | // empty
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void layerAdded(Layer newLayer) {
|
---|
75 | // empty
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public void layerRemoved(Layer oldLayer) {
|
---|
80 | if (oldLayer == WMSLayer.this) {
|
---|
81 | Main.removeProjectionChangeListener(listener);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | });
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public Action[] getMenuEntries() {
|
---|
89 | List<Action> ret = new ArrayList<>();
|
---|
90 | ret.addAll(Arrays.asList(super.getMenuEntries()));
|
---|
91 | ret.add(SeparatorLayerAction.INSTANCE);
|
---|
92 | ret.add(new LayerSaveAction(this));
|
---|
93 | ret.add(new LayerSaveAsAction(this));
|
---|
94 | ret.add(new BookmarkWmsAction());
|
---|
95 | return ret.toArray(new Action[]{});
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | protected TileSource getTileSource(ImageryInfo info) throws IllegalArgumentException {
|
---|
101 | if (info.getImageryType() == ImageryType.WMS && info.getUrl() != null) {
|
---|
102 | TemplatedWMSTileSource.checkUrl(info.getUrl());
|
---|
103 | TemplatedWMSTileSource tileSource = new TemplatedWMSTileSource(info);
|
---|
104 | info.setAttribution(tileSource);
|
---|
105 | return tileSource;
|
---|
106 | }
|
---|
107 | return null;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * This action will add a WMS layer menu entry with the current WMS layer
|
---|
112 | * URL and name extended by the current resolution.
|
---|
113 | * When using the menu entry again, the WMS cache will be used properly.
|
---|
114 | */
|
---|
115 | public class BookmarkWmsAction extends AbstractAction {
|
---|
116 | /**
|
---|
117 | * Constructs a new {@code BookmarkWmsAction}.
|
---|
118 | */
|
---|
119 | public BookmarkWmsAction() {
|
---|
120 | super(tr("Set WMS Bookmark"));
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public void actionPerformed(ActionEvent ev) {
|
---|
125 | ImageryLayerInfo.addLayer(new ImageryInfo(info));
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Checks that WMS layer is a grabber-compatible one (HTML or WMS).
|
---|
132 | * @throws IllegalStateException if imagery time is neither HTML nor WMS
|
---|
133 | * @since 8068
|
---|
134 | */
|
---|
135 | public void checkGrabberType() {
|
---|
136 | }
|
---|
137 |
|
---|
138 | private static TileLoaderFactory loaderFactory = new CachedTileLoaderFactory("WMS") {
|
---|
139 | @Override
|
---|
140 | protected TileLoader getLoader(TileLoaderListener listener, String cacheName, int connectTimeout,
|
---|
141 | int readTimeout, Map<String, String> headers, String cacheDir) throws IOException {
|
---|
142 | return new WMSCachedTileLoader(listener, cacheName, connectTimeout, readTimeout, headers, cacheDir);
|
---|
143 | }
|
---|
144 |
|
---|
145 | };
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | protected TileLoaderFactory getTileLoaderFactory() {
|
---|
149 | return loaderFactory;
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | protected Map<String, String> getHeaders(TileSource tileSource) {
|
---|
154 | if (tileSource instanceof TemplatedWMSTileSource) {
|
---|
155 | return ((TemplatedWMSTileSource)tileSource).getHeaders();
|
---|
156 | }
|
---|
157 | return null;
|
---|
158 | }
|
---|
159 | }
|
---|