class DragFile : View("drop txt file and View") { val dragFileModel = DragFileModel(this) override val root = vbox { label(title) { addClass(DragStyles.heading) } label(dragFileModel.lineCountProperty) scrollpane { minHeight = 320.0 hbox { textarea(dragFileModel.fileLinesProperty) { minHeight = 320.0 promptText = "Drop file here" setOnDragOver { event -> dragFileModel.handleDragOver(event) } setOnDragEntered { event -> dragFileModel.handleDragEntered(event) } setOnDragExited { event -> dragFileModel.handleDragExited(event) } setOnDragDropped { event -> dragFileModel.handleDragDropped(event) } } } } } } class DragFileModel(mainView: DragFile) { var lineList: List<String> = emptyList() val fileLinesProperty = SimpleStringProperty(this, "leftLines", "initial") var lines by fileLinesProperty val lineCountProperty = SimpleIntegerProperty(this, "leftLineCount", 0) var lineCount by lineCountProperty fun handleDragOver(event: DragEvent) { event.acceptTransferModes(TransferMode.MOVE) event.consume() } fun handleDragEntered(event: DragEvent) { println("Entered ${event.source}") event.consume() } fun handleDragExited(event: DragEvent) { println("Exited ${event.source}") event.consume() } fun handleDragDropped(event: DragEvent) { println("Dropped ${event.source}") val db = event.dragboard if (db.hasFiles()) { db.files.map { processFile(it.absolutePath) } } event.consume() } private fun processFile(filename: String) { lineList = FileService.getFileLines(filename) lines = lineList.joinToString(separator = "\n") lineCount = lineList.count() } } class FileService { companion object { fun getFileLines(filename: String): List<String> { val lineList = mutableListOf<String>() File(filename).useLines { lines -> lines.forEach { lineList.add(it) } } return lineList } } } class DragStyles : Stylesheet() { companion object { val heading by cssclass() val dropzone by cssclass() } init { label and heading { padding = box(10.px) fontSize = 20.px fontWeight = FontWeight.BOLD } dropzone { borderColor += box( top = Color.RED, right = Color.DARKGREEN, left = Color.ORANGE, bottom = Color.RED ) padding = box(40.px) } } }