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 2016/03/15 02:38:09 UTC

svn commit: r1735008 - in /pivot/trunk/wtk/src/org/apache/pivot/wtk/text: ComponentNode.java Node.java TextNode.java

Author: rwhitcomb
Date: Tue Mar 15 01:38:09 2016
New Revision: 1735008

URL: http://svn.apache.org/viewvc?rev=1735008&view=rev
Log:
Misc. TextPane changes (more):
* Add the supporting methods in ComponentNode, Node, and TextNode
  to support the "getText(start, end)" method just added to TextPane.
* Add some Javadoc (mostly about offsets being document-relative or
  node-relative).
* Correct some weird Javadoc line breaks.

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ComponentNode.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java

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=1735008&r1=1735007&r2=1735008&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 Tue Mar 15 01:38:09 2016
@@ -21,6 +21,7 @@ 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.Span;
 import org.apache.pivot.wtk.TextArea;
 import org.apache.pivot.wtk.TextInput;
 import org.apache.pivot.wtk.TextPane;
@@ -101,19 +102,30 @@ public class ComponentNode extends Block
         return "";
     }
 
+    public String getSubstring(Span range) {
+        return getText(this.component).substring(range.start, range.end + 1);
+    }
+
+    public String getSubstring(int start, int end) {
+        return getText(this.component).substring(start, end);
+    }
+
+    public CharSequence getCharacters(Span range) {
+        return getText(this.component).subSequence(range.start, range.end + 1);
+    }
+
+    public CharSequence getCharacters(int start, int end) {
+        return getText(this.component).subSequence(start, end);
+    }
+
     @Override
     public char getCharacterAt(int offset) {
-        String componentText = getText();
-        if (offset < 0 || offset >= componentText.length()) {
-            throw new IndexOutOfBoundsException();
-        }
-        return componentText.charAt(offset);
+        return getText(this.component).charAt(offset);
     }
 
     @Override
     public int getCharacterCount() {
-        String componentText = getText();
-        return componentText.length();
+        return getText(this.component).length();
     }
 
     @Override

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java?rev=1735008&r1=1735007&r2=1735008&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java Tue Mar 15 01:38:09 2016
@@ -18,6 +18,7 @@ package org.apache.pivot.wtk.text;
 
 import org.apache.pivot.collections.Sequence;
 import org.apache.pivot.util.ListenerList;
