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/06/28 17:59:30 UTC

svn commit: r1750549 - /pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java

Author: rwhitcomb
Date: Tue Jun 28 17:59:30 2016
New Revision: 1750549

URL: http://svn.apache.org/viewvc?rev=1750549&view=rev
Log:
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.

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java?rev=1750549&r1=1750548&r2=1750549&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkin.java Tue Jun 28 17:59:30 2016
@@ -816,6 +816,81 @@ public class TextPaneSkin extends Contai
         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);