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 265 additions and 157 deletions

View File

@@ -11,6 +11,8 @@ import ovh.gasser.newshapes.ui.listeners.MenuEditListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
public class App {
@@ -19,6 +21,8 @@ public class App {
private SCollection model;
private JCheckBoxMenuItem editFill;
private JCheckBoxMenuItem editBorder;
private JMenuItem editGroup;
private JMenuItem editUngroup;
private App() throws HeadlessException {
final JFrame frame = new JFrame("Reactive shapes");
@@ -71,12 +75,38 @@ public class App {
private JMenu buildFileMenu(ShapesView sview) {
JMenu menuFile = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem addRectItem = new JMenuItem("Add SRectangle");
JMenuItem addCircleItem = new JMenuItem("Add SCircle");
JMenuItem addTextItem = new JMenuItem("Add Text");
JMenuItem htmlExportItem = new JMenuItem("Export to HTML");
JMenuItem svgExportItem = new JMenuItem("Export to SVG");
JMenuItem exitItem = new JMenuItem("Exit");
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
openItem.addActionListener(evt -> {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JSON Files", "json"));
if (chooser.showOpenDialog(sview) == JFileChooser.APPROVE_OPTION) {
sview.getController().loadDrawing(chooser.getSelectedFile());
}
});
saveItem.addActionListener(evt -> {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JSON Files", "json"));
if (chooser.showSaveDialog(sview) == JFileChooser.APPROVE_OPTION) {
java.io.File file = chooser.getSelectedFile();
if (!file.getName().endsWith(".json")) {
file = new java.io.File(file.getAbsolutePath() + ".json");
}
sview.getController().saveDrawing(file);
}
});
addRectItem.addActionListener(new MenuAddListener("SRectangle", model, sview));
addCircleItem.addActionListener(new MenuAddListener("SCircle", model, sview));
addTextItem.addActionListener(evt -> sview.getController().enterTextMode());
@@ -95,6 +125,10 @@ public class App {
}
});
exitItem.addActionListener(evt -> System.exit(0));
menuFile.add(openItem);
menuFile.add(saveItem);
menuFile.addSeparator();
menuFile.add(addRectItem);
menuFile.add(addCircleItem);
menuFile.add(addTextItem);
@@ -108,32 +142,62 @@ public class App {
private JMenu buildEditMenu(ShapesView sview) {
MenuEditListener editListener = new MenuEditListener(model, sview, sview.getController());
JMenu menuEdit = new JMenu("Edit");
JMenuItem cutItem = new JMenuItem("Cut");
JMenuItem copyItem = new JMenuItem("Copy");
JMenuItem pasteItem = new JMenuItem("Paste");
JMenuItem editColor = new JMenuItem("Change color");
JMenuItem editBorderColor = new JMenuItem("Change border color");
JMenuItem deleteItem = new JMenuItem("Delete");
editGroup = new JMenuItem("Group");
editUngroup = new JMenuItem("Ungroup");
editFill = new JCheckBoxMenuItem("Fill Shape");
editBorder = new JCheckBoxMenuItem("Draw border");
cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK));
copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK));
editGroup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_DOWN_MASK));
editUngroup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
cutItem.addActionListener(evt -> sview.getController().cutSelection());
copyItem.addActionListener(evt -> sview.getController().copySelection());
pasteItem.addActionListener(evt -> sview.getController().pasteClipboard());
editColor.addActionListener(editListener);
editBorderColor.addActionListener(editListener);
deleteItem.addActionListener(editListener);
editGroup.addActionListener(evt -> sview.getController().group());
editUngroup.addActionListener(evt -> sview.getController().ungroup());
editFill.addActionListener(editListener);
editBorder.addActionListener(editListener);
editGroup.setEnabled(false);
editUngroup.setEnabled(false);
menuEdit.add(cutItem);
menuEdit.add(copyItem);
menuEdit.add(pasteItem);
menuEdit.addSeparator();
menuEdit.add(editColor);
menuEdit.add(editBorderColor);
menuEdit.add(deleteItem);
menuEdit.addSeparator();
menuEdit.add(editGroup);
menuEdit.add(editUngroup);
menuEdit.addSeparator();
menuEdit.add(editBorder);
menuEdit.add(editFill);
return menuEdit;
}
private void updateMenuState(Iterable<Shape> selectedShapes) {
int selectionCount = 0;
boolean singleCollectionSelected = false;
boolean hasToggleableShapes = false;
boolean allFilled = true;
boolean allStroked = true;
for (Shape s : selectedShapes) {
selectionCount++;
singleCollectionSelected = selectionCount == 1 && s instanceof SCollection;
if (s instanceof SText) {
continue;
}
@@ -145,6 +209,8 @@ public class App {
}
}
editGroup.setEnabled(selectionCount > 1);
editUngroup.setEnabled(selectionCount == 1 && singleCollectionSelected);
updateMenuItem(editFill, hasToggleableShapes, allFilled);
updateMenuItem(editBorder, hasToggleableShapes, allStroked);
}

View File

