KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > impl > DrawComponentImpl


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.impl;
28
29 import java.awt.Point JavaDoc;
30 import java.awt.Rectangle JavaDoc;
31 import java.awt.geom.AffineTransform JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.beans.PropertyChangeSupport JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 import org.nightlabs.editor2d.DrawComponent;
37 import org.nightlabs.editor2d.DrawComponentContainer;
38 import org.nightlabs.editor2d.EditorGuide;
39 import org.nightlabs.editor2d.MultiLayerDrawComponent;
40 import org.nightlabs.editor2d.render.RenderModeManager;
41 import org.nightlabs.editor2d.render.Renderer;
42 import org.nightlabs.editor2d.util.EditorModelUtil;
43 import org.nightlabs.i18n.I18nText;
44 import org.nightlabs.i18n.I18nTextBuffer;
45
46 /**
47  * The Base Implementation of the Interface {@link DrawComponent}
48  *
49  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
50  */

51 public class DrawComponentImpl
52 implements DrawComponent
53 {
54     protected long id = ID_EDEFAULT;
55     protected I18nText name = new I18nTextBuffer();
56     protected int x = X_EDEFAULT;
57     protected int y = Y_EDEFAULT;
58     protected int width = WIDTH_EDEFAULT;
59     protected int height = HEIGHT_EDEFAULT;
60     protected double rotation = ROTATION_EDEFAULT;
61     protected EditorGuide horizontalGuide = null;
62     protected EditorGuide verticalGuide = null;
63     protected int rotationX = ROTATION_X_DEFAULT;
64     protected int rotationY = ROTATION_Y_DEFAULT;
65     protected transient Rectangle JavaDoc bounds = BOUNDS_EDEFAULT;
66     protected AffineTransform JavaDoc affineTransform = new AffineTransform JavaDoc();
67     protected int tmpRotationX = TMP_ROTATION_X_EDEFAULT;
68     protected int tmpRotationY = TMP_ROTATION_Y_EDEFAULT;
69
70     protected transient PropertyChangeSupport JavaDoc listeners = null;
71     protected PropertyChangeSupport JavaDoc getPropertyChangeSupport()
72     {
73         if (listeners == null)
74             listeners = new PropertyChangeSupport JavaDoc(this);
75         return listeners;
76     }
77     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l){
78         getPropertyChangeSupport().addPropertyChangeListener(l);
79     }
80     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l){
81         getPropertyChangeSupport().removePropertyChangeListener(l);
82     }
83     protected void firePropertyChange(String JavaDoc prop, Object JavaDoc oldValue, Object JavaDoc newValue){
84         getPropertyChangeSupport().firePropertyChange(prop, oldValue, newValue);
85     }
86     protected void firePropertyChange(String JavaDoc prop, int oldValue, int newValue){
87         getPropertyChangeSupport().firePropertyChange(prop, oldValue, newValue);
88     }
89     protected void firePropertyChange(String JavaDoc prop, double oldValue, double newValue){
90         getPropertyChangeSupport().firePropertyChange(prop, new Double JavaDoc(oldValue), new Double JavaDoc(newValue));
91     }
92     protected void firePropertyChange(String JavaDoc prop, boolean oldValue, boolean newValue){
93         getPropertyChangeSupport().firePropertyChange(prop, oldValue, newValue);
94     }
95         
96     public DrawComponentImpl() {
97         super();
98     }
99
100     /**
101      * return the ID of the DrawComponent
102      * @see org.nightlabs.editor2d.DrawComponent#getId()
103      */

104     public long getId() {
105         return id;
106     }
107
108     /**
109      * sets the ID and fires a PropertyChange with the PropertyName {@link DrawComponent#PROP_ID }
110      * @see org.nightlabs.editor2d.DrawComponent#setId(long)
111      */

112     public void setId(long newId) {
113         long oldId = id;
114         id = newId;
115         firePropertyChange(PROP_ID, new Long JavaDoc(oldId), new Long JavaDoc(newId));
116     }
117
118     /**
119      * returns the name based on the languageID from the I18nText
120      * @see org.nightlabs.editor2d.DrawComponent#getName()
121      */

122     public String JavaDoc getName() {
123         return name.getText(getLanguageId());
124     }
125
126     /**
127      * puts the given name into the I18nText based on currentLanguageID (getLanguageID())
128      * and fires a PropertyChange with the PropertyName {@link DrawComponent#PROP_NAME}
129      * @see org.nightlabs.editor2d.DrawComponent#setName(String)
130      */

131     public void setName(String JavaDoc newName) {
132         String JavaDoc oldName = name.getText(getLanguageId());
133         name.setText(getLanguageId(), newName);
134         firePropertyChange(PROP_NAME, oldName, newName);
135     }
136
137     /**
138      * the default languageID is Locale.ENGLISH.getLanguage()
139      */

