PIVOT-891:  Add double/triple click support in TextPane the same way it is
supported (now) in TextArea.

Double click selects the word immediately under/around the mouse pointer,
and triple click selects the line (paragraph) that is under the mouse.

Essentially we just duplicated the code from TextArea into TextPane with
appropriate (small) modifications for the difference in APIs.


git-svn-id: https://svn.apache.org/repos/asf/pivot/trunk@1750549 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java b/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java
index 7c843c6..36cc6c2 100644
--- a/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java
+++ b/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java
@@ -816,6 +816,81 @@
         return consumed;
     }
 
+    private void selectSpan(TextPane textPane, Document document, int start) {
+        int rowStart = getRowOffset(document, start);
+        int rowLength = getRowLength(document, start);
+        if (start - rowStart >= rowLength) {
+            start = rowStart + rowLength - 1;
+            if (start < 0) {
+                return;
+            }
+            char ch = document.getCharacterAt(start);
+            if (ch == '\r' || ch == '\n') {
+                start--;
+            }
+        }
+        if (start < 0) {
+            return;
+        }
+        char ch = document.getCharacterAt(start);
+        int selectionStart = start;
+        int selectionLength = 1;
+        if (Character.isWhitespace(ch)) {
+            // Move backward to beginning of whitespace block
+            // but not before the beginning of the line.
+            do {
+                selectionStart--;
+            } while (selectionStart >= rowStart
+                && Character.isWhitespace(document.getCharacterAt(selectionStart)));
+            selectionStart++;
+            selectionLength = start - selectionStart;
+            // Move forward to end of whitespace block
+            // but not past the end of the text or the end of line
+            do {
+                selectionLength++;
+            } while (selectionStart + selectionLength - rowStart < rowLength
+                && Character.isWhitespace(document.getCharacterAt(selectionStart + selectionLength)));
+        } else if (Character.isJavaIdentifierPart(ch)) {
+            // Move backward to beginning of identifier block
+            do {
+                selectionStart--;
+            } while (selectionStart >= rowStart
+                && Character.isJavaIdentifierPart(document.getCharacterAt(selectionStart)));
+            selectionStart++;
+            selectionLength = start - selectionStart;
+            // Move forward to end of identifier block
+            // but not past end of text
+            do {
+                selectionLength++;
+            } while (selectionStart + selectionLength - rowStart < rowLength
+                && Character.isJavaIdentifierPart(document.getCharacterAt(selectionStart
+                    + selectionLength)));
+        } else {
+            return;
+        }
+        textPane.setSelection(selectionStart, selectionLength);
+    }
+
+    @Override
+    public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
+        boolean consumed = super.mouseClick(component, button, x, y, count);
+
+        TextPane textPane = (TextPane) component;
+        Document document = textPane.getDocument();
+
+        if (button == Mouse.Button.LEFT) {
+            int index = getInsertionPoint(x, y);
+            if (index != -1) {
+                if (count == 2) {
+                    selectSpan(textPane, document, index);
+                } else if (count == 3) {
+                    textPane.setSelection(getRowOffset(document, index), getRowLength(document, index));
+                }
+            }
+        }
+        return consumed;
+    }
+
     @Override
     public boolean keyTyped(final Component component, char character) {
         boolean consumed = super.keyTyped(component, character);