001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.model;
034:
035: import java.awt.Image;
036: import java.awt.MediaTracker;
037: import java.io.IOException;
038: import java.util.ArrayList;
039: import java.util.Collections;
040: import java.util.List;
041:
042: import javax.swing.JButton;
043:
044: import com.vividsolutions.jts.geom.Envelope;
045: import com.vividsolutions.jts.util.Assert;
046: import com.vividsolutions.jump.util.Blackboard;
047: import com.vividsolutions.jump.workbench.ui.LayerNameRenderer;
048: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
049: import com.vividsolutions.jump.workbench.ui.renderer.RenderingManager;
050: import com.vividsolutions.wms.BoundingBox;
051: import com.vividsolutions.wms.MapRequest;
052: import com.vividsolutions.wms.WMService;
053:
054: import java.net.URL;
055:
056: import java.lang.ref.Reference;
057: import java.lang.ref.SoftReference;
058:
059: /**
060: * A Layerable that retrieves images from a Web Map Server.
061: */
062: public class WMSLayer extends AbstractLayerable implements Cloneable {
063: private String format;
064:
065: private List layerNames = new ArrayList();
066:
067: private String srs;
068:
069: private int alpha = 255;
070:
071: private WMService service;
072:
073: private String wmsVersion = WMService.WMS_1_0_0;
074:
075: protected Reference oldImage;
076: protected URL oldURL;
077:
078: /**
079: * Called by Java2XML
080: */
081: public WMSLayer() {
082: init();
083: }
084:
085: public WMSLayer(LayerManager layerManager, String serverURL,
086: String srs, List layerNames, String format, String version)
087: throws IOException {
088: this (layerManager, initializedService(serverURL, version), srs,
089: layerNames, format);
090: }
091:
092: private static WMService initializedService(String serverURL,
093: String version) throws IOException {
094: WMService initializedService = new WMService(serverURL, version);
095: initializedService.initialize();
096: return initializedService;
097: }
098:
099: public WMSLayer(LayerManager layerManager,
100: WMService initializedService, String srs, List layerNames,
101: String format) throws IOException {
102: this (layerManager, initializedService, srs, layerNames, format,
103: initializedService.getVersion());
104: }
105:
106: public WMSLayer(String title, LayerManager layerManager,
107: WMService initializedService, String srs, List layerNames,
108: String format) throws IOException {
109: this (title, layerManager, initializedService, srs, layerNames,
110: format, initializedService.getVersion());
111: }
112:
113: public WMSLayer(String title, LayerManager layerManager,
114: WMService initializedService, String srs, List layerNames,
115: String format, String version) {
116: super (title, layerManager);
117: setService(initializedService);
118: setSRS(srs);
119: this .layerNames = new ArrayList(layerNames);
120: setFormat(format);
121: init();
122: this .wmsVersion = version;
123: }
124:
125: public WMSLayer(LayerManager layerManager,
126: WMService initializedService, String srs, List layerNames,
127: String format, String version) {
128: this ((String) layerNames.get(0), layerManager,
129: initializedService, srs, layerNames, format, version);
130: }
131:
132: protected void init() {
133: getBlackboard().put(
134: RenderingManager.USE_MULTI_RENDERING_THREAD_QUEUE_KEY,
135: true);
136: getBlackboard().put(LayerNameRenderer.USE_CLOCK_ANIMATION_KEY,
137: true);
138: }
139:
140: private void setService(WMService service) {
141: this .service = service;
142: this .serverURL = service.getServerUrl();
143: }
144:
145: public int getAlpha() {
146: return alpha;
147: }
148:
149: /**
150: * @param alpha
151: * 0-255 (255 is opaque)
152: */
153: public void setAlpha(int alpha) {
154: this .alpha = alpha;
155: }
156:
157: public Image createImage(LayerViewPanel panel) throws IOException {
158:
159: MapRequest request = createRequest(panel);
160: URL newURL = request.getURL();
161:
162: Image image;
163:
164: // look if last request equals new one.
165: // if it does take the image from the cache.
166: if (oldURL == null || !newURL.equals(oldURL)
167: || oldImage == null
168: || (image = (Image) oldImage.get()) == null) {
169: image = request.getImage();
170: MediaTracker mt = new MediaTracker(new JButton());
171: mt.addImage(image, 0);
172:
173: try {
174: mt.waitForID(0);
175: } catch (InterruptedException e) {
176: Assert.shouldNeverReachHere();
177: }
178: oldImage = new SoftReference(image);
179: oldURL = newURL;
180: }
181:
182: return image;
183: }
184:
185: private BoundingBox toBoundingBox(String srs, Envelope e) {
186: return new BoundingBox(srs, e.getMinX(), e.getMinY(), e
187: .getMaxX(), e.getMaxY());
188: }
189:
190: public MapRequest createRequest(LayerViewPanel panel)
191: throws IOException {
192: MapRequest request = getService().createMapRequest();
193: request.setBoundingBox(toBoundingBox(srs, panel.getViewport()
194: .getEnvelopeInModelCoordinates()));
195: request.setFormat(format);
196: request.setImageWidth(panel.getWidth());
197: request.setImageHeight(panel.getHeight());
198: request.setLayers(layerNames);
199: request.setTransparent(true);
200:
201: return request;
202: }
203:
204: public String getFormat() {
205: return format;
206: }
207:
208: public void setFormat(String format) {
209: this .format = format;
210: }
211:
212: public void addLayerName(String layerName) {
213: layerNames.add(layerName);
214: }
215:
216: public List getLayerNames() {
217: return Collections.unmodifiableList(layerNames);
218: }
219:
220: public void setSRS(String srs) {
221: this .srs = srs;
222: }
223:
224: public String getSRS() {
225: return srs;
226: }
227:
228: public Object clone() throws java.lang.CloneNotSupportedException {
229: WMSLayer clone = (WMSLayer) super .clone();
230: clone.layerNames = new ArrayList(this .layerNames);
231:
232: return clone;
233: }
234:
235: public void removeAllLayerNames() {
236: layerNames.clear();
237: }
238:
239: private Blackboard blackboard = new Blackboard();
240:
241: private String serverURL;
242:
243: public Blackboard getBlackboard() {
244: return blackboard;
245: }
246:
247: public WMService getService() throws IOException {
248: if (service == null) {
249: Assert.isTrue(serverURL != null);
250: setService(initializedService(serverURL, wmsVersion));
251: }
252: return service;
253: }
254:
255: public String getServerURL() {
256: //Called by Java2XML [Jon Aquino 2004-02-23]
257: return serverURL;
258: }
259:
260: public void setServerURL(String serverURL) {
261: //Called by Java2XML [Jon Aquino 2004-02-23]
262: this .serverURL = serverURL;
263: }
264:
265: public String getWmsVersion() {
266: return wmsVersion;
267: }
268:
269: public void setWmsVersion(String wmsVersion) {
270: this.wmsVersion = wmsVersion;
271: }
272: }
|