MapViewFunctions.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui.mapView;
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferedImage;
  5. import java.util.HashMap;
  6. import javax.imageio.ImageIO;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.jxmapviewer.JXMapViewer;
  10. import org.jxmapviewer.OSMTileFactoryInfo;
  11. import org.jxmapviewer.VirtualEarthTileFactoryInfo;
  12. import org.jxmapviewer.painter.Painter;
  13. import org.jxmapviewer.viewer.TileFactoryInfo;
  14. import org.jxmapviewer.viewer.WaypointPainter;
  15. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  16. import javafx.beans.binding.Bindings;
  17. import javafx.scene.control.CheckBox;
  18. import javafx.scene.control.ContextMenu;
  19. import javafx.scene.control.MenuItem;
  20. import javafx.scene.input.ContextMenuEvent;
  21. public final class MapViewFunctions {
  22. private static final Log log = LogFactory.getLog(MapViewFunctions.class);
  23. /**
  24. * Hash map to save, scaled images
  25. */
  26. public static HashMap<String, BufferedImage> imageMap = new HashMap<String, BufferedImage>();
  27. /**
  28. * the selected mapType "Default", "Road", "Satellite", "Hybrid"
  29. */
  30. private static String mapType = "Default";
  31. /**
  32. * private constructor to avoid instantiation
  33. */
  34. private MapViewFunctions() {
  35. }
  36. /**
  37. * resets the hash map with the pictures
  38. */
  39. public static void resetImageMap() {
  40. imageMap = new HashMap<String, BufferedImage>();
  41. }
  42. /**
  43. * load and scale waypoint images and save them in a HashMap
  44. */
  45. public static void initializeWaypointImages() {
  46. if (WorldView.getWaypoints() == null) {
  47. return;
  48. }
  49. imageMap = new HashMap<String, BufferedImage>(WorldView.getWaypoints().size());
  50. for (CustomWaypoint w : WorldView.getWaypoints()) {
  51. BufferedImage origImage = null;
  52. // image not loaded
  53. if (!imageMap.containsKey(w.getDeviceType())) {
  54. // try load image
  55. try {
  56. origImage = ImageIO.read(w.getResource());
  57. } catch (Exception ex) {
  58. log.warn("couldn't read Waypoint png", ex);
  59. }
  60. // loading complete
  61. if (origImage != null) {
  62. // scale image down
  63. BufferedImage myImg = MapViewFunctions.scaleImage(origImage, CustomWaypointRenderer.SCALEWIDTH,
  64. CustomWaypointRenderer.SCALEHEIGHT);
  65. // save image in hash map
  66. imageMap.put(w.getDeviceType(), myImg);
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * Scale a given BufferedImage down to given width w and given height h
  73. *
  74. * @param loadImg
  75. * @param w
  76. * @param h
  77. * @return
  78. */
  79. public static BufferedImage scaleImage(BufferedImage loadImg, int w, int h) {
  80. BufferedImage imgOut = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  81. Graphics2D graphics = imgOut.createGraphics();
  82. graphics.drawImage(loadImg, 0, 0, w, h, null);
  83. graphics.dispose();
  84. return imgOut;
  85. }
  86. /**
  87. * change all pixels from a given color to another given color (under a
  88. * given alpha value)
  89. *
  90. * @param image
  91. * @param toChange
  92. * change all pixels with this color to another color
  93. * @param changeWith
  94. * new color
  95. * @param alpha
  96. * @return
  97. */
  98. public static BufferedImage colorImage(BufferedImage image, Color toChange, Color changeWith, int alpha) {
  99. int width = image.getWidth();
  100. int height = image.getHeight();
  101. BufferedImage imgOut = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  102. for (int xx = 0; xx < width; xx++) {
  103. for (int yy = 0; yy < height; yy++) {
  104. Color originalColor = new Color(image.getRGB(xx, yy), true);
  105. // pixel needs to be changed
  106. if (originalColor.equals(toChange) && originalColor.getAlpha() == alpha) {
  107. imgOut.setRGB(xx, yy, changeWith.getRGB());
  108. }
  109. }
  110. }
  111. return imgOut;
  112. }
  113. /**
  114. * Returns either an EdgePainter (case input "edge") or a WaypointPainter
  115. * (case input "waypoint") based on input
  116. *
  117. * @param mode
  118. * 0 or 1
  119. * @return EdgePainter or WaypointPainter if existing in CompoundPainter
  120. * otherwise null
  121. */
  122. private static Painter<JXMapViewer> getRequestedPainter(String requested) {
  123. // return value
  124. switch (requested) {
  125. case "edge":
  126. return WorldView.edgePainter;
  127. case "waypoint":
  128. return WorldView.waypointPainter;
  129. default:
  130. return null;
  131. }
  132. }
  133. /**
  134. * change the shown map based on the given string
  135. *
  136. * @param string
  137. */
  138. public static void changeMapView(String selected) {
  139. mapType = selected;
  140. switch (selected) {
  141. case "Default":
  142. TileFactoryInfo defaultTileFactoryInfo = new OSMTileFactoryInfo();
  143. WorldView.internMapViewer.setTileFactory(new CustomTileFactory(defaultTileFactoryInfo));
  144. break;
  145. case "Road":
  146. TileFactoryInfo roadTileFactoryInfo = new VirtualEarthTileFactoryInfo(VirtualEarthTileFactoryInfo.MAP);
  147. WorldView.internMapViewer.setTileFactory(new CustomTileFactory(roadTileFactoryInfo));
  148. break;
  149. case "Satellite":
  150. TileFactoryInfo sateliteTileFactoryInfo = new VirtualEarthTileFactoryInfo(
  151. VirtualEarthTileFactoryInfo.SATELLITE);
  152. WorldView.internMapViewer.setTileFactory(new CustomTileFactory(sateliteTileFactoryInfo));
  153. break;
  154. case "Hybrid":
  155. TileFactoryInfo hybridTileFactoryInfo = new VirtualEarthTileFactoryInfo(VirtualEarthTileFactoryInfo.HYBRID);
  156. WorldView.internMapViewer.setTileFactory(new CustomTileFactory(hybridTileFactoryInfo));
  157. break;
  158. }
  159. }
  160. /**
  161. * Check if Checkboxes or mapType where changed, last time symbol-rep. layer
  162. * was shown
  163. */
  164. public static void checkVBoxChanged() {
  165. EdgePainter edgePainter = (EdgePainter) getRequestedPainter("edge");
  166. WaypointPainter<CustomWaypoint> waypointPainter = (WaypointPainter<CustomWaypoint>) getRequestedPainter(
  167. "waypoint");
  168. // Checkboxes were changed last time symbolRep-Layer was shown
  169. if (!WorldView.controller.edgesVisibleCheckbox.isSelected()) {
  170. edgePainter.setShowEdges(false);
  171. }
  172. if (!WorldView.controller.nodeLabelCheckbox.isSelected()) {
  173. CustomWaypointRenderer renderer = new CustomWaypointRenderer();
  174. renderer.setShowLabels(false);
  175. waypointPainter.setRenderer(renderer);
  176. }
  177. if (!WorldView.controller.edgeWeightCheckbox.isSelected()) {
  178. edgePainter.setShowWeights(false);
  179. }
  180. if (!mapType.equals("Default")) {
  181. changeMapView(mapType);
  182. }
  183. }
  184. /**
  185. * Show a ContextMenu when map was right clicked to change symbol layer
  186. * checkbox properties
  187. *
  188. * @param event
  189. * contextMenu mouse event
  190. */
  191. public static void contextMenuRequest(ContextMenuEvent event) {
  192. // Declare context menu and items
  193. final ContextMenu menu = new ContextMenu();
  194. final MenuItem edgeVisible = new MenuItem("Hide Edges");
  195. final MenuItem weightVisible = new MenuItem("Hide Weights");
  196. final MenuItem labelVisible = new MenuItem("Hide Labels");
  197. // the checkboxes in dhe symbol layer
  198. CheckBox edgeCheckbox = WorldView.controller.edgesVisibleCheckbox;
  199. CheckBox weightCheckbox = WorldView.controller.edgeWeightCheckbox;
  200. CheckBox labelCheckbox = WorldView.controller.nodeLabelCheckbox;
  201. // define the actions when clicked on menu item
  202. edgeVisible.setOnAction((actionEvent) -> {
  203. if (edgeCheckbox.isSelected()) {
  204. edgeCheckbox.setSelected(false);
  205. } else {
  206. edgeCheckbox.setSelected(true);
  207. }
  208. });
  209. weightVisible.setOnAction((actionEvent) -> {
  210. if (weightCheckbox.isSelected()) {
  211. weightCheckbox.setSelected(false);
  212. } else {
  213. weightCheckbox.setSelected(true);
  214. }
  215. });
  216. labelVisible.setOnAction((actionEvent) -> {
  217. if (labelCheckbox.isSelected()) {
  218. labelCheckbox.setSelected(false);
  219. } else {
  220. labelCheckbox.setSelected(true);
  221. }
  222. });
  223. // bind the text properties to the menu item, so that they change
  224. // depending on the selection property
  225. edgeVisible.textProperty()
  226. .bind(Bindings.when(edgeCheckbox.selectedProperty()).then("Hide Edges").otherwise("Show Edges"));
  227. weightVisible.textProperty()
  228. .bind(Bindings.when(weightCheckbox.selectedProperty()).then("Hide Weights").otherwise("Show Weights"));
  229. labelVisible.textProperty()
  230. .bind(Bindings.when(labelCheckbox.selectedProperty()).then("Hide Labels").otherwise("Show Labels"));
  231. menu.getItems().addAll(edgeVisible, weightVisible, labelVisible);
  232. // show context menu at the clicked point
  233. menu.show(Main.getInstance().getPrimaryStage(), event.getScreenX(), event.getScreenY());
  234. }
  235. /**
  236. * switch to previous Waypoint
  237. */
  238. public static void switchToPreviousWaypoint() {
  239. CustomWaypoint selectedWaypoint = CustomMapClickListener.selectedNode;
  240. if (selectedWaypoint == null && WorldView.getWaypointsAsArrayList().size() > 0) {
  241. CustomMapClickListener.selectWaypoint(WorldView.getWaypointsAsArrayList().get(0));
  242. } else {
  243. int index = WorldView.getWaypointsAsArrayList().indexOf(selectedWaypoint);
  244. if (index == 0) {
  245. CustomMapClickListener.selectWaypoint(
  246. WorldView.getWaypointsAsArrayList().get(WorldView.getWaypointsAsArrayList().size() - 1));
  247. } else {
  248. CustomMapClickListener.selectWaypoint(WorldView.getWaypointsAsArrayList().get(index - 1));
  249. }
  250. }
  251. }
  252. /**
  253. * switch to next Waypoint
  254. */
  255. public static void switchToNextWaypoint() {
  256. CustomWaypoint selectedWaypoint = CustomMapClickListener.selectedNode;
  257. if (selectedWaypoint == null && WorldView.getWaypointsAsArrayList().size() > 0) {
  258. CustomMapClickListener.selectWaypoint(WorldView.getWaypointsAsArrayList().get(0));
  259. } else {
  260. int index = WorldView.getWaypointsAsArrayList().indexOf(selectedWaypoint);
  261. if (index == WorldView.getWaypointsAsArrayList().size() - 1) {
  262. CustomMapClickListener.selectWaypoint(WorldView.getWaypointsAsArrayList().get(0));
  263. } else {
  264. CustomMapClickListener.selectWaypoint(WorldView.getWaypointsAsArrayList().get(index + 1));
  265. }
  266. }
  267. }
  268. }