You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2013/01/18 22:26:59 UTC

svn commit: r1435352 - in /pivot/branches/2.0.x: ./ wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java

Author: rwhitcomb
Date: Fri Jan 18 21:26:59 2013
New Revision: 1435352

URL: http://svn.apache.org/viewvc?rev=1435352&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.

This is a merge of revision 1435351 from trunk to branches/2.0.x.

Modified:
    pivot/branches/2.0.x/   (props changed)
    pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java

Propchange: pivot/branches/2.0.x/
------------------------------------------------------------------------------
  Merged /pivot/trunk:r1435351

Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java?rev=1435352&r1=1435351&r2=1435352&view=diff
==============================================================================
--- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java (original)
+++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java Fri Jan 18 21:26:59 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;