From a417fb57d5ade3c1237f675aad890480d258eac0 Mon Sep 17 00:00:00 2001 From: Julian Raufelder Date: Fri, 16 Nov 2018 23:23:15 +0100 Subject: [PATCH] Enhance closing behavior Co-authored-by: DonatJR --- src/main/scala/de/htwg/se/sudoku/Sudoku.scala | 12 ++------- .../scala/de/htwg/se/sudoku/aview/Tui.scala | 27 +++++++++++++------ .../htwg/se/sudoku/aview/gui/SwingGui.scala | 12 ++++++--- .../ControllerInterface.scala | 2 ++ .../controllerBaseImpl/Controller.scala | 5 +++- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/main/scala/de/htwg/se/sudoku/Sudoku.scala b/src/main/scala/de/htwg/se/sudoku/Sudoku.scala index 9ebe9de..84c9d29 100755 --- a/src/main/scala/de/htwg/se/sudoku/Sudoku.scala +++ b/src/main/scala/de/htwg/se/sudoku/Sudoku.scala @@ -1,6 +1,7 @@ package de.htwg.se.sudoku import java.awt.GraphicsEnvironment +import java.io.BufferedReader import akka.actor.{ActorSystem, Props} import akka.pattern.ask @@ -38,18 +39,9 @@ object Sudoku { controller.createNewGrid def main(args: Array[String]): Unit = { - var input: String = "" + tui.processInput(new BufferedReader(Console.in)) - do { - Thread.sleep(200) - - if (Console.in.ready()) { - input = readLine() - tui.processInputLine(input) - } - } while (input != "q") webserver.unbind() fileIoHttpServer.unbind() - controller.finish() } } diff --git a/src/main/scala/de/htwg/se/sudoku/aview/Tui.scala b/src/main/scala/de/htwg/se/sudoku/aview/Tui.scala index fd9b883..b7c52b1 100644 --- a/src/main/scala/de/htwg/se/sudoku/aview/Tui.scala +++ b/src/main/scala/de/htwg/se/sudoku/aview/Tui.scala @@ -1,26 +1,36 @@ package de.htwg.se.sudoku.aview +import java.io.BufferedReader + import com.typesafe.scalalogging.{LazyLogging, Logger} -import de.htwg.se.sudoku.controller.controllerComponent.ControllerInterface -import de.htwg.se.sudoku.controller.controllerComponent.GameStatus -import de.htwg.se.sudoku.controller.controllerComponent.{ - CandidatesChanged, - CellChanged, - GridSizeChanged -} +import de.htwg.se.sudoku.controller.controllerComponent._ import scala.swing.Reactor import scala.swing.event.Event class Tui(controller: ControllerInterface) extends Reactor with LazyLogging { + var stopProcessingInput = false + + def processInput(input: BufferedReader) = { + while (!stopProcessingInput) { + if (input.ready()) { + val line = input.readLine() + processInputLine(line) + } else { + Thread.sleep(200) // don't waste cpu cycles if no input is given + } + } + } + + listenTo(controller) def size = controller.gridSize def randomCells: Int = size * size / 8 def processInputLine(input: String): Unit = { input match { - case "q" => + case "q" => controller.finish() case "e" => controller.createEmptyGrid case "n" => controller.createNewGrid case "z" => controller.undo @@ -46,6 +56,7 @@ class Tui(controller: ControllerInterface) extends Reactor with LazyLogging { case event: GridSizeChanged => printTui case event: CellChanged => printTui case event: CandidatesChanged => printCandidates + case event: SudokuShutdown => stopProcessingInput = true } def printTui: Unit = { diff --git a/src/main/scala/de/htwg/se/sudoku/aview/gui/SwingGui.scala b/src/main/scala/de/htwg/se/sudoku/aview/gui/SwingGui.scala index 7b51052..f2b632e 100644 --- a/src/main/scala/de/htwg/se/sudoku/aview/gui/SwingGui.scala +++ b/src/main/scala/de/htwg/se/sudoku/aview/gui/SwingGui.scala @@ -1,7 +1,10 @@ package de.htwg.se.sudoku.aview.gui -import de.htwg.se.sudoku.controller.controllerComponent.{CandidatesChanged, CellChanged, ControllerInterface, GridSizeChanged} +import java.awt.event.WindowEvent + +import de.htwg.se.sudoku.controller.controllerComponent._ import de.htwg.se.sudoku.util.Observer +import javax.swing.WindowConstants import scala.swing.Swing.LineBorder import scala.swing._ @@ -16,7 +19,9 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer { title = "HTWG Sudoku" var cells = Array.ofDim[CellPanel](controller.gridSize, controller.gridSize) - override def closeOperation(): Unit = System.exit(0) + override def closeOperation(): Unit = controller.finish() + + peer.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) def highlightpanel = new FlowPanel { contents += new Label("Highlight:") @@ -82,7 +87,7 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer { controller.load }) contents += new MenuItem(Action("Quit") { - System.exit(0) + controller.finish() }) } contents += new Menu("Edit") { @@ -133,6 +138,7 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer { case event: GridSizeChanged => resize(event.newSize) case event: CellChanged => redraw case event: CandidatesChanged => redraw + case event: SudokuShutdown => peer.dispatchEvent(new WindowEvent(peer, WindowEvent.WINDOW_CLOSING)) } def resize(gridSize: Int) = { diff --git a/src/main/scala/de/htwg/se/sudoku/controller/controllerComponent/ControllerInterface.scala b/src/main/scala/de/htwg/se/sudoku/controller/controllerComponent/ControllerInterface.scala index ce7b9e9..d002dcc 100644 --- a/src/main/scala/de/htwg/se/sudoku/controller/controllerComponent/ControllerInterface.scala +++ b/src/main/scala/de/htwg/se/sudoku/controller/controllerComponent/ControllerInterface.scala @@ -73,3 +73,5 @@ class CellChanged extends Event case class GridSizeChanged(newSize: Int) extends Event class CandidatesChanged extends Event + +class SudokuShutdown extends Event diff --git a/src/main/scala/de/htwg/se/sudoku/controller/controllerComponent/controllerBaseImpl/Controller.scala b/src/main/scala/de/htwg/se/sudoku/controller/controllerComponent/controllerBaseImpl/Controller.scala index c93ec19..0dcdb3d 100644 --- a/src/main/scala/de/htwg/se/sudoku/controller/controllerComponent/controllerBaseImpl/Controller.scala +++ b/src/main/scala/de/htwg/se/sudoku/controller/controllerComponent/controllerBaseImpl/Controller.scala @@ -170,5 +170,8 @@ class Controller @Inject()(var grid: GridInterface) grid = grid.setShowCandidates(row, col) } - override def finish(): Unit = fileIo.unbind() + override def finish(): Unit = { + publish(new SudokuShutdown) + fileIo.unbind() + } }