1 7 package com.nightlabs.editor2d.impl; 8 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 import com.nightlabs.editor2d.DrawComponent; 15 import com.nightlabs.editor2d.EditorRuler; 16 import com.nightlabs.editor2d.Layer; 17 import com.nightlabs.editor2d.MultiLayerDrawComponent; 18 import com.nightlabs.editor2d.render.RenderModeManager; 19 import com.nightlabs.i18n.I18nText; 20 21 public class MultiLayerDrawComponentImpl 22 extends DrawComponentContainerImpl 23 implements MultiLayerDrawComponent 24 { 25 33 protected Layer currentLayer = null; 34 35 43 protected static final boolean GRID_ENABLED_EDEFAULT = false; 44 45 53 protected boolean gridEnabled = GRID_ENABLED_EDEFAULT; 54 55 63 protected static final boolean RULERS_ENABLED_EDEFAULT = false; 64 65 73 protected boolean rulersEnabled = RULERS_ENABLED_EDEFAULT; 74 75 83 protected static final boolean SNAP_TO_GEOMETRY_EDEFAULT = false; 84 85 93 protected boolean snapToGeometry = SNAP_TO_GEOMETRY_EDEFAULT; 94 95 103 protected static final double ZOOM_EDEFAULT = 0.0; 104 105 113 protected double zoom = ZOOM_EDEFAULT; 114 115 123 protected EditorRuler leftRuler = null; 124 125 133 protected EditorRuler topRuler = null; 134 135 143 protected static final long LAST_ID_EDEFAULT = 0L; 144 145 153 protected long lastID = LAST_ID_EDEFAULT; 154 155 163 protected static final Map CLASS2_DRAW_COMPONENTS_EDEFAULT = null; 164 165 173 protected transient Map class2DrawComponents = CLASS2_DRAW_COMPONENTS_EDEFAULT; 174 175 180 public MultiLayerDrawComponentImpl() { 181 super(); 182 setRenderModeManager(new RenderModeManager()); 184 } 185 186 191 public Layer getCurrentLayer() { 192 return currentLayer; 193 } 194 195 200 public void setCurrentLayer(Layer newCurrentLayer) 201 { 202 Layer oldCurrentLayer = currentLayer; 203 currentLayer = newCurrentLayer; 204 firePropertyChange(PROP_CURRENT_LAYER, oldCurrentLayer, currentLayer); 205 } 206 207 212 public boolean isGridEnabled() { 213 return gridEnabled; 214 } 215 216 221 public void setGridEnabled(boolean newGridEnabled) { 222 gridEnabled = newGridEnabled; 223 } 224 225 230 public boolean isRulersEnabled() { 231 return rulersEnabled; 232 } 233 234 239 public void setRulersEnabled(boolean newRulersEnabled) { 240 rulersEnabled = newRulersEnabled; 241 } 242 243 248 public boolean isSnapToGeometry() { 249 return snapToGeometry; 250 } 251 252 257 public void setSnapToGeometry(boolean newSnapToGeometry) { 258 snapToGeometry = newSnapToGeometry; 259 } 260 261 266 public double getZoom() { 267 return zoom; 268 } 269 270 275 public void setZoom(double newZoom) { 276 zoom = newZoom; 277 } 278 279 284 public EditorRuler getLeftRuler() { 285 return leftRuler; 286 } 287 288 293 public void setLeftRuler(EditorRuler newLeftRuler) { 294 leftRuler = newLeftRuler; 295 } 296 297 302 public EditorRuler getTopRuler() { 303 return topRuler; 304 } 305 306 311 public void setTopRuler(EditorRuler newTopRuler) { 312 topRuler = newTopRuler; 313 } 314 315 320 public long getLastID() { 321 return lastID; 322 } 323 324 329 public void setLastID(long newLastID) { 330 lastID = newLastID; 331 } 332 333 338 public Map getClass2DrawComponents() 339 { 340 if (class2DrawComponents == null) 341 class2DrawComponents = new HashMap (); 342 343 return class2DrawComponents; 344 } 345 346 351 public void setClass2DrawComponents(Map newClass2DrawComponents) { 352 class2DrawComponents = newClass2DrawComponents; 353 } 354 355 360 public Map getId2DrawComponent() 361 { 362 if (id2DrawComponent == null) 363 id2DrawComponent = new HashMap (); 364 365 return id2DrawComponent; 366 } 367 368 373 public void setId2DrawComponent(Map newId2DrawComponent) { 374 id2DrawComponent = newId2DrawComponent; 375 } 376 377 382 public long nextID() { 383 return ++lastID; 384 } 385 386 387 392 public DrawComponent getDrawComponent(long id) { 393 return (DrawComponent) getId2DrawComponent().get(new Long (id)); 394 } 395 396 public void registerDrawComponent(DrawComponent drawComponent) 397 { 398 lastID = nextID(); 399 drawComponent.setId(lastID); 400 getId2DrawComponent().put(new Long (lastID), drawComponent); 401 402 Class dcClass = drawComponent.getClass(); 405 Collection typeCollection = null; 406 if (!getClass2DrawComponents().containsKey(dcClass)) 407 { 408 typeCollection = new ArrayList (); 409 typeCollection.add(drawComponent); 410 getClass2DrawComponents().put(dcClass, typeCollection); 411 } 413 else if (getClass2DrawComponents().containsKey(dcClass)) 414 { 415 typeCollection = (Collection ) getClass2DrawComponents().get(dcClass); 416 typeCollection.add(drawComponent); 417 } 418 419 if (drawComponent.getName() == I18nText.DEFAULT_LANGUAGEID && typeCollection != null) { 421 drawComponent.setName(drawComponent.getTypeName() + " " + typeCollection.size()); 422 } 423 } 424 425 public void unregisterDrawComponent(DrawComponent drawComponent) 426 { 427 getId2DrawComponent().remove(new Long (drawComponent.getId())); 428 429 Class dcClass = drawComponent.getClass(); 430 if (getClass2DrawComponents().containsKey(dcClass)) { 431 Collection typeCollection = (Collection ) getClass2DrawComponents().get(dcClass); 432 typeCollection.remove(drawComponent); 433 } 434 } 435 436 public void unregisterDrawComponent(long id) 437 { 438 DrawComponent dc = (DrawComponent) getId2DrawComponent().remove(new Long (id)); 439 440 Class dcClass = dc.getClass(); 441 if (getClass2DrawComponents().containsKey(dcClass)) { 442 Collection typeCollection = (Collection ) getClass2DrawComponents().get(dcClass); 443 typeCollection.remove(dc); 444 } 445 } 446 447 452 public String toString() 453 { 454 StringBuffer result = new StringBuffer (super.toString()); 455 result.append(" (gridEnabled: "); 456 result.append(gridEnabled); 457 result.append(", rulersEnabled: "); 458 result.append(rulersEnabled); 459 result.append(", snapToGeometry: "); 460 result.append(snapToGeometry); 461 result.append(", zoom: "); 462 result.append(zoom); 463 result.append(", lastID: "); 464 result.append(lastID); 465 result.append(", class2DrawComponents: "); 466 result.append(class2DrawComponents); 467 result.append(", id2DrawComponent: "); 468 result.append(id2DrawComponent); 469 result.append(')'); 470 return result.toString(); 471 } 472 473 protected transient Map id2DrawComponent = null; 474 475 public Collection getDrawComponents(Class type) 476 { 477 if (getClass2DrawComponents().containsKey(type)) { 478 return (Collection ) getClass2DrawComponents().get(type); 479 } 480 return null; 481 } 482 483 } | Popular Tags |