You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/09/06 21:34:04 UTC

svn commit: r993133 - in /pivot/trunk: core/test/org/apache/pivot/json/test/JSONSerializerTest.java tests/src/org/apache/pivot/tests/sample.txt tests/src/org/apache/pivot/tests/text_area_test.bxml wtk/src/org/apache/pivot/wtk/TextArea2.java

Author: gbrown
Date: Mon Sep  6 19:34:04 2010
New Revision: 993133

URL: http://svn.apache.org/viewvc?rev=993133&view=rev
Log:
Initial testing and bug fixing on new TextArea; add a simple test case to retrieve the count of a map object using JSON.get().

Modified:
    pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java
    pivot/trunk/tests/src/org/apache/pivot/tests/sample.txt
    pivot/trunk/tests/src/org/apache/pivot/tests/text_area_test.bxml
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea2.java

Modified: pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java?rev=993133&r1=993132&r2=993133&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java Mon Sep  6 19:34:04 2010
@@ -106,6 +106,7 @@ public class JSONSerializerTest {
 
         jsonSerializer.getJSONSerializerListeners().add(jsonSerializerListener);
         Object o1 = jsonSerializer.readObject(getClass().getResourceAsStream("sample.json"));
+        assertEquals(JSON.get(o1, "count"), 6);
 
         jsonSerializer.getJSONSerializerListeners().remove(jsonSerializerListener);
         Object o2 = jsonSerializer.readObject(getClass().getResourceAsStream("sample.json"));

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/sample.txt
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/sample.txt?rev=993133&r1=993132&r2=993133&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/sample.txt (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/sample.txt Mon Sep  6 19:34:04 2010
@@ -1,11 +1,2 @@
-Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
-
-Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
-
-Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
-
-Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
-
-Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
-
-Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
+ABCD
+EFG
\ No newline at end of file

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/text_area_test.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/text_area_test.bxml?rev=993133&r1=993132&r2=993133&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/text_area_test.bxml (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/text_area_test.bxml Mon Sep  6 19:34:04 2010
@@ -21,11 +21,26 @@ limitations under the License.
     xmlns="org.apache.pivot.wtk">
     <windowStateListeners>
     function windowOpened(window) {
-        textArea.insertText(",", 3);
+        textArea.insertText("Hello\nWorld", 5);
+        textArea.insertText("Foo\n", 11);
+        textArea.removeText(2, 5);
     }
     </windowStateListeners>
 
     <ScrollPane>
-        <TextArea2 bxml:id="textArea" text="ABC"/>
+        <TextArea2 bxml:id="textArea" text="@sample.txt">
+            <textAreaContentListeners>
+            <![CDATA[
+            function textChanged(textArea) {
+                java.lang.System.out.println(textArea.text + "\n-");
+
+                for (var i = 0, n = textArea.paragraphs.length; i < n; i++) {
+                    var paragraph = textArea.paragraphs.get(i);
+                    java.lang.System.out.println("[" + i + "] " + paragraph.offset);
+                }
+            }
+            ]]>
+            </textAreaContentListeners>
+        </TextArea2>
     </ScrollPane>
 </Window>

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea2.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea2.java?rev=993133&r1=993132&r2=993133&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea2.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea2.java Mon Sep  6 19:34:04 2010
@@ -95,7 +95,8 @@ public class TextArea2 extends Component
             int count = text.length();
 
             if (count > 0) {
-                if (textArea.characterCount + count > textArea.maximumLength) {
+                if (textArea != null
+                    && textArea.characterCount + count > textArea.maximumLength) {
                     throw new IllegalArgumentException("Insertion of text would exceed maximum length.");
                 }
 
@@ -297,12 +298,10 @@ public class TextArea2 extends Component
                 throw new IllegalArgumentException("paragraph is already in use by another text area.");
             }
 
-            // Determine insertion count
-            CharSequence characters = paragraph.getCharacters();
-            int characterCount = characters.length();
+            // Determine insertion count, including terminator character
+            int characterCount = paragraph.characters.length();
 
-            // Include terminator character
-            if (index < getLength() - 1) {
+            if (getLength() > 0) {
                 characterCount++;
             }
 
@@ -310,23 +309,30 @@ public class TextArea2 extends Component
                 throw new IllegalArgumentException("Insertion of text would exceed maximum length.");
             }
 
-            paragraphs.insert(paragraph, index);
-            paragraph.textArea = TextArea2.this;
-
-            // Update offsets and character count
-            if (index == 0) {
-                paragraph.offset = 0;
+            // Set the paragraph offset
+            if (index == paragraphs.getLength()) {
+                paragraph.offset = TextArea2.this.characterCount;
+
+                // Include terminator character
+                if (index > 0) {
+                    paragraph.offset++;
+                }
             } else {
                 paragraph.offset = paragraphs.get(index).offset;
             }
 
+            // Insert the paragraph
+            paragraphs.insert(paragraph, index);
+            paragraph.textArea = TextArea2.this;
+
+            // Update offsets and character count
             updateParagraphOffsets(index + 1, characterCount);
             TextArea2.this.characterCount += characterCount;
 
             // Update selection state
             int previousSelectionStart = selectionStart;
             int previousSelectionLength = selectionLength;
-            selectionStart = paragraph.offset + characters.length();
+            selectionStart = paragraph.offset + paragraph.characters.length();
             selectionLength = 0;
 
             // Fire change events
@@ -359,20 +365,15 @@ public class TextArea2 extends Component
             if (count > 0) {
                 int characterCount = 0;
                 for (int i = 0, n = removed.getLength(); i < n; i++) {
-                    removed.get(i).textArea = null;
-
-                    // Update character count
                     Paragraph paragraph = removed.get(i);
-                    CharSequence characters = paragraph.getCharacters();
-                    characterCount += characters.length();
-
-                    // Include terminator character
-                    if (index + i < getLength() - 1) {
-                        characterCount++;
-                    }
+                    paragraph.textArea = null;
+                    paragraph.offset = -1;
+                    characterCount += paragraph.characters.length();
                 }
 
-                // Update offsets and character count
+                // Update offsets and character count, including terminator characters
+                characterCount -= count - 1;
+
                 updateParagraphOffsets(index, -characterCount);
                 TextArea2.this.characterCount -= characterCount;
 
@@ -561,21 +562,20 @@ public class TextArea2 extends Component
         // Get paragraph and character offset at beginIndex
         int paragraphIndex = getParagraphAt(beginIndex);
         Paragraph paragraph = paragraphs.get(paragraphIndex);
-        CharSequence characters = paragraph.getCharacters();
 
         int characterOffset = beginIndex - paragraph.offset;
 
         // Read characters until endIndex is reached, appending to text builder
         // and moving to next paragraph as needed
         for (int i = 0; i < count; i++) {
-            textBuilder.append(characters.charAt(characterOffset++));
+            textBuilder.append(paragraph.characters.charAt(characterOffset++));
 
-            if (characterOffset == characters.length()) {
+            if (characterOffset == paragraph.characters.length()
+                && i < count - 1) {
                 textBuilder.append('\n');
+                i++;
 
                 paragraph = paragraphs.get(++paragraphIndex);
-                characters = paragraph.getCharacters();
-
                 characterOffset = 0;
             }
         }
@@ -593,10 +593,6 @@ public class TextArea2 extends Component
             throw new IllegalArgumentException();
         }
 
-        if (text.length() > maximumLength) {
-            throw new IllegalArgumentException("Text length is greater than maximum length.");
-        }
-
         try {
             setText(new StringReader(text));
         } catch (IOException exception) {
@@ -634,7 +630,7 @@ public class TextArea2 extends Component
         int c = textReader.read();
         while (c != -1) {
             if (++characterCount > maximumLength) {
-                throw new IllegalArgumentException("Insertion of text would exceed maximum length.");
+                throw new IllegalArgumentException("Text length is greater than maximum length.");
             }
 
             if (c == '\n') {
@@ -681,10 +677,9 @@ public class TextArea2 extends Component
 
                 if (c == '\n') {
                     // Split paragraph at current offset
-                    CharSequence characters = paragraph.getCharacters();
-                    int count = characters.length();
+                    int count = paragraph.characters.length();
 
-                    CharSequence trailingCharacters = characters.subSequence(characterOffset, count);
+                    CharSequence trailingCharacters = paragraph.characters.subSequence(characterOffset, count);
                     paragraph.removeText(characterOffset, count);
                     paragraph.insertText(textBuilder, characterOffset);
 
@@ -726,10 +721,10 @@ public class TextArea2 extends Component
             endParagraph.removeText(0, (index + count) - endParagraph.offset);
 
             // Remove intervening paragraphs
-            paragraphSequence.remove(beginParagraphIndex, endParagraphIndex - beginParagraphIndex);
+            paragraphSequence.remove(beginParagraphIndex, (endParagraphIndex - 1) - beginParagraphIndex);
 
             // Remove leading text
-            beginParagraph.removeText(index - beginParagraph.offset, beginParagraph.getCharacters().length());
+            beginParagraph.removeText(index - beginParagraph.offset, beginParagraph.characters.length());
         }
     }
 
@@ -783,9 +778,9 @@ public class TextArea2 extends Component
         Paragraph paragraph = paragraphs.get(paragraphIndex);
 
         int characterOffset = index - paragraph.offset;
-        CharSequence characters = paragraph.getCharacters();
 
-        return (characterOffset == characters.length()) ? '\n' : characters.charAt(characterOffset);
+        return (characterOffset == paragraph.characters.length()) ?
+            '\n' : paragraph.characters.charAt(characterOffset);
     }
 
     /**