package skeb.glue

import scala.gui._
import scala.gui.widget._

/** A GUI window containing a spreasheet widget. The implementat is
  * not relevant for this tutorial and is not commented. */
abstract class Spreadsheet(width: Int, height: Int)
         extends gui.container.Window {
  def sheet: Sheet
  val matrixModel = new model.ImplementedMatrix(height, width) {
    override def columnName(column: Int) = column.toString }
  val matrix = new widget.FreeRenderMatrix(matrixModel) {
    override def renderValueForCell(col: int, row: Int): String =
      sheet(col, row).value.humanString }
  lay { matrix }
  subscribe(this, matrix)
  case class CellUpdated(cell: skeb.Cell) extends Event
  toplevel eventloop {
    case this.Closing() => System.exit(0)
    case matrix.CellUpdated(row, col) =>
      sheet(col, row).userSets(matrixModel.cell(row, col).value match {
        case None => None
        case Some("") => None
        case Some(v) => Some(v.toString) })}
}