+import org.apache.pivot.wtk.Span;
 
 /**
  * Abstract base class for document nodes.
@@ -39,6 +40,9 @@ public abstract class Node {
             }
         }
 
+        /**
+         * @param offset Offset relative to this node.
+         */
         @Override
         public void nodeInserted(Node node, int offset) {
             for (NodeListener listener : this) {
@@ -46,6 +50,9 @@ public abstract class Node {
             }
         }
 
+        /**
+         * @param offset Offset relative to this node.
+         */
         @Override
         public void nodesRemoved(Node node, Sequence<Node> removed, int offset) {
             for (NodeListener listener : this) {
@@ -53,13 +60,19 @@ public abstract class Node {
             }
         }
 
+        /**
+         * @param offset Offset relative to this node.
+         */
         @Override
-        public void rangeInserted(Node node, int offset, int span) {
+        public void rangeInserted(Node node, int offset, int characterCount) {
             for (NodeListener listener : this) {
-                listener.rangeInserted(node, offset, span);
+                listener.rangeInserted(node, offset, characterCount);
             }
         }
 
+        /**
+         * @param offset Offset relative to this node.
+         */
         @Override
         public void rangeRemoved(Node node, int offset, int characterCount) {
             for (NodeListener listener : this) {
@@ -93,7 +106,7 @@ public abstract class Node {
     }
 
     /**
-     * Returns the node's offset within its parent.
+     * Returns the node's offset relative to its parent.
      *
      * @return The integer offset of the node's first character within its
      * parent element.
@@ -102,6 +115,9 @@ public abstract class Node {
         return offset;
     }
 
+    /**
+     * Set the offset of this node relative to its parent.
+     */
     protected void setOffset(int offset) {
         int previousOffset = this.offset;
 
@@ -112,25 +128,37 @@ public abstract class Node {
     }
 
     /**
-     * Returns the node's offset within the document.
+     * Returns the node's offset within the document, which will be the
+     * offset of our parent (if any) added to our own offset.
      */
     public int getDocumentOffset() {
         return (parent == null) ? 0 : parent.getDocumentOffset() + offset;
     }
 
     /**
+     * Returns a {@link Span} that describes the content range of this node
+     * relative to the whole document.
+     */
+    public Span getDocumentSpan() {
+        int docOffset = getDocumentOffset();
+        int nodeLength = getCharacterCount();
+        // The "end" of a Span is inclusive, so subtract one here
+        return new Span(docOffset, docOffset + nodeLength - 1);
+    }
+
+    /**
      * Inserts a range into the node. Note that the contents of the range,
      * rather than the range itself, is added to the node.
      *
      * @param range
-     * @param offsetArgument
+     * @param offsetArgument Offset relative to this node.
      */
     public abstract void insertRange(Node range, int offsetArgument);
 
     /**
      * Removes a range from the node.
      *
-     * @param offsetArgument
+     * @param offsetArgument Offset relative to this node.
      * @param characterCount
      * @return The removed range. This will be a copy of the node structure
      * relative to this node.
@@ -140,7 +168,7 @@ public abstract class Node {
     /**
      * Replaces an existing range with a new range.
      *
-     * @param offsetArgument
+     * @param offsetArgument Offset relative to this node.
      * @param characterCount
      * @param range
      * @return The removed range. This will be a copy of the node structure
@@ -156,7 +184,7 @@ public abstract class Node {
     /**
      * Returns a range from the node.
      *
-     * @param offsetArgument
+     * @param offsetArgument Offset relative to this node.
      * @param characterCount
      * @return A node containing a copy of the node structure spanning the given
      * range, relative to this node.
@@ -166,7 +194,7 @@ public abstract class Node {
     /**
      * Returns the character at the given offset.
      *
-     * @param offsetArgument
+     * @param offsetArgument Offset relative to this node.
      */
     public abstract char getCharacterAt(int offsetArgument);
 
@@ -183,9 +211,14 @@ public abstract class Node {
     public abstract Node duplicate(boolean recursive);
 
     /**
-     * Called to notify a node that a range has been inserted.
+     * Called to notify parent nodes and other listeners for the node
+     * that a range has been inserted.  All parents are notified first.
+     * <p> Note: The offset used to notify parents is the given offset
+     * added to the offset of this node (that is, it will be parent-relative).
+     * Therefore the topmost node will be given the offset into the whole document.
+     * Listeners for this node will just be given the offset relative to this node.
      *
-     * @param offsetArgument
+     * @param offsetArgument Offset relative to this node.
      * @param characterCount
      */
     protected void rangeInserted(int offsetArgument, int characterCount) {
@@ -197,9 +230,14 @@ public abstract class Node {
     }
 
     /**
-     * Called to notify a node that a range has been removed.
+     * Called to notify parent nodes and other listeners for the node
+     * that a range has been removed.  All parents are notified first.
+     * <p> Note: The offset used to notify parents is the given offset
+     * added to the offset of this node (that is, it will be parent-relative).
+     * Therefore the topmost node will be given the offset into the whole document.
+     * Listeners for this node will just be given the offset relative to this node.
      *
-     * @param offsetArgument
+     * @param offsetArgument Offset relative to this node.
      * @param characterCount
      */
     protected void rangeRemoved(int offsetArgument, int characterCount) {
@@ -211,10 +249,15 @@ public abstract class Node {
     }
 
     /**
-     * Called to notify a node that some child nodes has been removed.
+     * Called to notify parent nodes and other listeners for the node
+     * that child nodes have been removed.  All parents are notified first.
+     * <p> Note: The offset used to notify parents is the given offset
+     * added to the offset of this node (that is, it will be parent-relative).
+     * Therefore the topmost node will be given the offset into the whole document.
+     * Listeners for this node will just be given the offset relative to this node.
      *
      * @param removed
-     * @param offsetArgument
+     * @param offsetArgument Offset relative to this node.
      */
     protected void nodesRemoved(Sequence<Node> removed, int offsetArgument) {
         if (parent != null) {
@@ -225,9 +268,14 @@ public abstract class Node {
     }
 
     /**
-     * Called to notify a node that a child node has been inserted.
+     * Called to notify parent nodes and other listeners for the node
+     * that a child node has been inserted.  All parents are notified first.
+     * <p> Note: The offset used to notify parents is the given offset
+     * added to the offset of this node (that is, it will be parent-relative).
+     * Therefore the topmost node will be given the offset into the whole document.
+     * Listeners for this node will just be given the offset relative to this node.
      *
-     * @param offsetArgument
+     * @param offsetArgument Offset relative to this node.
      */
     protected void nodeInserted(int offsetArgument) {
         if (parent != null) {

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java?rev=1735008&r1=1735007&r2=1735008&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java Tue Mar 15 01:38:09 2016
@@ -17,6 +17,7 @@
 package org.apache.pivot.wtk.text;
 
 import org.apache.pivot.util.ListenerList;
+import org.apache.pivot.wtk.Span;
 
 /**
  * Node representing a sequence of characters.
@@ -24,6 +25,9 @@ import org.apache.pivot.util.ListenerLis
 public final class TextNode extends Node {
     private static class TextNodeListenerList extends ListenerList<TextNodeListener> implements
         TextNodeListener {
+        /**
+         * @param index Index into this node.
+         */
         @Override
         public void charactersInserted(TextNode textNode, int index, int count) {
             for (TextNodeListener listener : this) {
@@ -31,6 +35,9 @@ public final class TextNode extends Node
             }
         }
 
+        /**
+         * @param index Index into this node.
+         */
         @Override
         public void charactersRemoved(TextNode textNode, int index, int count) {
             for (TextNodeListener listener : this) {
@@ -75,13 +82,16 @@ public final class TextNode extends Node
         insertText(text, 0);
     }
 
+    /**
+     * @param index Index into this node.
+     */
     public void insertText(CharSequence text, int index) {
         if (text == null) {
             throw new IllegalArgumentException("text is null.");
         }
 
         if (index < 0 || index > characters.length()) {
-            throw new IndexOutOfBoundsException();
+            throw new IndexOutOfBoundsException("Index " + index + " outside of [0, " + characters.length() + "]");
         }
 
         int characterCount = text.length();
@@ -92,9 +102,14 @@ public final class TextNode extends Node
         }
     }
 
+    /**
+     * @param index Index into this node.
+     */
     public void removeText(int index, int count) {
         if (index < 0 || index + count > characters.length()) {
-            throw new IndexOutOfBoundsException();
+            throw new IndexOutOfBoundsException("Index " + index +
+                    " less than 0 or index+count " + count +
+                    " greater than length " + characters.length());
         }
 
         if (count > 0) {
@@ -105,6 +120,10 @@ public final class TextNode extends Node
         }
     }
 
+    public String getSubstring(Span range) {
+        return characters.substring(range.start, range.end + 1);
+    }
+
     public String getSubstring(int start, int end) {
         return characters.substring(start, end);
     }
@@ -113,12 +132,16 @@ public final class TextNode extends Node
         return characters;
     }
 
+    public CharSequence getCharacters(int start, int end) {
+        return characters.subSequence(start, end);
+    }
+
+    public CharSequence getCharacters(Span range) {
+        return characters.subSequence(range.start, range.end + 1);
+    }
+
     @Override
     public char getCharacterAt(int index) {
-        if (index < 0 || index >= characters.length()) {
-            throw new IndexOutOfBoundsException();
-        }
-
         return characters.charAt(index);
     }
 
@@ -127,6 +150,9 @@ public final class TextNode extends Node
         return characters.length();
     }
 
+    /**
+     * @param offset Offset into this text node.
+     */
     @Override
     public void insertRange(Node range, int offset) {
         if (!(range instanceof TextNode)) {