Code Monkey home page Code Monkey logo

Comments (6)

edvin avatar edvin commented on May 20, 2024 1

It seems to work great, though it is one of the very few wrapper classes in TornadoFX. I liked the idea of just extending existing API's, but that's not always possible :)

from tornadofx.

hastebrot avatar hastebrot commented on May 20, 2024

I've created a TransformatedListContext [1] in Groovy a while ago (the gist includes an implementation, main method and specification tests). It allows to have a masterList and a targetList. Maybe this gives some hints on how to implement this for TornodoFX, maybe my approach is suboptimal, I had to be very careful with weak references in the observable list.

Example:

def setup() {
    listContext = new TransformatedListContext()
    targetList_integers = FXCollections.observableArrayList(1, 2, 3, 4, 5)
}

def "setSort() and setFilter()"() {
    given:
    def masterList = listContext.register(targetList_integers)

    when:
    listContext.sort = { int a, int b -> b <=> a } as Comparator
    listContext.filter = { int a -> a > 3 } as Predicate

    then:
    targetList_integers == [5, 4]
    masterList == [1, 2, 3, 4, 5]
}

[1] https://gist.github.com/hastebrot/efe0a4d39e054730f8090ef9facfe050

from tornadofx.

edvin avatar edvin commented on May 20, 2024

Thanks! I'm working on an implementation right now, I'll see if I can glean some ideas from your list :)

from tornadofx.

edvin avatar edvin commented on May 20, 2024

I commited SortedFilteredList now. It is a delegate for an ObservableList<T>, and wraps the data in the way described in the top of this issue. You can connect it to a TableView or ListView like this:

val table = TableView<Person>()
val data = SortedFilteredList(persons).bindTo(table)

It can also be used ad hoc. Filter the data by updating the predicate:

data.predicate = { it.age > 18 }

Special support is added to update the predicate based on an Observable. This is extremely handy for TextField based filtering. Let's pretend that Person has a matches(query: String): Boolean function. With this, you can perform live filtering very easily:

textfield {
    promptText = "Filter query"
    data.filterWhen(textProperty(), { query, item -> item.matches(query) } )
}

The same technique can be used for selection based on a ComboBox or similar.

from tornadofx.

edvin avatar edvin commented on May 20, 2024

Added a simple test:

    data class Person(val name: String, val age: Int)

    val persons = FXCollections.observableArrayList(
            Person("Samantha Stuart", 34),
            Person("Tom Marks", 42),
            Person("Stuart Gills", 17),
            Person("Nicole Williams", 27))

    @Test
    fun filterTest() {
        val data = SortedFilteredList(persons)
        data.predicate = { it.name.contains("Stu")}
        Assert.assertEquals(2, data.size)
    }

from tornadofx.

thomasnield avatar thomasnield commented on May 20, 2024

This is pretty convenient. It always felt odd doing all that work to created sorted and filtered lists.

from tornadofx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.