140     protected transient String JavaDoc languageId = DEFAULT_LANGUAGE_ID;
141     
142     /**
143      * the LanguageID is formated like in java.util.Locale (DE, EN, ...)
144      *@return the current languageID of the DrawComponent
145      *@see org.nightlabs.editor2d.DrawComponent#getLanguageId()
146      */

147     public String JavaDoc getLanguageId() {
148         return languageId;
149     }
150     
151     /**
152      * sets the languageID and fires a PropertyChange with the PropertyName
153      * {@link DrawComponent#PROP_LANGUAGE_ID}
154      *
155      * @param newLanguageId the languageID to set
156      * @see org.nightlabs.editor2d.DrawComponent#setLanguageId(String)
157      */

158     public void setLanguageId(String JavaDoc newLanguageId) {
159         String JavaDoc oldLanguageId = languageId;
160         primSetLanguageId(newLanguageId);
161         firePropertyChange(PROP_LANGUAGE_ID, oldLanguageId, languageId);
162     }
163     
164     /**
165      * sets the languageID without firing a PropertyChange
166      * @param newLanguageId the languageID to set
167      */

168     protected void primSetLanguageId(String JavaDoc newLanguageId) {
169         languageId = newLanguageId;
170     }
171     
172     /**
173      * @return the I18nText which contains the names for each languageID
174      * @see org.nightlabs.editor2d.DrawComponent#getI18nText()
175      */

176     public I18nText getI18nText() {
177         return name;
178     }
179     
180     /**
181      * sets the I18nText which contains the names for each languageID and fires a propertyChange
182      * with the PropertyName {@link DrawComponent#PROP_NAME}
183      * @param text the I18nText to set
184      * @see org.nightlabs.i18n.I18nText
185      * @see org.nightlabs.editor2d.DrawComponent#setI18nText(I18nText)
186      */

187     public void setI18nText(I18nText text) {
188         this.name = text;
189         firePropertyChange(PROP_NAME, null, name);
190     }
191     
192     /**
193      * this Method is only a wrapper which internally calls getBounds().x
194      * @return the X-Coordinate in absolute Coordinates
195      * @see org.nightlabs.editor2d.DrawComponent#getX()
196      */

197     public int getX() {
198         return getBounds().x;
199     }
200
201     /**
202      * sets the X-Coordinate of the DrawComponent and fires a propertyChange
203      * with the PropertyName {@link DrawComponent#PROP_X}
204      * @param newX the X-Coordinate to set in absolute Coordinates
205      * @see org.nightlabs.editor2d.DrawComponent#setX(int)
206      */

207     public void setX(int newX)
208     {
209         int oldX = x;
210         x = newX;
211         primSetX(x);
212         firePropertyChange(PROP_X, oldX, x);
213     }
214
215     /**
216      * sets the X-Coordinate of the DrawComponent without firing a propertyChange
217      * @param x the X-Coordinate to set in absolute Coordinates
218      */

219     protected void primSetX(int x)
220     {
221         if (x == getBounds().x)
222             return;
223         setLocation(x, getY());
224     }
225     
226     /**
227      * this Method is only a wrapper which internally calls getBounds().y
228      * @return the Y-Coordinate in absolute Coordinates
229      * @see org.nightlabs.editor2d.DrawComponent#getY()
230      */

231     public int getY() {
232         return getBounds().y;
233     }
234
235     /**
236      * sets the Y-Coordinate of the DrawComponent and fires a propertyChange
237      * @param newY the Y-Coordinate to set in absolute Coordinates
238      * @see org.nightlabs.editor2d.DrawComponent#setY(int)
239      */

240     public void setY(int newY)
241     {
242         int oldY = y;
243         y = newY;
244         primSetY(y);
245         firePropertyChange(PROP_Y, oldY, y);
246     }
247
248     /**
249      * sets the Y-Coordinate of the DrawComponent without firing a propertyChange
250      * @param y the Y-Coordinate to set in absolute Coordinates
251      */

252     protected void primSetY(int y)
253     {
254         if (y == getBounds().y)
255             return;
256         setLocation(getX(), y);
257     }
258     
259     /**
260      * this Method is only a wrapper which internally calls getBounds().width
261      * @return the Width in absolute Coordinates
262      * @see org.nightlabs.editor2d.DrawComponent#getWidth()
263      */

264     public int getWidth() {
265         return getBounds().width;
266     }
267
268     /**
269      * sets the Width of the DrawComponent and fires a propertyChange
270      * @param newWidth the Width to set in absolute Coordinates
271      * @see org.nightlabs.editor2d.DrawComponent#setWidth(int)
272      */

