Add delete shape operation
This commit is contained in:
parent
98b05e435e
commit
b4eac668c8
@ -8,8 +8,8 @@ import java.util.TreeMap;
|
||||
|
||||
public abstract class AbstractShape implements Shape {
|
||||
|
||||
private Map<String, Attributes> attributes = new TreeMap<>();
|
||||
private Rectangle bounds;
|
||||
private final Map<String, Attributes> attributes = new TreeMap<>();
|
||||
private final Rectangle bounds;
|
||||
|
||||
AbstractShape() {
|
||||
this(null);
|
||||
|
@ -8,16 +8,17 @@ import ovh.gasser.newshapes.attributes.SelectionAttributes;
|
||||
import ovh.gasser.newshapes.util.Streamable;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Spliterator;
|
||||
|
||||
public class SCollection extends AbstractShape implements Streamable<Shape> {
|
||||
private final static Logger logger = LoggerFactory.getLogger(SCollection.class);
|
||||
private final List<Shape> children;
|
||||
private List<Shape> children;
|
||||
|
||||
private SCollection(Shape... shapes) {
|
||||
this.children = List.of(shapes);
|
||||
this.children = new ArrayList<>(List.of(shapes));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -58,6 +59,12 @@ public class SCollection extends AbstractShape implements Streamable<Shape> {
|
||||
return children.spliterator();
|
||||
}
|
||||
|
||||
public void remove(Shape s) {
|
||||
if (!children.remove(s)) {
|
||||
logger.error("Unable to delete shape: {}", s);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("SCollection{");
|
||||
|
@ -6,12 +6,14 @@ import ovh.gasser.newshapes.Selection;
|
||||
import ovh.gasser.newshapes.shapes.SCollection;
|
||||
import ovh.gasser.newshapes.shapes.Shape;
|
||||
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Controller {
|
||||
private final static Logger logger = LoggerFactory.getLogger(ShapesView.class);
|
||||
private final static Logger logger = LoggerFactory.getLogger(Controller.class);
|
||||
private final ShapesView view;
|
||||
private final Shape model;
|
||||
private Selection selection;
|
||||
@ -19,6 +21,7 @@ public class Controller {
|
||||
Controller(ShapesView view, Shape model) {
|
||||
this.view = view;
|
||||
this.model = model;
|
||||
|
||||
var adapter = new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent evt) {
|
||||
@ -32,6 +35,16 @@ public class Controller {
|
||||
};
|
||||
this.view.addMouseMotionListener(adapter);
|
||||
this.view.addMouseListener(adapter);
|
||||
|
||||
this.view.setFocusable(true);
|
||||
boolean res = this.view.requestFocusInWindow();
|
||||
assert res;
|
||||
this.view.addKeyListener(new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
handleKeyPressed(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleMouseDragged(MouseEvent evt) {
|
||||
@ -55,6 +68,26 @@ public class Controller {
|
||||
view.repaint();
|
||||
}
|
||||
|
||||
private void handleKeyPressed(KeyEvent evt) {
|
||||
switch (evt.getKeyCode()) {
|
||||
case KeyEvent.VK_DELETE:
|
||||
deleteSelected();
|
||||
break;
|
||||
default:
|
||||
logger.warn("Pressed unhandled key: {}", evt.getKeyChar());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteSelected() {
|
||||
if (selection == null) return;
|
||||
logger.debug("Deleting selected shape(s)");
|
||||
var sc = (SCollection) this.model;
|
||||
sc.remove(selection.shape);
|
||||
selection = null;
|
||||
view.repaint();
|
||||
}
|
||||
|
||||
private void resetSelection() {
|
||||
logger.debug("Un-selecting {}", selection.shape);
|
||||
selection.unselect();
|
||||
|
@ -14,7 +14,7 @@ public class ShapeDraftman implements ShapeVisitor {
|
||||
|
||||
private static final ColorAttributes DEFAULT_COLOR_ATTRIBUTES =
|
||||
new ColorAttributes(false, true, Color.BLACK, Color.BLACK);
|
||||
private Graphics2D g2d;
|
||||
private final Graphics2D g2d;
|
||||
|
||||
|
||||
public ShapeDraftman(Graphics graph) {
|
||||
|
@ -23,8 +23,8 @@ public class HTMLDraftman implements ShapeVisitor {
|
||||
|
||||
private static final String FOOTER_TEMPLATE = "</body>";
|
||||
|
||||
private PrintWriter htmlOutput;
|
||||
private PrintWriter cssOutput;
|
||||
private final PrintWriter htmlOutput;
|
||||
private final PrintWriter cssOutput;
|
||||
|
||||
public HTMLDraftman(PrintWriter htmlOutput, PrintWriter cssOutput) {
|
||||
this.htmlOutput = htmlOutput;
|
||||
|
Loading…
x
Reference in New Issue
Block a user