Browse Source

Added MapView class

sorted functionality to different classes 
documented code
dominik 8 năm trước cách đây
mục cha
commit
276b1e894c

+ 41 - 82
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/ButtonManager.java

@@ -1,29 +1,17 @@
 package de.tu_darmstadt.informatik.tk.scopviz.ui;
 
 import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
 
-import org.graphstream.graph.Edge;
 import org.graphstream.graph.implementations.Graphs;
-import org.jxmapviewer.JXMapViewer;
-import org.jxmapviewer.OSMTileFactoryInfo;
-import org.jxmapviewer.painter.CompoundPainter;
-import org.jxmapviewer.painter.Painter;
-import org.jxmapviewer.viewer.DefaultTileFactory;
-import org.jxmapviewer.viewer.GeoPosition;
-import org.jxmapviewer.viewer.TileFactoryInfo;
 import org.jxmapviewer.viewer.WaypointPainter;
 
-import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
 import de.tu_darmstadt.informatik.tk.scopviz.main.MyGraph;
-import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.CustomMapClickListener;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.CustomWaypoint;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.CustomWaypointRenderer;
-import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.EdgePainter;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.MapViewFunctions;
+import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.WorldView;
 import javafx.beans.value.ObservableValue;
 import javafx.event.ActionEvent;
 import javafx.scene.control.Button;
