1 /* 2 * Level.java 22 oct. 2011 3 * 4 * Sweet Home 3D, Copyright (c) 2011 Emmanuel PUYBARET / eTeks <info@eteks.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 package com.eteks.sweethome3d.model; 21 22 import java.io.IOException; 23 import java.io.ObjectInputStream; 24 25 /** 26 * A level in a home. 27 * @author Emmanuel Puybaret 28 * @since 3.4 29 */ 30 public class Level extends HomeObject { 31 private static final long serialVersionUID = 1L; 32 33 /** 34 * The properties of a level that may change. <code>PropertyChangeListener</code>s added 35 * to a level will be notified under a property name equal to the string value of one these properties. 36 */ 37 public enum Property {NAME, ELEVATION, HEIGHT, FLOOR_THICKNESS, BACKGROUND_IMAGE, VISIBLE, VIEWABLE, ELEVATION_INDEX}; 38 39 private String name; 40 private float elevation; 41 private float floorThickness; 42 private float height; 43 private BackgroundImage backgroundImage; 44 private boolean visible; 45 private boolean viewable; 46 private int elevationIndex; 47 48 /** 49 * Creates a home level. 50 * @param name the name of the level 51 * @param elevation the elevation of the bottom of the level 52 * @param floorThickness the floor thickness of the level 53 * @param height the height of the level 54 */ Level(String name, float elevation, float floorThickness, float height)55 public Level(String name, float elevation, float floorThickness, float height) { 56 this(createId("level"), name, elevation, floorThickness, height); 57 } 58 59 /** 60 * Creates a home level. 61 * @param id the ID of the level 62 * @param name the name of the level 63 * @param elevation the elevation of the bottom of the level 64 * @param floorThickness the floor thickness of the level 65 * @param height the height of the level 66 * @since 6.4 67 */ Level(String id, String name, float elevation, float floorThickness, float height)68 public Level(String id, String name, float elevation, float floorThickness, float height) { 69 super(id); 70 this.name = name; 71 this.elevation = elevation; 72 this.floorThickness = floorThickness; 73 this.height = height; 74 this.visible = true; 75 this.viewable = true; 76 this.elevationIndex = -1; 77 } 78 79 /** 80 * Initializes new level fields to their default values 81 * and reads level from <code>in</code> stream with default reading method. 82 */ readObject(ObjectInputStream in)83 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 84 this.visible = true; 85 this.viewable = true; 86 this.elevationIndex = -1; 87 in.defaultReadObject(); 88 } 89 90 /** 91 * Returns the name of this level. 92 */ getName()93 public String getName() { 94 return this.name; 95 } 96 97 /** 98 * Sets the name of this level. Once this level 99 * is updated, listeners added to this level will receive a change notification. 100 */ setName(String name)101 public void setName(String name) { 102 if (name != this.name 103 && (name == null || !name.equals(this.name))) { 104 String oldName = this.name; 105 this.name = name; 106 firePropertyChange(Property.NAME.name(), oldName, name); 107 } 108 } 109 110 /** 111 * Returns the elevation of the bottom of this level. 112 */ getElevation()113 public float getElevation() { 114 return this.elevation; 115 } 116 117 /** 118 * Sets the elevation of this level. Once this level is updated, 119 * listeners added to this level will receive a change notification. 120 */ setElevation(float elevation)121 public void setElevation(float elevation) { 122 if (elevation != this.elevation) { 123 float oldElevation = this.elevation; 124 this.elevation = elevation; 125 firePropertyChange(Property.ELEVATION.name(), oldElevation, elevation); 126 } 127 } 128 129 /** 130 * Returns the floor thickness of this level. 131 */ getFloorThickness()132 public float getFloorThickness() { 133 return this.floorThickness; 134 } 135 136 /** 137 * Sets the floor thickness of this level. Once this level is updated, 138 * listeners added to this level will receive a change notification. 139 */ setFloorThickness(float floorThickness)140 public void setFloorThickness(float floorThickness) { 141 if (floorThickness != this.floorThickness) { 142 float oldFloorThickness = this.floorThickness; 143 this.floorThickness = floorThickness; 144 firePropertyChange(Property.FLOOR_THICKNESS.name(), oldFloorThickness, floorThickness); 145 } 146 } 147 148 /** 149 * Returns the height of this level. 150 */ getHeight()151 public float getHeight() { 152 return this.height; 153 } 154 155 /** 156 * Sets the height of this level. Once this level is updated, 157 * listeners added to this level will receive a change notification. 158 */ setHeight(float height)159 public void setHeight(float height) { 160 if (height != this.height) { 161 float oldHeight = this.height; 162 this.height = height; 163 firePropertyChange(Property.HEIGHT.name(), oldHeight, height); 164 } 165 } 166 167 /** 168 * Returns the plan background image of this level. 169 */ getBackgroundImage()170 public BackgroundImage getBackgroundImage() { 171 return this.backgroundImage; 172 } 173 174 /** 175 * Sets the plan background image of this level and fires a <code>PropertyChangeEvent</code>. 176 */ setBackgroundImage(BackgroundImage backgroundImage)177 public void setBackgroundImage(BackgroundImage backgroundImage) { 178 if (backgroundImage != this.backgroundImage) { 179 BackgroundImage oldBackgroundImage = this.backgroundImage; 180 this.backgroundImage = backgroundImage; 181 firePropertyChange(Property.BACKGROUND_IMAGE.name(), oldBackgroundImage, backgroundImage); 182 } 183 } 184 185 /** 186 * Returns <code>true</code> if this level is visible. 187 */ isVisible()188 public boolean isVisible() { 189 return this.visible; 190 } 191 192 /** 193 * Sets whether this level is visible or not. Once this level is updated, 194 * listeners added to this level will receive a change notification. 195 */ setVisible(boolean visible)196 public void setVisible(boolean visible) { 197 if (visible != this.visible) { 198 this.visible = visible; 199 firePropertyChange(Property.VISIBLE.name(), !visible, visible); 200 } 201 } 202 203 /** 204 * Returns <code>true</code> if this level is viewable. 205 * @since 5.0 206 */ isViewable()207 public boolean isViewable() { 208 return this.viewable; 209 } 210 211 /** 212 * Sets whether this level is viewable or not. Once this level is updated, 213 * listeners added to this level will receive a change notification. 214 * @since 5.0 215 */ setViewable(boolean viewable)216 public void setViewable(boolean viewable) { 217 if (viewable != this.viewable) { 218 this.viewable = viewable; 219 firePropertyChange(Property.VIEWABLE.name(), !viewable, viewable); 220 } 221 } 222 223 /** 224 * Returns <code>true</code> if this level is viewable and visible. 225 * @since 5.0 226 */ isViewableAndVisible()227 public boolean isViewableAndVisible() { 228 return this.viewable && this.visible; 229 } 230 231 /** 232 * Returns the index of this level used to order levels at the same elevation. 233 * @since 5.0 234 */ getElevationIndex()235 public int getElevationIndex() { 236 return this.elevationIndex; 237 } 238 239 /** 240 * Sets the index of this level used to order levels at the same elevation. 241 * @since 5.0 242 */ setElevationIndex(int elevationIndex)243 public void setElevationIndex(int elevationIndex) { 244 if (elevationIndex != this.elevationIndex) { 245 int oldElevationIndex = this.elevationIndex; 246 this.elevationIndex = elevationIndex; 247 firePropertyChange(Property.ELEVATION_INDEX.name(), oldElevationIndex, elevationIndex); 248 } 249 } 250 251 /** 252 * Returns a clone of this level. 253 */ 254 @Override clone()255 public Level clone() { 256 return (Level)super.clone(); 257 } 258 } 259