前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用javafx框架tornadofx制作的ip地址定位小工具

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

原创
作者头像
用户6167008
修改2019-11-12 17:43:19
3.3K6
修改2019-11-12 17:43:19
举报

使用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()
        }
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档