save and load server address

This commit is contained in:
Hendrik Schutter 2019-06-05 16:05:10 +02:00
parent c80233155f
commit e32766e581
4 changed files with 114 additions and 11 deletions

View File

@ -1,18 +1,31 @@
package org.hso.texturesyncclient.app
import org.hso.texturesyncclient.controller.RootController
import org.hso.texturesyncclient.controller.SettingsController
import org.hso.texturesyncclient.view.importView.ImportView
import org.hso.texturesyncclient.view.mainView.MainView
import org.hso.texturesyncclient.view.mainView.MainViewController
import org.hso.texturesyncclient.view.startupView.StartupView
import org.hso.texturesyncclient.view.startupView.StartupViewController
import tornadofx.App
class Main: App(MainView::class){
class Main: App(StartupView::class){
val controller = RootController()
private val svc: StartupViewController by inject()
init {
// TODO get saved IP address, if found try to connect, else show StartupView
SettingsController.init()
if (SettingsController.serverAddressIsSet()) {
//load settings in ui and try to connect
println("serverAddress is set")
svc.setServerAddress(SettingsController.getServerAddress())
svc.btnConnectAction(SettingsController.getServerAddress())
} else {
println("serverAddress is not set")
}
}
}

View File

@ -1,5 +1,7 @@
package org.hso.texturesyncclient.controller
import com.sun.scenario.Settings
import javafx.application.Platform
import javafx.collections.ObservableList
import javafx.stage.DirectoryChooser
import org.hso.texturesyncclient.controller.net.Connection
@ -30,6 +32,7 @@ class RootController : Controller() {
private lateinit var con: Connection
init {
/*var data = Texture()
var img = con.getTexturePreview(data.textureHash)
@ -46,9 +49,9 @@ class RootController : Controller() {
}
mvc.addElement(test)*/
}
/**
* calculate the resolution, get today's date -> upload to server
* @param path the absolute path of the file on the client's system
@ -63,7 +66,7 @@ class RootController : Controller() {
val bimg = ImageIO.read(File(path)) //image for obtaining resolution
val resolution = Pair(bimg.height, bimg.width)
val cal = Calendar.getInstance() //calendar obj with current time
val hash = Sha256(data)
val hash = Sha256(data)
//Todo free image
@ -79,18 +82,20 @@ class RootController : Controller() {
}
/**
* Initialize connection to server
* Initialize connection to server and switch to MainView if connected
* @param name server name as IP or domain
*/
fun initConnection(name: String) {
try {
con = Connection(InetAddress.getByName(name))
con.ping()
println("Connection successful")
// TODO store server ip for next start
println("Connection to Server successful")
SettingsController.setServerAddress(name)
switchToMainView()
println("swithing to MainView @ initCon")
} catch (e: Exception) {
println(e)
println("Connection to Server NOT successful")
}
}
@ -112,8 +117,10 @@ class RootController : Controller() {
}
fun switchToMainView(){
find(StartupView::class).replaceWith(MainView::class, sizeToScene = true, centerOnScreen = true)
fun switchToMainView() {
Platform.runLater { //avoid the exception that occurs then this is called from an not tornadoFx thread
find(StartupView::class).replaceWith(MainView::class, sizeToScene = true, centerOnScreen = true)
}
}
fun exportTexture(data: Texture) {

View File

@ -0,0 +1,79 @@
package org.hso.texturesyncclient.controller
import java.io.*
import java.util.Properties;
class SettingsController {
companion object {
private lateinit var serverAddress: String
private lateinit var props: Properties
private val userHome = System.getProperty("user.home")
private val osName = System.getProperty("os.name")
private lateinit var dirPath: String //path to settings file
private lateinit var settingsFile: File //settings file
private val defautAddressValue: String = " "
fun init() {
props = Properties()
if (osName.contains("Windows")) {
dirPath = userHome + "/Documents/TextureSync";
} else {
dirPath = userHome + "/.TextureSync";
}
settingsFile = File(dirPath + "/config.xml"); //open Settings file
if (!settingsFile.exists()) {
println("settings not found! Will create new one")
File(dirPath).mkdir()
settingsFile.createNewFile()
serverAddress = defautAddressValue //load default value
saveSettings()
} else {
println("settings found")
loadSettings()
}
}
fun serverAddressIsSet(): Boolean {
if (serverAddress == defautAddressValue) {
return false
}
return true
}
private fun loadSettings() {
val inputStream: InputStream
inputStream = FileInputStream(settingsFile);
props.loadFromXML(inputStream);
serverAddress = props.getProperty("serverAddress")
inputStream.close();
}
private fun saveSettings() {
val outputStream: OutputStream
props.setProperty("serverAddress", serverAddress);
outputStream = FileOutputStream(settingsFile);
props.storeToXML(outputStream, "TextureSync settings");
outputStream.close();
println("settings saved")
}
fun getServerAddress(): String {
return serverAddress;
}
fun setServerAddress(serverAddress: String) {
this.serverAddress = serverAddress
saveSettings()
}
}
}

View File

@ -10,6 +10,11 @@ class StartupViewController : Controller() {
private val sv = find(StartupView::class)
private val rootc = find(RootController::class)
fun setServerAddress(address: String){
sv.tfServerIP.text = address
sv.tfServerIP.isFocusTraversable = false
}
fun btnConnectAction(name: String) {
sv.labelStatus.text = "Verbinden ..."
sv.tfServerIP.isEditable = false
@ -25,7 +30,6 @@ class StartupViewController : Controller() {
sv.tfServerIP.isEditable = true
sv.btnConnect.isDisable = false
sv.tfServerIP.clear()
rootc.switchToMainView()
}
}