1 | package org.openstreetmap.josm.plugins.openLayers;
|
---|
2 |
|
---|
3 | import java.awt.Dimension;
|
---|
4 | import java.awt.Graphics2D;
|
---|
5 | import java.beans.PropertyChangeEvent;
|
---|
6 | import java.beans.PropertyChangeListener;
|
---|
7 |
|
---|
8 | import javax.swing.Action;
|
---|
9 | import javax.swing.Icon;
|
---|
10 |
|
---|
11 | import org.mozilla.javascript.NativeArray;
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
14 | import org.openstreetmap.josm.data.Bounds;
|
---|
15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
17 | import org.openstreetmap.josm.gui.MapView;
|
---|
18 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
19 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
20 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Class that displays a OpenLayers layer.
|
---|
25 | *
|
---|
26 | * @author Francisco R. Santos <frsantos@gmail.com>
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public class OpenLayersLayer extends Layer implements PropertyChangeListener, MyHtmlBlockPanel.ViewUpdateListener {
|
---|
30 |
|
---|
31 | private Browser browser;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Creates the layer
|
---|
35 | */
|
---|
36 | public OpenLayersLayer() {
|
---|
37 | super("OpenLayers");
|
---|
38 |
|
---|
39 | this.browser = new Browser(OpenLayersPlugin.pluginDir + "yahoo.html", this);
|
---|
40 |
|
---|
41 | if( Main.map != null )
|
---|
42 | {
|
---|
43 | LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight());
|
---|
44 | LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0);
|
---|
45 | browser.executeAsyncScript("zoomMapToExtent(" + bottomLeft.lon() + ","
|
---|
46 | + bottomLeft.lat() + "," + topRight.lon() + "," + topRight.lat() + ")");
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Draws current map onto the graphics
|
---|
52 | */
|
---|
53 | @Override
|
---|
54 | public void paint(Graphics2D g, MapView mv, Bounds bounds) {
|
---|
55 | setSize(Main.map.mapView.getSize());
|
---|
56 | browser.paint(g);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Sets the size of the layer
|
---|
61 | */
|
---|
62 | public void setSize(Dimension dim) {
|
---|
63 | browser.setSize(dim);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public Icon getIcon() {
|
---|
68 | return ImageProvider.get("OpenLayers");
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public Object getInfoComponent() {
|
---|
73 | return null;
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public Action[] getMenuEntries() {
|
---|
78 | return new Action[] {
|
---|
79 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
80 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
81 | SeparatorLayerAction.INSTANCE,
|
---|
82 | // color,
|
---|
83 | new RenameLayerAction(getAssociatedFile(), this),
|
---|
84 | SeparatorLayerAction.INSTANCE,
|
---|
85 | new LayerListPopup.InfoAction(this) };
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public String getToolTipText() {
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public boolean isMergable(Layer other) {
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public void mergeFrom(Layer from) {
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void destroy() {
|
---|
108 | if( Main.map != null )
|
---|
109 | Main.map.mapView.removePropertyChangeListener(this);
|
---|
110 |
|
---|
111 | OpenLayersPlugin.layer = null;
|
---|
112 | StorageManager.flush();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
116 | if( !isVisible() )
|
---|
117 | return;
|
---|
118 |
|
---|
119 | String prop = evt.getPropertyName();
|
---|
120 | if ("center".equals(prop) || "scale".equals(prop)) {
|
---|
121 | zoomToMapView();
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void zoomToMapView()
|
---|
126 | {
|
---|
127 | LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight());
|
---|
128 | LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0);
|
---|
129 | Object value = browser.executeScript("zoomMapToExtent(" + bottomLeft.lon() + "," + bottomLeft.lat() + "," + topRight.lon() + "," + topRight.lat() + ")");
|
---|
130 | if (value != null && false) {
|
---|
131 | // TODO: should not be touching Main.map.mapView here as this
|
---|
132 | // causes endless recurrency - should do the scalling ourselves
|
---|
133 |
|
---|
134 | // Get actual extent from browser
|
---|
135 | NativeArray array = (NativeArray)value;
|
---|
136 | double left = ((Double)array.get(0, null)).doubleValue();
|
---|
137 | double bottom = ((Double)array.get(1, null)).doubleValue();
|
---|
138 | double right = ((Double)array.get(2, null)).doubleValue();
|
---|
139 | double top = ((Double)array.get(3, null)).doubleValue();
|
---|
140 | bottomLeft = new LatLon(bottom, left);
|
---|
141 | topRight = new LatLon(top, right);
|
---|
142 |
|
---|
143 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
144 | v.visit(Main.proj.latlon2eastNorth(bottomLeft));
|
---|
145 | v.visit(Main.proj.latlon2eastNorth(topRight));
|
---|
146 | System.out.println("Recalculating position (" + left + "," + bottom + "," + right + "," + top + ")");
|
---|
147 | Main.map.mapView.recalculateCenterScale(v);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void region_update(int x, int y, int w, int h) {
|
---|
152 | String status = browser.getStatus();
|
---|
153 |
|
---|
154 | org.openstreetmap.josm.Main.map.mapView.repaint(x, y, w, h);
|
---|
155 |
|
---|
156 | org.openstreetmap.josm.Main.map.statusLine.setHelpText(status);
|
---|
157 | }
|
---|
158 | }
|
---|