TextureSync/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt

212 lines
7.7 KiB
Kotlin
Raw Normal View History

package org.hso.texturesyncclient.controller
2019-06-05 16:05:10 +02:00
import javafx.application.Platform
2019-06-03 17:33:23 +02:00
import javafx.collections.ObservableList
2019-06-06 14:41:30 +02:00
import javafx.event.EventHandler
2019-06-04 18:20:09 +02:00
import javafx.stage.DirectoryChooser
2019-06-06 14:41:30 +02:00
import org.hso.texturesyncclient.alerts.JFXOkayCancelAlert
2019-06-07 09:52:09 +02:00
import org.hso.texturesyncclient.controller.net.AutoConnect
2019-06-03 16:46:57 +02:00
import org.hso.texturesyncclient.controller.net.Connection
2019-06-04 18:20:09 +02:00
import org.hso.texturesyncclient.model.GUIModel
2019-06-04 14:57:54 +02:00
import org.hso.texturesyncclient.model.Sha256
import org.hso.texturesyncclient.model.Texture
import org.hso.texturesyncclient.model.TextureFormat
2019-06-05 16:23:45 +02:00
import org.hso.texturesyncclient.view.importView.ImportView
2019-06-03 16:46:57 +02:00
import org.hso.texturesyncclient.view.importView.ImportViewController
2019-06-04 16:05:47 +02:00
import org.hso.texturesyncclient.view.mainView.MainView
import org.hso.texturesyncclient.view.mainView.MainViewController
2019-06-04 16:05:47 +02:00
import org.hso.texturesyncclient.view.startupView.StartupView
2019-06-03 16:46:57 +02:00
import org.hso.texturesyncclient.view.startupView.StartupViewController
import tornadofx.Controller
2019-06-04 18:20:09 +02:00
import tornadofx.observable
2019-06-03 16:46:57 +02:00
import java.net.InetAddress
2019-06-03 20:42:28 +02:00
import java.util.Calendar
2019-06-03 21:40:02 +02:00
import java.io.File
import javax.imageio.ImageIO
2019-06-04 14:57:54 +02:00
import java.util.UUID
import java.nio.file.Files
import java.io.FileOutputStream
2019-06-04 14:57:54 +02:00
2019-06-03 17:33:23 +02:00
class RootController : Controller() {
private val mvc: MainViewController by inject()
2019-06-03 16:46:57 +02:00
private val svc: StartupViewController by inject()
private val ivc: ImportViewController by inject()
2019-06-04 16:05:47 +02:00
private lateinit var con: Connection
2019-06-03 17:33:23 +02:00
private var lastExportDir: String = System.getProperty("user.home")
2019-06-05 16:05:10 +02:00
2019-06-03 17:33:23 +02:00
/**
* calculate the resolution, get today's date -> upload to server
* @param path the absolute path of the file on the client's system
* @param name the file name
* @param tags all tags for the file
*/
fun importTexture(path: String, name: String, tags: ObservableList<String>) {
2019-06-04 14:57:54 +02:00
val data = Files.readAllBytes(File(path).toPath()) // this is the image as byte array
2019-06-03 17:33:23 +02:00
2019-06-04 14:57:54 +02:00
val uuid = UUID.randomUUID()
val format = if (File(path).extension.toLowerCase() == "png") TextureFormat.PNG else TextureFormat.JPEG
2019-06-03 21:40:02 +02:00
val bimg = ImageIO.read(File(path)) //image for obtaining resolution
2019-06-04 14:57:54 +02:00
val resolution = Pair(bimg.height, bimg.width)
val cal = Calendar.getInstance() //calendar obj with current time
2019-06-05 16:05:10 +02:00
val hash = Sha256(data)
2019-06-04 14:57:54 +02:00
//Todo free image
2019-06-03 20:42:28 +02:00
2019-06-04 14:57:54 +02:00
val newTexture = Texture(uuid, name, tags.toTypedArray(), format, resolution, cal, hash)
2019-06-03 20:42:28 +02:00
2019-06-04 16:05:47 +02:00
try {
con.uploadTexture(newTexture, data)
println("Texture upload successful")
} catch (e: Exception) {
println(e)
}
}
/**
2019-06-05 16:05:10 +02:00
* Initialize connection to server and switch to MainView if connected
2019-06-04 16:05:47 +02:00
* @param name server name as IP or domain
*/
fun initConnection(name: String) {
2019-06-07 09:52:09 +02:00
if (name == " ") {
//no user input, try automatic connect or restore address from settings file
println("try auto connect")
try {
val foundServer = AutoConnect.searchServer()
if (foundServer != null) {
println("[auto] server found")
con = foundServer
con.ping()
println("auto Connection to Server successful")
switchStartupToMain()
} else {
println("[auto] no server found")
}
} catch (e: Exception) {
println(e)
println("auto Connection to Server NOT successful")
}
if (SettingsController.serverAddressIsSet()) {
println("[file] try connect with settings file")
try {
2019-06-07 10:00:03 +02:00
svc.setServerAddress(SettingsController.getServerAddress())
2019-06-07 09:52:09 +02:00
con = Connection(InetAddress.getByName(SettingsController.getServerAddress()))
con.ping()
println("[file] Connection to Server successful")
switchStartupToMain()
} catch (e: Exception) {
println(e)
println("[file] Connection to Server NOT successful")
}
}else{
println("[file] no address in settings file")
}
} else {
//try to connect with user input
try {
println("try connect with user input")
con = Connection(InetAddress.getByName(name))
con.ping()
println("Connection to Server successful")
SettingsController.setServerAddress(name) //store address in settings file
switchStartupToMain()
println("swithing to MainView @ initCon")
} catch (e: Exception) {
println(e)
println("Connection to Server NOT successful")
}
2019-06-04 18:20:09 +02:00
}
}
2019-06-04 16:05:47 +02:00
2019-06-04 18:20:09 +02:00
fun search(tags: ObservableList<String>): ArrayList<GUIModel> {
val previewList = arrayListOf<GUIModel>()
2019-06-04 16:05:47 +02:00
2019-06-04 18:20:09 +02:00
try {
con.query(tags.toTypedArray()).forEach {
previewList.add(GUIModel(it, con.getTexturePreview(it.textureHash)))
}
2019-06-04 16:05:47 +02:00
} catch (e: Exception) {
println(e)
}
2019-06-04 18:20:09 +02:00
println(previewList.size)
return previewList
2019-06-03 17:33:23 +02:00
}
2019-06-03 22:24:18 +02:00
// TODO this could be a companion object
2019-06-05 16:23:45 +02:00
fun switchStartupToMain() {
Platform.runLater {
find(StartupView::class).replaceWith(MainView::class, sizeToScene = true, centerOnScreen = true)
}
2019-06-05 16:23:45 +02:00
}
// These runLater calls should be unnecessary
2019-06-05 16:23:45 +02:00
fun switchMainToImport() {
Platform.runLater {
find(MainView::class).replaceWith(ImportView::class, sizeToScene = true, centerOnScreen = true)
}
2019-06-05 16:23:45 +02:00
}
fun switchImportToMain() {
Platform.runLater {
find(ImportView::class).replaceWith(MainView::class, sizeToScene = true, centerOnScreen = true)
}
2019-06-04 18:20:09 +02:00
}
/**
* save the texture file to a local directory
* @param data the texture as meta element
*/
2019-06-04 18:20:09 +02:00
fun exportTexture(data: Texture) {
val directoryChooser = DirectoryChooser()
directoryChooser.title = "Export Verzeichnis wählen"
directoryChooser.initialDirectory = File(lastExportDir)
2019-06-04 18:20:09 +02:00
val dir = directoryChooser.showDialog(primaryStage)
if (dir != null) {
val extension = if (data.format == TextureFormat.PNG) ".png" else ".jpeg" //get file format
val filePath = "$dir/${data.name}$extension" //build absolute exported texture path
val exportedFile = File(filePath) //create file
val fileout = FileOutputStream(exportedFile)
fileout.write(con.getTextureFile(data.textureHash)) //write bytes in file
fileout.close()
lastExportDir = dir.absolutePath //store last user chosen dir
2019-06-04 18:20:09 +02:00
}
}
/**
* show the detailed meta information in the DetailView
* @param data the texture as meta element
*/
2019-06-04 18:20:09 +02:00
fun showDetail(data: Texture) {
mvc.setPreview3DTexture(con.getTexturePreview(data.textureHash))
mvc.setMeta(data.name, data.resolution.toString(), "")
mvc.setTags(data.tags.toList().observable())
}
/**
* remove a texture from the FolderView and the server
* @param data the texture as meta element
*/
fun deleteTexture(data: Texture) {
2019-06-06 14:41:30 +02:00
val dialogDelete = JFXOkayCancelAlert(
"Löschen",
"Wirklich löschen?",
"-fx-button-type: RAISED; -fx-background-color: #2b7bbb; -fx-text-fill: #000000;"
)
dialogDelete.okayAction = EventHandler {
mvc.removeTextureFromView(data)
con.deleteTexture(data)
2019-06-06 14:41:30 +02:00
}
dialogDelete.cancelAction = EventHandler {
// Do nothing
2019-06-06 14:41:30 +02:00
}
dialogDelete.showAndWait()
}
}