diff --git a/src/main/java/com/github/creme332/controller/CanvasController.java b/src/main/java/com/github/creme332/controller/CanvasController.java index a79475ac..4cb548b7 100644 --- a/src/main/java/com/github/creme332/controller/CanvasController.java +++ b/src/main/java/com/github/creme332/controller/CanvasController.java @@ -4,6 +4,8 @@ import java.awt.event.MouseWheelEvent; import java.awt.geom.Line2D; import java.awt.geom.Point2D; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.awt.Point; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; @@ -15,7 +17,7 @@ import com.github.creme332.model.ShapeWrapper; import com.github.creme332.view.Canvas; -public class CanvasController { +public class CanvasController implements PropertyChangeListener { private Canvas canvas; /** @@ -35,6 +37,8 @@ public CanvasController(AppState app, Canvas canvas) { this.canvas = canvas; this.model = app.getCanvasModel(); + app.addPropertyChangeListener(this); + canvas.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { @@ -175,4 +179,16 @@ private void resetCanvasView() { model.setCellSize(CanvasModel.DEFAULT_CELL_SIZE); canvas.repaint(); } + + @Override + public void propertyChange(PropertyChangeEvent e) { + final String propertyName = e.getPropertyName(); + // if mode has changed while a shape is being drawn + if ("mode".equals(propertyName) && currentWrapper != null) { + // erase incomplete shape + model.getShapes().remove(currentWrapper); + currentWrapper = null; + canvas.repaint(); + } + } } diff --git a/src/main/java/com/github/creme332/model/AppState.java b/src/main/java/com/github/creme332/model/AppState.java index 1d8352d3..e0e57a6b 100644 --- a/src/main/java/com/github/creme332/model/AppState.java +++ b/src/main/java/com/github/creme332/model/AppState.java @@ -128,6 +128,7 @@ public CanvasModel getCanvasModel() { public void addPropertyChangeListener(PropertyChangeListener listener) { support.addPropertyChangeListener("sidebarVisibility", listener); + support.addPropertyChangeListener("mode", listener); } public boolean getSideBarVisibility() {