KickJava   Java API By Example, From Geeks To Geeks.

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


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.Rectangle JavaDoc;
30 import java.awt.geom.AffineTransform JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.nightlabs.editor2d.DrawComponent;
37 import org.nightlabs.editor2d.DrawComponentContainer;
38 import org.nightlabs.editor2d.MultiLayerDrawComponent;
39 import org.nightlabs.editor2d.render.RenderModeManager;
40 import org.nightlabs.util.Utils;
41
42 /**
43  * The Base Implementation of the Interface {@link DrawComponentContainer}
44  *
45  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
46  */

47 public class DrawComponentContainerImpl
48 extends DrawComponentImpl
49 implements DrawComponentContainer
50 {
51     /**
52      * Instances of {@link DrawComponent} where some of them might be {@link DrawComponentContainer}.
53      */

54     protected List JavaDoc drawComponents = null;
55         
56     public DrawComponentContainerImpl() {
57         super();
58     }
59
60     /**
61      * @return Returns instances of {@link DrawComponent}. Some of them are instances
62      * of {@link DrawComponentContainer}. This method never returns <tt>null</tt>.
63      *@see DrawComponentContainer#getDrawComponents()
64      */

65     public List JavaDoc getDrawComponents()
66     {
67         if (drawComponents == null) {
68             drawComponents = new ArrayList JavaDoc();
69         }
70         return drawComponents;
71     }
72
73     /**
74      * @see DrawComponentContainer#setDrawComponents(List)
75      */

76     public void setDrawComponents(List JavaDoc drawComponents) {
77         this.drawComponents = drawComponents;
78     }
79
80     /**
81      * adds a DrawComponent to the DrawComponent-List and fires
82      * a PropertyChange with the PropertyName {@link DrawComponentContainer#CHILD_ADDED}
83      *
84      * Furthermore it sets the Parent for the drawComponent to this DrawComponentContainer
85      * and calls {@link getRoot().registerDrawComponent(drawComponent)} to register it at
86      * the MultiLayerDrawComponent, e.g. to recieve an unique id and sets the
87      * RenderModeManager.
88      * The cached bounds are also reset.
89      *
90      * @see DrawComponentContainer#addDrawComponent(DrawComponent)
91      */

92     public void addDrawComponent(DrawComponent drawComponent)
93     {
94         primAddDrawComponent(drawComponent);
95         firePropertyChange(CHILD_ADDED, null, drawComponents);
96     }
97
98     protected void primAddDrawComponent(DrawComponent drawComponent)
99     {
100 // drawComponent.addPropertyChangeListener(pcl);
101
drawComponent.setParent(this);
102         getRoot().registerDrawComponent(drawComponent);
103         drawComponent.setRenderModeManager(getRoot().getRenderModeManager());
104         getDrawComponents().add(drawComponent);
105         bounds = null;
106     }
107     
108     /**
109      * adds a DrawComponent at the given index in the DrawComponent-List
110      * {@link DrawComponentContainer#getDrawComponents()} and fires a PropertyChange
111      * with the PropertyName {@link DrawComponentContainer#CHILD_ADDED}
112      *
113      * Furthermore it sets the Parent for the drawComponent to this DrawComponentContainer
114      * and calls {@link MultiLayerDrawComponent.registerDrawComponent(drawComponent)} to register it at
115      * the MultiLayerDrawComponent, e.g. to recieve an unique id and sets the
116      * RenderModeManager.
117      * The cached bounds are also reset.
118      *
119      * always use this Method to add a DrawComponent, never add it directly
120      * to the drawComponents-List, because this skips the registration in the
121      * MultiLayerDrawComponent
122      *
123      * @param drawComponent the DrawComponent to add
124      * @param index the index in the DrawComponent-List
125      * @see addDrawComponent(DrawComponent drawComponent)
126      * @see MultiLayerDrawComponent.registerDrawComponent(DrawComponent drawComponent)
127      */

128     public void addDrawComponent(DrawComponent drawComponent, int index)
129     {
130         primAddDrawComponent(drawComponent, index);
131         firePropertyChange(CHILD_ADDED, null, drawComponents);
132     }
133     
134     protected void primAddDrawComponent(DrawComponent drawComponent, int index)
135     {
136 // drawComponent.addPropertyChangeListener(pcl);
137
drawComponent.setParent(this);
138         getRoot().registerDrawComponent(drawComponent);
139         drawComponent.setRenderModeManager(getRoot().getRenderModeManager());
140         getDrawComponents().add(index, drawComponent);
141         bounds = null;
142     }
143     
144     /**
145      * removes the given DrawComponent at the given index from the container
146      * and fires a PropertyChange with the PropertyName {@link DrawComponentContainer#CHILD_REMOVED}
147      *
148      * This Method unregisters the drawComponent from the MultiLayerDrawComponent
149      * and removes it from the drawComponents-List
150      *
151      * always use this Method to remove a DrawComponent, never remove it directly
152      * from the drawComponents-List, because this skips the registration in the
153      * MultiLayerDrawComponent
154      *
155      * @param drawComponent the DrawComponent to remove
156      * @see MultiLayerDrawComponent#unregisterDrawComponent(DrawComponent drawComponent)
157      * @see DrawComponentContainer#removeDrawComponent(DrawComponent)
158      */

159     public void removeDrawComponent(DrawComponent drawComponent)
160     {
161         primRemoveDrawComponent(drawComponent);
162         firePropertyChange(CHILD_REMOVED, null, drawComponents);
163     }
164     
165     protected void primRemoveDrawComponent(DrawComponent drawComponent)
166     {
167 // drawComponent.removePropertyChangeListener(pcl);
168
getRoot().unregisterDrawComponent(drawComponent);
169         getDrawComponents().remove(drawComponent);
170         bounds = null;
171     }
172
173     /**
174      * removes the given DrawComponent at the given index from the container
175      * and fires a PropertyChange with the PropertyName {@link DrawComponentContainer#CHILD_REMOVED}
176      *
177      * This Method unregisters the drawComponent from the MultiLayerDrawComponent
178      * and removes it from the drawComponents-List
179      *
180      * always use this Method to remove a DrawComponent, never remove it directly
181      * from the drawComponents-List, because this skips the registration in the
182      * MultiLayerDrawComponent
183      *
184      * @param drawComponent the DrawComponent to remove
185      * @see MultiLayerDrawComponent#unregisterDrawComponent(long)
186      * @see DrawComponentContainer#removeDrawComponent(int)
187      */

188     public void removeDrawComponent(int index)
189     {
190         primRemoveDrawComponent(index);
191         firePropertyChange(CHILD_REMOVED, null, drawComponents);
192     }
193
194     protected void primRemoveDrawComponent(int index)
195     {
196         DrawComponent dc = (DrawComponent) getDrawComponents().get(index);
197 // dc.removePropertyChangeListener(pcl);
198
long idToRemove = dc.getId();
199         getRoot().unregisterDrawComponent(idToRemove);
200         getDrawComponents().remove(index);
201         bounds = null;
202     }
203
204     /**
205      * @return the MultiLayerDrawComponent which is the Root Component
206      * for all DrawComponents
207      * @see DrawComponent#getRoot()
208      */

209     public MultiLayerDrawComponent getRoot()
210     {
211         DrawComponentContainer parent = getParent();
212         if (parent == null) {
213             if (this instanceof MultiLayerDrawComponent)
214                 return (MultiLayerDrawComponent) this;
215             else
216                 throw new IllegalStateException JavaDoc("Member parent may not be null for DrawComponentContainer "+this.toString());
217         }
218         
219         if (parent instanceof MultiLayerDrawComponent) {
220             return (MultiLayerDrawComponent) parent;
221         }
222         
223         while (!(parent instanceof MultiLayerDrawComponent)) {
224             parent = parent.getParent();
225         }
226         
227         return (MultiLayerDrawComponent) parent;
228     }
229
230     /**
231      * @return the Bounds which corresponds to the bounds of all
232      * contained children bounds
233      * @see DrawComponent#getBounds()
234      */

235     public Rectangle JavaDoc getBounds()
236     {
237         if (bounds != null) {
238             return bounds;
239         }
240         else
241         {
242             Rectangle JavaDoc oldBounds = bounds;
243             
244             int minX = Integer.MAX_VALUE;
245             int minY = Integer.MAX_VALUE;
246             int maxX = Integer.MIN_VALUE;
247             int maxY = Integer.MIN_VALUE;
248             
249             if (getDrawComponents().isEmpty())
250                 return new Rectangle JavaDoc(0, 0, 0, 0);
251             
252             for (Iterator JavaDoc it = getDrawComponents().iterator(); it.hasNext(); )
253             {
254                 DrawComponent dc = (DrawComponent) it.next();
255                 Rectangle JavaDoc bounds = dc.getBounds();
256                 minX = Math.min((int)bounds.getMinX(), minX);
257                 minY = Math.min((int)bounds.getMinY(), minY);
258                 maxX = Math.max((int)bounds.getMaxX(), maxX);
259                 maxY = Math.max((int)bounds.getMaxY(), maxY);
260             }
261             bounds = new Rectangle JavaDoc(minX, minY, maxX-minX, maxY-minY);
262             
263             firePropertyChange(PROP_BOUNDS, oldBounds, bounds);
264             return bounds;
265         }
266     }
267     
268     /**
269      * sets the {@link RenderModeManager} for this container and all its children
270      * @see DrawComponent#setRenderModeManager(RenderModeManager)
271      */

272     public void setRenderModeManager(RenderModeManager man)
273     {
274         super.setRenderModeManager(man);
275         for (Iterator JavaDoc it = getDrawComponents().iterator(); it.hasNext(); ) {
276             DrawComponent dc = (DrawComponent) it.next();
277             dc.setRenderModeManager(renderModeManager);
278         }
279     }
280         
281     /**
282      * sets the renderMode for this container and all its children
283      * @see DrawComponent#setRenderMode(int)
284      */

285     public void setRenderMode(int mode)
286     {
287         super.setRenderMode(mode);
288         for (Iterator JavaDoc it = getDrawComponents().iterator(); it.hasNext(); ) {
289             DrawComponent dc = (DrawComponent) it.next();
290             dc.setRenderMode(mode);
291         }
292     }
293     
294     public Class JavaDoc getRenderModeClass() {
295         return DrawComponentContainer.class;
296     }
297         
298     public void notifyChildTransform(DrawComponent child)
299     {
300         // TODO: implement this check (contains in Command)
301
bounds = null;
302         setBounds(getBounds());
303         System.out.println("DrawComponent changed DrawComponentContainer BOUNDS!");
304     }
305
306     public String JavaDoc getTypeName() {
307         return "DrawComponentContainer";
308     }
309
310     /**
311      * sets the languageID for this container and all its children
312      * @see DrawComponent#setLanguageId(String)
313      */

314     public void setLanguageId(String JavaDoc newLanguageId)
315     {
316         super.setLanguageId(newLanguageId);
317         for (Iterator JavaDoc it = getDrawComponents().iterator(); it.hasNext(); ) {
318             DrawComponent dc = (DrawComponent) it.next();
319             dc.setLanguageId(newLanguageId);
320         }
321     }
322
323     /**
324      * @see org.nightlabs.editor2d.DrawComponentContainer#findDrawComponents(java.lang.Class)
325      */

326     public Collection JavaDoc findDrawComponents(Class JavaDoc type)
327     {
328         return findDrawComponents(type, false);
329     }
330     /**
331      * @see org.nightlabs.editor2d.DrawComponentContainer#findDrawComponents(java.lang.Class, boolean)
332      */

333     public Collection JavaDoc findDrawComponents(Class JavaDoc type, boolean canSelfPackage)
334     {
335         ArrayList JavaDoc res = new ArrayList JavaDoc();
336
337         if (drawComponents != null) {
338             for (Iterator JavaDoc it = drawComponents.iterator(); it.hasNext(); ) {
339                 DrawComponent dc = (DrawComponent) it.next();
340
341                 if (type.isInstance(dc)) {
342                     res.add(dc);
343
344                     if (!canSelfPackage)
345                         continue;
346                 }
347
348                 if (dc instanceof DrawComponentContainer) {
349                     DrawComponentContainer dcc = (DrawComponentContainer)dc;
350                     res.addAll(dcc.findDrawComponents(type, canSelfPackage));
351                 }
352             } // for (Iterator it = drawComponents.iterator(); it.hasNext(); ) {
353
} // if (drawComponents != null) {
354

355         return res;
356     }
357
358     /**
359      * sets the rotation for this container and all its children
360      * @see DrawComponent#setRotation(double)
361      */

362     public void setRotation(double newRotation)
363     {
364         for (Iterator JavaDoc it = getDrawComponents().iterator(); it.hasNext(); ) {
365             DrawComponent dc = (DrawComponent) it.next();
366             dc.setRotationMember(newRotation);
367         }
368         super.setRotation(newRotation);
369     }
370     
371     /**
372      * transforms this container and all its children
373      * @see DrawComponent#transform(AffineTransform)
374      */

375     public void transform(AffineTransform JavaDoc at)
376     {
377         for (Iterator JavaDoc it = getDrawComponents().iterator(); it.hasNext(); ) {
378             DrawComponent dc = (DrawComponent) it.next();
379 // dc.transform(at);
380
dc.transform(at, true);
381         }
382         super.transform(at);
383     }
384
385 // /**
386
// * transforms this container and all its children
387
// * @see DrawComponent#transform(AffineTransform)
388
// */
389
// public void transform(AffineTransform at, boolean fromParent)
390
// {
391
// for (Iterator it = getDrawComponents().iterator(); it.hasNext(); ) {
392
// DrawComponent dc = (DrawComponent) it.next();
393
//// dc.transform(at);
394
// dc.transform(at, true);
395
// }
396
// super.transform(at, fromParent);
397
// }
398

399 // public Object clone()
400
// {
401
// DrawComponentContainerImpl container = (DrawComponentContainerImpl) super.clone();
402
// container.drawComponents = new ArrayList(getDrawComponents().size());
403
//
404
// // make a temporary List to avoid concurrent modification
405
// List tempList = new ArrayList(getDrawComponents().size());
406
// for (Iterator itTemp = getDrawComponents().iterator(); itTemp.hasNext(); ) {
407
// tempList.add(itTemp.next());
408
// }
409
//
410
// // clone all children and add them to the drawComponents-List
411
// for (Iterator it = tempList.iterator(); it.hasNext(); ) {
412
// DrawComponent child = (DrawComponent) it.next();
413
// DrawComponent clone = (DrawComponent) child.clone();
414
//
415
// container.primAddDrawComponent(clone);
416
// }
417
// return container;
418
// }
419

420     public Object JavaDoc clone(DrawComponentContainer parent)
421     {
422         DrawComponentContainerImpl container = (DrawComponentContainerImpl) super.clone(parent);
423         container.drawComponents = new ArrayList JavaDoc(getDrawComponents().size());
424         
425         // clone all children and add them to the drawComponents-List
426
for (Iterator JavaDoc it = getDrawComponents().iterator(); it.hasNext(); ) {
427             DrawComponent child = (DrawComponent) it.next();
428             DrawComponent clone = (DrawComponent) child.clone(container);
429         }
430         return container;
431     }
432     
433 // /**
434
// * clones this container and all its children
435
// * @see DrawComponent#clone()
436
// */
437
// public DrawComponent clone()
438
// {
439
// DrawComponentContainer container = new DrawComponentContainerImpl();
440
// container = (DrawComponentContainer) assign(container);
441
// return container;
442
// }
443
//
444
// protected DrawComponent assign(DrawComponent dc)
445
// {
446
// super.assign(dc);
447
// if (dc instanceof DrawComponentContainer) {
448
// DrawComponentContainer container = (DrawComponentContainer) dc;
449
// for (Iterator it = getDrawComponents().iterator(); it.hasNext(); ) {
450
// DrawComponent child = (DrawComponent) it.next();
451
// DrawComponent clone = child.clone();
452
// container.addDrawComponent(clone);
453
// }
454
// }
455
// return dc;
456
// }
457
} //DrawComponentContainerImpl
458
Popular Tags