@@ -117,30 +117,6 @@ public class ShapeDraftman implements ShapeVisitor {
drawHandlerIfSelected(text);
}
@Override
public void visitPolygon(SPolygon polygon) {
ColorAttributes colAttrs = (ColorAttributes) polygon.getAttributes(ColorAttributes.ID);
if (colAttrs == null) {
colAttrs = DEFAULT_COLOR_ATTRIBUTES;
}
java.util.List<Point> points = polygon.getPoints();
int[] xPoints = points.stream().mapToInt(p -> p.x).toArray();
int[] yPoints = points.stream().mapToInt(p -> p.y).toArray();
int nPoints = points.size();
if (colAttrs.filled) {
this.g2d.setColor(colAttrs.filledColor);
this.g2d.fillPolygon(xPoints, yPoints, nPoints);
}
if (colAttrs.stroked) {
this.g2d.setColor(colAttrs.strokedColor);
this.g2d.drawPolygon(xPoints, yPoints, nPoints);
}
drawHandlerIfSelected(polygon);
}
private Color resolveTextColor(ColorAttributes attrs) {
if (attrs == null) {
return Color.BLACK;

View File

@@ -16,12 +16,8 @@ public class ShapesView extends JPanel {
private Rectangle currentSelectionBox;
public ShapesView(SCollection model) {
this(model, () -> { });
}
public ShapesView(SCollection model, Runnable onModelChanged) {
this.model = model;
this.controller = new Controller(this, model, onModelChanged);
this.controller = new Controller(this, model);
}
@Override

View File

@@ -1,128 +0,0 @@
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 javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class BoxSelectionTest {
@Test
void draggingSelectionBoxSelectsAllIntersectingShapes() throws Exception {
SRectangle first = SRectangle.create(20, 20, 30, 30);
SRectangle second = SRectangle.create(80, 80, 30, 30);
SRectangle outside = SRectangle.create(160, 160, 30, 30);
ShapesView view = createView(first, second, outside);
dragSelection(view, new Point(5, 5), new Point(100, 100), 0);
assertTrue(isSelected(first));
assertTrue(isSelected(second));
assertFalse(isSelected(outside));
}
@Test
void shiftDraggingSelectionBoxAddsToExistingSelection() throws Exception {
SRectangle first = SRectangle.create(10, 10, 30, 30);
SRectangle second = SRectangle.create(80, 10, 30, 30);
SRectangle outside = SRectangle.create(160, 10, 30, 30);
ShapesView view = createView(first, second, outside);
click(view, new Point(20, 20), 0);
dragSelection(view, new Point(70, 5), new Point(120, 50), InputEvent.SHIFT_DOWN_MASK);
assertTrue(isSelected(first));
assertTrue(isSelected(second));
assertFalse(isSelected(outside));
}
@Test
void shapesViewPaintsSelectionBoxDuringRendering() throws Exception {
ShapesView view = createView();
BufferedImage withoutSelectionBox = paintView(view, null);
BufferedImage withSelectionBox = paintView(view, new Rectangle(10, 10, 40, 40));
assertTrue(imagesDiffer(withoutSelectionBox, withSelectionBox));
}
private ShapesView createView(Shape... shapes) throws InvocationTargetException, InterruptedException {
final ShapesView[] ref = new ShapesView[1];
SwingUtilities.invokeAndWait(() -> {
ref[0] = new ShapesView(SCollection.of(shapes));
ref[0].setOpaque(true);
ref[0].setBackground(Color.WHITE);
ref[0].setSize(240, 240);
});
return ref[0];
}
private void click(ShapesView view, Point point, int modifiers) throws InvocationTargetException, InterruptedException {
dispatch(view, MouseEvent.MOUSE_PRESSED, point, modifiers);
dispatch(view, MouseEvent.MOUSE_RELEASED, point, modifiers);
}
private void dragSelection(ShapesView view, Point start, Point end, int modifiers) throws InvocationTargetException, InterruptedException {
dispatch(view, MouseEvent.MOUSE_PRESSED, start, modifiers);
dispatch(view, MouseEvent.MOUSE_DRAGGED, end, modifiers);
dispatch(view, MouseEvent.MOUSE_RELEASED, end, modifiers);
}
private void dispatch(ShapesView view, int eventId, Point point, int modifiers)
throws InvocationTargetException, InterruptedException {
SwingUtilities.invokeAndWait(() -> view.dispatchEvent(new MouseEvent(
view,
eventId,
System.currentTimeMillis(),
modifiers,
point.x,
point.y,
1,
false,
MouseEvent.BUTTON1
)));
}
private BufferedImage paintView(ShapesView view, Rectangle box)
throws InvocationTargetException, InterruptedException {
final BufferedImage[] ref = new BufferedImage[1];
SwingUtilities.invokeAndWait(() -> {
view.setCurrentSelectionBox(box);
BufferedImage image = new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
try {
view.paint(graphics);
} finally {
graphics.dispose();
}
ref[0] = image;
});
return ref[0];
}
private boolean imagesDiffer(BufferedImage first, BufferedImage second) {
for (int y = 0; y < first.getHeight(); y++) {
for (int x = 0; x < first.getWidth(); x++) {
if (first.getRGB(x, y) != second.getRGB(x, y)) {
return true;
}
}
}
return false;
}
private boolean isSelected(Shape shape) {
return ((SelectionAttributes) shape.getAttributes(SelectionAttributes.ID)).selected;
}
}

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