@@ -66,115 +66,109 @@ class LevelSpaceMenu(tabManager: TabManager, val backingModelManager: ModelManag
6666 }
6767}
6868
69- object LevelSpaceMenu {
70- abstract class NewTabAction (name : String , modelManager : ModelManager ) extends AbstractAction (name) {
71- val tabManager = App .app.tabManager
69+ object LevelSpaceMenu {
70+ abstract class NewTabAction (name : String , modelManager : ModelManager ) extends AbstractAction (name) {
71+ val tabManager = App .app.tabManager
7272
73- def filePath : Option [String ]
73+ def filePath : Option [String ]
7474
75- def actingTab : Option [CodeTab ] =
76- filePath.flatMap(path => locateExistingTab(path) orElse createNewTab(path))
75+ def actingTab : Option [CodeTab ] =
76+ filePath.flatMap(path => locateExistingTab(path) orElse createNewTab(path))
7777
78- private def locateExistingTab (path : String ): Option [CodeTab ] =
79- modelManager.existingTab(path)
78+ private def locateExistingTab (path : String ): Option [CodeTab ] =
79+ modelManager.existingTab(path)
8080
81- private def createNewTab (path : String ): Option [CodeTab ] = {
82- modelManager.registerTab(path) { workspace =>
83- val tab = new ModelCodeTab (workspace, tabManager, modelManager)
84- tabManager.addTab(tab, tab.tabName)
85- tab
86- }
81+ private def createNewTab (path : String ): Option [CodeTab ] = {
82+ modelManager.registerTab(path) { workspace =>
83+ val tab = new ModelCodeTab (workspace, tabManager, modelManager)
84+ tabManager.addTab(tab, tab.tabName)
85+ tab
8786 }
88-
89- override def actionPerformed (actionEvent : ActionEvent ): Unit =
90- actingTab.foreach(tabManager.setSelectedTab)
9187 }
9288
93- class OpenModelAction (fileName : String , modelManager : ModelManager )
94- extends NewTabAction (fileName, modelManager) {
95- override def filePath : Option [String ] = Some (fileName)
96- }
89+ override def actionPerformed (actionEvent : ActionEvent ): Unit =
90+ actingTab.foreach(tabManager.setSelectedTab)
91+ }
9792
98- class SelectModelAction (name : String , modelManager : ModelManager )
99- extends NewTabAction (name, modelManager) {
100-
101- override def filePath : Option [String ] = selectFile
102-
103- override def actingTab : Option [CodeTab ] =
104- try {
105- super .actingTab
106- } catch {
107- case e : CompilerException =>
108- // we shouldn't have to raise an exception here, we should just be able to open it, but
109- // in order to do that, we'll need to change child models not to compile in their constructors
110- throw new ExtensionException (s " $filePath did not compile properly. There is probably something wrong " +
111- s " with its code. Exception said ${e.getMessage}" )
112- case e : IOException =>
113- throw new ExtensionException (s " There was no model file at the path: \" $filePath\" " )
114- }
93+ class OpenModelAction (fileName : String , modelManager : ModelManager ) extends NewTabAction (fileName, modelManager) {
94+ override def filePath : Option [String ] = Some (fileName)
95+ }
96+
97+ class SelectModelAction (name : String , modelManager : ModelManager ) extends NewTabAction (name, modelManager) {
98+ override def filePath : Option [String ] = selectFile
99+
100+ override def actingTab : Option [CodeTab ] =
101+ try {
102+ super .actingTab
103+ } catch {
104+ case e : CompilerException =>
105+ // we shouldn't have to raise an exception here, we should just be able to open it, but
106+ // in order to do that, we'll need to change child models not to compile in their constructors
107+ throw new ExtensionException (s " $filePath did not compile properly. There is probably something wrong " +
108+ s " with its code. Exception said ${e.getMessage}" )
109+ case e : IOException =>
110+ throw new ExtensionException (s " There was no model file at the path: \" $filePath\" " )
111+ }
112+
113+ private def selectFile : Option [String ] =
114+ showLoadSelection.flatMap(path =>
115+ if (ModelsLibrary .getModelPaths.contains(path)) {
116+ showLibraryModelErrorMessage()
117+ None
118+ } else
119+ Some (path))
120+
121+ private def showLoadSelection : Option [String ] =
122+ try {
123+ Some (FileDialog .showFiles(App .app.frame, " Load a LevelSpace Model..." , LOADFILE ))
124+ } catch {
125+ case e : UserCancelException =>
126+ Exceptions .ignore(e)
127+ None
128+ }
115129
130+ private def showLibraryModelErrorMessage (): Unit =
131+ JOptionPane .showMessageDialog(
132+ App .app.frame,
133+ """ |The model you selected is a library model, which cannot be opened in a LevelSpace code tab.
134+ |Please save the model elsewhere and try re-opening""" .stripMargin)
135+ }
116136
117- private def selectFile : Option [String ] =
118- showLoadSelection.flatMap(path =>
119- if (ModelsLibrary .getModelPaths.contains(path)) {
120- showLibraryModelErrorMessage()
121- None
122- } else
123- Some (path))
137+ class NewModelAction (name : String , modelManager : ModelManager ) extends NewTabAction (name, modelManager) {
138+ override def filePath : Option [String ] = {
139+ FileDialog .setDirectory(App .app.workspace.getModelDir)
124140
125- private def showLoadSelection : Option [String ] =
141+ val ws = App .app.workspace
142+ val loader = FileFormat .basicLoader
143+ val controller = new SaveModel .Controller {
144+ def chooseFilePath (modelType : org.nlogo.api.ModelType ): Option [java.net.URI ] = {
126145 try {
127- Some (FileDialog .showFiles(App .app.frame, " Load a LevelSpace Model..." , LOADFILE ))
146+ val userEntry = FileDialog .showFiles(App .app.frame, " Select a path for new Model..." , SAVEFILE )
147+ // we basically need to write an empty NetLogo model in before we read...
148+ val fileName =
149+ if (userEntry.endsWith(" .nlogo" ) || userEntry.endsWith(" .nlogox" )) userEntry else userEntry + " .nlogox"
150+ val path = Paths .get(fileName)
151+ if (Files .exists(path)) {
152+ val fileAlreadyExists = s " The file $fileName already exists. Please choose a different name "
153+ throw new ExtensionException (fileAlreadyExists)
154+ }
155+ Some (path.toUri)
128156 } catch {
129157 case e : UserCancelException =>
130158 Exceptions .ignore(e)
131159 None
132160 }
133-
134- private def showLibraryModelErrorMessage (): Unit =
135- JOptionPane .showMessageDialog(
136- App .app.frame,
137- """ |The model you selected is a library model, which cannot be opened in a LevelSpace code tab.
138- |Please save the model elsewhere and try re-opening""" .stripMargin)
139- }
140-
141- class NewModelAction (name : String , modelManager : ModelManager )
142- extends NewTabAction (name, modelManager) {
143-
144- override def filePath : Option [String ] = {
145- FileDialog .setDirectory(App .app.workspace.getModelDir)
146-
147- val ws = App .app.workspace
148- val loader = FileFormat .basicLoader
149- val controller = new SaveModel .Controller {
150- def chooseFilePath (modelType : org.nlogo.api.ModelType ): Option [java.net.URI ] = {
151- try {
152- val userEntry = FileDialog .showFiles(App .app.frame, " Select a path for new Model..." , SAVEFILE )
153- // we basically need to write an empty NetLogo model in before we read...
154- val fileName =
155- if (userEntry.endsWith(" .nlogo" ) || userEntry.endsWith(" .nlogox" )) userEntry else userEntry + " .nlogox"
156- val path = Paths .get(fileName)
157- if (Files .exists(path)) {
158- val fileAlreadyExists = s " The file $fileName already exists. Please choose a different name "
159- throw new ExtensionException (fileAlreadyExists)
160- }
161- Some (path.toUri)
162- } catch {
163- case e : UserCancelException =>
164- Exceptions .ignore(e)
165- None
166- }
167- }
168- def shouldSaveModelOfDifferingVersion (version : String ): Boolean = true
169- def warnInvalidFileFormat (format : String ): Unit = {
170- // we force users to save in NetLogo, so this doesn't happen
171- }
172161 }
173- val modelTracker = new ModelTracker {
174- def compiler = App .app.workspace.compiler
175- def getExtensionManager () = App .app.workspace.getExtensionManager
162+ def shouldSaveModelOfDifferingVersion ( version : String ) : Boolean = true
163+ def warnInvalidFileFormat ( format : String ) : Unit = {
164+ // we force users to save in NetLogo, so this doesn't happen
176165 }
177- SaveModel (org.nlogo.core.Model (), loader, controller, modelTracker, Version ).flatMap(_.apply().toOption.map(uri => Paths .get(uri).toString))
178166 }
167+ val modelTracker = new ModelTracker {
168+ def compiler = App .app.workspace.compiler
169+ def getExtensionManager () = App .app.workspace.getExtensionManager
170+ }
171+ SaveModel (org.nlogo.core.Model (), loader, controller, modelTracker, Version ).flatMap(_.apply().toOption.map(uri => Paths .get(uri).toString))
179172 }
180173 }
174+ }
0 commit comments