1 7 package com.nightlabs.editor2d.impl; 8 9 import java.awt.Point ; 10 import java.awt.Rectangle ; 11 import java.awt.geom.AffineTransform ; 12 import java.beans.PropertyChangeListener ; 13 import java.beans.PropertyChangeSupport ; 14 15 import com.nightlabs.editor2d.DrawComponent; 16 import com.nightlabs.editor2d.DrawComponentContainer; 17 import com.nightlabs.editor2d.EditorGuide; 18 import com.nightlabs.editor2d.MultiLayerDrawComponent; 19 import com.nightlabs.editor2d.render.RenderModeManager; 20 import com.nightlabs.editor2d.render.Renderer; 21 import com.nightlabs.editor2d.util.EditorModelUtil; 22 import com.nightlabs.i18n.I18nText; 23 import com.nightlabs.i18n.I18nTextBuffer; 24 25 public class DrawComponentImpl 26 implements DrawComponent 27 { 28 public static final long ID_EDEFAULT = 0L; 30 public static final int X_EDEFAULT = 0; 31 public static final int Y_EDEFAULT = 0; 32 public static final int WIDTH_EDEFAULT = 0; 33 public static final int HEIGHT_EDEFAULT = 0; 34 public static final Rectangle BOUNDS_EDEFAULT = new Rectangle (X_EDEFAULT, Y_EDEFAULT, WIDTH_EDEFAULT, HEIGHT_EDEFAULT); 35 public static final double ROTATION_EDEFAULT = 0.0; 36 public static final int TMP_ROTATION_X_EDEFAULT = ROTATION_X_DEFAULT; 37 public static final int TMP_ROTATION_Y_EDEFAULT = ROTATION_Y_DEFAULT; 38 public static final int RENDER_MODE_EDEFAULT = RenderModeManager.DEFAULT_MODE; 39 40 protected long id = ID_EDEFAULT; 41 protected I18nText name = new I18nTextBuffer(); 42 protected int x = X_EDEFAULT; 43 protected int y = Y_EDEFAULT; 44 protected int width = WIDTH_EDEFAULT; 45 protected int height = HEIGHT_EDEFAULT; 46 protected double rotation = ROTATION_EDEFAULT; 47 protected EditorGuide horizontalGuide = null; 48 protected EditorGuide verticalGuide = null; 49 protected int rotationX = ROTATION_X_DEFAULT; 50 protected int rotationY = ROTATION_Y_DEFAULT; 51 protected transient Rectangle bounds = BOUNDS_EDEFAULT; 52 protected AffineTransform affineTransform = new AffineTransform (); 53 protected transient int tmpRotationX = TMP_ROTATION_X_EDEFAULT; 54 protected transient int tmpRotationY = TMP_ROTATION_Y_EDEFAULT; 55 56 protected transient PropertyChangeSupport listeners = null; 57 protected PropertyChangeSupport getPropertyChangeSupport() 58 { 59 if (listeners == null) 60 listeners = new PropertyChangeSupport (this); 61 return listeners; 62 } 63 public void addPropertyChangeListener(PropertyChangeListener l){ 64 getPropertyChangeSupport().addPropertyChangeListener(l); 65 } 66 public void removePropertyChangeListener(PropertyChangeListener l){ 67 getPropertyChangeSupport().removePropertyChangeListener(l); 68 } 69 protected void firePropertyChange(String prop, Object oldValue, Object newValue){ 70 getPropertyChangeSupport().firePropertyChange(prop, oldValue, newValue); 71 } 72 protected void firePropertyChange(String prop, int oldValue, int newValue){ 73 getPropertyChangeSupport().firePropertyChange(prop, oldValue, newValue); 74 } 75 protected void firePropertyChange(String prop, double oldValue, double newValue){ 76 getPropertyChangeSupport().firePropertyChange(prop, new Double (oldValue), new Double (newValue)); 77 } 78 protected void firePropertyChange(String prop, boolean oldValue, boolean newValue){ 79 getPropertyChangeSupport().firePropertyChange(prop, oldValue, newValue); 80 } 81 82 public DrawComponentImpl() { 83 super(); 84 } 85 86 91 public long getId() { 92 return id; 93 } 94 95 98 public void setId(long newId) { 99 long oldId = id; 100 id = newId; 101 firePropertyChange(PROP_ID, new Long (oldId), new Long (newId)); 102 } 103 104 109 public String getName() { 110 return name.getText(getLanguageId()); 111 } 112 113 118 public void setName(String newName) { 119 String oldName = name.getText(getLanguageId()); 120 name.setText(getLanguageId(), newName); 121 firePropertyChange(PROP_NAME, oldName, newName); 122 } 123 124 protected transient String languageId = DEFAULT_LANGUAGE_ID; 125 public String getLanguageId() { 126 return languageId; 127 } 128 public void setLanguageId(String newLanguageId) { 129 String oldLanguageId = languageId; 130 primSetLanguageId(newLanguageId); 131 firePropertyChange(PROP_LANGUAGE_ID, oldLanguageId, languageId); 132 } 133 protected void primSetLanguageId(String newLanguageId) { 134 languageId = newLanguageId; 135 } 136 137 public I18nText getI18nText() { 138 return name; 139 } 140 public void setI18nText(I18nText text) { 141 this.name = text; 142 firePropertyChange(PROP_NAME, null, name); 143 } 144 145 150 public int getX() { 151 return getBounds().x; 152 } 153 154 159 public void setX(int newX) 160 { 161 int oldX = x; 162 x = newX; 163 primSetX(x); 164 firePropertyChange(PROP_X, oldX, x); 165 } 166 167 protected void primSetX(int x) 168 { 169 if (x == getBounds().x) 170 return; 171 172 setLocation(x, getY()); 173 } 174 175 180 public int getY() { 181 return getBounds().y; 182 } 183 184 189 public void setY(int newY) 190 { 191 int oldY = y; 192 y = newY; 193 194 primSetY(y); 195 196 firePropertyChange(PROP_Y, oldY, y); 197 } 198 199 protected void primSetY(int y) 200 { 201 if (y == getBounds().y) 202 return; 203 204 setLocation(getX(), y); 205 } 206 207 212 public int getWidth() { 213 return getBounds().width; 214 } 215 216 221 public void setWidth(int newWidth) 222 { 223 int oldWidth = width; 224 width = newWidth; 225 226 primSetWidth(width); 227 228 firePropertyChange(PROP_Y, oldWidth, width); 229 } 230 231 protected void primSetWidth(float width) 232 { 233 if (width == getBounds().width) 234 return; 235 236 setSize((int)width, getHeight()); 237 } 238 239 244 public int getHeight() { 245 return getBounds().height; 246 } 247 248 253 public void setHeight(int newHeight) 254 { 255 int oldHeight = height; 256 height = newHeight; 257 258 primSetHeight(height); 259 260 firePropertyChange(PROP_Y, oldHeight, height); 261 } 262 263 protected void primSetHeight(float height) 264 { 265 if (height == getBounds().height) 266 return; 267 268 setSize(getWidth(), (int)height); 269 } 270 271 276 public double getRotation() { 277 return rotation; 278 } 279 280 285 public void setRotation(double newRotation) 286 { 287 double oldRotation = rotation; 288 primSetRotation(newRotation); 289 290 firePropertyChange(PROP_Y, oldRotation, rotation); 291 } 292 293 protected transient double rotationLimit = 360.0d; 294 protected void primSetRotation(double newRotation) 295 { 296 double oldRotation = rotation; 297 rotation = EditorModelUtil.getConstrainedValue(newRotation, rotationLimit); 298 299 if (oldRotation == newRotation) 300 return; 301 302 double degreesToRotateInRadians; 303 double degreesToRotate = EditorModelUtil.calcDiffRotation(rotation, oldRotation); 304 degreesToRotateInRadians = Math.toRadians(degreesToRotate); 305 306 if (degreesToRotate != 0) 307 { 308 atUtil.setToIdentity(); 309 if (tmpRotationX != TMP_ROTATION_X_EDEFAULT && tmpRotationY != TMP_ROTATION_Y_EDEFAULT) 310 atUtil.rotate(degreesToRotateInRadians, getTmpRotationX(), getTmpRotationY()); 311 else 312 atUtil.rotate(degreesToRotateInRadians, getRotationX(), getRotationY()); 313 transform(atUtil); 314 } 315 } 316 317 322 public EditorGuide getHorizontalGuide() { 323 return horizontalGuide; 324 } 325 326 331 public void setHorizontalGuide(EditorGuide newHorizontalGuide) 332 { 333 EditorGuide old = horizontalGuide; 334 horizontalGuide = newHorizontalGuide; 335 firePropertyChange(PROP_HORIZONTAL_GUIDE, old, horizontalGuide); 336 } 337 338 343 public EditorGuide getVerticalGuide() { 344 return verticalGuide; 345 } 346 347 352 public void setVerticalGuide(EditorGuide newVerticalGuide) 353 { 354 EditorGuide old = verticalGuide; 355 verticalGuide = newVerticalGuide; 356 firePropertyChange(PROP_VERTICAL_GUIDE, old, verticalGuide); 357 } 358 359 364 public int getRotationX() 365 { 366 if (rotationX == ROTATION_X_DEFAULT) 367 return (int) getBounds().getCenterX(); 368 369 return rotationX; 370 } 371 372 377 public void setRotationX(int newRotationX) 378 { 379 int oldRotationX = rotationX; 380 primSetRotationX(newRotationX); 381 firePropertyChange(PROP_ROTATION_X, oldRotationX, rotationX); 382 } 383 384 protected void primSetRotationX(int newRotationX) { 385 rotationX = newRotationX; 386 } 387 388 393 public int getRotationY() 394 { 395 if (rotationY == ROTATION_Y_DEFAULT) 396 return (int) getBounds().getCenterY(); 397 398 return rotationY; 399 } 400 401 406 public void setRotationY(int newRotationY) 407 { 408 int oldRotationY = rotationY; 409 primSetRotationY(newRotationY); 410 firePropertyChange(PROP_ROTATION_X, oldRotationY, rotationY); 411 } 412 413 protected void primSetRotationY(int newRotationY) { 414 rotationY = newRotationY; 415 } 416 417 422 public Rectangle getBounds() 423 { 424 if (bounds == null) { 425 bounds = new Rectangle (x, y, width, height); 426 } 427 return bounds; 428 } 429 430 435 public void setBounds(Rectangle newBounds) 436 { 437 Rectangle oldBounds = bounds; 438 primSetBounds(newBounds); 439 440 bounds = newBounds; 441 x = bounds.x; 442 y = bounds.y; 443 width = bounds.width; 444 height = bounds.height; 445 446 firePropertyChange(PROP_BOUNDS, oldBounds, bounds); 447 } 448 449 454 public AffineTransform getAffineTransform() { 455 return affineTransform; 456 } 457 458 463 public void setAffineTransform(AffineTransform newAffineTransform) 464 { 465 } 469 470 475 public int getTmpRotationX() { 476 return tmpRotationX; 477 } 478 479 484 public void setTmpRotationX(int newTmpRotationX) { 485 int oldTmpRotationX = tmpRotationX; 486 tmpRotationX = newTmpRotationX; 487 firePropertyChange(PROP_TMP_ROTATION_X, oldTmpRotationX, tmpRotationX); 488 } 489 490 495 public int getTmpRotationY() { 496 return tmpRotationY; 497 } 498 499 504 public void setTmpRotationY(int newTmpRotationY) { 505 int oldTmpRotationY = tmpRotationY; 506 tmpRotationY = newTmpRotationY; 507 firePropertyChange(PROP_TMP_ROTATION_Y, oldTmpRotationY, tmpRotationY); 508 } 509 510 protected void primSetBounds(Rectangle newBounds) 511 { 512 if (getBounds().equals(newBounds)) 513 return; 514 515 if (newBounds.x == getX() && newBounds.y == getY()) { 516 setSize(newBounds.width, newBounds.height); 517 return; 518 } 519 520 if (newBounds.width == getWidth() && newBounds.height == getHeight()) { 521 setLocation(newBounds.x, newBounds.y); 522 return; 523 } 524 525 setSize(newBounds.width, newBounds.height); 526 setLocation(newBounds.x, newBounds.y); 527 } 528 529 public void setLocation(int newX, int newY) 530 { 531 atUtil.setToIdentity(); 532 atUtil.translate((float)newX - (float)getX(), (float)newY - (float)getY()); 533 534 transformRotationCenter(atUtil); 535 transform(atUtil); 536 } 537 538 protected void setSize(int newWidth, int newHeight) 539 { 540 atUtil.setToIdentity(); 541 float ratioY = (float)newHeight / (float)getHeight(); 542 ratioY = EditorModelUtil.checkFactor(ratioY); 543 float ratioX = (float)newWidth / (float)getWidth(); 544 ratioX = EditorModelUtil.checkFactor(ratioX); 545 float distanceX = (float)getX() - ((float)getX()*ratioX); 546 float distanceY = (float)getY() - ((float)getY()*ratioY); 547 atUtil.translate(distanceX, distanceY); 548 atUtil.scale(ratioX, ratioY); 549 550 transformRotationCenter(atUtil); 551 transform(atUtil); 552 } 553 554 protected void transformRotationCenter(AffineTransform at) 555 { 556 if (rotationX != ROTATION_X_DEFAULT || rotationY != ROTATION_Y_DEFAULT) { 557 Point rotationCenter = new Point (getRotationX(), getRotationY()); 558 Point newRotationCenter = new Point (); 559 at.transform(rotationCenter, newRotationCenter); 560 rotationX = newRotationCenter.x; 561 rotationY = newRotationCenter.y; 562 } 563 } 564 565 protected final AffineTransform atUtil = new AffineTransform (); 566 567 572 public void transform(AffineTransform newAT) 573 { 574 AffineTransform oldAT = affineTransform; 575 affineTransform.preConcatenate(newAT); 576 bounds = null; 577 578 } 580 581 586 public String toString() 587 { 588 StringBuffer result = new StringBuffer (super.toString()); 589 result.append(" (id: "); 590 result.append(id); 591 result.append(", name: "); 592 result.append(name); 593 result.append(", x: "); 594 result.append(x); 595 result.append(", y: "); 596 result.append(y); 597 result.append(", width: "); 598 result.append(width); 599 result.append(", height: "); 600 result.append(height); 601 result.append(", rotation: "); 602 result.append(rotation); 603 result.append(", rotationX: "); 604 result.append(rotationX); 605 result.append(", rotationY: "); 606 result.append(rotationY); 607 result.append(", bounds: "); 608 result.append(bounds); 609 result.append(", affineTransform: "); 610 result.append(affineTransform); 611 result.append(", tmpRotationX: "); 612 result.append(tmpRotationX); 613 result.append(", tmpRotationY: "); 614 result.append(tmpRotationY); 615 result.append(", renderMode: "); 616 result.append(renderMode); 617 result.append(')'); 618 return result.toString(); 619 } 620 621 protected int renderMode = RENDER_MODE_EDEFAULT; 622 623 631 protected DrawComponentContainer parent = null; 632 633 public void setRenderMode(int mode) 634 { 635 int oldRenderMode = renderMode; 636 primSetRenderMode(mode); 637 firePropertyChange(PROP_RENDER_MODE, oldRenderMode, renderMode); 638 } 639 640 645 public DrawComponentContainer getParent() { 646 return parent; 647 } 648 649 654 public void setParent(DrawComponentContainer newParent) 655 { 656 DrawComponentContainer oldParent = parent; 657 primSetParent(newParent); 658 firePropertyChange(PROP_PARENT, oldParent, parent); 659 } 660 661 protected void primSetParent(DrawComponentContainer newParent) { 662 parent = newParent; 663 } 664 665 public int getRenderMode() { 666 return renderMode; 667 } 668 669 protected void primSetRenderMode(int mode) 670 { 671 renderMode = mode; 672 if (getRenderModeManager() != null) 673 renderer = getRenderModeManager().getRenderer(renderMode, getRenderModeClass()); 674 } 675 676 protected transient Renderer renderer; 677 public Renderer getRenderer() 678 { 679 if (renderer == null) 680 renderer = getRenderModeManager().getRenderer(getRenderMode(), getRenderModeClass()); 681 682 return renderer; 683 } 684 685 protected transient Class renderModeClass = this.getClass(); 686 public Class getRenderModeClass() { 687 return renderModeClass; 688 } 689 690 protected transient RenderModeManager renderModeManager = null; 691 public void setRenderModeManager(RenderModeManager man) { 692 renderModeManager = man; 693 } 694 public RenderModeManager getRenderModeManager() 695 { 696 if (renderModeManager == null && getRoot() != null) { 697 renderModeManager = getRoot().getRenderModeManager(); 698 } 699 return renderModeManager; 700 } 701 702 public String getTypeName() { 706 return "DrawComponent"; 707 } 708 709 public MultiLayerDrawComponent getRoot() 710 { 711 if (getParent() != null) 712 return getParent().getRoot(); 713 return null; 714 } 715 716 } | Popular Tags |