Skip to content
Snippets Groups Projects
Commit 50e2bfd1 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Support for importing GPX files. Both core and UI code.

parent 7f191d31
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,7 @@ import javafx.fxml.FXML; ...@@ -11,6 +11,7 @@ import javafx.fxml.FXML;
import javafx.scene.control.Menu; import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem; import javafx.scene.control.MenuItem;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import tdt4140.gr1800.app.core.IDocumentImporter;
import tdt4140.gr1800.app.core.IDocumentStorage; import tdt4140.gr1800.app.core.IDocumentStorage;
public class FileMenuController { public class FileMenuController {
...@@ -126,4 +127,20 @@ public class FileMenuController { ...@@ -126,4 +127,20 @@ public class FileMenuController {
documentStorage.setDocumentLocation(oldStorage); documentStorage.setDocumentLocation(oldStorage);
} }
} }
@FXML
public void handleImportAction() {
FileChooser fileChooser = getFileChooser();
File selection = fileChooser.showOpenDialog(null);
// String path = selection.getPath();
// int pos = path.lastIndexOf('.');
// String ext = (pos > 0 ? path.substring(pos + 1) : null);
for (IDocumentImporter<File> importer : documentStorage.getDocumentImporters()) {
try {
importer.importDocument(selection);
break;
} catch (Exception e) {
}
}
}
} }
...@@ -2,6 +2,7 @@ package tdt4140.gr1800.app.ui; ...@@ -2,6 +2,7 @@ package tdt4140.gr1800.app.ui;
import java.util.Iterator; import java.util.Iterator;
import fxmapcontrol.Location;
import fxmapcontrol.MapBase; import fxmapcontrol.MapBase;
import fxmapcontrol.MapItemsControl; import fxmapcontrol.MapItemsControl;
import fxmapcontrol.MapNode; import fxmapcontrol.MapNode;
...@@ -10,10 +11,12 @@ import javafx.fxml.FXML; ...@@ -10,10 +11,12 @@ import javafx.fxml.FXML;
import javafx.scene.control.ComboBox; import javafx.scene.control.ComboBox;
import javafx.scene.control.Slider; import javafx.scene.control.Slider;
import tdt4140.gr1800.app.core.App; import tdt4140.gr1800.app.core.App;
import tdt4140.gr1800.app.core.GeoLocated;
import tdt4140.gr1800.app.core.GeoLocations; import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.IGeoLocationsListener;
import tdt4140.gr1800.app.core.LatLong; import tdt4140.gr1800.app.core.LatLong;
public class FxAppController { public class FxAppController implements IGeoLocationsListener {
@FXML @FXML
private FileMenuController fileMenuController; private FileMenuController fileMenuController;
...@@ -36,12 +39,17 @@ public class FxAppController { ...@@ -36,12 +39,17 @@ public class FxAppController {
app = new App(); app = new App();
fileMenuController.setDocumentStorage(app.getDocumentStorage()); fileMenuController.setDocumentStorage(app.getDocumentStorage());
fileMenuController.setOnDocumentChanged(documentStorage -> initMapMarkers()); fileMenuController.setOnDocumentChanged(documentStorage -> initMapMarkers());
geoLocationsSelector.getSelectionModel().selectedItemProperty().addListener((stringProperty, oldValue, newValue) -> updateGeoLocations()); geoLocationsSelector.getSelectionModel().selectedItemProperty().addListener((stringProperty, oldValue, newValue) -> updateGeoLocations());
mapView.getChildren().add(MapTileLayer.getOpenStreetMapLayer()); mapView.getChildren().add(MapTileLayer.getOpenStreetMapLayer());
mapView.zoomLevelProperty().bind(zoomSlider.valueProperty()); zoomSlider.valueProperty().addListener((prop, oldValue, newValue) -> {
mapView.setZoomLevel(zoomSlider.getValue());
});
markersParent = new MapItemsControl<MapNode>(); markersParent = new MapItemsControl<MapNode>();
mapView.getChildren().add(markersParent); mapView.getChildren().add(markersParent);
app.addGeoLocationsListener(this);
} }
private Object updateGeoLocations() { private Object updateGeoLocations() {
...@@ -50,11 +58,12 @@ public class FxAppController { ...@@ -50,11 +58,12 @@ public class FxAppController {
private void initMapMarkers() { private void initMapMarkers() {
markersParent.getItems().clear(); markersParent.getItems().clear();
geoLocationsSelector.getItems().clear();
for (String geoLocationName : app.getGeoLocationNames()) { for (String geoLocationName : app.getGeoLocationNames()) {
GeoLocations geoLocations = app.getGeoLocations(geoLocationName); GeoLocations geoLocations = app.getGeoLocations(geoLocationName);
MapMarker lastMarker = null; MapMarker lastMarker = null;
for (LatLong latLong : geoLocations) { for (GeoLocated geoLoc : geoLocations) {
MapMarker mapMarker = new MapMarker(latLong); MapMarker mapMarker = new MapMarker(geoLoc.getLatLong());
markersParent.getItems().add(mapMarker); markersParent.getItems().add(mapMarker);
if (geoLocations.isPath() && lastMarker != null) { if (geoLocations.isPath() && lastMarker != null) {
MapPathLine pathLine = new MapPathLine(lastMarker, mapMarker); MapPathLine pathLine = new MapPathLine(lastMarker, mapMarker);
...@@ -62,10 +71,10 @@ public class FxAppController { ...@@ -62,10 +71,10 @@ public class FxAppController {
} }
lastMarker = mapMarker; lastMarker = mapMarker;
} }
geoLocationsSelector.getItems().add(geoLocationName); geoLocationsSelector.getItems().add(geoLocationName + " (" + geoLocations.size() + ")");
} }
LatLong center = getCenter(null); LatLong center = getCenter(null);
System.out.println("Map markers initialized"); mapView.setCenter(new Location(center.latitude, center.longitude));
} }
private LatLong getCenter(GeoLocations geoLocations) { private LatLong getCenter(GeoLocations geoLocations) {
...@@ -79,8 +88,8 @@ public class FxAppController { ...@@ -79,8 +88,8 @@ public class FxAppController {
if (names != null) { if (names != null) {
geoLocations = app.getGeoLocations(names.next()); geoLocations = app.getGeoLocations(names.next());
} }
for (LatLong latLong : geoLocations) { for (GeoLocated geoLoc : geoLocations) {
double lat = latLong.latitude, lon = latLong.longitude; double lat = geoLoc.getLatitude(), lon = geoLoc.getLongitude();
latSum += lat; latSum += lat;
lonSum += lon; lonSum += lon;
num++; num++;
...@@ -91,4 +100,9 @@ public class FxAppController { ...@@ -91,4 +100,9 @@ public class FxAppController {
} }
return new LatLong(latSum / num, lonSum / num); return new LatLong(latSum / num, lonSum / num);
} }
@Override
public void geoLocationsUpdated(GeoLocations geoLocations) {
initMapMarkers();
}
} }
...@@ -3,14 +3,18 @@ ...@@ -3,14 +3,18 @@
<?import java.lang.*?> <?import java.lang.*?>
<?import javafx.scene.control.Menu?> <?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<Menu xmlns:fx="http://javafx.com/fxml" text="File" fx:controller="tdt4140.gr1800.app.ui.FileMenuController"> <Menu xmlns:fx="http://javafx.com/fxml" text="File" fx:controller="tdt4140.gr1800.app.ui.FileMenuController">
<items> <items>
<MenuItem text="New" onAction="#handleNewAction"/> <MenuItem text="New" onAction="#handleNewAction"/>
<MenuItem text="Open..." onAction="#handleOpenAction"/> <MenuItem text="Open..." onAction="#handleOpenAction"/>
<Menu fx:id="recentMenu" text="Open Recent"/> <Menu fx:id="recentMenu" text="Open Recent"/>
<SeparatorMenuItem/>
<MenuItem text="Save" onAction="#handleSaveAction"/> <MenuItem text="Save" onAction="#handleSaveAction"/>
<MenuItem text="Save As..." onAction="#handleSaveAsAction"/> <MenuItem text="Save As..." onAction="#handleSaveAsAction"/>
<MenuItem text="Save Copy As..." onAction="#handleSaveCopyAsAction"/> <MenuItem text="Save Copy As..." onAction="#handleSaveCopyAsAction"/>
<SeparatorMenuItem/>
<MenuItem text="Import..." onAction="#handleImportAction"/>
</items> </items>
</Menu> </Menu>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment