Skip to content

Instantly share code, notes, and snippets.

@Maartyl
Created December 21, 2016 09:29
Show Gist options
  • Save Maartyl/0e47c60b561c5bceb86e7f4e5414db62 to your computer and use it in GitHub Desktop.
Save Maartyl/0e47c60b561c5bceb86e7f4e5414db62 to your computer and use it in GitHub Desktop.
/**
* Created by maartyl on 17.12.16.
*/
import javafx.application.Application
import tornadofx.*
import java.io.File
import java.nio.file.Files
fun main(args: Array<String>) {
Application.launch(SortingApp::class.java, *args)
}
class SortingApp : App(Starter::class, Styles::class)
class Starter : View("setup") {
private var dirSrc: File? = null
private var dirDest: File? = null
override val root = vbox {
setPrefSize(800.0, 1000.0)
button("source path"){
setOnAction {
dirSrc = chooseDirectory("src", initialDirectory = dirSrc)
text = "source: $dirSrc"
}
}
button("source path"){
setOnAction {
dirDest = chooseDirectory("dest", initialDirectory = dirSrc)
text = "destination: $dirDest"
}
}
button("Start") {
this.prefWidth = 800.0
setOnAction {
val dirSrc = dirSrc
val dirDest = dirDest
if (dirSrc == null) {
text = "missing source"
return@setOnAction
}
if (dirDest == null) {
text = "missing destination"
return@setOnAction
}
val pics = dirSrc.absoluteFile.listFiles { f, it ->
it.endsWith(".jpg", ignoreCase = true) ||
it.endsWith(".jpeg", ignoreCase = true) ||
it.endsWith(".png", ignoreCase = true) ||
it.endsWith(".gif", ignoreCase = true)
}
fun moveFile(s: File){
Files.move(s.toPath(), File(dirDest.path + "/" + s.name).toPath())
}
if (pics.isEmpty()){
text = "no pictures found"
return@setOnAction
}
replaceWith(Decision(pics, ::moveFile), ViewTransition.Metro(0.3.seconds, ViewTransition.Direction.LEFT))
}
}
}
}
class Decision(val pics: Array<File>, val move: (File)->Unit) : View("decide") {
private var pos = -1
private var imgsetter: (String)->Unit by singleAssign()
fun nextpic() {
pos++
if (pos > pics.lastIndex) {
replaceWith(Starter::class)
return
}
val s = pics[pos]
println(s)
try {
imgsetter(s.absoluteFile.toURI().toString())
} catch(e: Exception) {
println(e)
nextpic()
}
}
override val root = vbox {
setPrefSize(800.0, 1000.0)
hbox {
button("Next") {
this.prefWidth = 400.0
setOnAction {
nextpic()
}
}
button("Move") {
this.prefWidth = 400.0
setOnAction {
try {
move(pics[pos])
} catch(e: Exception) {
println(e)
}
nextpic()
}
}
}
var iv = imageview {}
imgsetter = {
iv.replaceWith(imageview(it) {
iv = this
isPreserveRatio = true
isSmooth = true
fitWidthProperty().bind(primaryStage.widthProperty())
fitHeightProperty().bind(primaryStage.heightProperty())
}, ViewTransition.Metro(0.3.seconds, ViewTransition.Direction.LEFT))
}
nextpic()
}
}
class Styles : Stylesheet() {
init {
label {
fontSize = 40.px
//fontWeight = FontWeight.BOLD
//backgroundColor += c("#cecece")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment