1 Commits

Author SHA1 Message Date
f15b97b000 Implement #35: Copy/Paste Functionality
Some checks failed
CI / build-and-test (pull_request) Failing after 11s
2026-03-28 00:55:54 +01:00
5 changed files with 198 additions and 329 deletions

View File

@@ -68,12 +68,6 @@
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

View File

@@ -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;
}
}

View File

@@ -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;
}
}
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,198 @@
package ovh.gasser.newshapes.ui;
import org.junit.jupiter.api.Test;
import ovh.gasser.newshapes.attributes.SelectionAttributes;
import ovh.gasser.newshapes.shapes.SCollection;
import ovh.gasser.newshapes.shapes.SRectangle;
import ovh.gasser.newshapes.shapes.Shape;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ControllerTest {
@Test
void copyDoesNotMutateModelAndPasteSelectsOffsetClone() {
SCollection model = SCollection.of(SRectangle.create(10, 15, 30, 40, Color.BLUE));
ShapesView view = new ShapesView(model);
Controller controller = view.getController();
click(view, 15, 20, 0);
controller.copySelection();
assertEquals(1, model.stream().count());
controller.pasteClipboard();
List<Shape> shapes = model.stream().toList();
assertEquals(2, shapes.size());
assertEquals(new Rectangle(10, 15, 30, 40), shapes.get(0).getBounds());
assertEquals(new Rectangle(30, 35, 30, 40), shapes.get(1).getBounds());
assertFalse(isSelected(shapes.get(0)));
assertTrue(isSelected(shapes.get(1)));
}
@Test
void repeatedPasteKeepsIncreasingOffset() {
SCollection model = SCollection.of(SRectangle.create(5, 10, 20, 25, Color.RED));
ShapesView view = new ShapesView(model);
Controller controller = view.getController();
click(view, 10, 15, 0);
controller.copySelection();
controller.pasteClipboard();
controller.pasteClipboard();
List<Shape> shapes = model.stream().toList();
assertEquals(3, shapes.size());
assertEquals(new Rectangle(25, 30, 20, 25), shapes.get(1).getBounds());
assertEquals(new Rectangle(45, 50, 20, 25), shapes.get(2).getBounds());
assertFalse(isSelected(shapes.get(1)));
assertTrue(isSelected(shapes.get(2)));
}
@Test
void controlShortcutsCutAndPasteMultipleShapes() {
SCollection model = SCollection.of(
SRectangle.create(10, 10, 15, 20, Color.BLACK),
SRectangle.create(80, 25, 25, 30, Color.GREEN)
);
ShapesView view = new ShapesView(model);
click(view, 15, 15, 0);
click(view, 85, 30, InputEvent.SHIFT_DOWN_MASK);
pressShortcut(view, KeyEvent.VK_X);
assertEquals(0, model.stream().count());
pressShortcut(view, KeyEvent.VK_V);
List<Shape> shapes = model.stream().toList();
assertEquals(2, shapes.size());
assertEquals(new Rectangle(30, 30, 15, 20), shapes.get(0).getBounds());
assertEquals(new Rectangle(100, 45, 25, 30), shapes.get(1).getBounds());
assertTrue(isSelected(shapes.get(0)));
assertTrue(isSelected(shapes.get(1)));
}
@Test
void groupReplacesSelectedShapesWithCollectionAndSelectsIt() {
SRectangle rect1 = SRectangle.create(10, 10, 15, 20, Color.BLACK);
SRectangle rect2 = SRectangle.create(80, 25, 25, 30, Color.GREEN);
SRectangle rect3 = SRectangle.create(140, 40, 10, 10, Color.BLUE);
SCollection model = SCollection.of(rect1, rect2, rect3);
ShapesView view = new ShapesView(model);
Controller controller = view.getController();
click(view, 15, 15, 0);
click(view, 85, 30, InputEvent.SHIFT_DOWN_MASK);
controller.group();
List<Shape> shapes = model.stream().toList();
assertEquals(2, shapes.size());
SCollection group = assertInstanceOf(SCollection.class, shapes.get(0));
assertEquals(List.of(rect1, rect2), group.stream().toList());
assertEquals(new Rectangle(10, 10, 95, 45), group.getBounds());
assertTrue(isSelected(group));
assertFalse(isSelected(rect1));
assertFalse(isSelected(rect2));
assertFalse(isSelected(rect3));
}
@Test
void ungroupOnlyBreaksApartOneLevelAndSelectsChildren() {
SRectangle rect1 = SRectangle.create(10, 10, 15, 20, Color.BLACK);
SRectangle rect2 = SRectangle.create(80, 25, 25, 30, Color.GREEN);
SCollection innerGroup = SCollection.of(rect1, rect2);
SRectangle rect3 = SRectangle.create(140, 40, 10, 10, Color.BLUE);
SCollection outerGroup = SCollection.of(innerGroup, rect3);
SCollection model = SCollection.of(outerGroup);
ShapesView view = new ShapesView(model);
Controller controller = view.getController();
click(view, 15, 15, 0);
controller.ungroup();
List<Shape> shapes = model.stream().toList();
assertEquals(List.of(innerGroup, rect3), shapes);
assertTrue(isSelected(innerGroup));
assertTrue(isSelected(rect3));
assertFalse(isSelected(rect1));
assertFalse(isSelected(rect2));
}
@Test
void controlShortcutsGroupAndUngroupShapes() {
SCollection model = SCollection.of(
SRectangle.create(10, 10, 15, 20, Color.BLACK),
SRectangle.create(80, 25, 25, 30, Color.GREEN)
);
ShapesView view = new ShapesView(model);
click(view, 15, 15, 0);
click(view, 85, 30, InputEvent.SHIFT_DOWN_MASK);
pressShortcut(view, KeyEvent.VK_G, InputEvent.CTRL_DOWN_MASK);
Shape groupedShape = model.stream().toList().get(0);
assertTrue(groupedShape instanceof SCollection);
assertTrue(isSelected(groupedShape));
pressShortcut(view, KeyEvent.VK_U, InputEvent.CTRL_DOWN_MASK);
List<Shape> shapes = model.stream().toList();
assertEquals(2, shapes.size());
assertTrue(isSelected(shapes.get(0)));
assertTrue(isSelected(shapes.get(1)));
}
private static void click(ShapesView view, int x, int y, int modifiers) {
MouseEvent event = new MouseEvent(
view,
MouseEvent.MOUSE_PRESSED,
System.currentTimeMillis(),
modifiers,
x,
y,
1,
false,
MouseEvent.BUTTON1
);
for (MouseListener listener : view.getMouseListeners()) {
listener.mousePressed(event);
}
}
private static void pressShortcut(ShapesView view, int keyCode) {
pressShortcut(view, keyCode, InputEvent.CTRL_DOWN_MASK);
}
private static void pressShortcut(ShapesView view, int keyCode, int modifiers) {
KeyEvent event = new KeyEvent(
view,
KeyEvent.KEY_PRESSED,
System.currentTimeMillis(),
modifiers,
keyCode,
KeyEvent.CHAR_UNDEFINED
);
for (var listener : view.getKeyListeners()) {
listener.keyPressed(event);
}
}
private static boolean isSelected(Shape shape) {
SelectionAttributes attributes = (SelectionAttributes) shape.getAttributes(SelectionAttributes.ID);
return attributes != null && attributes.selected;
}
}