1 26 27 package org.nightlabs.editor2d.impl; 28 29 import java.awt.Color ; 30 import java.awt.Font ; 31 import java.awt.font.FontRenderContext ; 32 import java.awt.font.GlyphVector ; 33 import java.awt.font.TextLayout ; 34 import java.awt.geom.AffineTransform ; 35 import java.awt.geom.Point2D ; 36 import java.awt.geom.Rectangle2D ; 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 TEXT_EDEFAULT = "text"; 54 protected String 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 FONT_NAME_EDEFAULT = null; 66 protected String fontName = FONT_NAME_EDEFAULT; 67 68 public static final Font FONT_EDEFAULT = new Font ("Arial", Font.PLAIN, 12); 69 protected Font font = FONT_EDEFAULT; 70 71 public static final FontRenderContext FONT_RENDER_CONTEXT_EDEFAULT = new FontRenderContext (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 _text, Font _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 _text, String 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 protected TransformListener transformListener = new TransformListener() 124 { 125 public void transformChanged(AffineTransform at) 126 { 127 double scaleY = at.getScaleY(); 128 int oldFontSize = fontSize; 129 if (scaleY != 1) 130 { 131 int newFontSize = (int)((double)oldFontSize * scaleY); 132 LOGGER.debug("transform Changed!"); 134 LOGGER.debug("newFontSize = "+newFontSize); 135 } 136 } 137 }; 138 139 protected Point2D convertXY(float x, float y) 146 { 147 TextLayout layout = new TextLayout (text, font, FONT_RENDER_CONTEXT_EDEFAULT); 148 Rectangle2D textBounds = layout.getBounds(); 149 int newY = convertFloat(y) + convertDouble(textBounds.getHeight()); 150 return new Point2D.Float (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 _glyphVector, float _x, float _y) 162 { 163 Point2D p = convertXY(_x, _y); 164 _y = (float) p.getY(); 165 return new GeneralShape(_glyphVector.getOutline(_x, _y)); 166 } 167 168 173 protected GlyphVector createGlyphVector(String text, FontRenderContext frc, Font f) 174 { 175 return f.createGlyphVector(frc, text); 176 } 177 178 protected FontRenderContext frc = FONT_RENDER_CONTEXT_EDEFAULT; 180 public FontRenderContext 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 (fontName, fontStyle, fontSize); 194 } 195 196 protected void update() 197 { 198 updateFont(); 199 FontRenderContext 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 221 public String getText() { 222 return text; 223 } 224 225 230 public void setText(String newText) 231 { 232 String oldText = text; 233 text = newText; 234 name.setText(getLanguageId(), text); 236 237 if (!newText.equals(oldText)) { 238 update(); 239 firePropertyChange(PROP_TEXT, oldText, text); 240 } 241 } 242 243 248 public void setFont(Font newFont) 249 { 250 Font oldFont = font; 251 font = newFont; 252 update(); 253 firePropertyChange(PROP_FONT, oldFont, font); 254 } 255 256 261 public int getFontSize() { 262 return fontSize; 263 } 264 265 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 286 public boolean isBold() { 287 return bold; 288 } 289 290 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 308 public boolean isItalic() { 309 return italic; 310 } 311 312 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 330 public String getFontName() { 331 return fontName; 332 } 333 334 339 public void setFontName(String newFontName) 340 { 341 String oldFontName = fontName; 342 fontName = newFontName; 343 update(); 344 firePropertyChange(PROP_FONT_NAME, oldFontName, fontName); 345 } 346 347 352 public Font getFont() { 353 return font; 354 } 355 356 361 public String toString() 362 { 363 StringBuffer result = new StringBuffer (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 at, boolean fromParent) 381 { 382 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 405 public Object clone(DrawComponentContainer parent) 406 { 407 TextDrawComponentImpl text = (TextDrawComponentImpl) super.clone(parent); 408 text.font = new Font (fontName, fontStyle, fontSize); 409 text.fontName = new String (fontName); 410 return text; 411 } 412 413 } | Popular Tags |