1 package org
.argeo
.gis
.ui
.rcp
.swing
;
5 import java
.util
.Collection
;
6 import java
.util
.Collections
;
7 import java
.util
.HashMap
;
10 import javax
.jcr
.Node
;
12 import org
.argeo
.ArgeoException
;
13 import org
.argeo
.geotools
.jcr
.GeoJcrMapper
;
14 import org
.argeo
.geotools
.styling
.StylingUtils
;
15 import org
.argeo
.gis
.ui
.AbstractMapViewer
;
16 import org
.eclipse
.swt
.SWT
;
17 import org
.eclipse
.swt
.awt
.SWT_AWT
;
18 import org
.eclipse
.swt
.widgets
.Composite
;
19 import org
.geotools
.data
.FeatureSource
;
20 import org
.geotools
.geometry
.jts
.ReferencedEnvelope
;
21 import org
.geotools
.map
.DefaultMapContext
;
22 import org
.geotools
.map
.DefaultMapLayer
;
23 import org
.geotools
.map
.MapContext
;
24 import org
.geotools
.map
.MapLayer
;
25 import org
.geotools
.referencing
.CRS
;
26 import org
.geotools
.renderer
.lite
.StreamingRenderer
;
27 import org
.geotools
.styling
.Style
;
28 import org
.geotools
.swing
.JMapPane
;
29 import org
.opengis
.feature
.simple
.SimpleFeature
;
30 import org
.opengis
.feature
.simple
.SimpleFeatureType
;
31 import org
.opengis
.referencing
.crs
.CoordinateReferenceSystem
;
33 /** Map viewer implementation based on GeoTools Swing components. */
34 public class SwingMapViewer
extends AbstractMapViewer
{
35 private Composite embedded
;
36 private JMapPane mapPane
;
37 private VersatileZoomTool versatileZoomTool
;
39 private Map
<String
, MapLayer
> mapLayers
= Collections
40 .synchronizedMap(new HashMap
<String
, MapLayer
>());
42 public SwingMapViewer(Node context
, GeoJcrMapper geoJcrMapper
,
44 super(context
, geoJcrMapper
);
46 embedded
= new Composite(parent
, SWT
.EMBEDDED
| SWT
.NO_BACKGROUND
);
47 Frame frame
= SWT_AWT
.new_Frame(embedded
);
49 MapContext mapContext
= new DefaultMapContext();
50 // dummy call to make sure that the layers are initialized
52 mapPane
= new JMapPane(new StreamingRenderer(), mapContext
);
53 versatileZoomTool
= new VersatileZoomTool();
54 mapPane
.setCursorTool(versatileZoomTool
);
55 mapPane
.setBackground(Color
.WHITE
);
62 protected void addFeatureSource(String layerId
,
63 FeatureSource
<SimpleFeatureType
, SimpleFeature
> featureSource
,
66 style
= StylingUtils
.createLineStyle("BLACK", 1);
68 MapLayer mapLayer
= new DefaultMapLayer(featureSource
, (Style
) style
);
69 addMapLayer(layerId
, mapLayer
);
72 protected void addMapLayer(String layerId
, MapLayer mapLayer
) {
73 mapLayers
.put(layerId
, mapLayer
);
74 mapPane
.getMapContext().addLayer(mapLayer
);
77 public void addLayer(String layerId
, Collection
<?
> collection
, Object style
) {
79 style
= StylingUtils
.createLineStyle("BLACK", 1);
80 MapLayer mapLayer
= new DefaultMapLayer(collection
, (Style
) style
);
81 addMapLayer(layerId
, mapLayer
);
84 public void setStyle(String layerId
, Object style
) {
85 mapLayers
.get(layerId
).setStyle((Style
) style
);
88 public void setAreaOfInterest(ReferencedEnvelope areaOfInterest
) {
89 // mapPane.getMapContext().setAreaOfInterest(areaOfInterest);
90 CoordinateReferenceSystem crs
= mapPane
.getMapContext()
91 .getCoordinateReferenceSystem();
93 ReferencedEnvelope toDisplay
;
96 toDisplay
= areaOfInterest
.transform(crs
, true);
97 } catch (Exception e
) {
98 throw new ArgeoException("Cannot reproject " + areaOfInterest
,
102 toDisplay
= areaOfInterest
;
103 mapPane
.setDisplayArea(toDisplay
);
106 public void setCoordinateReferenceSystem(String crs
) {
108 CoordinateReferenceSystem crsObj
= CRS
.decode(crs
);
109 mapPane
.getMapContext().setCoordinateReferenceSystem(crsObj
);
111 } catch (Exception e
) {
112 throw new ArgeoException("Cannot set CRS '" + crs
+ "'", e
);