Author: rwhitcomb Date: Fri Nov 16 18:54:12 2012 New Revision: 1410537 URL: http://svn.apache.org/viewvc?rev=1410537&view=rev Log: Part of PIVOT-696: Add support for viewing tab characters in TextArea.
Already the TerraTextAreaSkin has a "tabWidth" style, and supports Ctrl-Tab entering a "tab" which is a number of spaces up to the tab width. This change extends that to the "setText" method. The TextArea.Skin interface adds a "getTabWidth" method so the component can know what the current setting is. Then in "setText", keep track of the current position in the line (since the last \n) and when a \t is encountered in the stream, convert it to a number of spaces according to the position and the tabWidth setting. The combined effect is that files with tab characters will look the same as with editors that really support tabs (albeit with the tabs replaced by the appropriate number of spaces). This is a merge of revision 1410536 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/TextArea.java pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java Propchange: pivot/branches/2.0.x/ ------------------------------------------------------------------------------ Merged /pivot/trunk:r1410536 Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/TextArea.java URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/TextArea.java?rev=1410537&r1=1410536&r2=1410537&view=diff ============================================================================== --- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/TextArea.java (original) +++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/TextArea.java Fri Nov 16 18:54:12 2012 @@ -283,6 +283,12 @@ public class TextArea extends Component * @param index */ public Bounds getCharacterBounds(int index); + + /** + * Returns the current setting of the "tabWidth" style + * (so "setText" uses the same value as Ctrl-Tab from user). + */ + public int getTabWidth(); } /** @@ -709,6 +715,8 @@ public class TextArea extends Component int characterCountLocal = 0; Paragraph paragraph = new Paragraph(); + int tabPosition = 0; + int tabWidth = ((TextArea.Skin)getSkin()).getTabWidth(); int c = textReader.read(); while (c != -1) { @@ -719,8 +727,19 @@ public class TextArea extends Component if (c == '\n') { paragraphsLocal.add(paragraph); paragraph = new Paragraph(); + tabPosition = 0; + } else if (c == '\t') { + int spaces = tabWidth - (tabPosition % tabWidth); + for (int i = 0; i < spaces; i++) { + if (++characterCountLocal > maximumLength) { + throw new IllegalArgumentException("Text length is greater than maximum length."); + } + paragraph.append(' '); + tabPosition++; + } } else { paragraph.append((char)c); + tabPosition++; } c = textReader.read(); Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java?rev=1410537&r1=1410536&r2=1410537&view=diff ============================================================================== --- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java (original) +++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java Fri Nov 16 18:54:12 2012 @@ -771,6 +771,7 @@ public class TextAreaSkin extends Compon this.acceptsEnter = acceptsEnter; } + @Override public int getTabWidth() { return tabWidth; }