diff --git a/src/main/scala/ocelot/desktop/OcelotDesktop.scala b/src/main/scala/ocelot/desktop/OcelotDesktop.scala index 48a1c2e..d202071 100644 --- a/src/main/scala/ocelot/desktop/OcelotDesktop.scala +++ b/src/main/scala/ocelot/desktop/OcelotDesktop.scala @@ -3,7 +3,7 @@ package ocelot.desktop import li.flor.nativejfilechooser.NativeJFileChooser import ocelot.desktop.audio.{Audio, SoundSource} import ocelot.desktop.ui.UiHandler -import ocelot.desktop.ui.widget.{ExitConfirmationDialog, RootWidget, SettingsDialog} +import ocelot.desktop.ui.widget.{NotificationDialog, ExitConfirmationDialog, RootWidget, SettingsDialog} import ocelot.desktop.util.FileUtils.getOcelotConfigDirectory import ocelot.desktop.util._ import org.apache.commons.io.FileUtils @@ -15,7 +15,7 @@ import totoro.ocelot.brain.nbt.{CompressedStreamTools, NBTTagCompound} import totoro.ocelot.brain.workspace.Workspace import java.io._ -import java.nio.file.{Files, Path} +import java.nio.file.{Files, Path, Paths} import java.util.concurrent.locks.{Lock, ReentrantLock} import javax.swing.JFileChooser import scala.io.Source @@ -54,7 +54,14 @@ object OcelotDesktop extends Logging { setupEventHandlers() - if (loadRecentWorkspace) load(Settings.get.recentWorkspace.map(new File(_))) + if (loadRecentWorkspace) { + if (!load(Settings.get.recentWorkspace.map(new File(_)))) { + UiHandler.root.modalDialogPool.pushDialog( + new NotificationDialog("Could not find the recent workspace...\nI will create a default one")) + root.workspaceView.createDefaultWorkspace() + Settings.get.recentWorkspace = None + } + } new Thread(() => { while (true) { @@ -178,16 +185,20 @@ object OcelotDesktop extends Logging { chooseDirectory(load, JFileChooser.OPEN_DIALOG) } - def load(dir: Option[File]): Unit = { + def load(dir: Option[File]): Boolean = { if (dir.isDefined) { - val path = dir.get + "/workspace.nbt" - val reader = new DataInputStream(new FileInputStream(path)) - val nbt = CompressedStreamTools.readCompressed(reader) - savePath = Some(dir.get.toPath) - Settings.get.recentWorkspace = dir.map(_.getCanonicalPath) - workspace.path = dir.get.toPath - loadWorld(nbt) + val path = Paths.get(dir.get.getCanonicalPath, "workspace.nbt") + if (Files.exists(path)) { + val reader = new DataInputStream(Files.newInputStream(path)) + val nbt = CompressedStreamTools.readCompressed(reader) + savePath = Some(dir.get.toPath) + Settings.get.recentWorkspace = dir.map(_.getCanonicalPath) + workspace.path = dir.get.toPath + loadWorld(nbt) + return true + } } + false } def chooseDirectory(fun: Option[File] => Unit, dialogType: Int): Unit = { diff --git a/src/main/scala/ocelot/desktop/Settings.scala b/src/main/scala/ocelot/desktop/Settings.scala index 9ac4cd0..63253c6 100644 --- a/src/main/scala/ocelot/desktop/Settings.scala +++ b/src/main/scala/ocelot/desktop/Settings.scala @@ -49,10 +49,8 @@ object Settings extends Logging { config.withValue(path, ConfigValueFactory.fromIterable(util.Arrays.asList(value.x, value.y))) } - def withValue(path: String, value: Option[Any]): Config = value match { - case Some(data) => config.withValue(path, ConfigValueFactory.fromAnyRef(data)) - case None => config - } + def withValue(path: String, value: Option[Any]): Config = + config.withValue(path, ConfigValueFactory.fromAnyRef(value.orNull)) } class Int2D(var x: Int, var y: Int) { diff --git a/src/main/scala/ocelot/desktop/ui/widget/NotificationDialog.scala b/src/main/scala/ocelot/desktop/ui/widget/NotificationDialog.scala new file mode 100644 index 0000000..5beaabc --- /dev/null +++ b/src/main/scala/ocelot/desktop/ui/widget/NotificationDialog.scala @@ -0,0 +1,29 @@ +package ocelot.desktop.ui.widget + +import ocelot.desktop.geometry.Padding2D +import ocelot.desktop.ui.layout.LinearLayout +import ocelot.desktop.ui.widget.modal.ModalDialog +import ocelot.desktop.util.Orientation + +class NotificationDialog(message: String) extends ModalDialog { + children :+= new PaddingBox(new Widget { + override val layout = new LinearLayout(this, orientation = Orientation.Vertical) + + children :++= message.split('\n').map(line => new PaddingBox(new Label { + override def text: String = line + }, Padding2D(bottom = 8))) + + children :+= new Widget { + children :+= new Filler + + children :+= new PaddingBox(new Button { + override def text: String = "Ok" + override def onClick(): Unit = close() + }, Padding2D(right = 8, top = 8)) + } + }, Padding2D.equal(16)) + + def onSaveSelected(): Unit = {} + + def onExitSelected(): Unit = {} +}