273     public void setWidth(int newWidth)
274     {
275         int oldWidth = width;
276         width = newWidth;
277         primSetWidth(width);
278         firePropertyChange(PROP_Y, oldWidth, width);
279     }
280
281     /**
282      * sets the Width of the DrawComponent without firing a propertyChange
283      * @param width the Width to set in absolute Coordinates
284      */

285     protected void primSetWidth(float width)
286     {
287         if (width == getBounds().width)
288             return;
289         setSize((int)width, getHeight());
290     }
291     
292     /**
293      * this Method is only a wrapper which internally calls getBounds().height
294      * @return the Height in absolute Coordinates
295      * @see org.nightlabs.editor2d.DrawComponent#getHeight()
296      */

297     public int getHeight() {
298         return getBounds().height;
299     }
300
301     /**
302      * sets the Height of the DrawComponent and fires a propertyChange
303      * @param newHeight the Height to set in absolute Coordinates
304      * @see org.nightlabs.editor2d.DrawComponent#setHeight(int)
305      */

306     public void setHeight(int newHeight)
307     {
308         int oldHeight = height;
309         height = newHeight;
310         primSetHeight(height);
311         firePropertyChange(PROP_Y, oldHeight, height);
312     }
313
314     /**
315      * sets the Height of the DrawComponent without firing a propertyChange
316      * @param height the Height to set in absolute Coordinates
317      */

318     protected void primSetHeight(float height)
319     {
320         if (height == getBounds().height)
321             return;
322         setSize(getWidth(), (int)height);
323     }
324     
325     /**
326      * @return the rotation of the DrawComponent in degrees
327      * @see org.nightlabs.editor2d.DrawComponent#getRotation()
328      */

329     public double getRotation() {
330         return rotation;
331     }
332
333     /**
334      * sets the Rotation of the DrawComponent in degrees and fires a propertyChange
335      * with the PropertyName DrawComponent.PROP_ROTATION
336      *
337      * @param newRotation the roation to set in degrees
338      * @see org.nightlabs.editor2d.DrawComponent#setRotation(double)
339      */

340     public void setRotation(double newRotation)
341     {
342         double oldRotation = rotation;
343         primSetRotation(newRotation);
344         firePropertyChange(PROP_Y, oldRotation, rotation);
345     }
346  
347     /**
348      * determines the max/min rotation.
349      * Each rotation above/under this limit is minimzied by this value
350      */

351     protected static transient double rotationLimit = 360.0d;
352     
353     /**
354      * sets the Rotation of the DrawComponent in degrees without firing a propertyChange
355      * @param newRotation the roation to set in degrees
356      */

357     protected void primSetRotation(double newRotation)
358     {
359         double oldRotation = rotation;
360         rotation = EditorModelUtil.getConstrainedValue(newRotation, rotationLimit);
361         
362         if (oldRotation == newRotation)
363             return;
364         
365         double degreesToRotateInRadians;
366         double degreesToRotate = EditorModelUtil.calcDiffRotation(rotation, oldRotation);
367         degreesToRotateInRadians = Math.toRadians(degreesToRotate);
368                 
369         if (degreesToRotate != 0)
370         {
371             atUtil.setToIdentity();
372             if (tmpRotationX != TMP_ROTATION_X_EDEFAULT && tmpRotationY != TMP_ROTATION_Y_EDEFAULT)
373                 atUtil.rotate(degreesToRotateInRadians, getTmpRotationX(), getTmpRotationY());
374             else
375                 atUtil.rotate(degreesToRotateInRadians, getRotationX(), getRotationY());
376             transform(atUtil);
377         }
378     }
379
380     /**
381      * @see org.nightlabs.editor2d.DrawComponent#getHorizontalGuide()
382      */

383     public EditorGuide getHorizontalGuide() {
384         return horizontalGuide;
385     }
386
387     /**
388      * sets the Horizontal EditorGuide and fires a propertyChange with the PropertyName
389      * {@link DrawComponent#PROP_HORIZONTAL_GUIDE}
390      * @see org.nightlabs.editor2d.DrawComponent#setHorizontalGuide(EditorGuide)
391      * @see org.nightlabs.editor2d.EditorGuide
392      */

393     public void setHorizontalGuide(EditorGuide newHorizontalGuide)
394     {
395         EditorGuide old = horizontalGuide;
396         horizontalGuide = newHorizontalGuide;
397         firePropertyChange(PROP_HORIZONTAL_GUIDE, old, horizontalGuide);
398     }
399
400     /**
401      * @see org.nightlabs.editor2d.DrawComponent#getVerticalGuide()
402      */

403     public EditorGuide getVerticalGuide() {
404         return verticalGuide;
405     }
406
407     /**
408      * sets the Vertical EditorGuide and fires a propertyChange with the PropertyName
409      * {@link DrawComponent#PROP_VERTICAL_GUIDE}
410      * @see org.nightlabs.editor2d.DrawComponent#setVerticalGuide(EditorGuide)
411      * @see org.nightlabs.editor2d.EditorGuide
412      */

