CustomTile.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui.mapView;
  2. import java.awt.image.BufferedImage;
  3. import java.lang.ref.SoftReference;
  4. import org.jxmapviewer.viewer.Tile;
  5. /**
  6. * The Tile class represents a particular square image piece of the world bitmap
  7. * at a particular zoom level.
  8. *
  9. * @author Dominik Renkel
  10. */
  11. public class CustomTile extends Tile {
  12. /**
  13. * Indicates that loading has succeeded. A PropertyChangeEvent will be fired
  14. * when the loading is completed
  15. */
  16. private boolean loaded = false;
  17. /**
  18. * loading priority
  19. */
  20. private Priority priority = Priority.High;
  21. /**
  22. * the tileFactory this tile is referenced to
  23. */
  24. private CustomTileFactory dtf;
  25. /**
  26. * is tile currently loading
  27. */
  28. private boolean isLoading = false;
  29. /**
  30. * The url of the image to load for this tile
  31. */
  32. private String url;
  33. /**
  34. * The image loaded for this Tile
  35. */
  36. SoftReference<BufferedImage> image = new SoftReference<BufferedImage>(null);
  37. /**
  38. * if image was loaded, when no internet connection was established, it
  39. * needs to reload
  40. */
  41. private Boolean reloadNeeded = false;
  42. /**
  43. * Create a new Tile at the specified tile point and zoom level
  44. *
  45. * @param x
  46. * the x value
  47. * @param y
  48. * the y value
  49. * @param zoom
  50. * the zoom level
  51. */
  52. public CustomTile(int x, int y, int zoom) {
  53. super(x, y, zoom);
  54. }
  55. /**
  56. * Create a new Tile that loads its data from the given URL. The URL must
  57. * resolve to an image
  58. *
  59. * @param x
  60. * the x value
  61. * @param y
  62. * the y value
  63. * @param zoom
  64. * the zoom level
  65. * @param url
  66. * the URL
  67. * @param priority
  68. * the priority
  69. * @param dtf
  70. * the tile factory
  71. */
  72. public CustomTile(int x, int y, int zoom, String url, Priority priority, CustomTileFactory dtf) {
  73. super(x, y, zoom);
  74. this.url = url;
  75. this.priority = priority;
  76. this.dtf = dtf;
  77. }
  78. /**
  79. * @return the Image associated with this Tile. This is a read only property
  80. * This may return null at any time, however if this returns null, a
  81. * load operation will automatically be started for it.
  82. */
  83. @Override
  84. public BufferedImage getImage() {
  85. BufferedImage img = image.get();
  86. if (img == null) {
  87. setLoaded(false);
  88. // tile factory can be null if the tile has invalid coords or zoom
  89. if (dtf != null) {
  90. dtf.startLoading(this);
  91. }
  92. }
  93. return img;
  94. }
  95. /**
  96. * Indicates if this tile's underlying image has been successfully loaded
  97. * yet.
  98. *
  99. * @return true if the Tile has been loaded
  100. */
  101. @Override
  102. public synchronized boolean isLoaded() {
  103. return loaded;
  104. }
  105. /**
  106. * Toggles the loaded state, and fires the appropriate property change
  107. * notification
  108. *
  109. * @param loaded
  110. * the loaded flag
  111. */
  112. public synchronized void setLoaded(boolean loaded) {
  113. boolean old = isLoaded();
  114. this.loaded = loaded;
  115. firePropertyChange("loaded", old, isLoaded());
  116. // image loaded, so no reload needed
  117. if (this.loaded) {
  118. this.reloadNeeded = false;
  119. }
  120. }
  121. /**
  122. * @return the isLoading
  123. */
  124. @Override
  125. public boolean isLoading() {
  126. return isLoading;
  127. }
  128. /**
  129. * @param isLoading
  130. * the isLoading to set
  131. */
  132. @Override
  133. public void setLoading(boolean isLoading) {
  134. this.isLoading = isLoading;
  135. }
  136. /**
  137. * tile could not be loaded, so reload when Internet connection is
  138. * reestablished
  139. *
  140. * @param reload
  141. */
  142. public void setReloadNeeded(boolean reload) {
  143. this.reloadNeeded = reload;
  144. }
  145. /**
  146. * get the reloadNeeded property
  147. *
  148. * @return reload needed
  149. */
  150. public Boolean getReloadNeeded() {
  151. return this.reloadNeeded;
  152. }
  153. /**
  154. * Gets the loading priority of this tile.
  155. *
  156. * @return the priority
  157. */
  158. @Override
  159. public Priority getPriority() {
  160. return priority;
  161. }
  162. /**
  163. * Set the loading priority of this tile.
  164. *
  165. * @param priority
  166. * the priority to set
  167. */
  168. @Override
  169. public void setPriority(Priority priority) {
  170. this.priority = priority;
  171. }
  172. /**
  173. * Gets the URL of this tile.
  174. *
  175. * @return the url
  176. */
  177. @Override
  178. public String getURL() {
  179. return url;
  180. }
  181. }