@@ -45,8 +33,6 @@ public final class ButtonManager {
 
 	private static GUIController controller;
 
-	private static JXMapViewer internMapViewer;
-
 	/**
 	 * Private Constructor to prevent Instantiation.
 	 */
@@ -66,16 +52,12 @@ public final class ButtonManager {
 		setBorderStyle(uButton);
 	}
 
-	public static void setViewer(JXMapViewer mapViewer) {
-		internMapViewer = mapViewer;
-	}
-
 	/**
 	 * Handler for zoom in Button
 	 */
 	public static final void zoomInAction(ActionEvent event) {
 		if (GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL)) {
-			internMapViewer.setZoom(internMapViewer.getZoom() - 1);
+			WorldView.internMapViewer.setZoom(WorldView.internMapViewer.getZoom() - 1);
 		} else {
 			Main.getInstance().getGraphManager().zoomIn();
 		}
@@ -86,7 +68,7 @@ public final class ButtonManager {
 	 */
 	public static final void zoomOutAction(ActionEvent event) {
 		if (GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL)) {
-			internMapViewer.setZoom(internMapViewer.getZoom() + 1);
+			WorldView.internMapViewer.setZoom(WorldView.internMapViewer.getZoom() + 1);
 		} else {
 			Main.getInstance().getGraphManager().zoomOut();
 		}
@@ -99,17 +81,23 @@ public final class ButtonManager {
 	private static void switchfromSymbolLayer() {
 
 		if (GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL)) {
+
+			// show toolbox and hide VBox
 			controller.toolbox.setVisible(true);
 			controller.symbolToolVBox.setVisible(false);
 
+			// make properties editable again
 			controller.propertiesObjectColumn.setEditable(true);
 
+			// show graph instead of map view
 			controller.swingNodeWorldView.setVisible(false);
 			controller.swingNode.setVisible(true);
 
+			// make map view mouse transparent
 			controller.stackPane.setMouseTransparent(true);
 			controller.swingNodeWorldView.setMouseTransparent(true);
 
+			// make graph non mouse transparent
 			controller.pane.setMouseTransparent(false);
 			controller.swingNode.setMouseTransparent(false);
 
@@ -164,15 +152,6 @@ public final class ButtonManager {
 	public static final void symbolRepAction(ActionEvent arg0) {
 
 		if (!(GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL))) {
-			controller.toolbox.setVisible(false);
-			controller.swingNode.setVisible(false);
-			controller.propertiesObjectColumn.setEditable(false);
-			controller.stackPane.setMouseTransparent(false);
-			controller.swingNodeWorldView.setMouseTransparent(false);
-
-			controller.pane.setMouseTransparent(true);
-			controller.swingNode.setMouseTransparent(true);
-			controller.symbolToolVBox.setVisible(true);
 
 			// add a copy of the underlay graph to the the symbol layer
 			MyGraph gClone = (MyGraph) Graphs.clone(GraphDisplayManager.getGraphManager(Layer.UNDERLAY).getGraph());
@@ -193,49 +172,32 @@ public final class ButtonManager {
 	 */
 	private static void activateWorldView() {
 
-		HashSet<GeoPosition> nodePositions = new HashSet<GeoPosition>();
-		HashSet<CustomWaypoint> waypoints = new HashSet<CustomWaypoint>();
-		HashSet<Edge> edges = new HashSet<Edge>();
-
-		// Get GeoPositions of nodes and get all waypoints created
-		MapViewFunctions.fetchGraphData(nodePositions, waypoints, edges);
-
-		// Create a line for all edges
-		EdgePainter edgePainter = new EdgePainter(edges);
-
-		// Create a waypoint painter that takes all the waypoints
-		WaypointPainter<CustomWaypoint> waypointPainter = new WaypointPainter<CustomWaypoint>();
-		waypointPainter.setWaypoints(waypoints);
-		waypointPainter.setRenderer(new CustomWaypointRenderer());
-
-		// Create a compound painter that uses all painters
-		List<Painter<JXMapViewer>> painters = new ArrayList<Painter<JXMapViewer>>();
-		painters.add(waypointPainter);
-		painters.add(edgePainter);
+		// dont show graph and toolbox
+		controller.toolbox.setVisible(false);
+		controller.swingNode.setVisible(false);
 
-		CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
+		// make properties uneditable
+		controller.propertiesObjectColumn.setEditable(false);
 
-		// Create a TileFactoryInfo for OpenStreetMap
-		TileFactoryInfo info = new OSMTileFactoryInfo();
-		DefaultTileFactory tileFactory = new DefaultTileFactory(info);
-		internMapViewer.setTileFactory(tileFactory);
+		// make map view non mouse transparent
+		controller.stackPane.setMouseTransparent(false);
+		controller.swingNodeWorldView.setMouseTransparent(false);
 
-		// Use 8 threads in parallel to load the tiles
-		tileFactory.setThreadPoolSize(8);
+		// make graph mouse transparent
+		controller.pane.setMouseTransparent(true);
+		controller.swingNode.setMouseTransparent(true);
 
-		// set Zoom and Center to show all node positions
-		internMapViewer.zoomToBestFit(nodePositions, 0.7);
+		// show VBox for map options
+		controller.symbolToolVBox.setVisible(true);
 
-		internMapViewer.setOverlayPainter(painter);
-
-		// "click on waypoints" listener
-		internMapViewer.addMouseListener(new CustomMapClickListener(internMapViewer, waypoints, edges));
+		WorldView.loadWorldView();
 
 		MapViewFunctions.checkVBoxChanged();
 
-		internMapViewer.repaint();
+		WorldView.internMapViewer.repaint();
 
-		controller.swingNodeWorldView.setContent(internMapViewer);
+		// set content to UI Element
+		controller.swingNodeWorldView.setContent(WorldView.internMapViewer);
 		controller.swingNodeWorldView.setVisible(true);
 	}
 
@@ -250,16 +212,15 @@ public final class ButtonManager {
 	 */
 	public static void edgeVisibleSwitch(ObservableValue<? extends Boolean> ov, Boolean oldVal, Boolean newVal) {
 
-		EdgePainter edgePainter = (EdgePainter) MapViewFunctions.getRequestedPainter("edge");
-
 		// Show edges
 		if (newVal) {
-			edgePainter.setShowEdges(true);
-			internMapViewer.repaint();
-			// Hide edges
+			WorldView.edgePainter.setShowEdges(true);
+			WorldView.internMapViewer.repaint();
+
 		} else {
-			edgePainter.setShowEdges(false);
-			internMapViewer.repaint();
+			// Hide edges
+			WorldView.edgePainter.setShowEdges(false);
+			WorldView.internMapViewer.repaint();
 		}
 	}
 
@@ -274,9 +235,7 @@ public final class ButtonManager {
 	 */
 	public static void labelVisibilitySwitcher(ObservableValue<? extends Boolean> ov, Boolean oldVal, Boolean newVal) {
 
-		WaypointPainter<CustomWaypoint> waypointPainter = (WaypointPainter<CustomWaypoint>) MapViewFunctions
-				.getRequestedPainter("waypoint");
-
+		WaypointPainter<CustomWaypoint> waypointPainter = WorldView.waypointPainter;
 		CustomWaypointRenderer renderer = new CustomWaypointRenderer();
 
 		// Show node labels
@@ -284,13 +243,14 @@ public final class ButtonManager {
 			renderer.setShowLabels(true);
 			waypointPainter.clearCache();
 			waypointPainter.setRenderer(renderer);
-			internMapViewer.repaint();
-			// Hide node labels
+			WorldView.internMapViewer.repaint();
+
 		} else {
+			// Hide node labels
 			renderer.setShowLabels(false);
 			waypointPainter.clearCache();
 			waypointPainter.setRenderer(renderer);
-			internMapViewer.repaint();
+			WorldView.internMapViewer.repaint();
 		}
 	}
 
@@ -306,15 +266,14 @@ public final class ButtonManager {
 	public static void edgeWeightVisibilitySwitcher(ObservableValue<? extends Boolean> ov, Boolean oldVal,
 			Boolean newVal) {
 
-		EdgePainter edgePainter = (EdgePainter) MapViewFunctions.getRequestedPainter("edge");
 		// Show Edges weights
 		if (newVal) {
-			edgePainter.setShowWeights(true);
-			internMapViewer.repaint();
+			WorldView.edgePainter.setShowWeights(true);
+			WorldView.internMapViewer.repaint();
 			// Hide Edges weights
 		} else {
-			edgePainter.setShowWeights(false);
-			internMapViewer.repaint();
+			WorldView.edgePainter.setShowWeights(false);
+			WorldView.internMapViewer.repaint();
 		}
 	}
 

+ 2 - 6
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/GUIController.java

@@ -17,7 +17,7 @@ import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.KeyboardShortcuts;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyAnimationTimer;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.ResizeListener;
-import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.MapViewFunctions;
+import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.WorldView;
 import javafx.beans.value.ChangeListener;
 import javafx.beans.value.ObservableValue;
 import javafx.collections.FXCollections;
@@ -177,18 +177,14 @@ public class GUIController implements Initializable {
 
 		JXMapViewer mapViewer = new JXMapViewer();
 
-		ButtonManager.setViewer(mapViewer);
-		MapViewFunctions.initialize(this, mapViewer);
+		WorldView.initAttributes(mapViewer, this);
 
 		// center map if double clicked / middle clicked
 		mapViewer.addMouseListener(new CenterMapListener(mapViewer));
-
 		// zoom with mousewheel
 		mapViewer.addMouseWheelListener(new ZoomMouseWheelListenerCursor(mapViewer));
-
 		// TODO make this work
 		mapViewer.addKeyListener(new PanKeyListener(mapViewer));
-
 		// "Drag map around" Listener
 		MouseInputListener mia = new PanMouseInputListener(mapViewer);
 		mapViewer.addMouseListener(mia);

+ 16 - 45
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/MapViewFunctions.java

@@ -11,7 +11,6 @@ import org.graphstream.graph.Node;
 import org.jxmapviewer.JXMapViewer;
 import org.jxmapviewer.OSMTileFactoryInfo;
 import org.jxmapviewer.VirtualEarthTileFactoryInfo;
-import org.jxmapviewer.painter.CompoundPainter;
 import org.jxmapviewer.painter.Painter;
 import org.jxmapviewer.viewer.DefaultTileFactory;
 import org.jxmapviewer.viewer.GeoPosition;
@@ -21,15 +20,10 @@ import org.jxmapviewer.viewer.WaypointPainter;
 import de.tu_darmstadt.informatik.tk.scopviz.main.GraphManager;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
 import de.tu_darmstadt.informatik.tk.scopviz.main.MainApp;
-import de.tu_darmstadt.informatik.tk.scopviz.ui.GUIController;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.GraphDisplayManager;
 
 public final class MapViewFunctions {
 
-	private static GUIController controller;
-
-	private static JXMapViewer mapViewer;
-
 	/**
 	 * private constructor to avoid instantiation
 	 */
@@ -37,11 +31,6 @@ public final class MapViewFunctions {
 
 	}
 
-	public static void initialize(GUIController guiController, JXMapViewer viewer) {
-		controller = guiController;
-		mapViewer = viewer;
-	}
-
 	/**
 	 * Scale a given BufferedImage down to given width w and given height h
 	 * 
@@ -165,36 +154,14 @@ public final class MapViewFunctions {
 	 * @return EdgePainter or WaypointPainter if existing in CompoundPainter
 	 *         otherwise null
 	 */
-	public static Painter<JXMapViewer> getRequestedPainter(String requested) {
-
-		// return types
-		EdgePainter edgePainter = null;
-		WaypointPainter<CustomWaypoint> waypointPainter = null;
-
-		// currently used compound painter
-		CompoundPainter<JXMapViewer> compPainter = null;
-
-		if (mapViewer.getOverlayPainter() instanceof CompoundPainter) {
-			compPainter = (CompoundPainter<JXMapViewer>) mapViewer.getOverlayPainter();
-		}
-		// search all painters in compound painter until they found an edge or
-		// waypoint painter
-		for (Painter<JXMapViewer> painter : compPainter.getPainters()) {
-			if (painter instanceof EdgePainter) {
-				edgePainter = (EdgePainter) painter;
-			} else {
-				if (painter instanceof WaypointPainter) {
-					waypointPainter = (WaypointPainter<CustomWaypoint>) painter;
-				}
-			}
-		}
+	private static Painter<JXMapViewer> getRequestedPainter(String requested) {
 
 		// return value
 		switch (requested) {
 		case "edge":
-			return edgePainter;
+			return WorldView.edgePainter;
 		case "waypoint":
-			return waypointPainter;
+			return WorldView.waypointPainter;
 		default:
 			return null;
 
@@ -206,32 +173,36 @@ public final class MapViewFunctions {
 	 * change the shown map based on the selected item in the ChoiceBox
 	 */
 	public static void changeMapView() {
-		String selected = controller.mapViewChoiceBox.getSelectionModel().getSelectedItem();
+		String selected = WorldView.controller.mapViewChoiceBox.getSelectionModel().getSelectedItem();
 
 		switch (selected) {
 		case "Default":
 			TileFactoryInfo defaultTileFactoryInfo = new OSMTileFactoryInfo();
-			mapViewer.setTileFactory(new DefaultTileFactory(defaultTileFactoryInfo));
+			WorldView.internMapViewer.setTileFactory(new DefaultTileFactory(defaultTileFactoryInfo));
 			break;
 
 		case "Road":
 			TileFactoryInfo roadTileFactoryInfo = new VirtualEarthTileFactoryInfo(VirtualEarthTileFactoryInfo.MAP);
-			mapViewer.setTileFactory(new DefaultTileFactory(roadTileFactoryInfo));
+			WorldView.internMapViewer.setTileFactory(new DefaultTileFactory(roadTileFactoryInfo));
 			break;
 
 		case "Satellite":
 			TileFactoryInfo sateliteTileFactoryInfo = new VirtualEarthTileFactoryInfo(
 					VirtualEarthTileFactoryInfo.SATELLITE);
-			mapViewer.setTileFactory(new DefaultTileFactory(sateliteTileFactoryInfo));
+			WorldView.internMapViewer.setTileFactory(new DefaultTileFactory(sateliteTileFactoryInfo));
 			break;
 
 		case "Hybrid":
 			TileFactoryInfo hybridTileFactoryInfo = new VirtualEarthTileFactoryInfo(VirtualEarthTileFactoryInfo.HYBRID);
-			mapViewer.setTileFactory(new DefaultTileFactory(hybridTileFactoryInfo));
+			WorldView.internMapViewer.setTileFactory(new DefaultTileFactory(hybridTileFactoryInfo));
 			break;
 		}
 	}
 
+	/**
+	 * Check if Checkboxes or ChoiceBox where changed, last time symbol-rep.
+	 * layer was shown
+	 */
 	public static void checkVBoxChanged() {
 
 		EdgePainter edgePainter = (EdgePainter) getRequestedPainter("edge");
@@ -239,18 +210,18 @@ public final class MapViewFunctions {
 				"waypoint");
 
 		// Checkboxes were changed last time symbolRep-Layer was shown
-		if (!controller.edgesVisibleCheckbox.isSelected()) {
+		if (!WorldView.controller.edgesVisibleCheckbox.isSelected()) {
 			edgePainter.setShowEdges(false);
 		}
-		if (!controller.nodeLabelCheckbox.isSelected()) {
+		if (!WorldView.controller.nodeLabelCheckbox.isSelected()) {
 			CustomWaypointRenderer renderer = new CustomWaypointRenderer();
 			renderer.setShowLabels(false);
 			waypointPainter.setRenderer(renderer);
 		}
-		if (!controller.edgeWeightCheckbox.isSelected()) {
+		if (!WorldView.controller.edgeWeightCheckbox.isSelected()) {
 			edgePainter.setShowWeights(false);
 		}
-		if (!controller.mapViewChoiceBox.getSelectionModel().getSelectedItem().equals("Default")) {
+		if (!WorldView.controller.mapViewChoiceBox.getSelectionModel().getSelectedItem().equals("Default")) {
 			MapViewFunctions.changeMapView();
 		}
 	}

+ 105 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/WorldView.java

@@ -0,0 +1,105 @@
+package de.tu_darmstadt.informatik.tk.scopviz.ui.mapView;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+import org.graphstream.graph.Edge;
+import org.jxmapviewer.JXMapViewer;
+import org.jxmapviewer.OSMTileFactoryInfo;
+import org.jxmapviewer.painter.CompoundPainter;
+import org.jxmapviewer.painter.Painter;
+import org.jxmapviewer.viewer.DefaultTileFactory;
+import org.jxmapviewer.viewer.GeoPosition;
+import org.jxmapviewer.viewer.TileFactoryInfo;
+import org.jxmapviewer.viewer.WaypointPainter;
+
+import de.tu_darmstadt.informatik.tk.scopviz.ui.GUIController;
+
+public class WorldView {
+
+	/*
+	 * intern map viewer
+	 */
+	public static JXMapViewer internMapViewer;
+
+	/*
+	 * edgePainter of overlayPainter
+	 */
+	public static EdgePainter edgePainter;
+
+	/*
+	 * waypointPointer of overlayPainter
+	 */
+	public static WaypointPainter<CustomWaypoint> waypointPainter;
+
+	/*
+	 * GUIController with UI elements
+	 */
+	public static GUIController controller;
+
+	
+	/**
+	 * private constructor to avoid instantiation
+	 */
+	private WorldView() {
+	}
+
+	/**
+	 * initialize attributes internMapViewer and controller
+	 * 
+	 * @param mapViewer
+	 * @param guiController
+	 */
+	public static void initAttributes(JXMapViewer mapViewer, GUIController guiController) {
+		internMapViewer = mapViewer;
+		controller = guiController;
+	}
+
+	/**
+	 * load map elements based on current underlay graph
+	 */
+	public static void loadWorldView() {
+
+		HashSet<GeoPosition> nodePositions = new HashSet<GeoPosition>();
+		HashSet<CustomWaypoint> waypoints = new HashSet<CustomWaypoint>();
+		HashSet<Edge> edges = new HashSet<Edge>();
+
+		// Get GeoPositions of nodes and get all waypoints created
+		MapViewFunctions.fetchGraphData(nodePositions, waypoints, edges);
+
+		// Create a line for all edges
+		edgePainter = new EdgePainter(edges);
+
+		// Create a waypoint painter that takes all the waypoints
+		waypointPainter = new WaypointPainter<CustomWaypoint>();
+		waypointPainter.setWaypoints(waypoints);
+		waypointPainter.setRenderer(new CustomWaypointRenderer());
+
+		// Create a compound painter that uses all painters
+		List<Painter<JXMapViewer>> painters = new ArrayList<Painter<JXMapViewer>>();
+		painters.add(waypointPainter);
+		painters.add(edgePainter);
+
+		CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
+
+		// Create a TileFactoryInfo for OpenStreetMap
+		TileFactoryInfo info = new OSMTileFactoryInfo();
+		DefaultTileFactory tileFactory = new DefaultTileFactory(info);
+		internMapViewer.setTileFactory(tileFactory);
+
+		// Use 8 threads in parallel to load the tiles
+		tileFactory.setThreadPoolSize(8);
+
+		// set Zoom and Center to show all node positions
+		internMapViewer.zoomToBestFit(nodePositions, 0.7);
+
+		internMapViewer.setOverlayPainter(painter);
+
+		// "click on waypoints" listener
+		internMapViewer.addMouseListener(new CustomMapClickListener(internMapViewer, waypoints, edges));
+
+		internMapViewer.repaint();
+	}
+
+}