Author: rwhitcomb Date: Sun Aug 9 00:53:55 2015 New Revision: 1694851 URL: http://svn.apache.org/r1694851 Log: Code cleanup: Allow ComponentNode inside a TextPane to contribute its text to the overall text of the component. Do this by extracting whatever text I can get from whatever component is being used. The only exceptions are ListView, TableView, etc. which have lists of data. Otherwise Containers search their children, and Label, Button, TextInput, TextArea and TextPane all contribute their text.
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ComponentNode.java pivot/trunk/wtk/src/org/apache/pivot/wtk/text/PlainTextSerializer.java Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java?rev=1694851&r1=1694850&r2=1694851&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java Sun Aug 9 00:53:55 2015 @@ -705,6 +705,8 @@ public class TextPane extends Container for (Node node : element) { if (node instanceof TextNode) { text.append(((TextNode) node).getCharacters()); + } else if (node instanceof ComponentNode) { + text.append(((ComponentNode) node).getText()); } else if (node instanceof Element) { addToText(text, (Element) node); } Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ComponentNode.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ComponentNode.java?rev=1694851&r1=1694850&r2=1694851&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ComponentNode.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ComponentNode.java Sun Aug 9 00:53:55 2015 @@ -17,7 +17,14 @@ package org.apache.pivot.wtk.text; import org.apache.pivot.util.ListenerList; +import org.apache.pivot.wtk.Button; import org.apache.pivot.wtk.Component; +import org.apache.pivot.wtk.Container; +import org.apache.pivot.wtk.Label; +import org.apache.pivot.wtk.TextArea; +import org.apache.pivot.wtk.TextInput; +import org.apache.pivot.wtk.TextPane; +import org.apache.pivot.wtk.content.ButtonData; /** * Node representing a live pivot component. @@ -62,14 +69,51 @@ public class ComponentNode extends Block } } + public String getText() { + return getText(this.component); + } + + private String getText(Component comp) { + if (comp instanceof TextInput) { + return ((TextInput)comp).getText(); + } else if (comp instanceof TextArea) { + return ((TextArea)comp).getText(); + } else if (comp instanceof TextPane) { + return ((TextPane)comp).getText(); + } else if (comp instanceof Label) { + return ((Label)comp).getText(); + } else if (comp instanceof Button) { + Object buttonData = ((Button)comp).getButtonData(); + if (buttonData instanceof ButtonData) { + return ((ButtonData)buttonData).getText(); + } else if (buttonData instanceof String) { + return (String)buttonData; + } else { + return buttonData.toString(); + } + } else if (comp instanceof Container) { + StringBuilder buf = new StringBuilder(); + for (Component child : (Container)comp) { + buf.append(getText(child)); + } + return buf.toString(); + } + return ""; + } + @Override public char getCharacterAt(int offset) { - return 0x00; + String componentText = getText(); + if (offset < 0 || offset >= componentText.length()) { + throw new IndexOutOfBoundsException(); + } + return componentText.charAt(offset); } @Override public int getCharacterCount() { - return 1; + String componentText = getText(); + return componentText.length(); } @Override @@ -84,11 +128,13 @@ public class ComponentNode extends Block @Override public Element getRange(int offset, int characterCount) { - if (offset < 0 || offset > 1) { + // Note: only supports getting the complete range of text + String componentText = getText(); + if (offset < 0 || offset >= componentText.length()) { throw new IndexOutOfBoundsException(); } - if (characterCount != 1) { + if (characterCount != componentText.length()) { throw new IllegalArgumentException("Invalid characterCount."); } Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/PlainTextSerializer.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/PlainTextSerializer.java?rev=1694851&r1=1694850&r2=1694851&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/PlainTextSerializer.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/PlainTextSerializer.java Sun Aug 9 00:53:55 2015 @@ -138,7 +138,11 @@ public class PlainTextSerializer impleme BufferedWriter bufferedWriter = new BufferedWriter(writer, BUFFER_SIZE); - if (object instanceof Element) { + if (object instanceof ComponentNode) { + ComponentNode compNode = (ComponentNode) object; + bufferedWriter.write(compNode.getText()); + bufferedWriter.newLine(); + } else if (object instanceof Element) { Element element = (Element) object; for (Node node : element) {