From fe16f85d82457330cc9d7965e4bc1332152b4c15 Mon Sep 17 00:00:00 2001 From: Dorving Date: Mon, 24 Oct 2022 17:55:58 +0200 Subject: [PATCH] Cleanups, added kotlin support, started with auto login support --- G-Earth/build.gradle.kts | 15 +++- .../java/gearth/extensions/ExtensionForm.java | 7 +- .../extensions/GEarthExtension.java | 9 ++ .../network/NetworkExtension.java | 6 +- .../network/NetworkExtensionsProducer.java | 3 + .../extensionstore/GExtensionStore.java | 3 +- .../GExtensionStoreController.java | 27 +++--- .../entities/StoreExtensionItem.java | 5 +- .../entities/categories/CategoryItem.java | 3 +- .../QueriedExtensionOverview.java | 4 +- .../entities/search/SearchOverview.java | 3 +- .../repository/StoreRepository.java | 9 +- .../connection/ConnectionController.java | 13 +++ .../src/main/kotlin/gearth/Configuration.kt | 25 ++++++ G-Earth/src/main/kotlin/gearth/Properties.kt | 14 +++ .../main/kotlin/gearth/PropertiesManager.kt | 90 +++++++++++++++++++ .../ui/subforms/connection/Connection.fxml | 75 +++++++++++++--- 17 files changed, 264 insertions(+), 47 deletions(-) create mode 100644 G-Earth/src/main/kotlin/gearth/Configuration.kt create mode 100644 G-Earth/src/main/kotlin/gearth/Properties.kt create mode 100644 G-Earth/src/main/kotlin/gearth/PropertiesManager.kt diff --git a/G-Earth/build.gradle.kts b/G-Earth/build.gradle.kts index 6cc5f23..f09e9e5 100644 --- a/G-Earth/build.gradle.kts +++ b/G-Earth/build.gradle.kts @@ -1,9 +1,12 @@ import org.gradle.internal.os.OperatingSystem plugins { + kotlin("jvm") version "1.6.10" + kotlin("plugin.serialization") version "1.6.10" id("org.beryx.runtime") version "1.12.5" id("org.openjfx.javafxplugin") version "0.0.11" `java-library` + `maven-publish` } description = "G-Earth" @@ -32,6 +35,7 @@ dependencies { implementation("org.json:json:20190722") implementation("org.jsoup:jsoup:1.14.2") implementation("org.slf4j:slf4j-jdk14:2.0.0-alpha0") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3") } java { @@ -56,7 +60,16 @@ application { mainClass.set("gearth.GEarthLauncher") applicationName = "G-Earth" } - +publishing { + publications { + create("maven") { + groupId = "dorving" + artifactId = "gearth" + version = "1.5.3" + from(components["java"]) + } + } +} runtime { addModules( "java.datatransfer", "java.desktop", "java.prefs", diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java index 6922c93..a33b2fe 100644 --- a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java @@ -2,15 +2,12 @@ package gearth.extensions; import gearth.misc.HostInfo; import gearth.misc.listenerpattern.Observable; +import gearth.protocol.HMessage; +import gearth.protocol.HPacket; import gearth.services.packet_info.PacketInfoManager; import javafx.application.HostServices; import javafx.application.Platform; -import javafx.beans.InvalidationListener; import javafx.stage.Stage; -import gearth.protocol.HMessage; -import gearth.protocol.HPacket; - -import java.util.function.Consumer; /** * Created by Jonas on 22/09/18. diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/GEarthExtension.java b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/GEarthExtension.java index 7a18161..77484fa 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/GEarthExtension.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/GEarthExtension.java @@ -1,8 +1,10 @@ package gearth.services.extension_handler.extensions; +import gearth.extensions.InternalExtensionBuilder; import gearth.misc.HostInfo; import gearth.misc.listenerpattern.Observable; import gearth.misc.listenerpattern.SynchronizedObservable; +import gearth.services.extension_handler.extensions.implementations.network.NetworkExtension; import gearth.services.packet_info.PacketInfoManager; import gearth.protocol.HMessage; import gearth.protocol.HPacket; @@ -11,6 +13,13 @@ import gearth.services.extension_handler.extensions.listeners.OmRemoveClickListe import gearth.services.extension_handler.extensions.listeners.OnClickListener; import gearth.services.extension_handler.extensions.listeners.OnDeleteListener; +/** + * Represents a type of extension created with one of the extension APIs. + * + * @see NetworkExtension extensions connecting to G-Earth from a different process. + * @see InternalExtensionBuilder internal extensions (JAR files) that follow + * the same communication protocol as {@link NetworkExtension network extensions}. + */ public abstract class GEarthExtension { diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtension.java b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtension.java index 8b55c95..48d89dc 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtension.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtension.java @@ -76,13 +76,15 @@ public class NetworkExtension extends GEarthExtension { byte side = message.readByte(); int byteLength = message.readInteger(); byte[] packetAsByteArray = message.readBytes(byteLength); - HPacket packet = new HPacket(packetAsByteArray); if (!packet.isCorrupted()) { + log("Forwarding incoming packet (packet="+packet+")"); sendMessage( side == 0 ? HMessage.Direction.TOCLIENT : HMessage.Direction.TOSERVER, packet ); + } else { + log("Received corrupted packet (packet="+packet+")"); } } else if (message.headerId() == NetworkExtensionInfo.INCOMING_MESSAGES_IDS.MANIPULATEDPACKET) { @@ -255,4 +257,4 @@ public class NetworkExtension extends GEarthExtension { public ExtensionType extensionType() { return ExtensionType.EXTERNAL; } -} \ No newline at end of file +} diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionsProducer.java b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionsProducer.java index f583d30..3a9f34f 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionsProducer.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionsProducer.java @@ -1,5 +1,7 @@ package gearth.services.extension_handler.extensions.implementations.network; +import gearth.Configuration; +import gearth.ConfigurationKt; import gearth.protocol.HPacket; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver; @@ -31,6 +33,7 @@ public class NetworkExtensionsProducer implements ExtensionProducer { port++; } + final Configuration configuration = ConfigurationKt.configuration(port); new Thread(() -> { try { diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/GExtensionStore.java b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/GExtensionStore.java index 1060092..0eea350 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/GExtensionStore.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/GExtensionStore.java @@ -3,7 +3,6 @@ package gearth.services.internal_extensions.extensionstore; import gearth.extensions.ExtensionForm; import gearth.extensions.ExtensionInfo; import gearth.services.internal_extensions.extensionstore.application.GExtensionStoreController; -import gearth.services.internal_extensions.extensionstore.application.entities.queriedoverviews.ByDateOverview; import gearth.services.internal_extensions.extensionstore.repository.StoreFetch; import gearth.services.internal_extensions.extensionstore.repository.StoreRepository; import javafx.application.HostServices; @@ -16,7 +15,7 @@ import javafx.application.HostServices; ) public class GExtensionStore extends ExtensionForm { - public static final int PAGESIZE = 20; + public static final int MAX_PAGES = 20; private StoreRepository repository = null; private GExtensionStoreController extensionStoreController = null; diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/GExtensionStoreController.java b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/GExtensionStoreController.java index f01de68..16f12da 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/GExtensionStoreController.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/GExtensionStoreController.java @@ -5,7 +5,6 @@ import gearth.services.internal_extensions.extensionstore.application.entities.C import gearth.services.internal_extensions.extensionstore.application.entities.HOverview; import gearth.services.internal_extensions.extensionstore.application.entities.categories.CategoryOverview; import gearth.services.internal_extensions.extensionstore.application.entities.installed.InstalledOverview; -import gearth.services.internal_extensions.extensionstore.application.entities.queriedoverviews.ByDateOverview; import gearth.services.internal_extensions.extensionstore.application.entities.queriedoverviews.ByRatingOverview; import gearth.services.internal_extensions.extensionstore.application.entities.queriedoverviews.ByUpdateOverview; import gearth.services.internal_extensions.extensionstore.application.entities.search.SearchOverview; @@ -55,10 +54,10 @@ public class GExtensionStoreController implements Initializable { Element seach_link = webView.getEngine().getDocument().getElementById("search_page"); Map> hOverviewSupplier = new HashMap<>(); - hOverviewSupplier.put(by_update_link, () -> new ByUpdateOverview(null, 0, GExtensionStore.PAGESIZE, getStoreRepository())); - hOverviewSupplier.put(by_rating_link, () -> new ByRatingOverview(null, 0, GExtensionStore.PAGESIZE, getStoreRepository())); - hOverviewSupplier.put(by_category_link, () -> new CategoryOverview(null, 0, GExtensionStore.PAGESIZE, getStoreRepository())); - hOverviewSupplier.put(installed_link, () -> new InstalledOverview(null, 0, GExtensionStore.PAGESIZE, getStoreRepository())); + hOverviewSupplier.put(by_update_link, () -> new ByUpdateOverview(null, 0, GExtensionStore.MAX_PAGES, getStoreRepository())); + hOverviewSupplier.put(by_rating_link, () -> new ByRatingOverview(null, 0, GExtensionStore.MAX_PAGES, getStoreRepository())); + hOverviewSupplier.put(by_category_link, () -> new CategoryOverview(null, 0, GExtensionStore.MAX_PAGES, getStoreRepository())); + hOverviewSupplier.put(installed_link, () -> new InstalledOverview(null, 0, GExtensionStore.MAX_PAGES, getStoreRepository())); hOverviewSupplier.put(seach_link, () -> new SearchOverview(null, getStoreRepository())); Arrays.asList(by_update_link, by_rating_link, by_category_link, installed_link, seach_link).forEach(l -> @@ -74,18 +73,18 @@ public class GExtensionStoreController implements Initializable { Map> newStartIndex = new HashMap<>(); newStartIndex.put(first_btn, () -> 0); - newStartIndex.put(prev_btn, () -> getCurrentOverview().getStartIndex() - GExtensionStore.PAGESIZE); - newStartIndex.put(next_btn, () -> getCurrentOverview().getStartIndex() + GExtensionStore.PAGESIZE); + newStartIndex.put(prev_btn, () -> getCurrentOverview().getStartIndex() - GExtensionStore.MAX_PAGES); + newStartIndex.put(next_btn, () -> getCurrentOverview().getStartIndex() + GExtensionStore.MAX_PAGES); newStartIndex.put(last_btn, () -> { - int lastPageSize = getCurrentOverview().getMaxAmount() % GExtensionStore.PAGESIZE; - if (lastPageSize == 0) lastPageSize = GExtensionStore.PAGESIZE; + int lastPageSize = getCurrentOverview().getMaxAmount() % GExtensionStore.MAX_PAGES; + if (lastPageSize == 0) lastPageSize = GExtensionStore.MAX_PAGES; return getCurrentOverview().getMaxAmount() - lastPageSize; }); Arrays.asList(first_btn, prev_btn, next_btn, last_btn).forEach(l -> ((EventTarget) l).addEventListener("click", event ->{ if (!initialized || l.getAttribute("class").contains("gdisabled")) return; - overwriteCurrentOverview(getCurrentOverview().getNewPage(newStartIndex.get(l).get(), GExtensionStore.PAGESIZE)); + overwriteCurrentOverview(getCurrentOverview().getNewPage(newStartIndex.get(l).get(), GExtensionStore.MAX_PAGES)); }, true)); @@ -164,7 +163,7 @@ public class GExtensionStoreController implements Initializable { WebUtils.removeClass(last_btn, "gdisabled"); boolean isLast = overview.getMaxAmount() <= overview.getAmount() + overview.getStartIndex(); - boolean isFirst = overview.getStartIndex() < GExtensionStore.PAGESIZE; + boolean isFirst = overview.getStartIndex() < GExtensionStore.MAX_PAGES; if (isLast) { WebUtils.addClass(next_btn, "gdisabled"); WebUtils.addClass(last_btn, "gdisabled"); @@ -173,8 +172,8 @@ public class GExtensionStoreController implements Initializable { WebUtils.addClass(first_btn, "gdisabled"); WebUtils.addClass(prev_btn, "gdisabled"); } - int thispage = Math.max(1, 1 + (overview.getStartIndex() / GExtensionStore.PAGESIZE)); - int lastpage = Math.max(1, 1 + ((overview.getMaxAmount() - 1) / GExtensionStore.PAGESIZE)); + int thispage = Math.max(1, 1 + (overview.getStartIndex() / GExtensionStore.MAX_PAGES)); + int lastpage = Math.max(1, 1 + ((overview.getMaxAmount() - 1) / GExtensionStore.MAX_PAGES)); webView.getEngine().executeScript("document.getElementById('paging_lbl').innerHTML = '" + thispage + " / " + lastpage + "';"); @@ -211,7 +210,7 @@ public class GExtensionStoreController implements Initializable { private void onFullInitialize() { initialized = true; - setRootOverview(new ByUpdateOverview(null, 0, GExtensionStore.PAGESIZE, getStoreRepository())); + setRootOverview(new ByUpdateOverview(null, 0, GExtensionStore.MAX_PAGES, getStoreRepository())); } public void gExtensionStore(GExtensionStore gExtensionStore) { diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/StoreExtensionItem.java b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/StoreExtensionItem.java index 45f7fd1..a32516e 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/StoreExtensionItem.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/StoreExtensionItem.java @@ -1,12 +1,9 @@ package gearth.services.internal_extensions.extensionstore.application.entities; -import gearth.protocol.HMessage; -import gearth.protocol.HPacket; import gearth.services.internal_extensions.extensionstore.GExtensionStore; import gearth.services.internal_extensions.extensionstore.application.GExtensionStoreController; import gearth.services.internal_extensions.extensionstore.application.WebUtils; import gearth.services.internal_extensions.extensionstore.application.entities.extensiondetails.StoreExtensionDetailsOverview; -import gearth.services.internal_extensions.extensionstore.application.entities.queriedoverviews.CategorizedOverview; import gearth.services.internal_extensions.extensionstore.repository.StoreRepository; import gearth.services.internal_extensions.extensionstore.repository.models.StoreExtension; import netscape.javascript.JSObject; @@ -25,7 +22,7 @@ public class StoreExtensionItem implements ContentItem { new StoreExtensionDetailsOverview( gExtensionStore.getController().getCurrentOverview(), 0, - GExtensionStore.PAGESIZE, + GExtensionStore.MAX_PAGES, storeExtension, gExtensionStore.getRepository() ) diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/categories/CategoryItem.java b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/categories/CategoryItem.java index d998c3f..22af532 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/categories/CategoryItem.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/categories/CategoryItem.java @@ -10,7 +10,6 @@ import gearth.services.internal_extensions.extensionstore.repository.models.ExtC import gearth.services.internal_extensions.extensionstore.repository.querying.ExtensionOrdering; import netscape.javascript.JSObject; -import java.util.Arrays; import java.util.Collections; public class CategoryItem implements ContentItem { @@ -27,7 +26,7 @@ public class CategoryItem implements ContentItem { new CategorizedOverview( gExtensionStore.getController().getCurrentOverview(), 0, - GExtensionStore.PAGESIZE, + GExtensionStore.MAX_PAGES, gExtensionStore.getRepository(), category ) diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/queriedoverviews/QueriedExtensionOverview.java b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/queriedoverviews/QueriedExtensionOverview.java index 447378c..d9de0ca 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/queriedoverviews/QueriedExtensionOverview.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/queriedoverviews/QueriedExtensionOverview.java @@ -33,7 +33,9 @@ public abstract class QueriedExtensionOverview extends HOverview { @Override public List getContentItems() { - return query(startIndex, limit).stream().map(StoreExtensionItem::new).collect(Collectors.toList()); + final List extensions = query(startIndex, limit); + System.out.println("found "+extensions.size()+" extensions"); + return extensions.stream().map(StoreExtensionItem::new).collect(Collectors.toList()); } @Override diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/search/SearchOverview.java b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/search/SearchOverview.java index 1c23d4d..ba5375c 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/search/SearchOverview.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/application/entities/search/SearchOverview.java @@ -3,7 +3,6 @@ package gearth.services.internal_extensions.extensionstore.application.entities. import gearth.services.internal_extensions.extensionstore.GExtensionStore; import gearth.services.internal_extensions.extensionstore.application.entities.ContentItem; import gearth.services.internal_extensions.extensionstore.application.entities.HOverview; -import gearth.services.internal_extensions.extensionstore.application.entities.extensiondetails.StoreExtensionDetailsOverview; import gearth.services.internal_extensions.extensionstore.application.entities.queriedoverviews.SearchedQueryOverview; import gearth.services.internal_extensions.extensionstore.repository.StoreRepository; @@ -51,7 +50,7 @@ public class SearchOverview extends HOverview { new SearchedQueryOverview( gExtensionStore.getController().getCurrentOverview(), 0, - GExtensionStore.PAGESIZE, + GExtensionStore.MAX_PAGES, storeRepository, searchComponent.getSearchKeyword(), searchComponent.getOrdering(), diff --git a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/repository/StoreRepository.java b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/repository/StoreRepository.java index 3eabb45..fc01e3b 100644 --- a/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/repository/StoreRepository.java +++ b/G-Earth/src/main/java/gearth/services/internal_extensions/extensionstore/repository/StoreRepository.java @@ -43,7 +43,14 @@ public class StoreRepository { String queryLower = queryString.toLowerCase(); - Stream stream = getExtensions().stream() + final List extensions = getExtensions(); + for (StoreExtension extension : extensions) { + System.out.println(extension.getTitle()); + for (String system : extension.getCompatibility().getSystems()) { + System.out.println(system); + } + } + Stream stream = extensions.stream() .filter(ext -> ext.getTitle().toLowerCase().contains(queryLower) || ext.getDescription().toLowerCase().contains(queryLower) || ext.getAuthors().stream().anyMatch(author -> author.getName().toLowerCase().contains(queryLower) || author.getUsername() != null && author.getUsername().toLowerCase().contains(queryLower)) diff --git a/G-Earth/src/main/java/gearth/ui/subforms/connection/ConnectionController.java b/G-Earth/src/main/java/gearth/ui/subforms/connection/ConnectionController.java index 3add757..a2c877e 100644 --- a/G-Earth/src/main/java/gearth/ui/subforms/connection/ConnectionController.java +++ b/G-Earth/src/main/java/gearth/ui/subforms/connection/ConnectionController.java @@ -1,12 +1,14 @@ package gearth.ui.subforms.connection; import gearth.GEarth; +import gearth.PropertiesKt; import gearth.misc.Cacher; import gearth.protocol.connection.HClient; import gearth.protocol.connection.HState; import gearth.protocol.connection.proxy.ProxyProviderFactory; import gearth.services.Constants; import javafx.application.Platform; +import javafx.beans.property.BooleanProperty; import javafx.event.ActionEvent; import javafx.scene.control.*; import gearth.protocol.HConnection; @@ -36,6 +38,10 @@ public class ConnectionController extends SubForm { public TextField txtfield_hotelversion; private final Object lock = new Object(); + public TextField outEmail; + public TextField outPassword; + public Button loginButton; + private volatile int fullyInitialized = 0; @@ -158,6 +164,13 @@ public class ConnectionController extends SubForm { inpPort.setDisable(getHConnection().getState() != HState.NOT_CONNECTED || cbx_autodetect.isSelected()); cbx_autodetect.setDisable(!useFlash()); + if (useFlash()) { + final BooleanProperty autoConnectProperty = PropertiesKt.getAutoConnectSelected(); + cbx_autodetect.selectedProperty().bindBidirectional(autoConnectProperty); + if (autoConnectProperty.get()) + btnConnect_clicked(null); + } + outHost.setDisable(!useFlash()); outPort.setDisable(!useFlash()); diff --git a/G-Earth/src/main/kotlin/gearth/Configuration.kt b/G-Earth/src/main/kotlin/gearth/Configuration.kt new file mode 100644 index 0000000..cf45501 --- /dev/null +++ b/G-Earth/src/main/kotlin/gearth/Configuration.kt @@ -0,0 +1,25 @@ +package gearth + +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.decodeFromStream +import java.io.File + +@kotlinx.serialization.Serializable +data class Configuration(val port: Int, val email: String, val password: String) + +private val json = Json { + prettyPrint = true +} + +@ExperimentalSerializationApi +val configurations by lazy { + val file = File("configurations.json") + if (file.exists()) { + json.decodeFromStream>(file.inputStream()) + } else + emptyArray() +} + +fun configuration(port: Int) = configurations.find { it.port == port } diff --git a/G-Earth/src/main/kotlin/gearth/Properties.kt b/G-Earth/src/main/kotlin/gearth/Properties.kt new file mode 100644 index 0000000..8276c8a --- /dev/null +++ b/G-Earth/src/main/kotlin/gearth/Properties.kt @@ -0,0 +1,14 @@ +package gearth + +import javafx.beans.property.SimpleBooleanProperty +import java.nio.file.Paths + +private val propertiesManager by lazy { PropertiesManager(Paths.get("session.properties")) } + + +val autoConnectSelected by lazy { + booleanProperty("autoConnectSelected", true) +} + +private fun booleanProperty(name: String, default: Boolean) = + SimpleBooleanProperty(default).apply { propertiesManager.bindBoolean(name, this) } \ No newline at end of file diff --git a/G-Earth/src/main/kotlin/gearth/PropertiesManager.kt b/G-Earth/src/main/kotlin/gearth/PropertiesManager.kt new file mode 100644 index 0000000..9e754d1 --- /dev/null +++ b/G-Earth/src/main/kotlin/gearth/PropertiesManager.kt @@ -0,0 +1,90 @@ +package gearth + +import javafx.beans.property.* +import javafx.scene.paint.Color +import javafx.scene.paint.Material +import javafx.scene.paint.PhongMaterial +import org.slf4j.LoggerFactory +import java.nio.file.Path +import java.nio.file.Paths +import java.util.* + +class PropertiesManager(private val saveFilePath: Path) { + + private val logger = LoggerFactory.getLogger(PropertiesManager::class.java) + private val properties = Properties() + private lateinit var saveThread: Thread + + fun loadFromFile(): Boolean { + + if (!saveFilePath.toFile().exists()) + return false + + try { + properties.load(saveFilePath.toFile().reader()) + } catch (e: Exception) { + e.printStackTrace() + } + return true + } + + fun startSaveThread() { + if (!this::saveThread.isInitialized) { + saveThread = Thread { + saveToFile() + Thread.sleep(2500L) + } + saveThread.start() + } + } + + fun saveToFile() { + logger.info("Saving properties to $saveFilePath") + try { + val saveFile = saveFilePath.toFile() + if (!saveFile.parentFile.exists()) + saveFile.parentFile.mkdir() + properties.store(saveFile.writer(), "Contains properties for the Qodat application.") + } catch (e: Exception) { + e.printStackTrace() + } + } + + fun bind(key: String, property: Property, transformer: (String) -> T) { + val value = properties.getProperty(key) + if (value != null) + property.value = transformer.invoke(value) + property.addListener { _ -> + properties.setProperty(key, property.value.toString()) + } + } + + /** + * TODO: finish + */ + fun bindMaterial(key: String, property: ObjectProperty) = + bind(key, property) { PhongMaterial() } + + fun bindPath(key: String, property: ObjectProperty) = + bind(key, property) { Paths.get(it) } + + inline fun > bindEnum(key: String, property: ObjectProperty) = + bind(key, property) { enumValueOf(it) } + + fun bindColor(key: String, property: ObjectProperty) = + bind(key, property) { Color.valueOf(it) } + + fun bindBoolean(key: String, property: BooleanProperty) = + bind(key, property) { java.lang.Boolean.parseBoolean(it) } + + fun bindDouble(key: String, property: DoubleProperty) = + bind(key, property) { java.lang.Double.parseDouble(it) } + + fun bindInt(key: String, property: IntegerProperty) = + bind(key, property) { Integer.parseInt(it) } + + fun bindString(key: String, property: StringProperty) = + bind(key, property) { it } + +} + diff --git a/G-Earth/src/main/resources/gearth/ui/subforms/connection/Connection.fxml b/G-Earth/src/main/resources/gearth/ui/subforms/connection/Connection.fxml index 923279c..be6496f 100644 --- a/G-Earth/src/main/resources/gearth/ui/subforms/connection/Connection.fxml +++ b/G-Earth/src/main/resources/gearth/ui/subforms/connection/Connection.fxml @@ -1,11 +1,19 @@ - - - - + + + + + + + + + + + + - + @@ -176,8 +184,9 @@ - - + + + @@ -185,13 +194,13 @@ - + - - + + @@ -212,8 +221,8 @@ - - + + @@ -235,7 +244,7 @@ - + @@ -249,5 +258,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +