1 26 27 package org.nightlabs.editor2d.impl; 28 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import org.nightlabs.editor2d.DrawComponent; 37 import org.nightlabs.editor2d.EditorRuler; 38 import org.nightlabs.editor2d.Layer; 39 import org.nightlabs.editor2d.MultiLayerDrawComponent; 40 import org.nightlabs.editor2d.render.RenderModeListener; 41 import org.nightlabs.editor2d.render.RenderModeManager; 42 43 public class MultiLayerDrawComponentImpl 44 extends DrawComponentContainerImpl 45 implements MultiLayerDrawComponent 46 { 47 protected static final boolean GRID_ENABLED_EDEFAULT = false; 48 protected boolean gridEnabled = GRID_ENABLED_EDEFAULT; 49 50 protected static final boolean RULERS_ENABLED_EDEFAULT = false; 51 protected boolean rulersEnabled = RULERS_ENABLED_EDEFAULT; 52 53 protected static final boolean SNAP_TO_GEOMETRY_EDEFAULT = false; 54 protected boolean snapToGeometry = SNAP_TO_GEOMETRY_EDEFAULT; 55 56 protected static final double ZOOM_EDEFAULT = 1.0; 57 protected double zoom = ZOOM_EDEFAULT; 58 59 protected EditorRuler leftRuler = null; 60 protected EditorRuler topRuler = null; 61 62 protected static final long LAST_ID_EDEFAULT = 0L; 63 protected long lastID = LAST_ID_EDEFAULT; 64 65 protected static final Map CLASS2_DRAW_COMPONENTS_EDEFAULT = null; 66 protected transient Map class2DrawComponents = CLASS2_DRAW_COMPONENTS_EDEFAULT; 67 68 public MultiLayerDrawComponentImpl() { 69 super(); 70 setRenderModeManager(new RenderModeManager()); 72 } 73 74 protected Layer currentLayer = null; 75 76 79 public Layer getCurrentLayer() { 80 return currentLayer; 81 } 82 83 86 public void setCurrentLayer(Layer newCurrentLayer) 87 { 88 Layer oldCurrentLayer = currentLayer; 89 currentLayer = newCurrentLayer; 90 firePropertyChange(PROP_CURRENT_LAYER, oldCurrentLayer, currentLayer); 91 } 92 93 96 public boolean isGridEnabled() { 97 return gridEnabled; 98 } 99 100 103 public void setGridEnabled(boolean newGridEnabled) { 104 gridEnabled = newGridEnabled; 105 } 106 107 110 public boolean isRulersEnabled() { 111 return rulersEnabled; 112 } 113 114 119 public void setRulersEnabled(boolean newRulersEnabled) { 120 rulersEnabled = newRulersEnabled; 121 } 122 123 128 public boolean isSnapToGeometry() { 129 return snapToGeometry; 130 } 131 132 137 public void setSnapToGeometry(boolean newSnapToGeometry) { 138 snapToGeometry = newSnapToGeometry; 139 } 140 141 146 public double getZoom() { 147 return zoom; 148 } 149 150 155 public void setZoom(double newZoom) { 156 zoom = newZoom; 157 } 158 159 164 public EditorRuler getLeftRuler() { 165 return leftRuler; 166 } 167 168 173 public void setLeftRuler(EditorRuler newLeftRuler) { 174 leftRuler = newLeftRuler; 175 } 176 177 182 public EditorRuler getTopRuler() { 183 return topRuler; 184 } 185 186 191 public void setTopRuler(EditorRuler newTopRuler) { 192 topRuler = newTopRuler; 193 } 194 195 200 public long getLastID() { 201 return lastID; 202 } 203 204 209 public void setLastID(long newLastID) { 210 lastID = newLastID; 211 } 212 213 218 public Map getClass2DrawComponents() 219 { 220 if (class2DrawComponents == null) 221 class2DrawComponents = new HashMap (); 222 223 return class2DrawComponents; 224 } 225 226 231 public void setClass2DrawComponents(Map newClass2DrawComponents) { 232 class2DrawComponents = newClass2DrawComponents; 233 } 234 235 240 public Map getId2DrawComponent() 241 { 242 if (id2DrawComponent == null) 243 id2DrawComponent = new HashMap (); 244 245 return id2DrawComponent; 246 } 247 248 253 public void setId2DrawComponent(Map newId2DrawComponent) { 254 id2DrawComponent = newId2DrawComponent; 255 } 256 257 262 public long nextID() { 263 return ++lastID; 264 } 265 266 267 272 public DrawComponent getDrawComponent(long id) { 273 return (DrawComponent) getId2DrawComponent().get(new Long (id)); 274 } 275 276 public void registerDrawComponent(DrawComponent drawComponent) 277 { 278 if (drawComponent != null) 279 { 280 if (drawComponent.getId() == DrawComponent.ID_EDEFAULT) 282 { 283 lastID = nextID(); 284 drawComponent.setId(lastID); 285 getId2DrawComponent().put(new Long (lastID), drawComponent); 286 287 Class dcClass = drawComponent.getClass(); 288 List typeList = null; 289 if (!getClass2DrawComponents().containsKey(dcClass)) 290 { 291 typeList = new ArrayList (); 292 typeList.add(drawComponent); 293 getClass2DrawComponents().put(dcClass, typeList); 294 getPropertyChangeSupport().firePropertyChange(TYPE_ADDED, null, dcClass); 295 } 296 else if (getClass2DrawComponents().containsKey(dcClass)) 297 { 298 typeList = (List ) getClass2DrawComponents().get(dcClass); 299 typeList.add(drawComponent); 300 } 301 302 if (drawComponent.getName() == "" && typeList != null) { 303 drawComponent.setName(drawComponent.getTypeName() + " " + typeList.size()); 304 } 305 306 drawComponent.setRenderMode(getRenderModeManager().getCurrentRenderMode()); 307 } 308 } 309 } 310 311 public void unregisterDrawComponent(DrawComponent drawComponent) 312 { 313 getId2DrawComponent().remove(new Long (drawComponent.getId())); 314 315 Class dcClass = drawComponent.getClass(); 316 if (getClass2DrawComponents().containsKey(dcClass)) { 317 Collection typeCollection = (Collection ) getClass2DrawComponents().get(dcClass); 318 typeCollection.remove(drawComponent); 319 } 320 } 321 322 327 public void unregisterDrawComponent(long id) 328 { 329 DrawComponent dc = (DrawComponent) getId2DrawComponent().remove(new Long (id)); 330 331 Class dcClass = dc.getClass(); 332 if (getClass2DrawComponents().containsKey(dcClass)) { 333 Collection typeCollection = (Collection ) getClass2DrawComponents().get(dcClass); 334 typeCollection.remove(dc); 335 } 336 } 337 338 343 public String toString() 344 { 345 StringBuffer result = new StringBuffer (super.toString()); 346 result.append(" (gridEnabled: "); 347 result.append(gridEnabled); 348 result.append(", rulersEnabled: "); 349 result.append(rulersEnabled); 350 result.append(", snapToGeometry: "); 351 result.append(snapToGeometry); 352 result.append(", zoom: "); 353 result.append(zoom); 354 result.append(", lastID: "); 355 result.append(lastID); 356 result.append(", class2DrawComponents: "); 357 result.append(class2DrawComponents); 358 result.append(", id2DrawComponent: "); 359 result.append(id2DrawComponent); 360 result.append(')'); 361 return result.toString(); 362 } 363 364 protected transient Map id2DrawComponent = null; 365 protected static final List EMPTY_LIST = new LinkedList (); 366 367 public List getDrawComponents(Class type) 368 { 369 if (getClass2DrawComponents().containsKey(type)) { 370 List types = (List ) getClass2DrawComponents().get(type); 371 if (types != null) 372 return types; 373 else 374 return EMPTY_LIST; 375 } 376 return EMPTY_LIST; 377 } 378 379 protected RenderModeListener renderModeListener = new RenderModeListener() 380 { 381 public void renderModeChanges(int renderMode) 382 { 383 setRenderMode(renderMode); 384 } 385 }; 386 387 public void setRenderModeManager(RenderModeManager man) 388 { 389 if (renderModeManager != null) 390 renderModeManager.removeRenderModeListener(renderModeListener); 391 392 this.renderModeManager = man; 393 renderModeManager.addRenderModeListener(renderModeListener); 394 super.setRenderModeManager(man); 395 } 396 397 } | Popular Tags |