KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <copyright>
3  * </copyright>
4  *
5  * $Id: DrawComponentImpl.java 1635 2005-08-26 19:28:30Z nozkiller $
6  */

7 package com.nightlabs.editor2d.impl;
8
9 import java.awt.Point JavaDoc;
10 import java.awt.Rectangle JavaDoc;
11 import java.awt.geom.AffineTransform JavaDoc;
12 import java.beans.PropertyChangeListener JavaDoc;
13 import java.beans.PropertyChangeSupport JavaDoc;
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 AffineTransform AFFINE_TRANSFORM_EDEFAULT = new AffineTransform();
29
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 JavaDoc BOUNDS_EDEFAULT = new Rectangle JavaDoc(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 JavaDoc bounds = BOUNDS_EDEFAULT;
52     protected AffineTransform JavaDoc affineTransform = new AffineTransform JavaDoc();
53     protected transient int tmpRotationX = TMP_ROTATION_X_EDEFAULT;
54     protected transient int tmpRotationY = TMP_ROTATION_Y_EDEFAULT;
55
56     protected transient PropertyChangeSupport JavaDoc listeners = null;
57     protected PropertyChangeSupport JavaDoc getPropertyChangeSupport()
58     {
59         if (listeners == null)
60             listeners = new PropertyChangeSupport JavaDoc(this);
61         return listeners;
62     }
63     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l){
64         getPropertyChangeSupport().addPropertyChangeListener(l);
65     }
66     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l){
67         getPropertyChangeSupport().removePropertyChangeListener(l);
68     }
69     protected void firePropertyChange(String JavaDoc prop, Object JavaDoc oldValue, Object JavaDoc newValue){
70         getPropertyChangeSupport().firePropertyChange(prop, oldValue, newValue);
71     }
72     protected void firePropertyChange(String JavaDoc prop, int oldValue, int newValue){
73         getPropertyChangeSupport().firePropertyChange(prop, oldValue, newValue);
74     }
75     protected void firePropertyChange(String JavaDoc prop, double oldValue, double newValue){
76         getPropertyChangeSupport().firePropertyChange(prop, new Double JavaDoc(oldValue), new Double JavaDoc(newValue));
77     }
78     protected void firePropertyChange(String JavaDoc prop, boolean oldValue, boolean newValue){
79         getPropertyChangeSupport().firePropertyChange(prop, oldValue, newValue);
80     }
81         
82     public DrawComponentImpl() {
83         super();
84     }
85
86     /**
87      * <!-- begin-user-doc -->
88      * <!-- end-user-doc -->
89      *
90      */

91     public long getId() {
92         return id;
93     }
94
95     /**
96      * @see DrawComponent
97      */

98     public void setId(long newId) {
99         long oldId = id;
100         id = newId;
101         firePropertyChange(PROP_ID, new Long JavaDoc(oldId), new Long JavaDoc(newId));
102     }
103
104     /**
105      * <!-- begin-user-doc -->
106      * <!-- end-user-doc -->
107      *
108      */

109     public String JavaDoc getName() {
110         return name.getText(getLanguageId());
111     }
112
113     /**
114      * <!-- begin-user-doc -->
115      * <!-- end-user-doc -->
116      *
117      */

