0

Person class:

public class Person {

    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

House class:

public class House {

    private final EventList<Person> residents = new BasicEventList<>();

    public void addResident(Person resident) {
        residents.add(resident);
    }

    public EventList<Person> getResidents() {
        return residents;
    }
}

init code:

EventList<House> houses = new BasicEventList<>();

CollectionList<House, Person> allResidents = new CollectionList<>(houses, new CollectionList.Model<House, Person>() {

    @Override
    public List<Person> getChildren(House parent) {
        return parent.getResidents();
    }
});

jTable.setModel(GlazedListsSwing.eventTableModel(allResidents, new String[]{"name", "age"}, new String[]{"Name", "Age"}, new boolean[]{false, false}));

House firstHouse = new House();
houses.add(firstHouse);
firstHouse.addResident(new Person("John", 18));

House secondHouse = new House();
houses.add(secondHouse);
secondHouse.addResident(new Person("Mary", 44));
secondHouse.addResident(new Person("Alisa", 6));

So there are houses, which contain lists of residents. The table should show all residents from all the houses.

New residents can be added to a house at any time, so the table should be able to reflect the changes. This is why EventList is an obvious choice to store the residents in one house.

However, GlazedLists requires that the CollectionList and the resident EventLists all use the same ListEventPublisher and ReadWriteLock.

The question

How am I supposed to pass the same ListEventPublisher and ReadWriteLock to both the CollectionList and the resident EventLists of every House instance, considering that

  • the table should update every time I remove or add a resident to a house?
  • instances of House class may be created both before and after the creation of the CollectionList itself and they all must be valid entries to it?

Note that the second criterion makes it impossible to just get the Publisher and Lock from the CollectionList and pass them to the constructors of new Houses. (because the houses may be created before the list itself)

Are there maybe other workarounds than sharing the Publisher and Lock?


Related question: How to deal with GlazedLists's PluggableList requirement for shared publisher and lock

Community
  • 1
  • 1
Tuupertunut
  • 741
  • 7
  • 9

1 Answers1

0

There is no documentation about it, but this approach works for sharing ListEventPublisher and ReadWriteLock between lists:

public static final ListEventPublisher PUBLISHER = ListEventAssembler.createListEventPublisher();
public static final ReadWriteLock LOCK = LockFactory.DEFAULT.createReadWriteLock();

Put those variables somewhere and then pass them to every constructor of new EventLists

I found this by looking at GlazedLists' source code and saw them creating their locks and publishers that way. Unfortunately I don't know whether this approach has any drawbacks or whether it is a correct way at all.

Tuupertunut
  • 741
  • 7
  • 9