Skip to content

Commit

Permalink
Added akka actors to create UIs
Browse files Browse the repository at this point in the history
Co-authored-by: DonatJR <[email protected]>
  • Loading branch information
SailReal and DonatJR committed Nov 16, 2018
1 parent 7b57f14 commit 0ccda39
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 26 deletions.
28 changes: 21 additions & 7 deletions src/main/scala/de/htwg/se/sudoku/Sudoku.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,36 @@ package de.htwg.se.sudoku

import java.awt.GraphicsEnvironment

import akka.actor.{ActorSystem, Props}
import akka.pattern.ask
import akka.util.Timeout
import com.google.inject.{Guice, Injector}
import de.htwg.se.sudoku.aview.gui.SwingGui
import de.htwg.se.sudoku.aview.{HttpServer, Tui}
import de.htwg.se.sudoku.aview.UiMessage.{Crash, CreateGui, CreateTui}
import de.htwg.se.sudoku.aview.{HttpServer, Tui, UiFactory}
import de.htwg.se.sudoku.controller.controllerComponent.ControllerInterface
import de.htwg.se.sudoku.model.fileIoComponent.FileIOInterface
import de.htwg.se.sudoku.model.fileIoComponent.fileIoMicroImpl.FileIoHttpServer

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.io.StdIn.readLine

object Sudoku {
implicit val timeout: Timeout = Timeout(29.seconds) // used for akka ask pattern

val injector: Injector = Guice.createInjector(new MicroSudokuModule)

implicit val actorSystem = ActorSystem("actorSystem")
val uiFactory = actorSystem.actorOf(Props[UiFactory])

uiFactory ! Crash // demonstrate error / restart handling
val controller: ControllerInterface = injector.getInstance(classOf[ControllerInterface])
val tui = new Tui(controller)

val tuiFuture = uiFactory ? CreateTui(controller)
val tui = Await.result(tuiFuture.mapTo[Tui], Duration.Inf)

if (!GraphicsEnvironment.isHeadless) {
val gui = new SwingGui(controller)
uiFactory ! CreateGui(controller)
}

val fileIoHttpServer: FileIoHttpServer = injector.getInstance(classOf[FileIoHttpServer])
val webserver = new HttpServer(controller)

Expand All @@ -29,6 +41,8 @@ object Sudoku {
var input: String = ""

do {
Thread.sleep(200)

if (Console.in.ready()) {
input = readLine()
tui.processInputLine(input)
Expand Down
17 changes: 17 additions & 0 deletions src/main/scala/de/htwg/se/sudoku/aview/UiFactory.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.htwg.se.sudoku.aview

import akka.actor.Actor
import de.htwg.se.sudoku.aview.UiMessage.{Crash, CreateGui, CreateTui}
import de.htwg.se.sudoku.aview.gui.SwingGui

case class UiFactory() extends Actor {

override def receive: Receive = {

case CreateGui(controller) => new SwingGui(controller)
case CreateTui(controller) =>
val tui = new Tui(controller)
sender ! tui
case Crash => throw new NullPointerException() // actor gets restarted by default
}
}
9 changes: 9 additions & 0 deletions src/main/scala/de/htwg/se/sudoku/aview/UiMessage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.htwg.se.sudoku.aview

import de.htwg.se.sudoku.controller.controllerComponent.ControllerInterface

object UiMessage {
case class CreateGui(controller: ControllerInterface)
case class CreateTui(controller: ControllerInterface)
case class Crash()
}
66 changes: 47 additions & 19 deletions src/main/scala/de/htwg/se/sudoku/aview/gui/SwingGui.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package de.htwg.se.sudoku.aview.gui

import de.htwg.se.sudoku.controller.controllerComponent.{ CandidatesChanged, CellChanged, ControllerInterface, GridSizeChanged }

import de.htwg.se.sudoku.controller.controllerComponent.{CandidatesChanged, CellChanged, ControllerInterface, GridSizeChanged}
import de.htwg.se.sudoku.util.Observer

import scala.swing._
import scala.swing.Swing.LineBorder
import scala.swing._
import scala.swing.event._

class CellClicked(val row: Int, val column: Int) extends Event

class SwingGui(controller: ControllerInterface) extends Frame with Observer{
class SwingGui(controller: ControllerInterface) extends Frame with Observer {

listenTo(controller)

title = "HTWG Sudoku"
var cells = Array.ofDim[CellPanel](controller.gridSize, controller.gridSize)

override def closeOperation(): Unit = System.exit(0)

def highlightpanel = new FlowPanel {
contents += new Label("Highlight:")
for {index <- 0 to controller.gridSize} {
Expand Down Expand Up @@ -57,6 +57,7 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer{
}
}
}

val statusline = new TextField(controller.statusText, 20)

contents = new BorderPanel {
Expand All @@ -68,33 +69,59 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer{
menuBar = new MenuBar {
contents += new Menu("File") {
mnemonic = Key.F
contents += new MenuItem(Action("Empty") { controller.createEmptyGrid })
contents += new MenuItem(Action("New") { controller.createNewGrid })
contents += new MenuItem(Action("Save") {controller.save})
contents += new MenuItem(Action("Load") {controller.load})
contents += new MenuItem(Action("Quit") { System.exit(0) })
contents += new MenuItem(Action("Empty") {
controller.createEmptyGrid
})
contents += new MenuItem(Action("New") {
controller.createNewGrid
})
contents += new MenuItem(Action("Save") {
controller.save
})
contents += new MenuItem(Action("Load") {
controller.load
})
contents += new MenuItem(Action("Quit") {
System.exit(0)
})
}
contents += new Menu("Edit") {
mnemonic = Key.E
contents += new MenuItem(Action("Undo") { controller.undo })
contents += new MenuItem(Action("Redo") { controller.redo })
contents += new MenuItem(Action("Undo") {
controller.undo
})
contents += new MenuItem(Action("Redo") {
controller.redo
})
}
contents += new Menu("Solve") {
mnemonic = Key.S
contents += new MenuItem(Action("Solve") { controller.solve })
contents += new MenuItem(Action("Solve") {
controller.solve
})
}
contents += new Menu("Highlight") {
mnemonic = Key.H
for { index <- 0 to controller.gridSize } {
contents += new MenuItem(Action(index.toString) { controller.highlight(index) })
for {index <- 0 to controller.gridSize} {
contents += new MenuItem(Action(index.toString) {
controller.highlight(index)
})
}
}
contents += new Menu("Options") {
mnemonic = Key.O
contents += new MenuItem(Action("Show all candidates") { controller.toggleShowAllCandidates })
contents += new MenuItem(Action("Size 1*1") { controller.resize(1) })
contents += new MenuItem(Action("Size 4*4") { controller.resize(4) })
contents += new MenuItem(Action("Size 9*9") { controller.resize(9) })
contents += new MenuItem(Action("Show all candidates") {
controller.toggleShowAllCandidates
})
contents += new MenuItem(Action("Size 1*1") {
controller.resize(1)
})
contents += new MenuItem(Action("Size 4*4") {
controller.resize(4)
})
contents += new MenuItem(Action("Size 9*9") {
controller.resize(9)
})

}
}
Expand All @@ -104,7 +131,7 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer{

reactions += {
case event: GridSizeChanged => resize(event.newSize)
case event: CellChanged => redraw
case event: CellChanged => redraw
case event: CandidatesChanged => redraw
}

Expand All @@ -116,6 +143,7 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer{
add(statusline, BorderPanel.Position.South)
}
}

def redraw = {
for {
row <- 0 until controller.gridSize
Expand Down

0 comments on commit 0ccda39

Please sign in to comment.