413     public void setVerticalGuide(EditorGuide newVerticalGuide)
414     {
415         EditorGuide old = verticalGuide;
416         verticalGuide = newVerticalGuide;
417         firePropertyChange(PROP_VERTICAL_GUIDE, old, verticalGuide);
418     }
419
420     /**
421      * by default this Method always returns the X-Coordinate of the Center of the DrawComponent
422      * until the value has been set explict.
423      *
424      * @return the X-Coordinate of the RoationCenter in absolute Coordinates
425      * @see org.nightlabs.editor2d.DrawComponent#getRotationX()
426      */

427     public int getRotationX()
428     {
429         if (rotationX == ROTATION_X_DEFAULT)
430             return (int) getBounds().getCenterX();
431             
432         return rotationX;
433     }
434
435     /**
436      * sets the X-Coordinate for the RotationCenter of the DrawComponent in absolute Coordinates and
437      * fires a propertyChange with the PropertyName {@link DrawComponent#PROP_ROTATION_X}
438      * @param newRotationX the X-Coordinate of the RotationCenter to set in absolute Coordinates
439      * @see org.nightlabs.editor2d.DrawComponent#setRotationX(int)
440      */

441     public void setRotationX(int newRotationX)
442     {
443         int oldRotationX = rotationX;
444         primSetRotationX(newRotationX);
445         firePropertyChange(PROP_ROTATION_X, oldRotationX, rotationX);
446     }
447
448     /**
449      * sets the X-Coordinate for the RotationCenter of the DrawComponent in absolute Coordinates without
450      * firing a propertyChange
451      * @param newRotationX the X-Coordinate of the RotationCenter to set in absolute Coordinates
452      */

453     protected void primSetRotationX(int newRotationX) {
454         rotationX = newRotationX;
455     }
456     
457     /**
458      * by default this Method alwaya returns the Y-Coordinate of the Center of the DrawComponent
459      * until the value has been set explict.
460      *
461      * @return the Y-Coordinate of the RoationCenter in absolute Coordinates
462      * @see org.nightlabs.editor2d.DrawComponent#getRotationY()
463      */

464     public int getRotationY()
465     {
466         if (rotationY == ROTATION_Y_DEFAULT)
467             return (int) getBounds().getCenterY();
468         
469         return rotationY;
470     }
471
472     /**
473      * sets the Y-Coordinate for the RotationCenter of the DrawComponent in absolute Coordinates and
474      * fires a propertyChange with the PropertyName {@link DrawComponent#PROP_ROTATION_Y}
475      * @param newRotationY the Y-Coordinate of the RotationCenter to set in absolute Coordinates
476      * @see org.nightlabs.editor2d.DrawComponent#setRotationY(int)
477      */

478     public void setRotationY(int newRotationY)
479     {
480         int oldRotationY = rotationY;
481         primSetRotationY(newRotationY);
482         firePropertyChange(PROP_ROTATION_X, oldRotationY, rotationY);
483     }
484
485     /**
486      * sets the Y-Coordinate for the RotationCenter of the DrawComponent in absolute Coordinates without
487      * firing a propertyChange
488      * @param newRotationY the Y-Coordinate of the RotationCenter to set in absolute Coordinates
489      */

490     protected void primSetRotationY(int newRotationY) {
491         rotationY = newRotationY;
492     }
493     
494     /**
495      * returns the cached value of the bounds, which are cached until a new transformation took place
496      * if the cached value is null, a new Rectangle is returned is created from the simple members x,y,width,height
497      *
498      * @return the bounds of the DrawComponent in absolute coordinates
499      * @see org.nightlabs.editor2d.DrawComponent#getBounds()
500      */

501     public Rectangle JavaDoc getBounds()
502     {
503         if (bounds == null) {
504             bounds = new Rectangle JavaDoc(x, y, width, height);
505         }
506         return bounds;
507     }
508
509     /**
510      * sets the bounds of the DrawComponent and fires a propertyChange
511      * @param newBounds the bounds to set in absolute coordinates
512      * @see org.nightlabs.editor2d.DrawComponent#setBounds(Rectangle)
513      */

514     public void setBounds(Rectangle JavaDoc newBounds)
515     {
516         Rectangle JavaDoc oldBounds = bounds;
517         primSetBounds(newBounds);
518         
519         bounds = newBounds;
520         x = bounds.x;
521         y = bounds.y;
522         width = bounds.width;
523         height = bounds.height;
524
525         firePropertyChange(PROP_BOUNDS, oldBounds, bounds);
526     }
527     
528     /**
529      * sets the bounds of the DrawComponent without firing a propertyChange
530      * @param newBounds the bounds to set in absolute coordinates
531      */

