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.2KB

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