Compare commits
1 Commits
feature/is
...
feature/is
| Author | SHA1 | Date | |
|---|---|---|---|
| fdcd2b1873 |
6
pom.xml
6
pom.xml
@@ -68,12 +68,6 @@
|
|||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
|
||||||
<artifactId>jackson-databind</artifactId>
|
|
||||||
<version>2.18.3</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package ovh.gasser.newshapes.command;
|
||||||
|
|
||||||
|
import ovh.gasser.newshapes.shapes.SCollection;
|
||||||
|
import ovh.gasser.newshapes.shapes.Shape;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class AddShapeCommand implements Command {
|
||||||
|
private final SCollection model;
|
||||||
|
private final List<IndexedShape> shapes;
|
||||||
|
|
||||||
|
public AddShapeCommand(SCollection model, Shape shape) {
|
||||||
|
this(model, List.of(shape));
|
||||||
|
}
|
||||||
|
|
||||||
|
public AddShapeCommand(SCollection model, Collection<? extends Shape> shapes) {
|
||||||
|
this.model = model;
|
||||||
|
int baseIndex = model.size();
|
||||||
|
List<IndexedShape> indexedShapes = new ArrayList<>();
|
||||||
|
int offset = 0;
|
||||||
|
for (Shape shape : shapes) {
|
||||||
|
indexedShapes.add(new IndexedShape(baseIndex + offset, shape));
|
||||||
|
offset++;
|
||||||
|
}
|
||||||
|
this.shapes = List.copyOf(indexedShapes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
shapes.stream()
|
||||||
|
.sorted(Comparator.comparingInt(IndexedShape::index))
|
||||||
|
.forEach(entry -> {
|
||||||
|
if (!model.contains(entry.shape())) {
|
||||||
|
model.insert(entry.index(), entry.shape());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
List<IndexedShape> reversed = new ArrayList<>(shapes);
|
||||||
|
reversed.sort(Comparator.comparingInt(IndexedShape::index).reversed());
|
||||||
|
reversed.forEach(entry -> {
|
||||||
|
if (model.contains(entry.shape())) {
|
||||||
|
model.remove(entry.shape());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private record IndexedShape(int index, Shape shape) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package ovh.gasser.newshapes.command;
|
||||||
|
|
||||||
|
import ovh.gasser.newshapes.attributes.ColorAttributes;
|
||||||
|
import ovh.gasser.newshapes.shapes.Shape;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ChangeColorCommand implements Command {
|
||||||
|
private final List<ShapeColor> before;
|
||||||
|
private final List<ShapeColor> after;
|
||||||
|
|
||||||
|
public ChangeColorCommand(Collection<? extends Shape> shapes, Map<Shape, ColorAttributes> before, Map<Shape, ColorAttributes> after) {
|
||||||
|
this.before = snapshots(shapes, before);
|
||||||
|
this.after = snapshots(shapes, after);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
apply(after);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
apply(before);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<ShapeColor> snapshots(Collection<? extends Shape> shapes, Map<Shape, ColorAttributes> colors) {
|
||||||
|
return shapes.stream()
|
||||||
|
.map(shape -> new ShapeColor(shape, copy(colors.get(shape))))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void apply(List<ShapeColor> colors) {
|
||||||
|
colors.forEach(shapeColor -> shapeColor.shape().addAttributes(shapeColor.attributes()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ColorAttributes copy(ColorAttributes attrs) {
|
||||||
|
return new ColorAttributes(attrs.filled, attrs.stroked, attrs.filledColor, attrs.strokedColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private record ShapeColor(Shape shape, ColorAttributes attributes) {}
|
||||||
|
}
|
||||||
11
src/main/java/ovh/gasser/newshapes/command/Command.java
Normal file
11
src/main/java/ovh/gasser/newshapes/command/Command.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package ovh.gasser.newshapes.command;
|
||||||
|
|
||||||
|
public interface Command {
|
||||||
|
void execute();
|
||||||
|
|
||||||
|
void undo();
|
||||||
|
|
||||||
|
default void redo() {
|
||||||
|
execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
package ovh.gasser.newshapes.command;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CommandHistory {
|
||||||
|
public static final int DEFAULT_LIMIT = 100;
|
||||||
|
|
||||||
|
private final Deque<Command> undoStack = new ArrayDeque<>();
|
||||||
|
private final Deque<Command> redoStack = new ArrayDeque<>();
|
||||||
|
private final List<CommandHistoryListener> listeners = new ArrayList<>();
|
||||||
|
private final int limit;
|
||||||
|
|
||||||
|
public CommandHistory() {
|
||||||
|
this(DEFAULT_LIMIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandHistory(int limit) {
|
||||||
|
this.limit = Math.max(1, limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void execute(Command command) {
|
||||||
|
command.execute();
|
||||||
|
undoStack.push(command);
|
||||||
|
trimUndoStack();
|
||||||
|
redoStack.clear();
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void undo() {
|
||||||
|
if (!canUndo()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Command command = undoStack.pop();
|
||||||
|
command.undo();
|
||||||
|
redoStack.push(command);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void redo() {
|
||||||
|
if (!canRedo()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Command command = redoStack.pop();
|
||||||
|
command.redo();
|
||||||
|
undoStack.push(command);
|
||||||
|
trimUndoStack();
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean canUndo() {
|
||||||
|
return !undoStack.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean canRedo() {
|
||||||
|
return !redoStack.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
undoStack.clear();
|
||||||
|
redoStack.clear();
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(CommandHistoryListener listener) {
|
||||||
|
listeners.add(listener);
|
||||||
|
listener.onHistoryChanged(canUndo(), canRedo());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void trimUndoStack() {
|
||||||
|
while (undoStack.size() > limit) {
|
||||||
|
undoStack.removeLast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void notifyListeners() {
|
||||||
|
boolean canUndo = canUndo();
|
||||||
|
boolean canRedo = canRedo();
|
||||||
|
listeners.forEach(listener -> listener.onHistoryChanged(canUndo, canRedo));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package ovh.gasser.newshapes.command;
|
||||||
|
|
||||||
|
public interface CommandHistoryListener {
|
||||||
|
void onHistoryChanged(boolean canUndo, boolean canRedo);
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package ovh.gasser.newshapes.command;
|
||||||
|
|
||||||
|
import ovh.gasser.newshapes.shapes.Shape;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MoveShapeCommand implements Command {
|
||||||
|
private final List<Shape> shapes;
|
||||||
|
private final int dx;
|
||||||
|
private final int dy;
|
||||||
|
|
||||||
|
public MoveShapeCommand(Collection<? extends Shape> shapes, int dx, int dy) {
|
||||||
|
this.shapes = List.copyOf(shapes);
|
||||||
|
this.dx = dx;
|
||||||
|
this.dy = dy;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
shapes.forEach(shape -> shape.translate(dx, dy));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
shapes.forEach(shape -> shape.translate(-dx, -dy));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package ovh.gasser.newshapes.command;
|
||||||
|
|
||||||
|
import ovh.gasser.newshapes.shapes.SCollection;
|
||||||
|
import ovh.gasser.newshapes.shapes.Shape;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class RemoveShapeCommand implements Command {
|
||||||
|
private final SCollection model;
|
||||||
|
private final List<IndexedShape> shapes;
|
||||||
|
|
||||||
|
public RemoveShapeCommand(SCollection model, Shape shape) {
|
||||||
|
this(model, List.of(shape));
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoveShapeCommand(SCollection model, Collection<? extends Shape> shapes) {
|
||||||
|
this.model = model;
|
||||||
|
this.shapes = shapes.stream()
|
||||||
|
.map(shape -> new IndexedShape(model.indexOf(shape), shape))
|
||||||
|
.filter(entry -> entry.index() >= 0)
|
||||||
|
.sorted(Comparator.comparingInt(IndexedShape::index))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
List<IndexedShape> reversed = new ArrayList<>(shapes);
|
||||||
|
reversed.sort(Comparator.comparingInt(IndexedShape::index).reversed());
|
||||||
|
reversed.forEach(entry -> {
|
||||||
|
if (model.contains(entry.shape())) {
|
||||||
|
model.remove(entry.shape());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
shapes.forEach(entry -> {
|
||||||
|
if (!model.contains(entry.shape())) {
|
||||||
|
model.insert(entry.index(), entry.shape());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private record IndexedShape(int index, Shape shape) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package ovh.gasser.newshapes.command;
|
||||||
|
|
||||||
|
import ovh.gasser.newshapes.shapes.AbstractShape;
|
||||||
|
import ovh.gasser.newshapes.shapes.Shape;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ResizeShapeCommand implements Command {
|
||||||
|
private final List<ShapeBounds> before;
|
||||||
|
private final List<ShapeBounds> after;
|
||||||
|
|
||||||
|
public ResizeShapeCommand(Collection<? extends Shape> shapes, Map<Shape, Rectangle> before, Map<Shape, Rectangle> after) {
|
||||||
|
this.before = snapshots(shapes, before);
|
||||||
|
this.after = snapshots(shapes, after);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
apply(after);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
apply(before);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<ShapeBounds> snapshots(Collection<? extends Shape> shapes, Map<Shape, Rectangle> states) {
|
||||||
|
return shapes.stream()
|
||||||
|
.map(shape -> new ShapeBounds(shape, states.get(shape)))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void apply(List<ShapeBounds> states) {
|
||||||
|
states.forEach(state -> {
|
||||||
|
if (!(state.shape() instanceof AbstractShape abstractShape)) {
|
||||||
|
throw new IllegalArgumentException("Resize commands support AbstractShape instances only");
|
||||||
|
}
|
||||||
|
abstractShape.setBounds(state.bounds());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private record ShapeBounds(Shape shape, Rectangle bounds) {
|
||||||
|
private ShapeBounds(Shape shape, Rectangle bounds) {
|
||||||
|
this.shape = shape;
|
||||||
|
this.bounds = new Rectangle(bounds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package ovh.gasser.newshapes.persistence;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.Version;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
||||||
import ovh.gasser.newshapes.shapes.SCollection;
|
|
||||||
import ovh.gasser.newshapes.shapes.Shape;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class DrawingSerializer {
|
|
||||||
private static final String VERSION = "1.0";
|
|
||||||
private final ObjectMapper mapper;
|
|
||||||
|
|
||||||
public DrawingSerializer() {
|
|
||||||
this.mapper = new ObjectMapper();
|
|
||||||
SimpleModule module = new SimpleModule("ShapeModule", new Version(1, 0, 0, null, null, null));
|
|
||||||
module.addSerializer(Shape.class, new ShapeSerializer());
|
|
||||||
module.addDeserializer(Shape.class, new ShapeDeserializer());
|
|
||||||
this.mapper.registerModule(module);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void save(SCollection model, File file) throws IOException {
|
|
||||||
DrawingData data = new DrawingData();
|
|
||||||
data.version = VERSION;
|
|
||||||
data.shapes = new java.util.ArrayList<>();
|
|
||||||
for (Shape shape : model) {
|
|
||||||
data.shapes.add(shape);
|
|
||||||
}
|
|
||||||
mapper.writerWithDefaultPrettyPrinter().writeValue(file, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SCollection load(File file) throws IOException {
|
|
||||||
DrawingData data = mapper.readValue(file, DrawingData.class);
|
|
||||||
return SCollection.of(data.shapes.toArray(new Shape[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class DrawingData {
|
|
||||||
public String version;
|
|
||||||
public java.util.List<Shape> shapes;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,159 +0,0 @@
|
|||||||
package ovh.gasser.newshapes.persistence;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonParser;
|
|
||||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
|
||||||
import ovh.gasser.newshapes.shapes.SCircle;
|
|
||||||
import ovh.gasser.newshapes.shapes.SCollection;
|
|
||||||
import ovh.gasser.newshapes.shapes.SPolygon;
|
|
||||||
import ovh.gasser.newshapes.shapes.SRectangle;
|
|
||||||
import ovh.gasser.newshapes.shapes.SText;
|
|
||||||
import ovh.gasser.newshapes.shapes.STriangle;
|
|
||||||
import ovh.gasser.newshapes.shapes.Shape;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ShapeDeserializer extends StdDeserializer<Shape> {
|
|
||||||
|
|
||||||
public ShapeDeserializer() {
|
|
||||||
super(Shape.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Shape deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
|
||||||
JsonNode node = p.getCodec().readTree(p);
|
|
||||||
String type = node.get("type").asText();
|
|
||||||
|
|
||||||
return switch (type) {
|
|
||||||
case "rectangle" -> deserializeRectangle(node);
|
|
||||||
case "circle" -> deserializeCircle(node);
|
|
||||||
case "triangle" -> deserializeTriangle(node);
|
|
||||||
case "text" -> deserializeText(node);
|
|
||||||
case "polygon" -> deserializePolygon(node);
|
|
||||||
case "collection" -> deserializeCollection(node, p, ctxt);
|
|
||||||
default -> throw new IllegalArgumentException("Unknown shape type: " + type);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private SRectangle deserializeRectangle(JsonNode node) {
|
|
||||||
int x = node.get("x").asInt();
|
|
||||||
int y = node.get("y").asInt();
|
|
||||||
int width = node.get("width").asInt();
|
|
||||||
int height = node.get("height").asInt();
|
|
||||||
|
|
||||||
Color strokeColor = extractStrokeColor(node);
|
|
||||||
return SRectangle.create(x, y, width, height, strokeColor);
|
|
||||||
}
|
|
||||||
|
|
||||||
private SCircle deserializeCircle(JsonNode node) {
|
|
||||||
int x = node.get("x").asInt();
|
|
||||||
int y = node.get("y").asInt();
|
|
||||||
int radius = node.get("radius").asInt();
|
|
||||||
|
|
||||||
Color strokeColor = extractStrokeColor(node);
|
|
||||||
Color fillColor = extractFillColor(node);
|
|
||||||
|
|
||||||
SCircle circle = SCircle.create(x + radius, y + radius, radius);
|
|
||||||
circle.addAttributes(new ovh.gasser.newshapes.attributes.ColorAttributes(
|
|
||||||
fillColor != null, strokeColor != null, strokeColor, fillColor));
|
|
||||||
return circle;
|
|
||||||
}
|
|
||||||
|
|
||||||
private STriangle deserializeTriangle(JsonNode node) {
|
|
||||||
int x = node.get("x").asInt();
|
|
||||||
int y = node.get("y").asInt();
|
|
||||||
int size = node.get("size").asInt();
|
|
||||||
|
|
||||||
Color strokeColor = extractStrokeColor(node);
|
|
||||||
Color fillColor = extractFillColor(node);
|
|
||||||
|
|
||||||
return STriangle.create(x, y, size, fillColor != null ? fillColor : Color.YELLOW,
|
|
||||||
strokeColor != null ? strokeColor : Color.BLACK);
|
|
||||||
}
|
|
||||||
|
|
||||||
private SText deserializeText(JsonNode node) {
|
|
||||||
int x = node.get("x").asInt();
|
|
||||||
int y = node.get("y").asInt();
|
|
||||||
String text = node.get("text").asText();
|
|
||||||
|
|
||||||
Color strokeColor = extractStrokeColor(node);
|
|
||||||
|
|
||||||
SText sText = SText.create(x, y, text);
|
|
||||||
if (strokeColor != null) {
|
|
||||||
sText.addAttributes(new ovh.gasser.newshapes.attributes.ColorAttributes(
|
|
||||||
true, true, strokeColor, strokeColor));
|
|
||||||
}
|
|
||||||
return sText;
|
|
||||||
}
|
|
||||||
|
|
||||||
private SPolygon deserializePolygon(JsonNode node) {
|
|
||||||
List<Point> points = new ArrayList<>();
|
|
||||||
JsonNode pointsNode = node.get("points");
|
|
||||||
for (JsonNode pointNode : pointsNode) {
|
|
||||||
int x = pointNode.get("x").asInt();
|
|
||||||
int y = pointNode.get("y").asInt();
|
|
||||||
points.add(new Point(x, y));
|
|
||||||
}
|
|
||||||
|
|
||||||
Color strokeColor = extractStrokeColor(node);
|
|
||||||
Color fillColor = extractFillColor(node);
|
|
||||||
|
|
||||||
SPolygon poly = SPolygon.create(points);
|
|
||||||
if (strokeColor != null || fillColor != null) {
|
|
||||||
poly.addAttributes(new ovh.gasser.newshapes.attributes.ColorAttributes(
|
|
||||||
fillColor != null, strokeColor != null,
|
|
||||||
strokeColor != null ? strokeColor : Color.BLACK,
|
|
||||||
fillColor != null ? fillColor : Color.BLACK));
|
|
||||||
}
|
|
||||||
return poly;
|
|
||||||
}
|
|
||||||
|
|
||||||
private SCollection deserializeCollection(JsonNode node, JsonParser p, DeserializationContext ctxt) throws IOException {
|
|
||||||
List<Shape> children = new ArrayList<>();
|
|
||||||
JsonNode shapesNode = node.get("shapes");
|
|
||||||
if (shapesNode != null && shapesNode.isArray()) {
|
|
||||||
for (JsonNode childNode : shapesNode) {
|
|
||||||
JsonParser childParser = childNode.traverse(p.getCodec());
|
|
||||||
children.add(deserialize(childParser, ctxt));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return SCollection.of(children.toArray(new Shape[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Color extractStrokeColor(JsonNode node) {
|
|
||||||
if (node.has("color")) {
|
|
||||||
JsonNode colorNode = node.get("color");
|
|
||||||
if (colorNode.has("stroked") && colorNode.get("stroked").asBoolean()) {
|
|
||||||
return hexToColor(colorNode.get("strokedColor").asText());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Color extractFillColor(JsonNode node) {
|
|
||||||
if (node.has("color")) {
|
|
||||||
JsonNode colorNode = node.get("color");
|
|
||||||
if (colorNode.has("filled") && colorNode.get("filled").asBoolean()) {
|
|
||||||
return hexToColor(colorNode.get("filledColor").asText());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Color hexToColor(String hex) {
|
|
||||||
if (hex == null || hex.isEmpty()) return Color.BLACK;
|
|
||||||
try {
|
|
||||||
return new Color(
|
|
||||||
Integer.parseInt(hex.substring(1, 3), 16),
|
|
||||||
Integer.parseInt(hex.substring(3, 5), 16),
|
|
||||||
Integer.parseInt(hex.substring(5, 7), 16)
|
|
||||||
);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return Color.BLACK;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
package ovh.gasser.newshapes.persistence;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
|
||||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
||||||
import ovh.gasser.newshapes.attributes.ColorAttributes;
|
|
||||||
import ovh.gasser.newshapes.shapes.SCircle;
|
|
||||||
import ovh.gasser.newshapes.shapes.SCollection;
|
|
||||||
import ovh.gasser.newshapes.shapes.SPolygon;
|
|
||||||
import ovh.gasser.newshapes.shapes.SRectangle;
|
|
||||||
import ovh.gasser.newshapes.shapes.SText;
|
|
||||||
import ovh.gasser.newshapes.shapes.STriangle;
|
|
||||||
import ovh.gasser.newshapes.shapes.Shape;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ShapeSerializer extends JsonSerializer<Shape> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void serialize(Shape shape, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
|
||||||
gen.writeStartObject();
|
|
||||||
|
|
||||||
if (shape instanceof SRectangle rect) {
|
|
||||||
gen.writeStringField("type", "rectangle");
|
|
||||||
writeRectangle(rect, gen);
|
|
||||||
} else if (shape instanceof SCircle circle) {
|
|
||||||
gen.writeStringField("type", "circle");
|
|
||||||
writeCircle(circle, gen);
|
|
||||||
} else if (shape instanceof STriangle tri) {
|
|
||||||
gen.writeStringField("type", "triangle");
|
|
||||||
writeTriangle(tri, gen);
|
|
||||||
} else if (shape instanceof SText text) {
|
|
||||||
gen.writeStringField("type", "text");
|
|
||||||
writeText(text, gen);
|
|
||||||
} else if (shape instanceof SPolygon poly) {
|
|
||||||
gen.writeStringField("type", "polygon");
|
|
||||||
writePolygon(poly, gen);
|
|
||||||
} else if (shape instanceof SCollection coll) {
|
|
||||||
gen.writeStringField("type", "collection");
|
|
||||||
writeCollection(coll, gen);
|
|
||||||
}
|
|
||||||
|
|
||||||
gen.writeEndObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeRectangle(SRectangle rect, JsonGenerator gen) throws IOException {
|
|
||||||
Rectangle bounds = rect.getBounds();
|
|
||||||
gen.writeNumberField("x", bounds.x);
|
|
||||||
gen.writeNumberField("y", bounds.y);
|
|
||||||
gen.writeNumberField("width", bounds.width);
|
|
||||||
gen.writeNumberField("height", bounds.height);
|
|
||||||
writeColorAttributes(rect, gen);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeCircle(SCircle circle, JsonGenerator gen) throws IOException {
|
|
||||||
Rectangle bounds = circle.getBounds();
|
|
||||||
gen.writeNumberField("x", bounds.x);
|
|
||||||
gen.writeNumberField("y", bounds.y);
|
|
||||||
gen.writeNumberField("radius", circle.getRadius());
|
|
||||||
writeColorAttributes(circle, gen);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeTriangle(STriangle tri, JsonGenerator gen) throws IOException {
|
|
||||||
Rectangle bounds = tri.getBounds();
|
|
||||||
gen.writeNumberField("x", bounds.x);
|
|
||||||
gen.writeNumberField("y", bounds.y);
|
|
||||||
gen.writeNumberField("size", bounds.width);
|
|
||||||
writeColorAttributes(tri, gen);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeText(SText text, JsonGenerator gen) throws IOException {
|
|
||||||
Rectangle bounds = text.getBounds();
|
|
||||||
gen.writeNumberField("x", bounds.x);
|
|
||||||
gen.writeNumberField("y", bounds.y);
|
|
||||||
gen.writeStringField("text", text.getText());
|
|
||||||
gen.writeStringField("fontName", text.getFontName());
|
|
||||||
gen.writeNumberField("fontSize", text.getFontSize());
|
|
||||||
gen.writeNumberField("fontStyle", text.getFontStyle());
|
|
||||||
writeColorAttributes(text, gen);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writePolygon(SPolygon poly, JsonGenerator gen) throws IOException {
|
|
||||||
gen.writeArrayFieldStart("points");
|
|
||||||
for (Point p : poly.getPoints()) {
|
|
||||||
gen.writeStartObject();
|
|
||||||
gen.writeNumberField("x", p.x);
|
|
||||||
gen.writeNumberField("y", p.y);
|
|
||||||
gen.writeEndObject();
|
|
||||||
}
|
|
||||||
gen.writeEndArray();
|
|
||||||
writeColorAttributes(poly, gen);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeCollection(SCollection coll, JsonGenerator gen) throws IOException {
|
|
||||||
gen.writeArrayFieldStart("shapes");
|
|
||||||
for (Shape child : coll) {
|
|
||||||
serialize(child, gen, null);
|
|
||||||
}
|
|
||||||
gen.writeEndArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeColorAttributes(Shape shape, JsonGenerator gen) throws IOException {
|
|
||||||
ColorAttributes attrs = (ColorAttributes) shape.getAttributes(ColorAttributes.ID);
|
|
||||||
if (attrs != null) {
|
|
||||||
gen.writeObjectFieldStart("color");
|
|
||||||
gen.writeBooleanField("filled", attrs.filled);
|
|
||||||
gen.writeBooleanField("stroked", attrs.stroked);
|
|
||||||
gen.writeStringField("filledColor", colorToHex(attrs.filledColor));
|
|
||||||
gen.writeStringField("strokedColor", colorToHex(attrs.strokedColor));
|
|
||||||
gen.writeEndObject();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String colorToHex(Color c) {
|
|
||||||
if (c == null) return "#000000";
|
|
||||||
return String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user