532     protected void primSetBounds(Rectangle JavaDoc newBounds)
533     {
534         if (getBounds().equals(newBounds))
535             return;
536         
537         if (newBounds.x == getX() && newBounds.y == getY()) {
538             setSize(newBounds.width, newBounds.height);
539             return;
540         }
541
542         if (newBounds.width == getWidth() && newBounds.height == getHeight()) {
543             setLocation(newBounds.x, newBounds.y);
544             return;
545         }
546             
547         setSize(newBounds.width, newBounds.height);
548         setLocation(newBounds.x, newBounds.y);
549     }
550
551     /**
552      * @return the AffineTransform which contains all transformation Information of
553      * the DrawComponent
554      *
555      * @see java.awt.geom.AffineTransform
556      * @see org.nightlabs.editor2d.DrawComponent#getAffineTransform()
557      */

558     public AffineTransform JavaDoc getAffineTransform() {
559         return affineTransform;
560     }
561
562     /**
563      *@see org.nightlabs.editor2d.DrawComponent#setAffineTransform(AffineTransform)
564      */

565     public void setAffineTransform(AffineTransform JavaDoc newAffineTransform)
566     {
567         AffineTransform JavaDoc oldAffineTransform = affineTransform;
568         affineTransform = newAffineTransform;
569         firePropertyChange(PROP_AFFINE_TRANSFORM, oldAffineTransform, affineTransform);
570     }
571
572     /**
573      * @see org.nightlabs.editor2d.DrawComponent#getTmpRotationX()
574      */

575     public int getTmpRotationX() {
576         return tmpRotationX;
577     }
578
579     /**
580      * sets the temporary X-Coordinate of the RotationCenter in absolute Coordinates and fires
581      * a propertyChange.
582      *
583      * if this value is set to something else then the default-Value (ROTATION_X_DEFAULT)
584      * and setRotation(double rotation) is called, this value is used as RotationCenter instead
585      * of getRotationX()
586      *
587      * @see org.nightlabs.editor2d.DrawComponent#setTmpRotationX(int)
588      */

589     public void setTmpRotationX(int newTmpRotationX) {
590         int oldTmpRotationX = tmpRotationX;
591         tmpRotationX = newTmpRotationX;
592         firePropertyChange(PROP_TMP_ROTATION_X, oldTmpRotationX, tmpRotationX);
593     }
594
595     /**
596      * @see org.nightlabs.editor2d.DrawComponent#getTmpRotationY()
597      */

598     public int getTmpRotationY() {
599         return tmpRotationY;
600     }
601
602     /**
603      * sets the temporary Y-Coordinate of the RotationCenter in absolute Coordinates and fires
604      * a propertyChange.
605      *
606      * if this value is set to something else then the default-Value (ROTATION_Y_DEFAULT)
607      * and setRotation(double rotation) is called, this value is used as RotationCenter instead
608      * of getRotationY()
609      *
610      * @see org.nightlabs.editor2d.DrawComponent#setTmpRotationY(int)
611      */

612     public void setTmpRotationY(int newTmpRotationY) {
613         int oldTmpRotationY = tmpRotationY;
614         tmpRotationY = newTmpRotationY;
615         firePropertyChange(PROP_TMP_ROTATION_Y, oldTmpRotationY, tmpRotationY);
616     }
617
618     /**
619      * sets the Location of the DrawComponent in absolute Coordinates
620      *
621      * @param newX the X-Cooridnate of the DrawComponent in absolute Coordinates
622      * @param newY the Y-Cooridnate of the DrawComponent in absolute Coordinates
623      *
624      * @see org.nightlabs.editor2d.DrawComponent#setLocation(int, int)
625      */

626     public void setLocation(int newX, int newY)
627     {
628         atUtil.setToIdentity();
629         atUtil.translate((float)newX - (float)getX(), (float)newY - (float)getY());
630         
631         transformRotationCenter(atUtil);
632         transform(atUtil);
633     }
634         
635     /**
636      * sets the size of the DrawComponent
637      *
638      * @param newWidth the Width of the DrawComponent in absolute Coordinates
639      * @param newHeight the Height of the DrawComponent in absolute Coordinates
640      *
641      */

642     protected void setSize(int newWidth, int newHeight)
643     {
644         atUtil.setToIdentity();
645         float ratioY = (float)newHeight / (float)getHeight();
646         ratioY = EditorModelUtil.checkFactor(ratioY);
647         float ratioX = (float)newWidth / (float)getWidth();
648         ratioX = EditorModelUtil.checkFactor(ratioX);
649         float distanceX = (float)getX() - ((float)getX()*ratioX);
650         float distanceY = (float)getY() - ((float)getY()*ratioY);
651         atUtil.translate(distanceX, distanceY);
652         atUtil.scale(ratioX, ratioY);
653         
654         transformRotationCenter(atUtil);
655         transform(atUtil);
656     }
657         
658     /**
659      * transforms the RotationCenter (getRotationX(), getRotationY()) based on the
660      * given AffineTransform
661      *
662      * @param at the AffineTransform to transform the RotationCenter
663      */

