1 7 package com.nightlabs.editor2d.impl; 8 9 import java.awt.Color ; 10 import java.awt.Font ; 11 import java.awt.Rectangle ; 12 import java.awt.font.FontRenderContext ; 13 import java.awt.font.GlyphVector ; 14 import java.awt.geom.AffineTransform ; 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 TEXT_EDEFAULT = "text"; 25 protected String 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 FONT_NAME_EDEFAULT = null; 37 protected String fontName = FONT_NAME_EDEFAULT; 38 39 protected static final Font FONT_EDEFAULT = new Font ("Arial", Font.PLAIN, 12); 40 protected Font font = FONT_EDEFAULT; 41 42 public TextDrawComponentImpl() { 43 super(); 44 } 45 46 48 protected int fontStyle = Font.PLAIN; 49 50 public TextDrawComponentImpl(String _text, Font _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 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 at) 75 { 76 double scaleY = at.getScaleY(); 77 int oldFontSize = fontSize; 78 if (scaleY != 1) 79 { 80 fontSize = (int)((double)fontSize * scaleY); 81 82 } 86 } 87 }; 88 89 protected GeneralShape createTextShape(GlyphVector _glyphVector, float _x, float _y) 90 { 91 return new GeneralShape(_glyphVector.getOutline(_x, _y)); 92 } 93 94 protected GlyphVector createGlyphVector(String text, FontRenderContext frc, Font f) 95 { 96 return f.createGlyphVector(frc, text); 97 } 98 99 protected FontRenderContext frc; 101 protected FontRenderContext getFontRenderContext() 102 { 103 if (frc == null) 104 frc = new FontRenderContext (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 (fontName, fontStyle, fontSize); 118 } 119 120 protected int baseLineY = -1; 121 protected void update() 122 { 123 int oldX = getX(); 126 int oldY = getY(); 127 129 updateFont(); 130 FontRenderContext frc = getFontRenderContext(); 131 137 primSetGeneralShape(createTextShape(createGlyphVector(text, frc, font), getX(), getY())); 139 generalShape.addTransformListener(transformListener); 140 primSetY(oldY); 141 142 if (getRotation() != 0) 143 { 144 double oldRotation = getRotation(); 146 rotation = 0; 147 primSetRotation(oldRotation); 148 rotation = oldRotation; 149 } 150 } 151 152 157 public String getText() { 158 return text; 159 } 160 161 166 public void setText(String newText) 167 { 168 String oldText = text; 169 text = newText; 170 name.setText(getLanguageId(), text); 172 173 if (!newText.equals(oldText)) { 174 update(); 175 firePropertyChange(PROP_TEXT, oldText, text); 176 } 177 } 178 179 184 public void setFont(Font newFont) 185 { 186 Font oldFont = font; 187 font = newFont; 188 update(); 189 firePropertyChange(PROP_FONT, oldFont, font); 190 } 191 192 197 public int getFontSize() { 198 return fontSize; 199 } 200 201 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 222 public boolean isBold() { 223 return bold; 224 } 225 226 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 244 public boolean isItalic() { 245 return italic; 246 } 247 248 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 266 public String getFontName() { 267 return fontName; 268 } 269 270 275 public void setFontName(String newFontName) 276 { 277 String oldFontName = fontName; 278 fontName = newFontName; 279 update(); 280 firePropertyChange(PROP_FONT_NAME, oldFontName, fontName); 281 } 282 283 288 public Font getFont() { 289 return font; 290 } 291 292 297 public String toString() 298 { 299 StringBuffer result = new StringBuffer (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 at) 317 { 318 Rectangle oldBounds = getBounds(); 319 if (generalShape != null) { 320 AffineTransform oldAT = affineTransform; 321 affineTransform.preConcatenate(at); 322 bounds = null; 323 generalShape = (GeneralShape) originalShape.clone(); 324 generalShape.transform(affineTransform); 328 } 329 firePropertyChange(PROP_BOUNDS, oldBounds, bounds); 330 } 331 332 } | Popular Tags |