Author: rwhitcomb Date: Fri Jan 18 21:24:19 2013 New Revision: 1435351 URL: http://svn.apache.org/viewvc?rev=1435351&view=rev Log: PIVOT-889: Allow newline characters in Label text to do a hard line break when "wrapText" is set true.
I think this is "what you would expect" in this case, so I'm not doing a new style (even for backward compatibility). If someone feels it is necessary it wouldn't be hard to add such a style. Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java?rev=1435351&r1=1435350&r2=1435351&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java Fri Jan 18 21:24:19 2013 @@ -127,22 +127,29 @@ public class LabelSkin extends Component int i = 0; while (i < n) { char c = text.charAt(i); - if (Character.isWhitespace(c)) { - lastWhitespaceIndex = i; - } - - Rectangle2D characterBounds = font.getStringBounds(text, i, i + 1, - fontRenderContext); - lineWidth += characterBounds.getWidth(); - - if (lineWidth > width - && lastWhitespaceIndex != -1) { - i = lastWhitespaceIndex; - + if (c == '\n') { lineWidth = 0; lastWhitespaceIndex = -1; preferredHeight += lineHeight; + } else { + if (Character.isWhitespace(c)) { + lastWhitespaceIndex = i; + } + + Rectangle2D characterBounds = font.getStringBounds(text, i, i + 1, + fontRenderContext); + lineWidth += characterBounds.getWidth(); + + if (lineWidth > width + && lastWhitespaceIndex != -1) { + i = lastWhitespaceIndex; + + lineWidth = 0; + lastWhitespaceIndex = -1; + + preferredHeight += lineHeight; + } } i++; @@ -244,21 +251,29 @@ public class LabelSkin extends Component StringCharacterIterator ci = new StringCharacterIterator(text); while (i < n) { char c = text.charAt(i); - if (Character.isWhitespace(c)) { - lastWhitespaceIndex = i; - } + if (c == '\n') { + appendLine(text, start, i, fontRenderContext); - Rectangle2D characterBounds = font.getStringBounds(ci, i, i + 1, fontRenderContext); - lineWidth += characterBounds.getWidth(); - - if (lineWidth > width - && lastWhitespaceIndex != -1) { - appendLine(text, start, lastWhitespaceIndex, fontRenderContext); - - i = lastWhitespaceIndex; start = i + 1; lineWidth = 0; lastWhitespaceIndex = -1; + } else { + if (Character.isWhitespace(c)) { + lastWhitespaceIndex = i; + } + + Rectangle2D characterBounds = font.getStringBounds(ci, i, i + 1, fontRenderContext); + lineWidth += characterBounds.getWidth(); + + if (lineWidth > width + && lastWhitespaceIndex != -1) { + appendLine(text, start, lastWhitespaceIndex, fontRenderContext); + + i = lastWhitespaceIndex; + start = i + 1; + lineWidth = 0; + lastWhitespaceIndex = -1; + } } i++; @@ -647,6 +662,8 @@ public class LabelSkin extends Component * Sets whether the text of the label will be wrapped to fit the Label's width. * Note that for wrapping to occur, the Label must specify a preferred width or * be placed in a container that constrains its width. + * Also note that newline characters (if wrapping is set true) will cause a hard + * line break. */ public void setWrapText(boolean wrapText) { this.wrapText = wrapText;