This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

ModelComponent.scala 1.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
package de.bbisping.coupledsim.tool.control

import scala.collection.mutable.ListBuffer
import de.bbisping.coupledsim.tool.arch.Action
import de.bbisping.coupledsim.tool.arch.Control
import de.bbisping.coupledsim.tool.arch.Undoable


trait ModelComponent {
  val main: Control
  
  val actionLog = ListBuffer[Action]()
  var undoDepth = 0
  
  def implementAction(a: Action) {
    if (a.implement(this) && a.isInstanceOf[Undoable]) {
      if (undoDepth > 0) {
        // delete detached potential redos
        actionLog.remove(0, undoDepth)
        undoDepth = 0
      }
      actionLog prepend a
    }
  }
  
  def undoLastAction(): Boolean = {
    if (actionLog.length > undoDepth) {
      val lastAction = actionLog(undoDepth)
      lastAction match {
        case ua: Undoable =>
          println("Undo" + ua)
          undoDepth += 1
          return ua.undo()
      }
    }
    false
  }
  
  def redoAction(): Boolean = {
    if (undoDepth > 0) {
      val undineAction = actionLog(undoDepth-1)
      undineAction match {
        case ua: Undoable =>
          println("Redo" + ua)
          undoDepth -= 1
          return ua.redo()
      }
    }
    false
  }
  
  def clearUndoLog() {
    actionLog.clear()
    undoDepth = 0
  }
  
  def notify(change: ModelComponent.Change)
  
  final def broadcast = main.broadcastChange(_)
  
  main.registerModelComponent(this)
}

object ModelComponent {
  trait Change
}