}
public void collectionChanged(CollectionEvent<CatalogPieceOfFurniture> ev) {
// If catalog tree model was garbage collected, remove this listener from catalog
CatalogTreeModel catalogTreeModel = this.catalogTreeModel.get();
FurnitureCatalog catalog = (FurnitureCatalog)ev.getSource();
if (catalogTreeModel == null) {
catalog.removeFurnitureListener(this);
} else {
CatalogPieceOfFurniture piece = ev.getItem();
switch (ev.getType()) {
case ADD :
if (piece.getCategory().getFurnitureCount() == 1) {
// Fire nodes inserted for new category
catalogTreeModel.fireTreeNodesInserted(new TreeModelEvent(catalogTreeModel,
new Object [] {catalog},
new int [] {Collections.binarySearch(catalog.getCategories(), piece.getCategory())},
new Object [] {piece.getCategory()}));
} else {
// Fire nodes inserted for new piece
catalogTreeModel.fireTreeNodesInserted(new TreeModelEvent(catalogTreeModel,
new Object [] {catalog, piece.getCategory()},
new int [] {ev.getIndex()},
new Object [] {piece}));
}
break;
case DELETE :
if (piece.getCategory().getFurnitureCount() == 0) {
// Fire nodes removed for deleted category
catalogTreeModel.fireTreeNodesRemoved(new TreeModelEvent(catalogTreeModel,
new Object [] {catalog},
new int [] {-(Collections.binarySearch(catalog.getCategories(), piece.getCategory()) + 1)},
new Object [] {piece.getCategory()}));
} else {
// Fire nodes removed for deleted piece
catalogTreeModel.fireTreeNodesRemoved(new TreeModelEvent(catalogTreeModel,
new Object [] {catalog, piece.getCategory()},