KickJava   Java API By Example, From Geeks To Geeks.

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


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.Font JavaDoc;
31 import java.awt.font.FontRenderContext JavaDoc;
32 import java.awt.font.GlyphVector JavaDoc;
33 import java.awt.font.TextLayout JavaDoc;
34 import java.awt.geom.AffineTransform JavaDoc;
35 import java.awt.geom.Point2D JavaDoc;
36 import java.awt.geom.Rectangle2D JavaDoc;
37
38 import org.apache.log4j.Logger;
39
40 import org.nightlabs.editor2d.DrawComponent;
41 import org.nightlabs.editor2d.DrawComponentContainer;
42 import org.nightlabs.editor2d.LineDrawComponent;
43 import org.nightlabs.editor2d.TextDrawComponent;
44 import org.nightlabs.editor2d.j2d.GeneralShape;
45 import org.nightlabs.editor2d.j2d.TransformListener;
46
47 public class TextDrawComponentImpl
48 extends ShapeDrawComponentImpl
49 implements TextDrawComponent
50 {
51     public static final Logger LOGGER = Logger.getLogger(TextDrawComponentImpl.class);
52     
53   public static final String JavaDoc TEXT_EDEFAULT = "text";
54   protected String JavaDoc text = TEXT_EDEFAULT;
55
56   public static final int FONT_SIZE_EDEFAULT = 12;
57   protected int fontSize = FONT_SIZE_EDEFAULT;
58
59   public static final boolean BOLD_EDEFAULT = false;
60   protected boolean bold = BOLD_EDEFAULT;
61
62   public static final boolean ITALIC_EDEFAULT = false;
63   protected boolean italic = ITALIC_EDEFAULT;
64
65   public static final String JavaDoc FONT_NAME_EDEFAULT = null;
66   protected String JavaDoc fontName = FONT_NAME_EDEFAULT;
67
68   public static final Font JavaDoc FONT_EDEFAULT = new Font JavaDoc("Arial", Font.PLAIN, 12);
69   protected Font JavaDoc font = FONT_EDEFAULT;
70
71   public static final FontRenderContext JavaDoc FONT_RENDER_CONTEXT_EDEFAULT = new FontRenderContext JavaDoc(null, true, false);
72   
73   public TextDrawComponentImpl() {
74         super();
75     }
76   
77   protected int fontStyle = Font.PLAIN;
78   protected int originalX = 0;
79   protected int originalY = 0;
80     
81   public TextDrawComponentImpl(String JavaDoc _text, Font JavaDoc _font, int _x, int _y)
82   {
83     this.text = _text;
84     this.font = _font;
85     this.x = _x;
86     this.y = _y;
87     originalX = x;
88     originalY = y;
89     this.fillColor = Color.BLACK;
90     this.fontName = font.getName();
91     this.fontSize = font.getSize();
92     primSetGeneralShape(createTextShape(createGlyphVector(text, getFontRenderContext(), font), x, y));
93     this.name.setText(getLanguageId(), text);
94     
95     generalShape.addTransformListener(transformListener);
96   }
97   
98   public TextDrawComponentImpl(String JavaDoc _text, String JavaDoc fontName, int fontSize, int fontStyle, int _x, int _y)
99   {
100     this.text = _text;
101     this.x = _x;
102     this.y = _y;
103     originalX = x;
104     originalY = y;
105     this.fillColor = Color.BLACK;
106     this.fontName = fontName;
107     this.fontSize = fontSize;
108     this.fontStyle = fontStyle;
109     updateFont();
110     primSetGeneralShape(createTextShape(createGlyphVector(text, getFontRenderContext(), font), x, y));
111     this.name.setText(getLanguageId(), text);
112     
113     generalShape.addTransformListener(transformListener);
114   }
115   
116   public void setGeneralShape(GeneralShape newGeneralShape)
117   {
118     super.setGeneralShape(newGeneralShape);
119     generalShape.addTransformListener(transformListener);
120   }
121   
122   // TODO: Make Text only scale uniform, then this sworks
123
protected TransformListener transformListener = new TransformListener()
124   {
125     public void transformChanged(AffineTransform JavaDoc at)
126     {
127       double scaleY = at.getScaleY();
128       int oldFontSize = fontSize;
129       if (scaleY != 1)
130       {
131         int newFontSize = (int)((double)oldFontSize * scaleY);
132 // setFontSize(newFontSize);
133
LOGGER.debug("transform Changed!");
134         LOGGER.debug("newFontSize = "+newFontSize);
135       }
136     }
137   };
138     
139 // protected Point2D convertXY(float x, float y)
140
// {
141
// Rectangle2D textBounds = font.getStringBounds(text, TextDrawComponentImpl.FONT_RENDER_CONTEXT_EDEFAULT);
142
// int newY = convertFloat(y) + convertDouble(textBounds.getHeight());
143
// return new Point2D.Float(x, newY);
144
// }
145
protected Point2D JavaDoc convertXY(float x, float y)
146   {
147     TextLayout JavaDoc layout = new TextLayout JavaDoc(text, font, FONT_RENDER_CONTEXT_EDEFAULT);
148     Rectangle2D JavaDoc textBounds = layout.getBounds();
149     int newY = convertFloat(y) + convertDouble(textBounds.getHeight());
150     return new Point2D.Float JavaDoc(x, newY);
151   }
152   
153   private int convertDouble(double d) {
154     return (int) Math.rint(d);
155   }
156   
157   private int convertFloat(float f) {
158     return (int) Math.rint(f);
159   }
160
161   protected GeneralShape createTextShape(GlyphVector JavaDoc _glyphVector, float _x, float _y)
162   {
163     Point2D JavaDoc p = convertXY(_x, _y);
164     _y = (float) p.getY();
165     return new GeneralShape(_glyphVector.getOutline(_x, _y));
166   }
167   
168 // protected GeneralShape createTextShape(GlyphVector _glyphVector, float _x, float _y)
169
// {
170
// return new GeneralShape(_glyphVector.getOutline(_x, _y));
171
// }
172

173   protected GlyphVector JavaDoc createGlyphVector(String JavaDoc text, FontRenderContext JavaDoc frc, Font JavaDoc f)
174   {
175     return f.createGlyphVector(frc, text);
176   }
177   
178   // TODO: should come from Graphics2D (Graphics2D.getFontRenderContext())
179
protected FontRenderContext JavaDoc frc = FONT_RENDER_CONTEXT_EDEFAULT;
180   public FontRenderContext JavaDoc getFontRenderContext()
181   {
182     return frc;
183   }
184     
185   protected void updateFont()
186   {
187     fontStyle = Font.PLAIN;
188     if (isBold())
189       fontStyle = fontStyle | Font.BOLD;
190     if (isItalic())
191       fontStyle = fontStyle | Font.ITALIC;
192       
193     font = new Font JavaDoc(fontName, fontStyle, fontSize);
194   }
195     
196   protected void update()
197   {
198     updateFont();
199     FontRenderContext JavaDoc frc = getFontRenderContext();
200     
201     if (generalShape.hasTransformListener())
202         generalShape.removeTransformListener(transformListener);
203     
204     primSetGeneralShape(createTextShape(createGlyphVector(text, frc, font), getX(), getY()));
205     generalShape.addTransformListener(transformListener);
206
207     if (getRotation() != 0)
208     {
209       double oldRotation = getRotation();
210       rotation = 0;
211       primSetRotation(oldRotation);
212       rotation = oldRotation;
213     }
214   }
215
216     /**
217      * <!-- begin-user-doc -->
218    * <!-- end-user-doc -->
219      * @generated
220      */

221   public String JavaDoc getText() {
222         return text;
223     }
224
225   /**
226    * <!-- begin-user-doc -->
227    * <!-- end-user-doc -->
228    *
229    */

230   public void setText(String JavaDoc newText)
231   {
232     String JavaDoc oldText = text;
233     text = newText;
234     // TODO: combine text with ID, like old SeatingMapEditor
235
name.setText(getLanguageId(), text);
236           
237     if (!newText.equals(oldText)) {
238       update();
239       firePropertyChange(PROP_TEXT, oldText, text);
240     }
241   }
242
243   /**
244    * <!-- begin-user-doc -->
245    * <!-- end-user-doc -->
246    *
247    */

248   public void setFont(Font JavaDoc newFont)
249   {
250     Font JavaDoc oldFont = font;
251     font = newFont;
252     update();
253     firePropertyChange(PROP_FONT, oldFont, font);
254   }
255
256     /**
257      * <!-- begin-user-doc -->
258    * <!-- end-user-doc -->
259      * @generated
260      */

261   public int getFontSize() {
262         return fontSize;
263     }
264
265   /**
266    * <!-- begin-user-doc -->
267    * <!-- end-user-doc -->
268    *
269    */

270   public void setFontSize(int newFontSize)
271   {
272     int oldFontSize = fontSize;
273     fontSize = newFontSize;
274     
275     if (oldFontSize != newFontSize) {
276       update();
277       firePropertyChange(PROP_FONT_SIZE, oldFontSize, fontSize);
278     }
279   }
280
281     /**
282      * <!-- begin-user-doc -->
283    * <!-- end-user-doc -->
284      * @generated
285      */

286   public boolean isBold() {
287         return bold;
288     }
289
290   /**
291    * <!-- begin-user-doc -->
292    * <!-- end-user-doc -->
293    *
294    */

295   public void setBold(boolean newBold)
296   {
297     boolean oldBold = bold;
298     bold = newBold;
299     update();
300     firePropertyChange(PROP_BOLD, oldBold, bold);
301   }
302
303     /**
304      * <!-- begin-user-doc -->
305    * <!-- end-user-doc -->
306      * @generated
307      */

308   public boolean isItalic() {
309         return italic;
310     }
311
312   /**
313    * <!-- begin-user-doc -->
314    * <!-- end-user-doc -->
315    *
316    */

317   public void setItalic(boolean newItalic)
318   {
319     boolean oldItalic = italic;
320     italic = newItalic;
321     update();
322     firePropertyChange(PROP_ITALIC, oldItalic, italic);
323   }
324
325     /**
326      * <!-- begin-user-doc -->
327    * <!-- end-user-doc -->
328      * @generated
329      */

330   public String JavaDoc getFontName() {
331         return fontName;
332     }
333
334   /**
335    * <!-- begin-user-doc -->
336    * <!-- end-user-doc -->
337    *
338    */

339   public void setFontName(String JavaDoc newFontName)
340   {
341     String JavaDoc oldFontName = fontName;
342     fontName = newFontName;
343     update();
344     firePropertyChange(PROP_FONT_NAME, oldFontName, fontName);
345   }
346
347     /**
348      * <!-- begin-user-doc -->
349    * <!-- end-user-doc -->
350      * @generated
351      */

352   public Font JavaDoc getFont() {
353         return font;
354     }
355
356     /**
357      * <!-- begin-user-doc -->
358    * <!-- end-user-doc -->
359      * @generated
360      */

361   public String JavaDoc toString()
362   {
363         StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
364         result.append(" (text: ");
365         result.append(text);
366         result.append(", fontSize: ");
367         result.append(fontSize);
368         result.append(", bold: ");
369         result.append(bold);
370         result.append(", italic: ");
371         result.append(italic);
372         result.append(", fontName: ");
373         result.append(fontName);
374         result.append(", font: ");
375         result.append(font);
376         result.append(')');
377         return result.toString();
378     }
379
380   public void transform(AffineTransform JavaDoc at, boolean fromParent)
381   {
382 // Rectangle oldBounds = getBounds();
383
if (generalShape != null) {
384         super.transform(at, fromParent);
385       generalShape = (GeneralShape) originalShape.clone();
386       if (!generalShape.hasTransformListener())
387         generalShape.addTransformListener(transformListener);
388       generalShape.transform(affineTransform);
389     }
390     
391     if (!fromParent && getParent() != null)
392       getParent().notifyChildTransform(this);
393
394     bounds = null;
395   }
396     
397 // public Object clone()
398
// {
399
// TextDrawComponentImpl text = (TextDrawComponentImpl) super.clone();
400
// text.font = new Font(fontName, fontStyle, fontSize);
401
// text.fontName = new String(fontName);
402
// return text;
403
// }
404

405   public Object JavaDoc clone(DrawComponentContainer parent)
406   {
407     TextDrawComponentImpl text = (TextDrawComponentImpl) super.clone(parent);
408     text.font = new Font JavaDoc(fontName, fontStyle, fontSize);
409     text.fontName = new String JavaDoc(fontName);
410     return text;
411   }
412     
413 // public DrawComponent clone()
414
// {
415
// TextDrawComponent text = new TextDrawComponentImpl();
416
// text = (TextDrawComponent) assign(text);
417
// return text;
418
// }
419
//
420
// protected DrawComponent assign(DrawComponent dc)
421
// {
422
// super.assign(dc);
423
// if (dc instanceof TextDrawComponent) {
424
// TextDrawComponent text = (TextDrawComponent) dc;
425
// text.setBold(isBold());
426
// text.setFont(getFont());
427
// text.setFontName(getFontName());
428
// text.setFontSize(getFontSize());
429
// text.setItalic(isItalic());
430
// }
431
// return dc;
432
// }
433
} //TextDrawComponentImpl
434
Popular Tags