You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GoogleMap.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo.reservation;
  5. import java.awt.geom.Point2D;
  6. import java.util.Collection;
  7. import java.util.Iterator;
  8. import com.itmill.toolkit.data.Container;
  9. import com.itmill.toolkit.data.Item;
  10. import com.itmill.toolkit.data.Property;
  11. import com.itmill.toolkit.data.util.IndexedContainer;
  12. import com.itmill.toolkit.terminal.PaintException;
  13. import com.itmill.toolkit.terminal.PaintTarget;
  14. import com.itmill.toolkit.terminal.Sizeable;
  15. import com.itmill.toolkit.ui.AbstractComponent;
  16. public class GoogleMap extends AbstractComponent implements Sizeable,
  17. Container.Viewer {
  18. private final String TAG_MARKERS = "markers";
  19. private final String TAG_MARKER = "marker";
  20. private int width = 400;
  21. private int height = 300;
  22. private int zoomLevel = 15;
  23. private Point2D.Double mapCenter;
  24. private Container dataSource;
  25. private Object itemMarkerHtmlPropertyId = new Object();
  26. private Object itemMarkerXPropertyId = new Object();
  27. private Object itemMarkerYPropertyId = new Object();
  28. public String getTag() {
  29. return "googlemap";
  30. }
  31. public void paintContent(PaintTarget target) throws PaintException {
  32. super.paintContent(target);
  33. if (null != mapCenter) {
  34. target.addAttribute("centerX", mapCenter.getX());
  35. target.addAttribute("centerY", mapCenter.getY());
  36. }
  37. target.addAttribute("zoom", zoomLevel);
  38. target.addAttribute("width", width);
  39. target.addAttribute("height", height);
  40. if (dataSource != null) {
  41. target.startTag(TAG_MARKERS);
  42. final Collection itemIds = dataSource.getItemIds();
  43. for (final Iterator it = itemIds.iterator(); it.hasNext();) {
  44. final Object itemId = it.next();
  45. final Item item = dataSource.getItem(itemId);
  46. Property p = item.getItemProperty(getItemMarkerXPropertyId());
  47. final Double x = (Double) (p != null ? p.getValue() : null);
  48. p = item.getItemProperty(getItemMarkerYPropertyId());
  49. final Double y = (Double) (p != null ? p.getValue() : null);
  50. if (x == null || y == null) {
  51. continue;
  52. }
  53. target.startTag(TAG_MARKER);
  54. target.addAttribute("x", x.doubleValue());
  55. target.addAttribute("y", y.doubleValue());
  56. p = item.getItemProperty(getItemMarkerHtmlPropertyId());
  57. final String h = (String) (p != null ? p.getValue() : null);
  58. target.addAttribute("html", h);
  59. target.endTag(TAG_MARKER);
  60. }
  61. target.endTag(TAG_MARKERS);
  62. }
  63. }
  64. public void setZoomLevel(int zoomLevel) {
  65. this.zoomLevel = zoomLevel;
  66. requestRepaint();
  67. }
  68. public int getZoomLevel() {
  69. return zoomLevel;
  70. }
  71. // Sizeable methods:
  72. public int getHeight() {
  73. return height;
  74. }
  75. public int getHeightUnits() {
  76. return Sizeable.UNITS_PIXELS;
  77. }
  78. public int getWidth() {
  79. return width;
  80. }
  81. public int getWidthUnits() {
  82. return Sizeable.UNITS_PIXELS;
  83. }
  84. public void setHeight(int height) {
  85. this.height = height;
  86. requestRepaint();
  87. }
  88. public void setHeightUnits(int units) {
  89. throw new UnsupportedOperationException();
  90. }
  91. public void setWidth(int width) {
  92. this.width = width;
  93. requestRepaint();
  94. }
  95. public void setWidthUnits(int units) {
  96. throw new UnsupportedOperationException();
  97. }
  98. public void setMapCenter(Point2D.Double center) {
  99. mapCenter = center;
  100. }
  101. public Point2D.Double getMapCenter() {
  102. return mapCenter;
  103. }
  104. // Container.Viewer methods:
  105. public Container getContainerDataSource() {
  106. return dataSource;
  107. }
  108. public void setContainerDataSource(Container newDataSource) {
  109. dataSource = newDataSource;
  110. requestRepaint();
  111. }
  112. // Item methods
  113. public Object getItemMarkerHtmlPropertyId() {
  114. return itemMarkerHtmlPropertyId;
  115. }
  116. public void setItemMarkerHtmlPropertyId(Object itemMarkerHtmlPropertyId) {
  117. this.itemMarkerHtmlPropertyId = itemMarkerHtmlPropertyId;
  118. requestRepaint();
  119. }
  120. public Object getItemMarkerXPropertyId() {
  121. return itemMarkerXPropertyId;
  122. }
  123. public void setItemMarkerXPropertyId(Object itemMarkerXPropertyId) {
  124. this.itemMarkerXPropertyId = itemMarkerXPropertyId;
  125. requestRepaint();
  126. }
  127. public Object getItemMarkerYPropertyId() {
  128. return itemMarkerYPropertyId;
  129. }
  130. public void setItemMarkerYPropertyId(Object itemMarkerYPropertyId) {
  131. this.itemMarkerYPropertyId = itemMarkerYPropertyId;
  132. requestRepaint();
  133. }
  134. // Marker add
  135. public Object addMarker(String html, Point2D.Double location) {
  136. if (location == null) {
  137. throw new IllegalArgumentException("Location must be non-null");
  138. }
  139. if (dataSource == null) {
  140. initDataSource();
  141. }
  142. final Object markerId = dataSource.addItem();
  143. if (markerId == null) {
  144. return null;
  145. }
  146. final Item marker = dataSource.getItem(markerId);
  147. Property p = marker.getItemProperty(getItemMarkerXPropertyId());
  148. p.setValue(new Double(location.x));
  149. p = marker.getItemProperty(getItemMarkerYPropertyId());
  150. p.setValue(new Double(location.y));
  151. p = marker.getItemProperty(getItemMarkerHtmlPropertyId());
  152. p.setValue(html);
  153. requestRepaint();
  154. return markerId;
  155. }
  156. public void removeMarker(Object markerId) {
  157. if (dataSource != null) {
  158. dataSource.removeItem(markerId);
  159. requestRepaint();
  160. }
  161. }
  162. public Item getMarkerItem(Object markerId) {
  163. if (dataSource != null) {
  164. return dataSource.getItem(markerId);
  165. } else {
  166. return null;
  167. }
  168. }
  169. // dataSource init helper:
  170. private void initDataSource() {
  171. dataSource = new IndexedContainer();
  172. dataSource.addContainerProperty(itemMarkerHtmlPropertyId, String.class,
  173. null);
  174. dataSource.addContainerProperty(itemMarkerXPropertyId, Double.class,
  175. new Double(0));
  176. dataSource.addContainerProperty(itemMarkerYPropertyId, Double.class,
  177. new Double(0));
  178. }
  179. public void clear() {
  180. setContainerDataSource(null);
  181. }
  182. public void setSizeFull() {
  183. // TODO Auto-generated method stub
  184. }
  185. public void setSizeUndefined() {
  186. // TODO Auto-generated method stub
  187. }
  188. }