| 1 7 package com.nightlabs.editor2d.impl; 8 9 import java.beans.PropertyChangeListener ; 10 import java.beans.PropertyChangeSupport ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 import java.util.Set ; 14 15 import com.nightlabs.editor2d.DrawComponent; 16 import com.nightlabs.editor2d.EditorGuide; 17 18 34 public class EditorGuideImpl 35 implements EditorGuide 36 { 37 41 public Set getParts() { 42 return getMap().keySet(); 43 } 44 45 53 protected static final boolean HORIZONTAL_EDEFAULT = false; 54 55 63 protected boolean horizontal = HORIZONTAL_EDEFAULT; 64 65 73 protected static final int POSITION_EDEFAULT = 0; 74 75 83 protected int position = POSITION_EDEFAULT; 84 85 93 protected static final Map MAP_EDEFAULT = null; 94 95 103 protected Map map = MAP_EDEFAULT; 104 105 113 protected static final PropertyChangeSupport LISTENERS_EDEFAULT = null; 114 115 123 protected PropertyChangeSupport listeners = new PropertyChangeSupport (this); 125 126 133 protected boolean listenersESet = false; 134 135 140 public EditorGuideImpl() { 141 super(); 142 } 143 144 149 public boolean isHorizontal() { 150 return horizontal; 151 } 152 153 158 public void setHorizontal(boolean newHorizontal) { 159 horizontal = newHorizontal; 160 } 161 162 167 public int getPosition() { 168 return position; 169 } 170 171 175 public void setPosition(int newPosition) 176 { 177 if (position != newPosition) { 178 int oldValue = position; 179 position = newPosition; 180 listeners.firePropertyChange(PROPERTY_POSITION, new Integer (oldValue), 181 new Integer (position)); 182 } 183 } 184 185 190 public Map getMap() { 191 if (map == null) { 192 map = new HashMap (); 193 } 194 return map; 195 } 196 197 202 public void setMap(Map newMap) { 203 map = newMap; 204 } 205 206 211 public void attachPart(DrawComponent drawComponent, int alignment) 212 { 213 if (getMap().containsKey(drawComponent) && getAlignment(drawComponent) == alignment) 214 return; 215 216 getMap().put(drawComponent, new Integer (alignment)); 217 EditorGuide parent = isHorizontal() ? drawComponent.getHorizontalGuide() : drawComponent.getVerticalGuide(); 218 if (parent != null && parent != this) { 219 parent.detachPart(drawComponent); 220 } 221 if (isHorizontal()) { 222 drawComponent.setHorizontalGuide(this); 223 } else { 224 drawComponent.setVerticalGuide(this); 225 } 226 listeners.firePropertyChange(PROPERTY_CHILDREN, null, drawComponent); 227 } 228 229 234 public void detachPart(DrawComponent drawComponent) 235 { 236 if (getMap().containsKey(drawComponent)) { 237 getMap().remove(drawComponent); 238 if (isHorizontal()) { 239 drawComponent.setHorizontalGuide(null); 240 } else { 241 drawComponent.setVerticalGuide(null); 242 } 243 listeners.firePropertyChange(PROPERTY_CHILDREN, null, drawComponent); 244 } 245 } 246 247 252 public int getAlignment(DrawComponent drawComponent) 253 { 254 if (getMap().get(drawComponent) != null) 255 return ((Integer )getMap().get(drawComponent)).intValue(); 256 return -2; 257 } 258 259 264 public void addPropertyChangeListener(PropertyChangeListener listener) { 265 listeners.addPropertyChangeListener(listener); 266 } 267 268 273 public void removePropertyChangeListener(PropertyChangeListener listener) { 274 listeners.removePropertyChangeListener(listener); 275 } 276 277 282 public String toString() 283 { 284 StringBuffer result = new StringBuffer (super.toString()); 285 result.append(" (horizontal: "); 286 result.append(horizontal); 287 result.append(", position: "); 288 result.append(position); 289 result.append(", map: "); 290 result.append(map); 291 result.append(", listeners: "); 292 result.append(')'); 293 return result.toString(); 294 } 295 296 } | Popular Tags |