Comment #2 on issue 114 by heuermh: Minor API improvements to PText
http://code.google.com/p/piccolo2d/issues/detail?id=114
Patch for some of the above:
Index: PText.java
===================================================================
--- PText.java (revision 622)
+++ PText.java (working copy)
@@ -75,12 +75,30 @@
public static final String PROPERTY_FONT = "font";
public static final int PROPERTY_CODE_FONT = 1 << 20;
- public static Font DEFAULT_FONT = new Font("Helvetica", Font.PLAIN,
12);
- public static double DEFAULT_GREEK_THRESHOLD = 5.5;
+ /**
+ * The property name that identifies a change of this node's text
paint (see
+ * {...@link #getTextPaint getTextPaint}). Both old and new value will be
set in any
+ * property change event.
+ */
+ public static final String PROPERTY_TEXT_PAINT = "text paint";
+ public static final int PROPERTY_CODE_TEXT_PAINT = 1 << 21;
- private String text;
- private Paint textPaint;
- private Font font;
+ /** Default font, 12 point <code>"SansSerif"</code>. */
+ //public static final Font DEFAULT_FONT = new Font(Font.SANS_SERIF,
Font.PLAIN,
12); jdk 1.6+
+ public static final Font DEFAULT_FONT = new Font("SansSerif",
Font.PLAIN, 12);
+
+ /** Default text paint, <code>Color.BLACK</code>. */
+ public static final Paint DEFAULT_TEXT_PAINT = Color.BLACK;
+
+ /** Default greek threshold, <code>5.5d</code>. */
+ public static double DEFAULT_GREEK_THRESHOLD = 5.5d;
+
+ /** Default text, <code>""</code>. */
+ public static final String DEFAULT_TEXT = "";
+
+ private String text = DEFAULT_TEXT;
+ private Paint textPaint = DEFAULT_TEXT_PAINT;
+ private Font font = DEFAULT_FONT;
protected double greekThreshold = DEFAULT_GREEK_THRESHOLD;
private float justification = Component.LEFT_ALIGNMENT;
private boolean constrainHeightToTextHeight = true;
@@ -89,13 +107,11 @@
public PText() {
super();
- setTextPaint(Color.BLACK);
- text = "";
}
- public PText(final String aText) {
+ public PText(final String text) {
this();
- setText(aText);
+ setText(text);
}
/**
@@ -132,8 +148,13 @@
* @param textPaint
*/
public void setTextPaint(final Paint textPaint) {
+ if (textPaint == this.textPaint) {
+ return;
+ }
+ final Paint oldTextPaint = this.textPaint;
this.textPaint = textPaint;
invalidatePaint();
+ firePropertyChange(PROPERTY_CODE_TEXT_PAINT, PROPERTY_TEXT_PAINT,
oldTextPaint, this.textPaint);
}
public boolean isConstrainWidthToTextWidth() {
@@ -191,13 +212,16 @@
* Set the text for this node. The text will be broken up into multiple
* lines based on the size of the text and the bounds width of this
node.
*/
- public void setText(final String aText) {
- final String old = text;
- text = aText == null ? "" : aText;
+ public void setText(final String text) {
+ if (text == this.text) {
+ return;
+ }
+ final String oldText = this.text;
+ this.text = text == null ? DEFAULT_TEXT : text;
lines = null;
recomputeLayout();
invalidatePaint();
- firePropertyChange(PROPERTY_CODE_TEXT, PROPERTY_TEXT, old, text);
+ firePropertyChange(PROPERTY_CODE_TEXT, PROPERTY_TEXT, oldText,
this.text);
}
/**
@@ -206,9 +230,6 @@
* @return the font of this PText.
*/
public Font getFont() {
- if (font == null) {
- font = DEFAULT_FONT;
- }
return font;
}
@@ -218,13 +239,16 @@
* node instead of changing the font size to get that same effect.
Using
* very large font sizes can slow performance.
*/
- public void setFont(final Font aFont) {
- final Font old = font;
- font = aFont;
+ public void setFont(final Font font) {
+ if (font == this.font) {
+ return;
+ }
+ final Font oldFont = this.font;
+ this.font = font == null ? DEFAULT_FONT : font;
lines = null;
recomputeLayout();
invalidatePaint();
- firePropertyChange(PROPERTY_CODE_FONT, PROPERTY_FONT, old, font);
+ firePropertyChange(PROPERTY_CODE_FONT, PROPERTY_FONT, oldFont,
this.font);
}
private static final TextLayout[] EMPTY_TEXT_LAYOUT_ARRAY = new
TextLayout[0];
--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings
--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---