Implement copy action
This commit is contained in:
@@ -15,10 +15,10 @@ import java.util.Optional;
|
||||
public class Controller {
|
||||
private final static Logger logger = LoggerFactory.getLogger(Controller.class);
|
||||
private final ShapesView view;
|
||||
private final Shape model;
|
||||
private final SCollection model;
|
||||
private Selection selection;
|
||||
|
||||
Controller(ShapesView view, Shape model) {
|
||||
Controller(ShapesView view, SCollection model) {
|
||||
this.view = view;
|
||||
this.model = model;
|
||||
|
||||
@@ -53,8 +53,7 @@ public class Controller {
|
||||
}
|
||||
|
||||
private void handleMousePressed(MouseEvent evt) {
|
||||
var sc = (SCollection) this.model;
|
||||
getTarget(evt, sc)
|
||||
getTarget(evt, this.model)
|
||||
.ifPresentOrElse(
|
||||
s -> {
|
||||
if (selection != null) resetSelection();
|
||||
@@ -73,18 +72,29 @@ public class Controller {
|
||||
case KeyEvent.VK_DELETE:
|
||||
deleteSelected();
|
||||
break;
|
||||
case KeyEvent.VK_C:
|
||||
copySelection();
|
||||
break;
|
||||
default:
|
||||
logger.warn("Pressed unhandled key: {}", evt.getKeyChar());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void copySelection() {
|
||||
if (selection == null) {
|
||||
logger.debug("No selection to copy");
|
||||
return;
|
||||
}
|
||||
this.model.add(selection.shape.clone());
|
||||
view.repaint();
|
||||
}
|
||||
|
||||
private void deleteSelected() {
|
||||
if (selection == null) return;
|
||||
logger.debug("Deleting selected shape(s)");
|
||||
var sc = (SCollection) this.model;
|
||||
sc.remove(selection.shape);
|
||||
selection = null;
|
||||
this.model.remove(selection.shape);
|
||||
resetSelection();
|
||||
view.repaint();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user