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

Tags for configuring markers, and some fixes.

parent d10866ee
No related branches found
No related tags found
No related merge requests found
Showing
with 88 additions and 15 deletions
...@@ -5,12 +5,13 @@ import java.io.FileInputStream; ...@@ -5,12 +5,13 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import tdt4140.gr1800.app.doc.DocumentStorageImpl; import tdt4140.gr1800.app.doc.AbstractDocumentStorageImpl;
import tdt4140.gr1800.app.doc.IDocumentImporter; import tdt4140.gr1800.app.doc.IDocumentImporter;
import tdt4140.gr1800.app.doc.IDocumentLoader; import tdt4140.gr1800.app.doc.IDocumentLoader;
import tdt4140.gr1800.app.doc.IDocumentPersistence; import tdt4140.gr1800.app.doc.IDocumentPersistence;
...@@ -20,7 +21,7 @@ import tdt4140.gr1800.app.json.GeoLocationsJsonPersistence; ...@@ -20,7 +21,7 @@ import tdt4140.gr1800.app.json.GeoLocationsJsonPersistence;
public class App { public class App {
private GeoLocationsPersistence geoLocationsLoader = new GeoLocationsJsonPersistence(); private GeoLocationsPersistence geoLocationsPersistence = new GeoLocationsJsonPersistence();
private Collection<GeoLocations> geoLocations = null; private Collection<GeoLocations> geoLocations = null;
...@@ -79,16 +80,18 @@ public class App { ...@@ -79,16 +80,18 @@ public class App {
@Override @Override
public Collection<GeoLocations> loadDocument(InputStream inputStream) throws Exception { public Collection<GeoLocations> loadDocument(InputStream inputStream) throws Exception {
return geoLocationsLoader.loadLocations(inputStream); return geoLocationsPersistence.loadLocations(inputStream);
} }
@Override @Override
public void saveDocument(Collection<GeoLocations> document, File documentLocation) throws Exception { public void saveDocument(Collection<GeoLocations> document, File documentLocation) throws Exception {
geoLocationsLoader.saveLocations(document, new FileOutputStream(documentLocation)); try (OutputStream output = new FileOutputStream(documentLocation)) {
geoLocationsPersistence.saveLocations(document, output);
}
} }
}; };
private DocumentStorageImpl<Collection<GeoLocations>, File> documentStorage = new DocumentStorageImpl<Collection<GeoLocations>, File>() { private AbstractDocumentStorageImpl<Collection<GeoLocations>, File> documentStorage = new AbstractDocumentStorageImpl<Collection<GeoLocations>, File>() {
@Override @Override
protected Collection<GeoLocations> getDocument() { protected Collection<GeoLocations> getDocument() {
......
package tdt4140.gr1800.app.core; package tdt4140.gr1800.app.core;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class GeoLocations implements Iterable<GeoLocated> { import org.junit.Assert;
public class GeoLocations implements Iterable<GeoLocated>, Tagged {
private String name; private String name;
...@@ -82,4 +87,26 @@ public class GeoLocations implements Iterable<GeoLocated> { ...@@ -82,4 +87,26 @@ public class GeoLocations implements Iterable<GeoLocated> {
public Iterator<GeoLocated> iterator() { public Iterator<GeoLocated> iterator() {
return locations.iterator(); return locations.iterator();
} }
//
private Set<String> tags = null;
@Override
public boolean hasTags(String... tags) {
return this.tags != null && this.tags.containsAll(Arrays.asList(tags));
}
public void addTags(String... tags) {
if (this.tags == null) {
this.tags = new HashSet<>();
}
this.tags.addAll(Arrays.asList(tags));
}
public void removeTags(String... tags) {
if (this.tags != null) {
this.tags.removeAll(Arrays.asList(tags));
}
}
} }
package tdt4140.gr1800.app.core;
public interface Tagged {
public boolean hasTags(String... tags);
}
...@@ -5,7 +5,7 @@ import java.io.InputStream; ...@@ -5,7 +5,7 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L>, IDocumentPersistence<D, L> { public abstract class AbstractDocumentStorageImpl<D, L> implements IDocumentStorage<L>, IDocumentPersistence<D, L> {
private L documentLocation; private L documentLocation;
...@@ -66,8 +66,8 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L>, ...@@ -66,8 +66,8 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L>,
@Override @Override
public void openDocument(L storage) throws IOException { public void openDocument(L storage) throws IOException {
try { try (InputStream input = toInputStream(storage)){
setDocumentAndLocation(loadDocument(toInputStream(storage)), storage); setDocumentAndLocation(loadDocument(input), storage);
} catch (Exception e) { } catch (Exception e) {
throw new IOException(e); throw new IOException(e);
} }
......
...@@ -62,6 +62,7 @@ public class GpxDocumentConverter implements IDocumentLoader<Collection<GeoLocat ...@@ -62,6 +62,7 @@ public class GpxDocumentConverter implements IDocumentLoader<Collection<GeoLocat
String name = (singleTrack ? trackName : String.format(trackSegmentFormat, trackName, segmentCount)); String name = (singleTrack ? trackName : String.format(trackSegmentFormat, trackName, segmentCount));
GeoLocations gl = new GeoLocations(name, convert(segment.getPoints())); GeoLocations gl = new GeoLocations(name, convert(segment.getPoints()));
gl.setPath(true); gl.setPath(true);
gl.addTags(Track.class.getName().toLowerCase());
geoLocations.add(gl); geoLocations.add(gl);
} }
} }
...@@ -70,10 +71,13 @@ public class GpxDocumentConverter implements IDocumentLoader<Collection<GeoLocat ...@@ -70,10 +71,13 @@ public class GpxDocumentConverter implements IDocumentLoader<Collection<GeoLocat
String routeName = (route.getName().isPresent() ? String.format(routeNameFormat, route.getName().get()) : String.format(routeCountFormat, routeCount)); String routeName = (route.getName().isPresent() ? String.format(routeNameFormat, route.getName().get()) : String.format(routeCountFormat, routeCount));
GeoLocations gl = new GeoLocations(routeName, convert(route.getPoints())); GeoLocations gl = new GeoLocations(routeName, convert(route.getPoints()));
gl.setPath(true); gl.setPath(true);
gl.addTags(Route.class.getName().toLowerCase());
geoLocations.add(gl); geoLocations.add(gl);
} }
if (! gpx.getWayPoints().isEmpty()) { if (! gpx.getWayPoints().isEmpty()) {
geoLocations.add(new GeoLocations("Waypoints", convert(gpx.getWayPoints()))); GeoLocations gl = new GeoLocations("Waypoints", convert(gpx.getWayPoints()));
gl.addTags(WayPoint.class.getName().toLowerCase());
geoLocations.add(gl);
} }
return geoLocations; return geoLocations;
} }
......
...@@ -38,6 +38,15 @@ public class GeoLocationsTest { ...@@ -38,6 +38,15 @@ public class GeoLocationsTest {
} }
} }
public static void assertGeoLocations(GeoLocations geoLocations, GeoLocations geoLocations2) {
Iterator<GeoLocated> it = geoLocations.iterator(), it2 = geoLocations2.iterator();
while (it.hasNext()) {
Assert.assertTrue(it2.hasNext());
checkGeoLocated(it2.next(), it.next());
}
Assert.assertFalse(it2.hasNext());
}
@Test @Test
public void testAddLocation() { public void testAddLocation() {
Assert.assertEquals(0, geoLocations.size()); Assert.assertEquals(0, geoLocations.size());
......
...@@ -3,6 +3,7 @@ package tdt4140.gr1800.app.ui; ...@@ -3,6 +3,7 @@ package tdt4140.gr1800.app.ui;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -146,8 +147,8 @@ public class FileMenuController { ...@@ -146,8 +147,8 @@ public class FileMenuController {
void handleFileImportAction(File selection) { void handleFileImportAction(File selection) {
for (IDocumentImporter importer : documentStorage.getDocumentImporters()) { for (IDocumentImporter importer : documentStorage.getDocumentImporters()) {
try { try (InputStream input = new FileInputStream(selection)) {
importer.importDocument(new FileInputStream(selection)); importer.importDocument(input);
break; break;
} catch (Exception e) { } catch (Exception e) {
} }
...@@ -187,8 +188,8 @@ public class FileMenuController { ...@@ -187,8 +188,8 @@ public class FileMenuController {
boolean handleURLImportAction(URL url) { boolean handleURLImportAction(URL url) {
for (IDocumentImporter importer : documentStorage.getDocumentImporters()) { for (IDocumentImporter importer : documentStorage.getDocumentImporters()) {
try { try (InputStream input = url.openStream()) {
importer.importDocument(url.openStream()); importer.importDocument(input);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
......
...@@ -75,6 +75,8 @@ public class FxAppController implements IDocumentListener<Collection<GeoLocation ...@@ -75,6 +75,8 @@ public class FxAppController implements IDocumentListener<Collection<GeoLocation
return null; return null;
} }
private IMapMarkerProvider mapMarkerProvider = new TagBasedMapMarkerProvider();
private void initMapMarkers() { private void initMapMarkers() {
markersParent.getItems().clear(); markersParent.getItems().clear();
geoLocationsSelector.getItems().clear(); geoLocationsSelector.getItems().clear();
...@@ -82,7 +84,7 @@ public class FxAppController implements IDocumentListener<Collection<GeoLocation ...@@ -82,7 +84,7 @@ public class FxAppController implements IDocumentListener<Collection<GeoLocation
GeoLocations geoLocations = app.getGeoLocations(geoLocationName); GeoLocations geoLocations = app.getGeoLocations(geoLocationName);
MapMarker lastMarker = null; MapMarker lastMarker = null;
for (GeoLocated geoLoc : geoLocations) { for (GeoLocated geoLoc : geoLocations) {
MapMarker mapMarker = new MapMarker(geoLoc.getLatLong()); MapMarker mapMarker = mapMarkerProvider.getMapMarker(geoLoc, geoLocations);
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);
......
package tdt4140.gr1800.app.ui;
import tdt4140.gr1800.app.core.GeoLocated;
import tdt4140.gr1800.app.core.GeoLocations;
public interface IMapMarkerProvider {
public MapMarker getMapMarker(GeoLocated geoLoc, GeoLocations geoLocations);
}
package tdt4140.gr1800.app.ui;
import tdt4140.gr1800.app.core.GeoLocated;
import tdt4140.gr1800.app.core.GeoLocations;
public class TagBasedMapMarkerProvider implements IMapMarkerProvider {
@Override
public MapMarker getMapMarker(GeoLocated geoLoc, GeoLocations geoLocations) {
MapMarker mapMarker = new MapMarker(geoLoc.getLatLong());
return mapMarker;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment