使用javafx框架tornadofx制作的ip地址定位小工具

2019-11-12 17:43:19 浏览数 (1)

使用https://gitee.com/lionsoul/ip2region 中的数据库

ui:

代码语言:javascript复制
class Ip2region : View("Ip2region") {
    val c: Ip2regionController by inject()
    val ip = stringProperty("101.105.35.57")
    val dbFile = stringProperty(File("").canonicalPath.plus("/plugins/ip2region/ip2region.db"))

    val algorithm = intProperty(1)
    override val root = borderpane {
        stylesheet {
            s(Stylesheet.button, Stylesheet.textField, Stylesheet.radioButton) {
                fontSize = 16.px
            }
        }

        prefWidth = 500.0
        paddingAll = 10.0
        top = vbox(10) {
            paddingBottom = 5.0
            alignment = Pos.CENTER

            hbox {
                button("...") {
                    tooltip("点我选择数据库文件")
                    action {
                        val imgType = listOf("*.db")
                        val efset = arrayOf(FileChooser.ExtensionFilter("$imgType", imgType))
                        val fileschoosed = chooseFile("选择图片", efset, FileChooserMode.Single) {
                            // p初始目录为当前项目目录
                            initialDirectory = File(File("").canonicalPath)
                        }

                        if (fileschoosed.isNotEmpty()) {
                            dbFile.value = fileschoosed.first().toString().replace("\", "/")
                        }
                    }
                }
                textfield(dbFile) { prefWidth = 480.0 }
            }
            textfield(ip) { promptText = "请输入ip地址,如:101.105.35.57" }
            hbox(4) {
                "01234.".toCharArray().forEach {
                    button("$it") {
                        action {
                            ip.value = ip.value.plus("$it")
                        }
                    }
                }
            }
            hbox(4) {
                "56789".toCharArray().forEach {
                    button("$it") {
                        action {
                            ip.value = ip.value.plus("$it")
                        }
                    }
                }
            }

            textfield(c.region) { promptText = "这里显示查寻结果" }

            hbox(10) {
                togglegroup {
                    val tg = this
                    selectedToggleProperty().onChange {
                        algorithm.value = tg.selectedToggle.userData as Int
                    }
                    radiobutton("B-tree") {
                        userData = 1
                        isSelected = true
                    }
                    radiobutton("Binary") { userData = 2 }
                    radiobutton("Memory") { userData = 3 }
                }
            }
            hbox(10) {
                button("开始查找") {
                    action {
                        try {
                            c.search(algorithm.value, ip.value, dbFile.value)
                        }catch (e:Exception){
//                            e.printStackTrace()
                            error("$e")
                        }
                    }
                }
                button("清除") {
                    action {
                        cleartxt()
                    }
                }
            }

        }
        bottom = hbox(10) {
            label(c.searchTime)
            label(c.msg)
        }
    }

    private fun cleartxt() {
        c.region.value = ""
        c.searchTime.value = ""
        ip.value = ""
        c.msg.value = ""
    }
}

controller:

代码语言:javascript复制
class Ip2regionController : Controller() {
    val region = stringProperty()
    val searchTime = stringProperty()
    val msg = stringProperty()

    /**
     * @param algorithm
     * DbSearcher.BTREE_ALGORITHM  = 1
     * DbSearcher.BINARY_ALGORITHM = 2
     * DbSearcher.MEMORY_ALGORITYM = 3
     *
     */
    fun search(algorithm: Int, ip: String, dbFile: String) {
        try {
            if (!Util.isIpAddress(ip)) {
                msg.value="Error: Invalid ip address"
                return
            }
            val config = DbConfig()
            val searcher = DbSearcher(config, dbFile)

            var sTime = 0.0
            var cTime = 0.0

            sTime = System.nanoTime().toDouble()

            //define the method
            when (algorithm) {
                DbSearcher.BTREE_ALGORITHM -> {
                    region.value = (searcher.btreeSearch(ip) as DataBlock).toString()
                }
                DbSearcher.BINARY_ALGORITHM -> {
                    region.value = (searcher.binarySearch(ip) as DataBlock).toString()
                }
                DbSearcher.MEMORY_ALGORITYM -> {
                    region.value = (searcher.memorySearch(ip) as DataBlock).toString()
                }
            }

            cTime = (System.nanoTime() - sTime) / 1000000
            searchTime.value ="查寻耗时: "  cTime.toString().plus(" millseconds")

            searcher.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}

0 人点赞