664     protected void transformRotationCenter(AffineTransform JavaDoc at)
665     {
666         if (rotationX != ROTATION_X_DEFAULT || rotationY != ROTATION_Y_DEFAULT) {
667             Point JavaDoc rotationCenter = new Point JavaDoc(getRotationX(), getRotationY());
668             Point JavaDoc newRotationCenter = new Point JavaDoc();
669             at.transform(rotationCenter, newRotationCenter);
670             rotationX = newRotationCenter.x;
671             rotationY = newRotationCenter.y;
672         }
673     }
674     
675     /**
676      * this AffineTransform is used for recurrent transformation to avoid to create each
677      * time a new AffineTransform, but instead just reset this to Identity
678      */

679     protected static transient final AffineTransform JavaDoc atUtil = new AffineTransform JavaDoc();
680     
681     /**
682      * return a String which prints all members with the current values
683      */

684     public String JavaDoc toString()
685     {
686         StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
687         result.append(" (id: ");
688         result.append(id);
689         result.append(", name: ");
690         result.append(name);
691         result.append(", x: ");
692         result.append(x);
693         result.append(", y: ");
694         result.append(y);
695         result.append(", width: ");
696         result.append(width);
697         result.append(", height: ");
698         result.append(height);
699         result.append(", rotation: ");
700         result.append(rotation);
701         result.append(", rotationX: ");
702         result.append(rotationX);
703         result.append(", rotationY: ");
704         result.append(rotationY);
705         result.append(", bounds: ");
706         result.append(bounds);
707         result.append(", affineTransform: ");
708         result.append(affineTransform);
709         result.append(", tmpRotationX: ");
710         result.append(tmpRotationX);
711         result.append(", tmpRotationY: ");
712         result.append(tmpRotationY);
713         result.append(", renderMode: ");
714         result.append(renderMode);
715         result.append(')');
716         return result.toString();
717     }
718     
719     /**
720      * the DrawComponentContainer in which the DrawComponent is contained
721      */

722     protected DrawComponentContainer parent = null;
723
724     /**
725      * @return the DrawComponentContainer in which the DrawComponent is contained
726      * @see org.nightlabs.editor2d.DrawComponentContainer
727      * @see org.nightlabs.editor2d.DrawComponent#getParent()
728      */

729     public DrawComponentContainer getParent() {
730         return parent;
731     }
732
733     /**
734      * sets the DrawComponentContainer in which the DrawComponent is contained
735      * and fires a propertyChange
736      * @param newParent the DrawComponentContainer to set
737      * @see org.nightlabs.editor2d.DrawComponentContainer
738      * @see org.nightlabs.editor2d.DrawComponent#setParent(DrawComponentContainer)
739      */

740     public void setParent(DrawComponentContainer newParent)
741     {
742         DrawComponentContainer oldParent = parent;
743         primSetParent(newParent);
744         firePropertyChange(PROP_PARENT, oldParent, parent);
745     }
746
747     /**
748      * sets the DrawComponentContainer in which the DrawComponent is contained
749      * without firing a propertyChange
750      * @param newParent the DrawComponentContainer to set
751      * @see org.nightlabs.editor2d.DrawComponentContainer
752      */

753     protected void primSetParent(DrawComponentContainer newParent) {
754         parent = newParent;
755     }
756     
757     /**
758      * the RenderMode of the DrawComponent which determines how the DrawComponent is drawn
759      */

760     protected int renderMode = RENDER_MODE_EDEFAULT;
761
762     /**
763      * @return the renderMode of the DrawComponent
764      * @see org.nightlabs.editor2d.render.RenderModeManager
765      * @see org.nightlabs.editor2d.DrawComponent#getRenderMode()
766      */

767     public int getRenderMode() {
768         return renderMode;
769     }
770     
771     /**
772      * sets the renderMode of the DrawComponent and fires a propertyChange
773      * @param mode the renderMode to set
774      * @see org.nightlabs.editor2d.render.RenderModeManager
775      * @see org.nightlabs.editor2d.DrawComponent#setRenderMode(int)
776      */

777     public void setRenderMode(int mode)
778     {
779 // int oldRenderMode = renderMode;
780
primSetRenderMode(mode);
781         firePropertyChange(PROP_RENDER_MODE, -1, renderMode);
782     }
783         
784     /**
785      * sets the renderMode of the DrawComponent without firing a propertyChange
786      * @param mode the renderMode to set
787      * @see org.nightlabs.editor2d.render.RenderModeManager
788      */

