1 /* 2 * FurnitureCatalog.java 7 avr. 2006 3 * 4 * Sweet Home 3D, Copyright (c) 2006 Emmanuel PUYBARET / eTeks <info@eteks.com> 5 * 6 * This program is free software; you can redistribute it and/or modify it under 7 * the terms of the GNU General Public License as published by the Free Software 8 * Foundation; either version 2 of the License, or (at your option) any later 9 * version. 10 * 11 * This program is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 * details. 15 * 16 * You should have received a copy of the GNU General Public License along with 17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 18 * Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 package com.eteks.sweethome3d.model; 21 22 import java.util.ArrayList; 23 import java.util.Collections; 24 import java.util.List; 25 26 /** 27 * Furniture catalog. 28 * @author Emmanuel Puybaret 29 */ 30 public class FurnitureCatalog { 31 private List<FurnitureCategory> categories = new ArrayList<FurnitureCategory>(); 32 private final CollectionChangeSupport<CatalogPieceOfFurniture> furnitureChangeSupport = 33 new CollectionChangeSupport<CatalogPieceOfFurniture>(this); 34 35 /** 36 * Returns the categories list sorted by name. 37 * @return an unmodifiable list of categories. 38 */ getCategories()39 public List<FurnitureCategory> getCategories() { 40 return Collections.unmodifiableList(this.categories); 41 } 42 43 /** 44 * Returns the count of categories in this catalog. 45 */ getCategoriesCount()46 public int getCategoriesCount() { 47 return this.categories.size(); 48 } 49 50 /** 51 * Returns the category at a given <code>index</code>. 52 */ getCategory(int index)53 public FurnitureCategory getCategory(int index) { 54 return this.categories.get(index); 55 } 56 57 /** 58 * Adds the furniture <code>listener</code> in parameter to this catalog. 59 */ addFurnitureListener(CollectionListener<CatalogPieceOfFurniture> listener)60 public void addFurnitureListener(CollectionListener<CatalogPieceOfFurniture> listener) { 61 this.furnitureChangeSupport.addCollectionListener(listener); 62 } 63 64 /** 65 * Removes the furniture <code>listener</code> in parameter from this catalog. 66 */ removeFurnitureListener(CollectionListener<CatalogPieceOfFurniture> listener)67 public void removeFurnitureListener(CollectionListener<CatalogPieceOfFurniture> listener) { 68 this.furnitureChangeSupport.removeCollectionListener(listener); 69 } 70 71 /** 72 * Adds <code>piece</code> of a given <code>category</code> to this catalog. 73 * Once the <code>piece</code> is added, furniture listeners added to this catalog will receive a 74 * {@link CollectionListener#collectionChanged(CollectionEvent) collectionChanged} 75 * notification. 76 * @param category the category of the piece. 77 * @param piece a piece of furniture. 78 */ add(FurnitureCategory category, CatalogPieceOfFurniture piece)79 public void add(FurnitureCategory category, CatalogPieceOfFurniture piece) { 80 int index = Collections.binarySearch(this.categories, category); 81 // If category doesn't exist yet, add it to categories 82 if (index < 0) { 83 category = new FurnitureCategory(category.getName()); 84 this.categories.add(-index - 1, category); 85 } else { 86 category = this.categories.get(index); 87 } 88 // Add current piece of furniture to category list 89 category.add(piece); 90 91 this.furnitureChangeSupport.fireCollectionChanged(piece, 92 category.getIndexOfPieceOfFurniture(piece), CollectionEvent.Type.ADD); 93 } 94 95 /** 96 * Deletes the <code>piece</code> from this catalog. 97 * If then piece category is empty, it will be removed from the categories of this catalog. 98 * Once the <code>piece</code> is deleted, furniture listeners added to this catalog will receive a 99 * {@link CollectionListener#collectionChanged(CollectionEvent) collectionChanged} 100 * notification. 101 * @param piece a piece of furniture in that category. 102 */ delete(CatalogPieceOfFurniture piece)103 public void delete(CatalogPieceOfFurniture piece) { 104 FurnitureCategory category = piece.getCategory(); 105 // Remove piece from its category 106 if (category != null) { 107 int pieceIndex = category.getIndexOfPieceOfFurniture(piece); 108 if (pieceIndex >= 0) { 109 category.delete(piece); 110 111 if (category.getFurnitureCount() == 0) { 112 // Make a copy of the list to avoid conflicts in the list returned by getCategories 113 this.categories = new ArrayList<FurnitureCategory>(this.categories); 114 this.categories.remove(category); 115 } 116 117 this.furnitureChangeSupport.fireCollectionChanged(piece, pieceIndex, CollectionEvent.Type.DELETE); 118 return; 119 } 120 } 121 122 throw new IllegalArgumentException("catalog doesn't contain piece " + piece.getName()); 123 } 124 } 125