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
9 changed files with 198 additions and 327 deletions

View File

@@ -1,54 +0,0 @@
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) {}
}

View File

@@ -1,44 +0,0 @@
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) {}
}

View File

@@ -1,11 +0,0 @@
package ovh.gasser.newshapes.command;
public interface Command {
void execute();
void undo();
default void redo() {
execute();
}
}

View File

@@ -1,85 +0,0 @@
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));
}
}

View File

@@ -1,5 +0,0 @@
package ovh.gasser.newshapes.command;
public interface CommandHistoryListener {
void onHistoryChanged(boolean canUndo, boolean canRedo);
}

View File

@@ -1,28 +0,0 @@
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));
}
}

View File

@@ -1,49 +0,0 @@
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) {}
}

View File

@@ -1,51 +0,0 @@
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);
}
}
}

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