From 1c5593a9a3f5d6da53ff531b9dab3111c65f591e Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Tue, 1 Dec 2020 10:48:18 +0100
Subject: [PATCH] Store zones in profile settings

---
 .../de/tobias/playpad/profile/Profile.java    | 17 ++++++--
 .../plugin/content/ContentPluginMain.scala    | 40 ++++++++++++++-----
 .../content/pad/ContentPlayerPadContent.scala |  9 +++--
 ...ayerPadContentSettingsViewController.scala |  7 ++--
 .../player/ContentPlayerViewController.scala  | 10 +++++
 .../settings/ZoneSettingsViewController.scala | 27 +++++++------
 6 files changed, 77 insertions(+), 33 deletions(-)

diff --git a/PlayWallCore/src/main/java/de/tobias/playpad/profile/Profile.java b/PlayWallCore/src/main/java/de/tobias/playpad/profile/Profile.java
index 4900d036..7e50c4dc 100644
--- a/PlayWallCore/src/main/java/de/tobias/playpad/profile/Profile.java
+++ b/PlayWallCore/src/main/java/de/tobias/playpad/profile/Profile.java
@@ -17,9 +17,7 @@ import org.dom4j.DocumentException;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
+import java.util.*;
 
 public class Profile {
 
@@ -30,11 +28,13 @@ public class Profile {
 	private static Profile currentProfile;
 
 	// Settings
-	private ProfileReference ref;
+	private final ProfileReference ref;
 
 	private ProfileSettings profileSettings;
 	private MappingCollection mappings;
 
+	private Map<String, Object> customSettings;
+
 	/**
 	 * Use {@link ProfileReferenceManager#addProfile(ProfileReference)} instead
 	 *
@@ -44,6 +44,7 @@ public class Profile {
 		this.ref = ref;
 		this.profileSettings = new ProfileSettings();
 		this.mappings = new MappingCollection();
+		this.customSettings = new HashMap<>();
 	}
 
 	public static Mapping createMappingWithDefaultActions() {
@@ -84,6 +85,14 @@ public class Profile {
 		return profileSettings;
 	}
 
+	public Object getCustomSettings(String name) {
+		return customSettings.get(name);
+	}
+
+	public void addCustomSettings(String name, Object settings) {
+		customSettings.put(name, settings);
+	}
+
 	public static Profile load(ProfileReference ref) throws DocumentException, IOException, ProfileNotFoundException {
 		if (ref == null) {
 			throw new IllegalArgumentException("Profile is null");
diff --git a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/ContentPluginMain.scala b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/ContentPluginMain.scala
index f0c2c1de..13ce8dac 100644
--- a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/ContentPluginMain.scala
+++ b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/ContentPluginMain.scala
@@ -6,10 +6,11 @@ import de.thecodelabs.utils.util.Localization
 import de.tobias.playpad.PlayPadPlugin
 import de.tobias.playpad.plugin.content.player.ContentPlayerViewController
 import de.tobias.playpad.plugin.content.settings.{ZoneConfiguration, ZoneSettingsViewController}
-import de.tobias.playpad.plugin.{Module, PlayPadPluginStub}
+import de.tobias.playpad.plugin.{Module, PlayPadPluginStub, SettingsListener}
+import de.tobias.playpad.profile.{Profile, ProfileListener}
 import javafx.application.Platform
 
-class ContentPluginMain extends PlayPadPluginStub {
+class ContentPluginMain extends PlayPadPluginStub with SettingsListener with ProfileListener {
 
 	private var module: Module = _
 
@@ -20,13 +21,10 @@ class ContentPluginMain extends PlayPadPluginStub {
 		Localization.addResourceBundle(localization)
 
 		PlayPadPlugin.getRegistries.getPadContents.loadComponentsFromFile("PadContent.xml", getClass.getClassLoader, module, localization)
-		Platform.runLater(() => {
-			ContentPluginMain.playerViewController = new ContentPlayerViewController
-			ContentPluginMain.playerViewController.configurePlayers(ContentPluginMain.configuration)
-			ContentPluginMain.playerViewController.showStage()
-		})
+		PlayPadPlugin.getInstance().addAdditionalProfileSettingsTab(() => new ZoneSettingsViewController)
 
-		PlayPadPlugin.getInstance().addGlobalSettingsTab(() => new ZoneSettingsViewController)
+		PlayPadPlugin.getInstance().addSettingsListener(this)
+		Profile.registerListener(this)
 	}
 
 	override def shutdown(): Unit = {
@@ -34,9 +32,31 @@ class ContentPluginMain extends PlayPadPluginStub {
 	}
 
 	override def getModule: Module = module
+
+	override def onLoad(profile: Profile): Unit = {
+		val path = profile.getRef.getCustomFilePath("Zones.json")
+		val zoneConfiguration = Storage.load(path, StorageTypes.JSON, classOf[ZoneConfiguration])
+		profile.addCustomSettings(ContentPluginMain.zoneConfigurationKey, zoneConfiguration)
+	}
+
+	override def onSave(profile: Profile): Unit = {
+		val path = profile.getRef.getCustomFilePath("Zones.json")
+		val zoneConfigurationObject = profile.getCustomSettings(ContentPluginMain.zoneConfigurationKey)
+		if (zoneConfigurationObject != null) {
+			Storage.save(path, StorageTypes.JSON, zoneConfigurationObject)
+		}
+	}
+
+	override def reloadSettings(oldProfile: Profile, currentProfile: Profile): Unit = {
+		val zoneConfiguration = currentProfile.getCustomSettings(ContentPluginMain.zoneConfigurationKey).asInstanceOf[ZoneConfiguration]
+		Platform.runLater(() => {
+			ContentPluginMain.playerViewController.configurePlayers(zoneConfiguration)
+		})
+	}
 }
 
 object ContentPluginMain {
-	var playerViewController: ContentPlayerViewController = _
-	lazy val configuration: ZoneConfiguration = Storage.load(StorageTypes.JSON, classOf[ZoneConfiguration])
+	lazy val playerViewController: ContentPlayerViewController = new ContentPlayerViewController
+
+	val zoneConfigurationKey = "ZoneConfiguration"
 }
\ No newline at end of file
diff --git a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/pad/ContentPlayerPadContent.scala b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/pad/ContentPlayerPadContent.scala
index 210e3d5b..389edd18 100644
--- a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/pad/ContentPlayerPadContent.scala
+++ b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/pad/ContentPlayerPadContent.scala
@@ -10,8 +10,9 @@ import de.tobias.playpad.pad.fade.{Fadeable, LinearFadeController}
 import de.tobias.playpad.pad.mediapath.MediaPath
 import de.tobias.playpad.pad.{Pad, PadStatus}
 import de.tobias.playpad.plugin.content.ContentPluginMain
-import de.tobias.playpad.plugin.content.settings.Zone
+import de.tobias.playpad.plugin.content.settings.{Zone, ZoneConfiguration}
 import de.tobias.playpad.plugin.content.util._
+import de.tobias.playpad.profile.Profile
 import de.tobias.playpad.volume.VolumeManager
 import javafx.application.Platform
 import javafx.beans.binding.{Bindings, ObjectBinding}
@@ -292,11 +293,13 @@ class ContentPlayerPadContent(val pad: Pad, val `type`: String) extends PadConte
 	}
 
 	def getSelectedZones: Seq[Zone] = {
+		val zoneConfiguration = Profile.currentProfile().getCustomSettings(ContentPluginMain.zoneConfigurationKey).asInstanceOf[ZoneConfiguration]
+
 		val customSettings = pad.getPadSettings.getCustomSettings
 		val selectedZoneNames = customSettings.getOrDefault(
 			ContentPlayerPadContentFactory.zones,
-			ContentPluginMain.configuration.zones.stream().map(zone => zone.getName).collect(Collectors.toList())
+			zoneConfiguration.zones.stream().map(zone => zone.getName).collect(Collectors.toList())
 		).asInstanceOf[util.List[String]]
-		ContentPluginMain.configuration.zones.asScala.filter(zone => selectedZoneNames.contains(zone.getName)).toSeq
+		zoneConfiguration.zones.asScala.filter(zone => selectedZoneNames.contains(zone.getName)).toSeq
 	}
 }
diff --git a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/pad/ContentPlayerPadContentSettingsViewController.scala b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/pad/ContentPlayerPadContentSettingsViewController.scala
index 08670f34..14d5e0ee 100644
--- a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/pad/ContentPlayerPadContentSettingsViewController.scala
+++ b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/pad/ContentPlayerPadContentSettingsViewController.scala
@@ -5,7 +5,8 @@ import java.util
 import de.thecodelabs.utils.util.Localization
 import de.tobias.playpad.pad.Pad
 import de.tobias.playpad.plugin.content.ContentPluginMain
-import de.tobias.playpad.plugin.content.settings.Zone
+import de.tobias.playpad.plugin.content.settings.{Zone, ZoneConfiguration}
+import de.tobias.playpad.profile.Profile
 import de.tobias.playpad.viewcontroller.PadSettingsTabViewController
 import javafx.beans.binding.Bindings
 import javafx.fxml.FXML
@@ -28,9 +29,9 @@ class ContentPlayerPadContentSettingsViewController(val pad: Pad) extends PadSet
 
 	load("view", "ContentPadSettings", Localization.getBundle)
 
-
 	override def init(): Unit = {
-		zoneListView.getItems.addAll(ContentPluginMain.configuration.zones)
+		val zoneConfiguration = Profile.currentProfile().getCustomSettings(ContentPluginMain.zoneConfigurationKey).asInstanceOf[ZoneConfiguration]
+		zoneListView.getItems.addAll(zoneConfiguration.zones)
 
 		addAllZonesButton.disableProperty().bind(Bindings.equal(Bindings.size(zoneListView.getCheckModel.getCheckedIndices), zoneListView.getItems.size()))
 		removeAllZonesButton.disableProperty().bind(Bindings.isEmpty(zoneListView.getCheckModel.getCheckedIndices))
diff --git a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/player/ContentPlayerViewController.scala b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/player/ContentPlayerViewController.scala
index 4fd71e57..5a71719d 100644
--- a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/player/ContentPlayerViewController.scala
+++ b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/player/ContentPlayerViewController.scala
@@ -56,6 +56,11 @@ class ContentPlayerViewController extends NVC {
 	}
 
 	def configurePlayers(configuration: ZoneConfiguration): Unit = {
+		if (configuration.zones.isEmpty) {
+			closeStage()
+			return
+		}
+
 		val parent = getParent.asInstanceOf[Pane]
 		parent.getChildren.clear()
 
@@ -66,6 +71,8 @@ class ContentPlayerViewController extends NVC {
 			parent.getChildren.add(mediaPlayerStack)
 		})
 
+		showStage()
+
 		getStageContainer.ifPresent(container => {
 			val stage = container.getStage
 
@@ -88,6 +95,9 @@ class ContentPlayerViewController extends NVC {
 		.foreach(mediaStack => mediaStack.removeActivePad(padIndex))
 
 	def highlight(zone: Zone, on: Boolean): Unit = {
+		if (getMediaStack(zone).isEmpty) {
+			return
+		}
 		getMediaStack(zone).head.highlight(on)
 	}
 
diff --git a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/settings/ZoneSettingsViewController.scala b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/settings/ZoneSettingsViewController.scala
index 137a12fa..bae668ff 100644
--- a/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/settings/ZoneSettingsViewController.scala
+++ b/PlayWallPlugins/PlayWallPluginContentPlayer/src/main/scala/de/tobias/playpad/plugin/content/settings/ZoneSettingsViewController.scala
@@ -1,17 +1,17 @@
 package de.tobias.playpad.plugin.content.settings
 
-import de.thecodelabs.storage.settings.{Storage, StorageTypes}
 import de.thecodelabs.utils.ui.scene.input.NumberTextField
 import de.thecodelabs.utils.util.Localization
 import de.tobias.playpad.plugin.content.ContentPluginMain
-import de.tobias.playpad.settings.GlobalSettings
+import de.tobias.playpad.profile.{Profile, ProfileSettings}
+import de.tobias.playpad.project.Project
 import de.tobias.playpad.viewcontroller.main.IMainViewController
-import de.tobias.playpad.viewcontroller.option.{GlobalSettingsTabViewController, IGlobalReloadTask}
+import de.tobias.playpad.viewcontroller.option.{IProfileReloadTask, ProfileSettingsTabViewController}
 import javafx.application.Platform
 import javafx.fxml.FXML
 import javafx.scene.control.{Button, ListCell, ListView, TextField}
 
-class ZoneSettingsViewController extends GlobalSettingsTabViewController with IGlobalReloadTask {
+class ZoneSettingsViewController extends ProfileSettingsTabViewController with IProfileReloadTask {
 
 	@FXML
 	var listView: ListView[Zone] = _
@@ -92,7 +92,7 @@ class ZoneSettingsViewController extends GlobalSettingsTabViewController with IG
 		val newConfiguration = new Zone
 		newConfiguration.setName(Localization.getString("plugin.content.player.settings.default_name"))
 
-		ContentPluginMain.configuration.zones.add(newConfiguration)
+		getZoneConfiguration.zones.add(newConfiguration)
 		listView.getItems.add(newConfiguration)
 	}
 
@@ -101,21 +101,19 @@ class ZoneSettingsViewController extends GlobalSettingsTabViewController with IG
 		val selectedItem = listView.getSelectionModel.getSelectedItem
 		if (selectedItem != null) {
 			listView.getItems.remove(selectedItem)
-			ContentPluginMain.configuration.zones.remove(selectedItem)
+			getZoneConfiguration.zones.remove(selectedItem)
 		}
 	}
 
-	override def loadSettings(settings: GlobalSettings): Unit = {
-		listView.getItems.setAll(ContentPluginMain.configuration.zones)
+	override def loadSettings(settings: Profile): Unit = {
+		listView.getItems.setAll(getZoneConfiguration.zones)
 	}
 
-	override def saveSettings(settings: GlobalSettings): Unit = {
+	override def saveSettings(settings: Profile): Unit = {
 		val selectedItem = listView.getSelectionModel.getSelectedItem
 		if (selectedItem != null) {
 			saveSettingsToZone(selectedItem)
 		}
-
-		Storage.save(StorageTypes.JSON, ContentPluginMain.configuration)
 	}
 
 	override def needReload(): Boolean = {
@@ -128,6 +126,9 @@ class ZoneSettingsViewController extends GlobalSettingsTabViewController with IG
 
 	override def name(): String = Localization.getString("plugin.content.player.settings")
 
-	override def getTask(settings: GlobalSettings, controller: IMainViewController): Runnable = () =>
-		Platform.runLater(() => ContentPluginMain.playerViewController.configurePlayers(ContentPluginMain.configuration))
+
+	override def getTask(settings: ProfileSettings, project: Project, controller: IMainViewController): Runnable = () =>
+		Platform.runLater(() => ContentPluginMain.playerViewController.configurePlayers(getZoneConfiguration))
+
+	private def getZoneConfiguration: ZoneConfiguration = Profile.currentProfile().getCustomSettings(ContentPluginMain.zoneConfigurationKey).asInstanceOf[ZoneConfiguration]
 }
-- 
GitLab