789     protected void primSetRenderMode(int mode)
790     {
791         renderMode = mode;
792         if (getRenderModeManager() != null)
793             renderer = getRenderModeManager().getRenderer(renderMode, getRenderModeClass());
794     }
795     
796     protected transient Renderer renderer;
797     
798     /**
799      * the Renderer which is responsible for how the the DrawComponent is drawn
800      * based on the renderMode and the renderModeClass, the RenderModeManager returns
801      * the corresponding Renderer
802      *
803      * @return the current Renderer for the DrawComponent
804      * @see org.nightlabs.editor2d.DrawComponent#getRenderer()
805      */

806     public Renderer getRenderer()
807     {
808         if (renderer == null)
809             renderer = getRenderModeManager().getRenderer(getRenderMode(), getRenderModeClass());
810         
811         return renderer;
812     }
813         
814     protected transient Class JavaDoc renderModeClass = this.getClass();
815     
816     /**
817      * IMPORTANT: Inheritated classes should override this Method
818      *
819      * @return the Class for which Renderers should be registered at the
820      * RenderModeManager
821      *
822      */

823     public Class JavaDoc getRenderModeClass() {
824         return renderModeClass;
825     }
826     
827     protected transient RenderModeManager renderModeManager = null;
828     
829     /**
830      * sets the RenderModeManager for the DrawComponent
831      * by Default the RenderModeManager is assigned from the Root MultiLayerDrawComponent
832      * @param man the RenderModeManager to set
833      * @see org.nightlabs.editor2d.render.RenderModeManager
834      * @see org.nightlabs.editor2d.DrawComponent#setRenderModeManager(RenderModeManager)
835      */

836     public void setRenderModeManager(RenderModeManager man) {
837         renderModeManager = man;
838     }
839     
840     /**
841      * @return the RenderModeManager where all Renderers are registered
842      * @see org.nightlabs.editor2d.render.RenderModeManager
843      * @see org.nightlabs.editor2d.DrawComponent#getRenderModeManager()
844      */

845     public RenderModeManager getRenderModeManager()
846     {
847         if (renderModeManager == null && getRoot() != null) {
848             renderModeManager = getRoot().getRenderModeManager();
849         }
850         return renderModeManager;
851     }
852             
853     /**
854      * IMPORTANT: Inheritated classes should override this Method
855      * @return the Description of the Type for the DrawComponent
856      */

857     public String JavaDoc getTypeName() {
858         return "DrawComponent";
859     }
860
861     /**
862      * returns the MultiLayerDrawComponent by going recursivly through all parents until the root is reached
863      * if the parent is null, also null is returned
864      *
865      * @return the Root-Model-Object of the DrawComponent
866      * @see org.nightlabs.editor2d.MultiLayerDrawComponent
867      * @see org.nightlabs.editor2d.DrawComponent#getRoot()
868      */

869     public MultiLayerDrawComponent getRoot()
870     {
871         if (getParent() != null)
872             return getParent().getRoot();
873         return null;
874     }
875     
876     /**
877      * @see org.nightlabs.editor2d.DrawComponent
878      */

879     public void setRotationMember(double value) {
880         this.rotation = value;
881     }
882         
883     /**
884      * This Method is only a Wrapper for the transform(AffineTransform at, boolean fromParent)-Method
885      * with the boolean set to false
886      *
887      * @see transform(AffineTransform newAT, boolean fromParent)
888      * @see org.nightlabs.editor2d.DrawComponent#transform(AffineTransform)
889      */

890     public void transform(AffineTransform JavaDoc newAT)
891     {
892         transform(newAT, false);
893 // firePropertyChange(TRANSFORM_CHANGED, null, affineTransform);
894
}
895     
896     /**
897      * This Method can be implemented by inheritans to avoid the firing of a propertyChange
898      * if the transformation comes from the parent.
899      *
900      * IMPORTANT:
901      * Inheritated classes should override this Method and transform the DrawComponent but also call
902      * super.transform(newAT, fromParent) first
903      *
904      * This is necessary because all Geometric Transformation
905      * (setX(), setY(), setWidth(), setHeight(), setLocation(), setSize(), setRotation())
906      * depend on this Method
907      *
908      * @param newAT the AffineTransform to transform
909      * @param fromParent determines if the Transformation comes from the parent DrawComponentContainer
910      * or if the DrawComponent is transformed directly, means it notificies the parent
911      * (getParent.notifyChildTransform(this)) about the transformation or not
912      *
913      * @see org.nightlabs.editor2d.DrawComponent#transform(AffineTransform, boolean)
914      */

915     public void transform(AffineTransform JavaDoc newAT, boolean fromParent)
916     {
917         // TODO: DrawComponent should firePropertyChange when transformation occures
918
// AffineTransform oldAT = affineTransform;
919
affineTransform.preConcatenate(newAT);
920         bounds = null;
921     }
922     
923     /**
924      * clears the cached bounds
925      * @see org.nightlabs.editor2d.DrawComponent#clearBounds()
926      */

