Commit c11867fa authored by Hallvard Trætteberg's avatar Hallvard Trætteberg

Formattering

parent 303ddb2e
......@@ -2,15 +2,15 @@ module simpleexample {
requires transitive com.fasterxml.jackson.core;
requires transitive com.fasterxml.jackson.databind;
requires javafx.fxml;
requires transitive javafx.graphics;
requires javafx.controls;
requires javafx.fxml;
requires transitive javafx.graphics;
requires javafx.controls;
requires fx.map.control;
exports simpleex.core;
exports simpleex.json;
exports simpleex.ui;
exports simpleex.ui;
opens simpleex.ui to javafx.fxml;
opens simpleex.ui to javafx.fxml;
}
......@@ -7,65 +7,65 @@ import java.util.List;
public class LatLongs implements Iterable<LatLong> {
final List<LatLong> latLongs = new ArrayList<>();
public LatLongs() {
}
public LatLongs(final double... latLongsArray) {
addLatLongs(latLongsArray);
}
public LatLongs(final LatLong... latLongs) {
addLatLongs(latLongs);
}
public LatLongs(final Collection<LatLong> latLongs) {
addLatLongs(latLongs);
}
@Override
public Iterator<LatLong> iterator() {
return latLongs.iterator();
}
public int getLatLongCount() {
return latLongs.size();
}
public LatLong getLatLong(final int num) {
return latLongs.get(num);
}
public void setLatLong(final int num, final LatLong latLong) {
latLongs.set(num, latLong);
}
public int addLatLong(final LatLong latLong) {
final int pos = latLongs.size();
latLongs.add(latLong);
return pos;
}
public final int addLatLongs(final Collection<LatLong> latLongs) {
final int pos = this.latLongs.size();
this.latLongs.addAll(latLongs);
return pos;
}
public final int addLatLongs(final LatLong... latLongs) {
return addLatLongs(List.of(latLongs));
}
public final int addLatLongs(final double... latLongsArray) {
final Collection<LatLong> latLongs = new ArrayList<>(latLongsArray.length / 2);
for (int i = 0; i < latLongsArray.length; i += 2) {
latLongs.add(new LatLong(latLongsArray[i], latLongsArray[i + 1]));
final List<LatLong> latLongs = new ArrayList<>();
public LatLongs() {
}
public LatLongs(final double... latLongsArray) {
addLatLongs(latLongsArray);
}
public LatLongs(final LatLong... latLongs) {
addLatLongs(latLongs);
}
public LatLongs(final Collection<LatLong> latLongs) {
addLatLongs(latLongs);
}
@Override
public Iterator<LatLong> iterator() {
return latLongs.iterator();
}
public int getLatLongCount() {
return latLongs.size();
}
public LatLong getLatLong(final int num) {
return latLongs.get(num);
}
public void setLatLong(final int num, final LatLong latLong) {
latLongs.set(num, latLong);
}
public int addLatLong(final LatLong latLong) {
final int pos = latLongs.size();
latLongs.add(latLong);
return pos;
}
public final int addLatLongs(final Collection<LatLong> latLongs) {
final int pos = this.latLongs.size();
this.latLongs.addAll(latLongs);
return pos;
}
public final int addLatLongs(final LatLong... latLongs) {
return addLatLongs(List.of(latLongs));
}
return addLatLongs(latLongs);
}
public LatLong removeLatLong(final int num) {
return latLongs.remove(num);
}
public final int addLatLongs(final double... latLongsArray) {
final Collection<LatLong> latLongs = new ArrayList<>(latLongsArray.length / 2);
for (int i = 0; i < latLongsArray.length; i += 2) {
latLongs.add(new LatLong(latLongsArray[i], latLongsArray[i + 1]));
}
return addLatLongs(latLongs);
}
public LatLong removeLatLong(final int num) {
return latLongs.remove(num);
}
}
......@@ -12,29 +12,31 @@ import simpleex.core.LatLong;
public class LatLongDeserializer extends JsonDeserializer<LatLong> {
private static final int ARRAY_JSON_NODE_SIZE = 2;
private static final int ARRAY_JSON_NODE_SIZE = 2;
@Override
public LatLong deserialize(final JsonParser jsonParser, final DeserializationContext deserContext)
throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return deserialize(jsonNode);
}
@Override
public LatLong deserialize(final JsonParser jsonParser,
final DeserializationContext deserContext) throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return deserialize(jsonNode);
}
public LatLong deserialize(final JsonNode jsonNode) throws JsonProcessingException {
if (jsonNode instanceof ObjectNode) {
final ObjectNode objectNode = (ObjectNode) jsonNode;
final double latitude = objectNode.get(LatLongSerializer.LATITUDE_FIELD_NAME).asDouble();
final double longitude = objectNode.get(LatLongSerializer.LONGITUDE_FIELD_NAME).asDouble();
return new LatLong(latitude, longitude);
} else if (jsonNode instanceof ArrayNode) {
final ArrayNode locationArray = (ArrayNode) jsonNode;
if (locationArray.size() == ARRAY_JSON_NODE_SIZE) {
final double latitude = locationArray.get(0).asDouble();
final double longitude = locationArray.get(1).asDouble();
return new LatLong(latitude, longitude);
}
public LatLong deserialize(final JsonNode jsonNode) throws JsonProcessingException {
if (jsonNode instanceof ObjectNode) {
final ObjectNode objectNode = (ObjectNode) jsonNode;
final double latitude =
objectNode.get(LatLongSerializer.LATITUDE_FIELD_NAME).asDouble();
final double longitude =
objectNode.get(LatLongSerializer.LONGITUDE_FIELD_NAME).asDouble();
return new LatLong(latitude, longitude);
} else if (jsonNode instanceof ArrayNode) {
final ArrayNode locationArray = (ArrayNode) jsonNode;
if (locationArray.size() == ARRAY_JSON_NODE_SIZE) {
final double latitude = locationArray.get(0).asDouble();
final double longitude = locationArray.get(1).asDouble();
return new LatLong(latitude, longitude);
}
}
return null;
}
return null;
}
}
......@@ -8,17 +8,17 @@ import simpleex.core.LatLong;
public class LatLongSerializer extends JsonSerializer<LatLong> {
public static final String LONGITUDE_FIELD_NAME = "longitude";
public static final String LATITUDE_FIELD_NAME = "latitude";
public static final String LONGITUDE_FIELD_NAME = "longitude";
public static final String LATITUDE_FIELD_NAME = "latitude";
@Override
public void serialize(final LatLong latLon, final JsonGenerator jsonGen,
final SerializerProvider provider) throws IOException {
jsonGen.writeStartObject();
jsonGen.writeFieldName(LATITUDE_FIELD_NAME);
jsonGen.writeNumber(latLon.getLatitude());
jsonGen.writeFieldName(LONGITUDE_FIELD_NAME);
jsonGen.writeNumber(latLon.getLongitude());
jsonGen.writeEndObject();
}
@Override
public void serialize(final LatLong latLon, final JsonGenerator jsonGen,
final SerializerProvider provider) throws IOException {
jsonGen.writeStartObject();
jsonGen.writeFieldName(LATITUDE_FIELD_NAME);
jsonGen.writeNumber(latLon.getLatitude());
jsonGen.writeFieldName(LONGITUDE_FIELD_NAME);
jsonGen.writeNumber(latLon.getLongitude());
jsonGen.writeEndObject();
}
}
......@@ -14,21 +14,21 @@ import simpleex.core.LatLongs;
public class LatLongsDeserializer extends JsonDeserializer<LatLongs> {
private final LatLongDeserializer latLongDeserializer = new LatLongDeserializer();
private final LatLongDeserializer latLongDeserializer = new LatLongDeserializer();
@Override
public LatLongs deserialize(final JsonParser jsonParser,
final DeserializationContext deserContext) throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
if (jsonNode instanceof ArrayNode) {
final ArrayNode latLongsArray = (ArrayNode) jsonNode;
final Collection<LatLong> latLongs = new ArrayList<>(latLongsArray.size());
for (final JsonNode latLongNode : latLongsArray) {
final LatLong latLong = latLongDeserializer.deserialize(latLongNode);
latLongs.add(latLong);
}
return new LatLongs(latLongs);
@Override
public LatLongs deserialize(final JsonParser jsonParser,
final DeserializationContext deserContext) throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
if (jsonNode instanceof ArrayNode) {
final ArrayNode latLongsArray = (ArrayNode) jsonNode;
final Collection<LatLong> latLongs = new ArrayList<>(latLongsArray.size());
for (final JsonNode latLongNode : latLongsArray) {
final LatLong latLong = latLongDeserializer.deserialize(latLongNode);
latLongs.add(latLong);
}
return new LatLongs(latLongs);
}
return null;
}
return null;
}
}
......@@ -9,29 +9,29 @@ import simpleex.core.LatLongs;
public class LatLongsModule extends Module {
@Override
public String getModuleName() {
return "LatLongsModule";
}
@Override
public String getModuleName() {
return "LatLongsModule";
}
@Override
public Version version() {
return Version.unknownVersion();
}
@Override
public Version version() {
return Version.unknownVersion();
}
private final SimpleSerializers serializers = new SimpleSerializers();
private final SimpleDeserializers deserializers = new SimpleDeserializers();
private final SimpleSerializers serializers = new SimpleSerializers();
private final SimpleDeserializers deserializers = new SimpleDeserializers();
public LatLongsModule() {
serializers.addSerializer(LatLong.class, new LatLongSerializer());
serializers.addSerializer(LatLongs.class, new LatLongsSerializer());
deserializers.addDeserializer(LatLong.class, new LatLongDeserializer());
deserializers.addDeserializer(LatLongs.class, new LatLongsDeserializer());
}
public LatLongsModule() {
serializers.addSerializer(LatLong.class, new LatLongSerializer());
serializers.addSerializer(LatLongs.class, new LatLongsSerializer());
deserializers.addDeserializer(LatLong.class, new LatLongDeserializer());
deserializers.addDeserializer(LatLongs.class, new LatLongsDeserializer());
}
@Override
public void setupModule(final SetupContext context) {
context.addSerializers(serializers);
context.addDeserializers(deserializers);
}
@Override
public void setupModule(final SetupContext context) {
context.addSerializers(serializers);
context.addDeserializers(deserializers);
}
}
......@@ -9,13 +9,13 @@ import simpleex.core.LatLongs;
public class LatLongsSerializer extends JsonSerializer<LatLongs> {
@Override
public void serialize(final LatLongs latLongs, final JsonGenerator jsonGen,
final SerializerProvider provider) throws IOException {
jsonGen.writeStartArray(latLongs.getLatLongCount());
for (final LatLong latLong : latLongs) {
jsonGen.writeObject(latLong);
@Override
public void serialize(final LatLongs latLongs, final JsonGenerator jsonGen,
final SerializerProvider provider) throws IOException {
jsonGen.writeStartArray(latLongs.getLatLongCount());
for (final LatLong latLong : latLongs) {
jsonGen.writeObject(latLong);
}
jsonGen.writeEndArray();
}
jsonGen.writeEndArray();
}
}
......@@ -7,84 +7,86 @@ import javafx.scene.input.MouseEvent;
public class DraggableNodeController {
private final NodeDraggedHandler nodeDraggedHandler;
public DraggableNodeController() {
this(null);
}
public DraggableNodeController(final NodeDraggedHandler nodeDraggedHandler) {
this.nodeDraggedHandler = (nodeDraggedHandler != null ? nodeDraggedHandler : (node, x, y) -> {});
}
private boolean immediate = false;
public void setImmediate(final boolean immediate) {
this.immediate = immediate;
}
private Node currentNode = null;
private Point2D startPoint = null;
private Point2D startTranslate = null;
private final EventHandler<MouseEvent> mousePressedHandler = this::mousePressed;
private final EventHandler<MouseEvent> mouseDraggedHandler = this::mouseDragged;
private final EventHandler<MouseEvent> mouseReleasedHandler = this::mouseReleased;
public void attach(final Node node) {
node.setOnMousePressed(mousePressedHandler);
node.setOnMouseDragged(mouseDraggedHandler);
node.setOnMouseReleased(mouseReleasedHandler);
}
public void detach(final Node node) {
node.setOnMousePressed(null);
node.setOnMouseDragged(null);
node.setOnMouseReleased(null);
}
private void mousePressed(final MouseEvent mouseEvent) {
if (currentNode == null && mouseEvent.getSource() instanceof Node) {
currentNode = (Node) mouseEvent.getSource();
startPoint = new Point2D(mouseEvent.getSceneX(), mouseEvent.getSceneY());
startTranslate = new Point2D(currentNode.getTranslateX(), currentNode.getTranslateY());
mouseEvent.consume();
private final NodeDraggedHandler nodeDraggedHandler;
public DraggableNodeController() {
this(null);
}
}
private void mouseDragged(final MouseEvent mouseEvent) {
if (currentNode != null && currentNode == mouseEvent.getSource()) {
final double dx = mouseEvent.getSceneX() - startPoint.getX();
final double dy = mouseEvent.getSceneY() - startPoint.getY();
updateNode(dx, dy);
public DraggableNodeController(final NodeDraggedHandler nodeDraggedHandler) {
this.nodeDraggedHandler =
(nodeDraggedHandler != null ? nodeDraggedHandler : (node, x, y) -> {
});
}
}
protected void updateNode(final double dx, final double dy) {
if (immediate) {
nodeDraggedHandler.nodeDragged(currentNode, dx, dy);
startPoint = startPoint.add(dx, dy);
} else if (currentNode != null) {
currentNode.setTranslateX(startTranslate.getX() + dx);
currentNode.setTranslateY(startTranslate.getY() + dy);
private boolean immediate = false;
public void setImmediate(final boolean immediate) {
this.immediate = immediate;
}
}
private void mouseReleased(final MouseEvent mouseEvent) {
if (currentNode != null && currentNode == mouseEvent.getSource()) {
final double dx = mouseEvent.getSceneX() - startPoint.getX();
final double dy = mouseEvent.getSceneY() - startPoint.getY();
if (!immediate) {
currentNode.setTranslateX(startTranslate.getX());
currentNode.setTranslateY(startTranslate.getY());
}
final Node node = currentNode;
currentNode = null;
nodeDraggedHandler.nodeDragged(node, dx, dy);
private Node currentNode = null;
private Point2D startPoint = null;
private Point2D startTranslate = null;
private final EventHandler<MouseEvent> mousePressedHandler = this::mousePressed;
private final EventHandler<MouseEvent> mouseDraggedHandler = this::mouseDragged;
private final EventHandler<MouseEvent> mouseReleasedHandler = this::mouseReleased;
public void attach(final Node node) {
node.setOnMousePressed(mousePressedHandler);
node.setOnMouseDragged(mouseDraggedHandler);
node.setOnMouseReleased(mouseReleasedHandler);
}
public void detach(final Node node) {
node.setOnMousePressed(null);
node.setOnMouseDragged(null);
node.setOnMouseReleased(null);
}
}
public interface NodeDraggedHandler {
public void nodeDragged(Node currentNode2, double dx, double dy);
}
private void mousePressed(final MouseEvent mouseEvent) {
if (currentNode == null && mouseEvent.getSource() instanceof Node) {
currentNode = (Node) mouseEvent.getSource();
startPoint = new Point2D(mouseEvent.getSceneX(), mouseEvent.getSceneY());
startTranslate = new Point2D(currentNode.getTranslateX(), currentNode.getTranslateY());
mouseEvent.consume();
}
}
private void mouseDragged(final MouseEvent mouseEvent) {
if (currentNode != null && currentNode == mouseEvent.getSource()) {
final double dx = mouseEvent.getSceneX() - startPoint.getX();
final double dy = mouseEvent.getSceneY() - startPoint.getY();
updateNode(dx, dy);
}
}
protected void updateNode(final double dx, final double dy) {
if (immediate) {
nodeDraggedHandler.nodeDragged(currentNode, dx, dy);
startPoint = startPoint.add(dx, dy);
} else if (currentNode != null) {
currentNode.setTranslateX(startTranslate.getX() + dx);
currentNode.setTranslateY(startTranslate.getY() + dy);
}
}
private void mouseReleased(final MouseEvent mouseEvent) {
if (currentNode != null && currentNode == mouseEvent.getSource()) {
final double dx = mouseEvent.getSceneX() - startPoint.getX();
final double dy = mouseEvent.getSceneY() - startPoint.getY();
if (!immediate) {
currentNode.setTranslateX(startTranslate.getX());
currentNode.setTranslateY(startTranslate.getY());
}
final Node node = currentNode;
currentNode = null;
nodeDraggedHandler.nodeDragged(node, dx, dy);
}
}
public interface NodeDraggedHandler {
public void nodeDragged(Node currentNode2, double dx, double dy);
}
}
......@@ -9,23 +9,23 @@ import simpleex.core.LatLongs;
public class FxApp extends Application {
@Override
public void start(final Stage stage) throws Exception {
final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FxApp.fxml"));
final Parent root = fxmlLoader.load();
final FxAppController controller = fxmlLoader.getController();
controller.setLatLongs(new LatLongs(63.1, 11.2, 63.2, 11.0));
final Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@Override
public void start(final Stage stage) throws Exception {
final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FxApp.fxml"));
final Parent root = fxmlLoader.load();
final FxAppController controller = fxmlLoader.getController();
controller.setLatLongs(new LatLongs(63.1, 11.2, 63.2, 11.0));
final Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(final String[] args) {
// only needed on ios
System.setProperty("os.target", "ios");
System.setProperty("os.name", "iOS");
System.setProperty("glass.platform", "ios");
System.setProperty("targetos.name", "iOS");
launch(args);
}
public static void main(final String[] args) {
// only needed on ios
System.setProperty("os.target", "ios");
System.setProperty("os.name", "iOS");
System.setProperty("glass.platform", "ios");
System.setProperty("targetos.name", "iOS");
launch(args);
}
}
......@@ -8,15 +8,15 @@ import simpleex.core.LatLong;
public class MapMarker extends MapItem<LatLong> {
public MapMarker(final LatLong latLong) {
setLocation(latLong);
final Circle circle = new Circle();
circle.setRadius(5);
circle.setFill(Color.BLUE);
getChildren().add(circle);
}
public MapMarker(final LatLong latLong) {
setLocation(latLong);
final Circle circle = new Circle();
circle.setRadius(5);
circle.setFill(Color.BLUE);
getChildren().add(circle);
}
public final void setLocation(final LatLong latLong) {
setLocation(new Location(latLong.getLatitude(), latLong.getLongitude()));
}
public final void setLocation(final LatLong latLong) {
setLocation(new Location(latLong.getLatitude(), latLong.getLongitude()));
}
}
......@@ -10,65 +10,65 @@ import org.junit.jupiter.api.Test;
public class LatLongTest {
@Test
public void testToString() {
final LatLong latLong = new LatLong(63.0, 10.0);
assertEquals(Double.toString(63) + "," + Double.toString(10), latLong.toString());
}
@Test
public void testToString() {
final LatLong latLong = new LatLong(63.0, 10.0);
assertEquals(Double.toString(63) + "," + Double.toString(10), latLong.toString());
}
@Test
public void testValueOf() {
testLatLong(LatLong.valueOf("63.0, 10.0"), 63.0, 10.0);
testLatLong(LatLong.valueOf("63.0, 10.0", ","), 63.0, 10.0);
testLatLong(LatLong.valueOf("63.0; 10.0", ";"), 63.0, 10.0);
try {
testLatLong(LatLong.valueOf("63.0; 10.0", ","), 63.0, 10.0);
fail("Should throw IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// ok
} catch (final Exception e) {
fail("Should throw IllegalArgumentException");
@Test
public void testValueOf() {
testLatLong(LatLong.valueOf("63.0, 10.0"), 63.0, 10.0);
testLatLong(LatLong.valueOf("63.0, 10.0", ","), 63.0, 10.0);
testLatLong(LatLong.valueOf("63.0; 10.0", ";"), 63.0, 10.0);
try {
testLatLong(LatLong.valueOf("63.0; 10.0", ","), 63.0, 10.0);
fail("Should throw IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// ok
} catch (final Exception e) {
fail("Should throw IllegalArgumentException");
}
}
}
private void testLatLong(final LatLong latLong, final double lat, final double lon) {
assertEquals(lat, latLong.getLatitude(), 0.0);
assertEquals(lon, latLong.getLongitude(), 0.0);
}
private void testLatLong(final LatLong latLong, final double lat, final double lon) {
assertEquals(lat, latLong.getLatitude(), 0.0);
assertEquals(lon, latLong.getLongitude(), 0.0);
}
@Test
public void testEquals() {
assertTrue(new LatLong(63.0, 10.0).equals(new LatLong(63.0, 10.0)));
assertFalse(new LatLong(10.0, 63.0).equals(new LatLong(63.0, 10.0)));
assertFalse(new LatLong(10.0, 63.0).equals(null));
}
@Test
public void testEquals() {
assertTrue(new LatLong(63.0, 10.0).equals(new LatLong(63.0, 10.0)));
assertFalse(new LatLong(10.0, 63.0).equals(new LatLong(63.0, 10.0)));
assertFalse(new LatLong(10.0, 63.0).equals(null));
}
@Test
public void testHashCode() {
final Map<LatLong, String> map = new HashMap<>();
map.put(new LatLong(63.0, 10.0), "first");
map.put(new LatLong(63.0, 10.0), "second");
assertEquals(1, map.size());
assertEquals("second", map.get(new LatLong(63.0, 10.0)));
}
@Test
public void testHashCode() {
final Map<LatLong, String> map = new HashMap<>();
map.put(new LatLong(63.0, 10.0), "first");
map.put(new LatLong(63.0, 10.0), "second");