1 26 27 package org.nightlabs.editor2d.impl; 28 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyChangeSupport ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import org.nightlabs.editor2d.DrawComponent; 36 import org.nightlabs.editor2d.EditorGuide; 37 38 54 public class EditorGuideImpl 55 implements EditorGuide 56 { 57 61 public Set getParts() { 62 return getMap().keySet(); 63 } 64 65 73 protected static final boolean HORIZONTAL_EDEFAULT = false; 74 75 83 protected boolean horizontal = HORIZONTAL_EDEFAULT; 84 85 93 protected static final int POSITION_EDEFAULT = 0; 94 95 103 protected int position = POSITION_EDEFAULT; 104 105 113 protected static final Map MAP_EDEFAULT = null; 114 115 123 protected Map map = MAP_EDEFAULT; 124 125 133 protected static final PropertyChangeSupport LISTENERS_EDEFAULT = null; 134 135 143 protected PropertyChangeSupport listeners = new PropertyChangeSupport (this); 145 146 153 protected boolean listenersESet = false; 154 155 160 public EditorGuideImpl() { 161 super(); 162 } 163 164 169 public boolean isHorizontal() { 170 return horizontal; 171 } 172 173 178 public void setHorizontal(boolean newHorizontal) { 179 horizontal = newHorizontal; 180 } 181 182 187 public int getPosition() { 188 return position; 189 } 190 191 195 public void setPosition(int newPosition) 196 { 197 if (position != newPosition) { 198 int oldValue = position; 199 position = newPosition; 200 listeners.firePropertyChange(PROPERTY_POSITION, new Integer (oldValue), 201 new Integer (position)); 202 } 203 } 204 205 210 public Map getMap() { 211 if (map == null) { 212 map = new HashMap (); 213 } 214 return map; 215 } 216 217 222 public void setMap(Map newMap) { 223 map = newMap; 224 } 225 226 231 public void attachPart(DrawComponent drawComponent, int alignment) 232 { 233 if (getMap().containsKey(drawComponent) && getAlignment(drawComponent) == alignment) 234 return; 235 236 getMap().put(drawComponent, new Integer (alignment)); 237 EditorGuide parent = isHorizontal() ? drawComponent.getHorizontalGuide() : drawComponent.getVerticalGuide(); 238 if (parent != null && parent != this) { 239 parent.detachPart(drawComponent); 240 } 241 if (isHorizontal()) { 242 drawComponent.setHorizontalGuide(this); 243 } else { 244 drawComponent.setVerticalGuide(this); 245 } 246 listeners.firePropertyChange(PROPERTY_CHILDREN, null, drawComponent); 247 } 248 249 254 public void detachPart(DrawComponent drawComponent) 255 { 256 if (getMap().containsKey(drawComponent)) { 257 getMap().remove(drawComponent); 258 if (isHorizontal()) { 259 drawComponent.setHorizontalGuide(null); 260 } else { 261 drawComponent.setVerticalGuide(null); 262 } 263 listeners.firePropertyChange(PROPERTY_CHILDREN, null, drawComponent); 264 } 265 } 266 267 272 public int getAlignment(DrawComponent drawComponent) 273 { 274 if (getMap().get(drawComponent) != null) 275 return ((Integer )getMap().get(drawComponent)).intValue(); 276 return -2; 277 } 278 279 284 public void addPropertyChangeListener(PropertyChangeListener listener) { 285 listeners.addPropertyChangeListener(listener); 286 } 287 288 293 public void removePropertyChangeListener(PropertyChangeListener listener) { 294 listeners.removePropertyChangeListener(listener); 295 } 296 297 302 public String toString() 303 { 304 StringBuffer result = new StringBuffer (super.toString()); 305 result.append(" (horizontal: "); 306 result.append(horizontal); 307 result.append(", position: "); 308 result.append(position); 309 result.append(", map: "); 310 result.append(map); 311 result.append(", listeners: "); 312 result.append(')'); 313 return result.toString(); 314 } 315 316 } | Popular Tags |