KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
30 import java.awt.Rectangle JavaDoc;
31 import java.awt.geom.AffineTransform JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.nightlabs.editor2d.DrawComponent;
35 import org.nightlabs.editor2d.DrawComponentContainer;
36 import org.nightlabs.editor2d.ShapeDrawComponent;
37 import org.nightlabs.editor2d.j2d.GeneralShape;
38
39 public class ShapeDrawComponentImpl
40 extends DrawComponentImpl
41 implements ShapeDrawComponent
42 {
43     /**
44      * The Default Fill Color = WHITE
45      */

46   protected static final Color JavaDoc FILL_COLOR_EDEFAULT = Color.WHITE;
47   protected Color JavaDoc fillColor = FILL_COLOR_EDEFAULT;
48
49   /**
50    * The Default Line Color = BLACK
51    */

52   protected static final Color JavaDoc LINE_COLOR_EDEFAULT = Color.BLACK;
53   protected Color JavaDoc lineColor = LINE_COLOR_EDEFAULT;
54
55   /**
56    * The Default Line Style = 1
57    */

58   protected static final int LINE_STYLE_EDEFAULT = 1;
59   protected int lineStyle = LINE_STYLE_EDEFAULT;
60
61   /**
62    * The Default Line Width = 1
63    */

64   protected static final int LINE_WIDTH_EDEFAULT = 1;
65   protected int lineWidth = LINE_WIDTH_EDEFAULT;
66
67   /**
68    * The Default GeneralShape = null
69    */

70   protected static final GeneralShape GENERAL_SHAPE_EDEFAULT = null;
71   protected GeneralShape generalShape = GENERAL_SHAPE_EDEFAULT;
72
73   /**
74    * The Default Fill Behaviour = true
75    */

76   protected static final boolean FILL_EDEFAULT = true;
77   protected boolean fill = FILL_EDEFAULT;
78
79   public ShapeDrawComponentImpl() {
80         super();
81     }
82
83     /**
84      * <!-- begin-user-doc -->
85    * <!-- end-user-doc -->
86      * @generated
87      */

88   public boolean isFill() {
89         return fill;
90     }
91
92     /**
93      * <!-- begin-user-doc -->
94    * <!-- end-user-doc -->
95      * @generated
96      */

97   public void setFill(boolean newFill) {
98         boolean oldFill = fill;
99         fill = newFill;
100         firePropertyChange(PROP_FILL, oldFill, fill);
101     }
102
103     /**
104      * <!-- begin-user-doc -->
105    * <!-- end-user-doc -->
106      * @generated
107      */

108   public Color JavaDoc getFillColor() {
109         return fillColor;
110     }
111
112     /**
113      * <!-- begin-user-doc -->
114    * <!-- end-user-doc -->
115      * @generated
116      */

117   public void setFillColor(Color JavaDoc newFillColor) {
118         Color JavaDoc oldFillColor = fillColor;
119         fillColor = newFillColor;
120         firePropertyChange(PROP_FILL_COLOR, oldFillColor, fillColor);
121   }
122
123     /**
124      * <!-- begin-user-doc -->
125    * <!-- end-user-doc -->
126      * @generated
127      */

128   public Color JavaDoc getLineColor() {
129         return lineColor;
130     }
131
132     /**
133      * <!-- begin-user-doc -->
134    * <!-- end-user-doc -->
135      * @generated
136      */

137   public void setLineColor(Color JavaDoc newLineColor) {
138         Color JavaDoc oldLineColor = lineColor;
139         lineColor = newLineColor;
140         firePropertyChange(PROP_LINE_COLOR, oldLineColor, lineColor);
141     }
142
143     /**
144      * <!-- begin-user-doc -->
145    * <!-- end-user-doc -->
146      * @generated
147      */

148   public int getLineStyle() {
149         return lineStyle;
150     }
151
152     /**
153      * <!-- begin-user-doc -->
154    * <!-- end-user-doc -->
155      * @generated
156      */

157   public void setLineStyle(int newLineStyle) {
158         int oldLineStyle = lineStyle;
159         lineStyle = newLineStyle;
160         firePropertyChange(PROP_LINE_COLOR, oldLineStyle, lineStyle);
161     }
162
163     /**
164      * <!-- begin-user-doc -->
165    * <!-- end-user-doc -->
166      * @generated
167      */

168   public int getLineWidth() {
169         return lineWidth;
170     }
171
172     /**
173      * <!-- begin-user-doc -->
174    * <!-- end-user-doc -->
175      * @generated
176      */

177   public void setLineWidth(int newLineWidth) {
178         int oldLineWidth = lineWidth;
179         lineWidth = newLineWidth;
180         firePropertyChange(PROP_LINE_WIDTH, oldLineWidth, lineWidth);
181     }
182
183     /**
184      * <!-- begin-user-doc -->
185    * <!-- end-user-doc -->
186      * @generated
187      */

188   public GeneralShape getGeneralShape() {
189         return generalShape;
190     }
191
192     /**
193      * <!-- begin-user-doc -->
194    * <!-- end-user-doc -->
195      * @generated
196      */

197   public String JavaDoc toString()
198   {
199         StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
200         result.append(" (fillColor: ");
201         result.append(fillColor);
202         result.append(", lineColor: ");
203         result.append(lineColor);
204         result.append(", lineStyle: ");
205         result.append(lineStyle);
206         result.append(", lineWidth: ");
207         result.append(lineWidth);
208         result.append(", generalShape: ");
209         result.append(generalShape);
210         result.append(", fill: ");
211         result.append(fill);
212         result.append(')');
213         return result.toString();
214     }
215
216   /**
217    * @return the Bounds of the ShapeDrawComponent
218    * if a GeneralShape is present (!= null), those bounds are returned,
219    * else a new Rectangle with the values of the members x, y, width, height is returned
220    *
221    */

222   public Rectangle JavaDoc getBounds()
223   {
224     if (getGeneralShape() != null)
225       return getGeneralShape().getBounds();
226     else
227       return super.getBounds();
228   }
229    
230   protected GeneralShape originalShape = null;
231   public GeneralShape getOriginalShape() {
232     return originalShape;
233   }
234      
235   /**
236    * transforms the GeneralShape based on the given AffineTransform
237    * this is done by first cloning the originalShape and then transforming the clone,
238    * this reduces rounding errors when transforming multiple times
239    * @param at the AffineTransform which contains the transform Information
240    * @see java.awt.geom.AffineTransform
241    */

242   public void transform(AffineTransform JavaDoc at, boolean fromParent)
243   {
244     Rectangle JavaDoc oldBounds = getBounds();
245     if (generalShape != null) {
246 // super.transform(at);
247
super.transform(at, fromParent);
248       generalShape = (GeneralShape) originalShape.clone();
249       generalShape.transform(affineTransform);
250     }
251     
252     if (!fromParent && getParent() != null)
253       getParent().notifyChildTransform(this);
254
255     bounds = null;
256   }
257   
258   public void setGeneralShape(GeneralShape newGeneralShape)
259   {
260     GeneralShape oldGeneralShape = generalShape;
261     primSetGeneralShape(newGeneralShape);
262     firePropertyChange(PROP_GENERAL_SHAPE, oldGeneralShape, generalShape);
263   }
264
265   protected void primSetGeneralShape(GeneralShape gs)
266   {
267     generalShape = gs;
268     originalShape = (GeneralShape) gs.clone();
269     bounds = null;
270   }
271   
272   public Class JavaDoc getRenderModeClass() {
273     return ShapeDrawComponent.class;
274   }
275   
276 // public String getTypeName() {
277
// return ModelPlugin.getResourceString("type.shapeDrawComponent");
278
// }
279
public String JavaDoc getTypeName() {
280     return "Shape";
281   }
282
283 // public Object clone()
284
// {
285
// ShapeDrawComponentImpl sdc = (ShapeDrawComponentImpl) super.clone();
286
// sdc.primSetGeneralShape((GeneralShape) generalShape.clone());
287
// sdc.fillColor = new Color(fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue(), fillColor.getAlpha());
288
// sdc.lineColor = new Color(lineColor.getRed(), lineColor.getGreen(), lineColor.getBlue(), lineColor.getAlpha());
289
// sdc.affineTransform = new AffineTransform();
290
// return sdc;
291
// }
292

293   public Object JavaDoc clone(DrawComponentContainer parent)
294   {
295     ShapeDrawComponentImpl sdc = (ShapeDrawComponentImpl) super.clone(parent);
296     sdc.generalShape = (GeneralShape) generalShape.clone();
297     sdc.originalShape = (GeneralShape) originalShape.clone();
298     sdc.fillColor = new Color JavaDoc(fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue(), fillColor.getAlpha());
299     sdc.lineColor = new Color JavaDoc(lineColor.getRed(), lineColor.getGreen(), lineColor.getBlue(), lineColor.getAlpha());
300     
301     sdc.fill = fill;
302     sdc.lineStyle = lineStyle;
303     sdc.lineWidth = lineWidth;
304     
305     return sdc;
306   }
307     
308 // public DrawComponent clone()
309
// {
310
// ShapeDrawComponent sdc = new ShapeDrawComponentImpl();
311
// sdc = (ShapeDrawComponent) assign(sdc);
312
// return sdc;
313
// }
314

315 // protected DrawComponent assign(DrawComponent dc)
316
// {
317
// if (dc instanceof ShapeDrawComponent) {
318
// ShapeDrawComponent sdc = (ShapeDrawComponent) dc;
319
// sdc.setGeneralShape((GeneralShape)getGeneralShape().clone());
320
// sdc.setFill(isFill());
321
// sdc.setFillColor(getFillColor());
322
// sdc.setLineColor(getLineColor());
323
// sdc.setLineStyle(getLineStyle());
324
// sdc.setLineWidth(getLineWidth());
325
// }
326
// super.assign(dc);
327
// dc.setAffineTransform(new AffineTransform());
328
// return dc;
329
// }
330
} //ShapeDrawComponentImpl
331
Popular Tags