KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <copyright>
3  * </copyright>
4  *
5  * $Id: TextDrawComponentImpl.java 1634 2005-08-26 19:26:15Z nozkiller $
6  */

7 package com.nightlabs.editor2d.impl;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.Font JavaDoc;
11 import java.awt.Rectangle JavaDoc;
12 import java.awt.font.FontRenderContext JavaDoc;
13 import java.awt.font.GlyphVector JavaDoc;
14 import java.awt.geom.AffineTransform JavaDoc;
15
16 import com.nightlabs.editor2d.TextDrawComponent;
17 import com.nightlabs.editor2d.j2d.GeneralShape;
18 import com.nightlabs.editor2d.j2d.TransformListener;
19
20 public class TextDrawComponentImpl
21 extends ShapeDrawComponentImpl
22 implements TextDrawComponent
23 {
24   protected static final String JavaDoc TEXT_EDEFAULT = "text";
25   protected String JavaDoc text = TEXT_EDEFAULT;
26
27   protected static final int FONT_SIZE_EDEFAULT = 12;
28   protected int fontSize = FONT_SIZE_EDEFAULT;
29
30   protected static final boolean BOLD_EDEFAULT = false;
31   protected boolean bold = BOLD_EDEFAULT;
32
33   protected static final boolean ITALIC_EDEFAULT = false;
34   protected boolean italic = ITALIC_EDEFAULT;
35
36   protected static final String JavaDoc FONT_NAME_EDEFAULT = null;
37   protected String JavaDoc fontName = FONT_NAME_EDEFAULT;
38
39   protected static final Font JavaDoc FONT_EDEFAULT = new Font JavaDoc("Arial", Font.PLAIN, 12);
40   protected Font JavaDoc font = FONT_EDEFAULT;
41
42   public TextDrawComponentImpl() {
43         super();
44     }
45
46 // public static final Logger LOGGER = Logger.getLogger(TextDrawComponentImpl.class);
47

48   protected int fontStyle = Font.PLAIN;
49   
50   public TextDrawComponentImpl(String JavaDoc _text, Font JavaDoc _font, int _x, int _y)
51   {
52     this.text = _text;
53     this.font = _font;
54     this.x = _x;
55     this.y = _y;
56     this.fillColor = Color.BLACK;
57     this.fontName = font.getName();
58     this.fontSize = font.getSize();
59 // this.generalShape = createTextShape(createGlyphVector(text, getFontRenderContext(), font), x, y);
60
primSetGeneralShape(createTextShape(createGlyphVector(text, getFontRenderContext(), font), x, y));
61     this.name.setText(getLanguageId(), text);
62     
63     generalShape.addTransformListener(transformListener);
64   }
65   
66   public void setGeneralShape(GeneralShape newGeneralShape)
67   {
68     super.setGeneralShape(newGeneralShape);
69     generalShape.addTransformListener(transformListener);
70   }
71   
72   protected TransformListener transformListener = new TransformListener()
73   {
74     public void transformChanged(AffineTransform JavaDoc at)
75     {
76       double scaleY = at.getScaleY();
77       int oldFontSize = fontSize;
78       if (scaleY != 1)
79       {
80         fontSize = (int)((double)fontSize * scaleY);
81         
82 // LOGGER.debug("scaleY = "+scaleY);
83
// LOGGER.debug("oldFontSize = "+oldFontSize);
84
// LOGGER.debug("newFontSize = "+fontSize);
85
}
86     }
87   };
88     
89   protected GeneralShape createTextShape(GlyphVector JavaDoc _glyphVector, float _x, float _y)
90   {
91     return new GeneralShape(_glyphVector.getOutline(_x, _y));
92   }
93    
94   protected GlyphVector JavaDoc createGlyphVector(String JavaDoc text, FontRenderContext JavaDoc frc, Font JavaDoc f)
95   {
96     return f.createGlyphVector(frc, text);
97   }
98   
99   // TODO: should come from Graphics2D (Graphics2D.getFontRenderContext())
100
protected FontRenderContext JavaDoc frc;
101   protected FontRenderContext JavaDoc getFontRenderContext()
102   {
103     if (frc == null)
104       frc = new FontRenderContext JavaDoc(null, true, false);
105     
106     return frc;
107   }
108     
109   protected void updateFont()
110   {
111     fontStyle = Font.PLAIN;
112     if (isBold())
113       fontStyle = fontStyle | Font.BOLD;
114     if (isItalic())
115       fontStyle = fontStyle | Font.ITALIC;
116       
117     font = new Font JavaDoc(fontName, fontStyle, fontSize);
118   }
119   
120   protected int baseLineY = -1;
121   protected void update()
122   {
123     // TODO: use getBounds().getMaxX(), getBounds().getMaxY() instead of getX(), getY()
124
// as ReferencePoint
125
int oldX = getX();
126     int oldY = getY();
127 // int oldBaselineY = baseLineY;
128

129     updateFont();
130     FontRenderContext JavaDoc frc = getFontRenderContext();
131 // // ascent, leading etc. see OReilly Java2D Graphics S.168
132
// LineMetrics lineMetrics = font.getLineMetrics(text, frc);
133
// int ascent = (int) lineMetrics.getAscent();
134
// baseLineY = getY() + ascent;
135
// generalShape = createTextShape(createGlyphVector(text, frc, font), getX(), baseLineY);
136

137 // generalShape = createTextShape(createGlyphVector(text, frc, font), getX(), getY());
138
primSetGeneralShape(createTextShape(createGlyphVector(text, frc, font), getX(), getY()));
139     generalShape.addTransformListener(transformListener);
140     primSetY(oldY);
141
142     if (getRotation() != 0)
143     {
144       // TODO: if rotated calc oldY before rotation and use this for primSetY()
145
double oldRotation = getRotation();
146       rotation = 0;
147       primSetRotation(oldRotation);
148       rotation = oldRotation;
149     }
150   }
151
152     /**
153      * <!-- begin-user-doc -->
154    * <!-- end-user-doc -->
155      * @generated
156      */

157   public String JavaDoc getText() {
158         return text;
159     }
160
161   /**
162    * <!-- begin-user-doc -->
163    * <!-- end-user-doc -->
164    *
165    */

166   public void setText(String JavaDoc newText)
167   {
168     String JavaDoc oldText = text;
169     text = newText;
170     // TODO: combine text with ID, like old SeatingMapEditor
171
name.setText(getLanguageId(), text);
172           
173     if (!newText.equals(oldText)) {
174       update();
175       firePropertyChange(PROP_TEXT, oldText, text);
176     }
177   }
178
179   /**
180    * <!-- begin-user-doc -->
181    * <!-- end-user-doc -->
182    *
183    */

184   public void setFont(Font JavaDoc newFont)
185   {
186     Font JavaDoc oldFont = font;
187     font = newFont;
188     update();
189     firePropertyChange(PROP_FONT, oldFont, font);
190   }
191
192     /**
193      * <!-- begin-user-doc -->
194    * <!-- end-user-doc -->
195      * @generated
196      */

197   public int getFontSize() {
198         return fontSize;
199     }
200
201   /**
202    * <!-- begin-user-doc -->
203    * <!-- end-user-doc -->
204    *
205    */

206   public void setFontSize(int newFontSize)
207   {
208     int oldFontSize = fontSize;
209     fontSize = newFontSize;
210     
211     if (oldFontSize != newFontSize) {
212       update();
213       firePropertyChange(PROP_FONT_SIZE, oldFontSize, fontSize);
214     }
215   }
216
217     /**
218      * <!-- begin-user-doc -->
219    * <!-- end-user-doc -->
220      * @generated
221      */

222   public boolean isBold() {
223         return bold;
224     }
225
226   /**
227    * <!-- begin-user-doc -->
228    * <!-- end-user-doc -->
229    *
230    */

231   public void setBold(boolean newBold)
232   {
233     boolean oldBold = bold;
234     bold = newBold;
235     update();
236     firePropertyChange(PROP_BOLD, oldBold, bold);
237   }
238
239     /**
240      * <!-- begin-user-doc -->
241    * <!-- end-user-doc -->
242      * @generated
243      */

244   public boolean isItalic() {
245         return italic;
246     }
247
248   /**
249    * <!-- begin-user-doc -->
250    * <!-- end-user-doc -->
251    *
252    */

253   public void setItalic(boolean newItalic)
254   {
255     boolean oldItalic = italic;
256     italic = newItalic;
257     update();
258     firePropertyChange(PROP_ITALIC, oldItalic, italic);
259   }
260
261     /**
262      * <!-- begin-user-doc -->
263    * <!-- end-user-doc -->
264      * @generated
265      */

266   public String JavaDoc getFontName() {
267         return fontName;
268     }
269
270   /**
271    * <!-- begin-user-doc -->
272    * <!-- end-user-doc -->
273    *
274    */

275   public void setFontName(String JavaDoc newFontName)
276   {
277     String JavaDoc oldFontName = fontName;
278     fontName = newFontName;
279     update();
280     firePropertyChange(PROP_FONT_NAME, oldFontName, fontName);
281   }
282
283     /**
284      * <!-- begin-user-doc -->
285    * <!-- end-user-doc -->
286      * @generated
287      */

288   public Font JavaDoc getFont() {
289         return font;
290     }
291
292     /**
293      * <!-- begin-user-doc -->
294    * <!-- end-user-doc -->
295      * @generated
296      */

297   public String JavaDoc toString()
298   {
299         StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
300         result.append(" (text: ");
301         result.append(text);
302         result.append(", fontSize: ");
303         result.append(fontSize);
304         result.append(", bold: ");
305         result.append(bold);
306         result.append(", italic: ");
307         result.append(italic);
308         result.append(", fontName: ");
309         result.append(fontName);
310         result.append(", font: ");
311         result.append(font);
312         result.append(')');
313         return result.toString();
314     }
315
316   public void transform(AffineTransform JavaDoc at)
317   {
318     Rectangle JavaDoc oldBounds = getBounds();
319     if (generalShape != null) {
320       AffineTransform JavaDoc oldAT = affineTransform;
321       affineTransform.preConcatenate(at);
322       bounds = null;
323       generalShape = (GeneralShape) originalShape.clone();
324       // TODO: calc FontScale direct from at and not from listener
325
// not working anymore with transforming always original with only one matrix
326
// generalShape.addTransformListener(transformListener);
327
generalShape.transform(affineTransform);
328     }
329     firePropertyChange(PROP_BOUNDS, oldBounds, bounds);
330   }
331   
332 } //TextDrawComponentImpl
333
Popular Tags