927     public void clearBounds()
928     {
929         bounds = null;
930         bounds = getBounds();
931         firePropertyChange(PROP_BOUNDS, null, bounds);
932     }
933         
934 // public Object clone()
935
// {
936
// try {
937
// DrawComponentImpl dc = (DrawComponentImpl) super.clone();
938
// dc.affineTransform = (AffineTransform)affineTransform.clone();
939
// dc.bounds = new Rectangle(getBounds());
940
// dc.name = new I18nTextBuffer();
941
// dc.id = DrawComponent.ID_EDEFAULT;
942
// name.copyTo(dc.name);
943
// dc.languageId = new String(languageId);
944
// dc.horizontalGuide = null;
945
// dc.verticalGuide = null;
946
//
947
// // clone PropertyChangeListeners
948
// dc.listeners = new PropertyChangeSupport(dc);
949
// PropertyChangeListener[] propertyChangeListeners = listeners.getPropertyChangeListeners();
950
// for (int i = 0; i < propertyChangeListeners.length - 1; i++) {
951
// dc.listeners.addPropertyChangeListener(propertyChangeListeners[i]);
952
// }
953
//
954
// // add To Parent to register in MultiLayerDrawComponent
955
// // TODO: should use primAddDrawComponent to avoid firing propertyChange
956
// dc.getParent().addDrawComponent(dc);
957
// return dc;
958
// } catch (CloneNotSupportedException e) {
959
// throw new IllegalStateException("How the hell can clone fail?! I implemented it!", e);
960
// }
961
// }
962

963     public Object JavaDoc clone(DrawComponentContainer parent)
964     {
965         try {
966             DrawComponentImpl dc = (DrawComponentImpl) super.clone();
967             dc.affineTransform = (AffineTransform JavaDoc)affineTransform.clone();
968             dc.bounds = new Rectangle JavaDoc(getBounds());
969             dc.name = new I18nTextBuffer();
970             dc.id = DrawComponent.ID_EDEFAULT;
971             name.copyTo(dc.name);
972             dc.languageId = new String JavaDoc(languageId);
973             dc.horizontalGuide = null;
974             dc.verticalGuide = null;
975             
976             dc.renderer = renderer;
977             dc.renderMode = renderMode;
978             dc.renderModeClass = renderModeClass;
979             dc.renderModeManager = renderModeManager;
980             dc.rotation = rotation;
981             dc.rotationX = rotationX;
982             dc.rotationY = rotationY;
983             dc.tmpRotationX = tmpRotationX;
984             dc.tmpRotationY = tmpRotationY;
985             dc.height = height;
986             dc.width = width;
987             dc.x = x;
988             dc.y = y;
989             
990             // clone PropertyChangeListeners
991
dc.listeners = new PropertyChangeSupport JavaDoc(dc);
992             PropertyChangeListener JavaDoc[] propertyChangeListeners = listeners.getPropertyChangeListeners();
993             for (int i = 0; i < propertyChangeListeners.length - 1; i++) {
994                 dc.listeners.addPropertyChangeListener(propertyChangeListeners[i]);
995             }
996             
997             // add To Parent to register in MultiLayerDrawComponent
998
parent.addDrawComponent(dc);
999             return dc;
1000        } catch (CloneNotSupportedException JavaDoc e) {
1001            throw new IllegalStateException JavaDoc("How the hell can clone fail?! I implemented it!", e);
1002        }
1003    }
1004    
1005    public Object JavaDoc clone()
1006    {
1007        return clone(getParent());
1008    }
1009    
1010// /**
1011
// * clones the DrawComponent
1012
// * @see org.nightlabs.editor2d.DrawComponent#clone()
1013
// */
1014
// public DrawComponent clone()
1015
// {
1016
// DrawComponent dc = new DrawComponentImpl();
1017
// dc = assign(dc);
1018
// return dc;
1019
// }
1020
//
1021
// protected DrawComponent assign(DrawComponent dc)
1022
// {
1023
// if (dc != null) {
1024
// dc.setAffineTransform((AffineTransform)getAffineTransform().clone());
1025
// dc.setBounds(new Rectangle(getBounds()));
1026
// getI18nText().copyTo(dc.getI18nText());
1027
// dc.setLanguageId(getLanguageId());
1028
// dc.setParent(getParent());
1029
// dc.setRenderMode(getRenderMode());
1030
// dc.setRotationX(getRotationX());
1031
// dc.setRotationY(getRotationY());
1032
// dc.setRotationMember(getRotation());
1033
// dc.getParent().addDrawComponent(dc);
1034
// return dc;
1035
// }
1036
// return null;
1037
// }
1038

1039} //DrawComponentImpl
1040
Popular Tags