118     public void setName(String JavaDoc newName) {
119         String JavaDoc oldName = name.getText(getLanguageId());
120         name.setText(getLanguageId(), newName);
121         firePropertyChange(PROP_NAME, oldName, newName);
122     }
123
124     protected transient String JavaDoc languageId = DEFAULT_LANGUAGE_ID;
125     public String JavaDoc getLanguageId() {
126         return languageId;
127     }
128     public void setLanguageId(String JavaDoc newLanguageId) {
129         String JavaDoc oldLanguageId = languageId;
130         primSetLanguageId(newLanguageId);
131         firePropertyChange(PROP_LANGUAGE_ID, oldLanguageId, languageId);
132     }
133     protected void primSetLanguageId(String JavaDoc 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     /**
146      * <!-- begin-user-doc -->
147      * <!-- end-user-doc -->
148      *
149      */

150     public int getX() {
151         return getBounds().x;
152     }
153
154     /**
155      * <!-- begin-user-doc -->
156      * <!-- end-user-doc -->
157      *
158      */

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     /**
176      * <!-- begin-user-doc -->
177      * <!-- end-user-doc -->
178      *
179      */

180     public int getY() {
181         return getBounds().y;
182     }
183
184     /**
185      * <!-- begin-user-doc -->
186      * <!-- end-user-doc -->
187      *
188      */

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     /**
208      * <!-- begin-user-doc -->
209      * <!-- end-user-doc -->
210      *
211      */

212     public int getWidth() {
213         return getBounds().width;
214     }
215
216     /**
217      * <!-- begin-user-doc -->
218      * <!-- end-user-doc -->
219      *
220      */

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     /**
240      * <!-- begin-user-doc -->
241      * <!-- end-user-doc -->
242      *
243      */

244     public int getHeight() {
245         return getBounds().height;
246     }
247
248     /**
249      * <!-- begin-user-doc -->
250      * <!-- end-user-doc -->
251      *
252      */

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     /**
272      * <!-- begin-user-doc -->
273      * <!-- end-user-doc -->
274      * @generated
275      */

276     public double getRotation() {
277         return rotation;
278     }
279
280     /**
281      * <!-- begin-user-doc -->
282      * <!-- end-user-doc -->
283      *
284      */

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     /**
318      * <!-- begin-user-doc -->
319      * <!-- end-user-doc -->
320      * @generated
321      */

322     public EditorGuide getHorizontalGuide() {
323         return horizontalGuide;
324     }
325
326     /**
327      * <!-- begin-user-doc -->
328      * <!-- end-user-doc -->
329      * @generated
330      */

331     public void setHorizontalGuide(EditorGuide newHorizontalGuide)
332     {
333         EditorGuide old = horizontalGuide;
334         horizontalGuide = newHorizontalGuide;
335         firePropertyChange(PROP_HORIZONTAL_GUIDE, old, horizontalGuide);
336     }
337
338     /**
339      * <!-- begin-user-doc -->
340      * <!-- end-user-doc -->
341      * @generated
342      */

343     public EditorGuide getVerticalGuide() {
344         return verticalGuide;
345     }
346
347     /**
348      * <!-- begin-user-doc -->
349      * <!-- end-user-doc -->
350      * @generated
351      */

352     public void setVerticalGuide(EditorGuide newVerticalGuide)
353     {
354         EditorGuide old = verticalGuide;
355         verticalGuide = newVerticalGuide;
356         firePropertyChange(PROP_VERTICAL_GUIDE, old, verticalGuide);
357     }
358
359     /**
360      * <!-- begin-user-doc -->
361      * <!-- end-user-doc -->
362      *
363      */

364     public int getRotationX()
365     {
366         if (rotationX == ROTATION_X_DEFAULT)
367             return (int) getBounds().getCenterX();
368             
369         return rotationX;
370     }
371
372     /**
373      * <!-- begin-user-doc -->
374      * <!-- end-user-doc -->
375      *
376      */

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     /**
389      * <!-- begin-user-doc -->
390      * <!-- end-user-doc -->
391      *
392      */

393     public int getRotationY()
394     {
395         if (rotationY == ROTATION_Y_DEFAULT)
396             return (int) getBounds().getCenterY();
397         
398         return rotationY;
399     }
400
401     /**
402      * <!-- begin-user-doc -->
403      * <!-- end-user-doc -->
404      *
405      */

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     /**
418      * <!-- begin-user-doc -->
419      * <!-- end-user-doc -->
420      *
421      */

422     public Rectangle JavaDoc getBounds()
423     {
424         if (bounds == null) {
425             bounds = new Rectangle JavaDoc(x, y, width, height);
426         }
427         return bounds;
428     }
429
430     /**
431      * <!-- begin-user-doc -->
432      * <!-- end-user-doc -->
433      *
434      */

435     public void setBounds(Rectangle JavaDoc newBounds)
436     {
437         Rectangle JavaDoc 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     /**
450      * <!-- begin-user-doc -->
451      * <!-- end-user-doc -->
452      * @generated
453      */

454     public AffineTransform JavaDoc getAffineTransform() {
455         return affineTransform;
456     }
457
458     /**
459      * <!-- begin-user-doc -->
460      * <!-- end-user-doc -->
461      *
462      */

463     public void setAffineTransform(AffineTransform JavaDoc newAffineTransform)
464     {
465 // AffineTransform oldAffineTransform = affineTransform;
466
// affineTransform = newAffineTransform;
467
// firePropertyChange(PROP_AFFINE_TRANSFORM, oldAffineTransform, affineTransform);
468
}
469
470     /**
471      * <!-- begin-user-doc -->
472      * <!-- end-user-doc -->
473      * @generated
474      */

475     public int getTmpRotationX() {
476         return tmpRotationX;
477     }
478
479     /**
480      * <!-- begin-user-doc -->
481      * <!-- end-user-doc -->
482      * @generated
483      */

484     public void setTmpRotationX(int newTmpRotationX) {
485         int oldTmpRotationX = tmpRotationX;
486         tmpRotationX = newTmpRotationX;
487         firePropertyChange(PROP_TMP_ROTATION_X, oldTmpRotationX, tmpRotationX);
488     }
489
490     /**
491      * <!-- begin-user-doc -->
492      * <!-- end-user-doc -->
493      * @generated
494      */

495     public int getTmpRotationY() {
496         return tmpRotationY;
497     }
498
499     /**
500      * <!-- begin-user-doc -->
501      * <!-- end-user-doc -->
502      * @generated
503      */

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 JavaDoc 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 JavaDoc at)
555     {
556         if (rotationX != ROTATION_X_DEFAULT || rotationY != ROTATION_Y_DEFAULT) {
557             Point JavaDoc rotationCenter = new Point JavaDoc(getRotationX(), getRotationY());
558             Point JavaDoc newRotationCenter = new Point JavaDoc();
559             at.transform(rotationCenter, newRotationCenter);
560             rotationX = newRotationCenter.x;
561             rotationY = newRotationCenter.y;
562         }
563     }
564     
565     protected final AffineTransform JavaDoc atUtil = new AffineTransform JavaDoc();
566     
567     /**
568      * <!-- begin-user-doc -->
569      * <!-- end-user-doc -->
570      *
571      */

572     public void transform(AffineTransform JavaDoc newAT)
573     {
574         AffineTransform JavaDoc oldAT = affineTransform;
575         affineTransform.preConcatenate(newAT);
576         bounds = null;
577         
578 // firePropertyChange(TRANSFORM_CHANGED, oldAT, affineTransform);
579
}
580
581     /**
582      * <!-- begin-user-doc -->
583      * <!-- end-user-doc -->
584      * @generated
585      */

586     public String JavaDoc toString()
587     {
588         StringBuffer JavaDoc result = new StringBuffer JavaDoc(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     /**
624      * The cached value of the '{@link #getParent() <em>Parent</em>}' reference.
625      * <!-- begin-user-doc -->
626      * <!-- end-user-doc -->
627      * @see #getParent()
628      * @generated
629      * @ordered
630      */

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     /**
641      * <!-- begin-user-doc -->
642      * <!-- end-user-doc -->
643      * @generated
644      */

645     public DrawComponentContainer getParent() {
646         return parent;
647     }
648
649     /**
650      * <!-- begin-user-doc -->
651      * <!-- end-user-doc -->
652      *
653      */

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 JavaDoc renderModeClass = this.getClass();
686     public Class JavaDoc 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() {
703
// return ModelPlugin.getResourceString("type.drawComponent");
704
// }
705
public String JavaDoc 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 } //DrawComponentImpl
717
Popular Tags