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 {
|
public abstract class AbstractShape implements Shape {
|
||||||
|
|
||||||
private Map<String, Attributes> attributes = new TreeMap<>();
|
private final Map<String, Attributes> attributes = new TreeMap<>();
|
||||||
private Rectangle bounds;
|
private final Rectangle bounds;
|
||||||
|
|
||||||
AbstractShape() {
|
AbstractShape() {
|
||||||
this(null);
|
this(null);
|
||||||
|
@ -8,16 +8,17 @@ import ovh.gasser.newshapes.attributes.SelectionAttributes;
|
|||||||
import ovh.gasser.newshapes.util.Streamable;
|
import ovh.gasser.newshapes.util.Streamable;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Spliterator;
|
import java.util.Spliterator;
|
||||||
|
|
||||||
public class SCollection extends AbstractShape implements Streamable<Shape> {
|
public class SCollection extends AbstractShape implements Streamable<Shape> {
|
||||||
private final static Logger logger = LoggerFactory.getLogger(SCollection.class);
|
private final static Logger logger = LoggerFactory.getLogger(SCollection.class);
|
||||||
private final List<Shape> children;
|
private List<Shape> children;
|
||||||
|
|
||||||
private SCollection(Shape... shapes) {
|
private SCollection(Shape... shapes) {
|
||||||
this.children = List.of(shapes);
|
this.children = new ArrayList<>(List.of(shapes));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -58,6 +59,12 @@ public class SCollection extends AbstractShape implements Streamable<Shape> {
|
|||||||
return children.spliterator();
|
return children.spliterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void remove(Shape s) {
|
||||||
|
if (!children.remove(s)) {
|
||||||
|
logger.error("Unable to delete shape: {}", s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder("SCollection{");
|
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.SCollection;
|
||||||
import ovh.gasser.newshapes.shapes.Shape;
|
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.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class Controller {
|
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 ShapesView view;
|
||||||
private final Shape model;
|
private final Shape model;
|
||||||
private Selection selection;
|
private Selection selection;
|
||||||
@ -19,6 +21,7 @@ public class Controller {
|
|||||||
Controller(ShapesView view, Shape model) {
|
Controller(ShapesView view, Shape model) {
|
||||||
this.view = view;
|
this.view = view;
|
||||||
this.model = model;
|
this.model = model;
|
||||||
|
|
||||||
var adapter = new MouseAdapter() {
|
var adapter = new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent evt) {
|
public void mousePressed(MouseEvent evt) {
|
||||||
@ -32,6 +35,16 @@ public class Controller {
|
|||||||
};
|
};
|
||||||
this.view.addMouseMotionListener(adapter);
|
this.view.addMouseMotionListener(adapter);
|
||||||
this.view.addMouseListener(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) {
|
private void handleMouseDragged(MouseEvent evt) {
|
||||||
@ -55,6 +68,26 @@ public class Controller {
|
|||||||
view.repaint();
|
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() {
|
private void resetSelection() {
|
||||||
logger.debug("Un-selecting {}", selection.shape);
|
logger.debug("Un-selecting {}", selection.shape);
|
||||||
selection.unselect();
|
selection.unselect();
|
||||||
|
@ -14,7 +14,7 @@ public class ShapeDraftman implements ShapeVisitor {
|
|||||||
|
|
||||||
private static final ColorAttributes DEFAULT_COLOR_ATTRIBUTES =
|
private static final ColorAttributes DEFAULT_COLOR_ATTRIBUTES =
|
||||||
new ColorAttributes(false, true, Color.BLACK, Color.BLACK);
|
new ColorAttributes(false, true, Color.BLACK, Color.BLACK);
|
||||||
private Graphics2D g2d;
|
private final Graphics2D g2d;
|
||||||
|
|
||||||
|
|
||||||
public ShapeDraftman(Graphics graph) {
|
public ShapeDraftman(Graphics graph) {
|
||||||
|
@ -23,8 +23,8 @@ public class HTMLDraftman implements ShapeVisitor {
|
|||||||
|
|
||||||
private static final String FOOTER_TEMPLATE = "</body>";
|
private static final String FOOTER_TEMPLATE = "</body>";
|
||||||
|
|
||||||
private PrintWriter htmlOutput;
|
private final PrintWriter htmlOutput;
|
||||||
private PrintWriter cssOutput;
|
private final PrintWriter cssOutput;
|
||||||
|
|
||||||
public HTMLDraftman(PrintWriter htmlOutput, PrintWriter cssOutput) {
|
public HTMLDraftman(PrintWriter htmlOutput, PrintWriter cssOutput) {
|
||||||
this.htmlOutput = htmlOutput;
|
this.htmlOutput = htmlOutput;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user