Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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).


git-svn-id: https://svn.apache.org/repos/asf/pivot/trunk@1410536 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 0ba3f2ccd566bc46f64983d54cad1468644ba03a 1 parent a2c7d17
Roger Lee Whitcomb authored
19 wtk/src/org/apache/pivot/wtk/TextArea.java
View
@@ -283,6 +283,12 @@ public void textRemoved(Paragraph paragraph, int index, int count) {
* @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 void setText(Reader textReader) throws IOException {
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 void setText(Reader textReader) throws IOException {
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();
1  wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java
View
@@ -771,6 +771,7 @@ public void setAcceptsEnter(boolean acceptsEnter) {
this.acceptsEnter = acceptsEnter;
}
+ @Override
public int getTabWidth() {
return tabWidth;
}
Please sign in to comment.
Something went wrong with that request. Please try again.