/
ReconfigureWorkspaceUI.scala
43 lines (34 loc) · 1.51 KB
/
ReconfigureWorkspaceUI.scala
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
// (C) Uri Wilensky. https://github.com/NetLogo/NetLogo
package org.nlogo.window
import java.awt.Container
import java.net.URI
import java.nio.file.{ Files, Paths }
import org.nlogo.window.Events.{ AfterLoadEvent, BeforeLoadEvent,
LoadBeginEvent, LoadEndEvent, LoadModelEvent, LoadWidgetsEvent }
import org.nlogo.api.{ CompilerServices, ModelType }
import org.nlogo.core.Model
import scala.util.Try
object ReconfigureWorkspaceUI {
def apply(linkParent: Container, uri: URI, modelType: ModelType, model: Model,
compilerServices: CompilerServices): Unit = {
new Loader(linkParent).loadHelper(uri, modelType, model, compilerServices)
}
private case class Loader(linkParent: Container) extends org.nlogo.window.Event.LinkChild {
def getLinkParent = linkParent
def loadHelper(modelURI: URI, modelType: ModelType, model: Model, compilerServices: CompilerServices) {
val uriOption = Try(Paths.get(modelURI)).toOption
.filterNot(p => p.getFileName.toString.startsWith("empty.nlogo"))
.filter(p => Files.isRegularFile(p))
.map(_.toString)
val beforeEvents = List(
new BeforeLoadEvent(uriOption, modelType),
new LoadBeginEvent())
val loadSectionEvents = List(
new LoadModelEvent(model),
new LoadWidgetsEvent(model.widgets))
val afterEvents = List(new LoadEndEvent(), new AfterLoadEvent())
// fire missles! (actually, just fire the events...)
for (e <- beforeEvents ::: loadSectionEvents ::: afterEvents) e.raise(this)
}
}
}