diff options
author | Markus Keller | 2016-01-21 15:05:56 +0000 |
---|---|---|
committer | Markus Keller | 2016-01-21 15:05:56 +0000 |
commit | 431d7aa5b700733697692dc5dfa5e334e05eeec3 (patch) | |
tree | df20315adf4330c96e3c22d43d39be68497ddd17 | |
parent | 585085075702b3c9665ad402cbe1a9f2889382a0 (diff) | |
download | eclipse.platform.swt-431d7aa5b700733697692dc5dfa5e334e05eeec3.tar.gz eclipse.platform.swt-431d7aa5b700733697692dc5dfa5e334e05eeec3.tar.xz eclipse.platform.swt-431d7aa5b700733697692dc5dfa5e334e05eeec3.zip |
Bug 484525: StyledText sends unexpected selection changed event
2 files changed, 76 insertions, 3 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java index 922d8d0560..53ac145dc4 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java @@ -115,7 +115,7 @@ public class StyledText extends Canvas { int columnX; // keep track of the horizontal caret position when changing lines/pages. Fixes bug 5935 int caretOffset; int caretAlignment; - Point selection = new Point(0, 0); // x and y are start and end caret offsets of selection + Point selection = new Point(0, 0); // x and y are start and end caret offsets of selection (x <= y) Point clipboardSelection; // x and y are start and end caret offsets of previous selection int selectionAnchor; // position of selection anchor. 0 based offset from beginning of text Point doubleClickSelection; // selection after last mouse double click @@ -1437,7 +1437,10 @@ public void addPaintObjectListener(PaintObjectListener listener) { * user changes the selection. * <p> * When <code>widgetSelected</code> is called, the event x and y fields contain - * the start and end caret indices of the selection. + * the start and end caret indices of the selection. The selection values returned are visual + * (i.e., x will always always be <= y). + * No event is sent when the caret is moved while the selection length is 0. + * </p><p> * <code>widgetDefaultSelected</code> is not called for StyledTexts. * </p> * @@ -9670,6 +9673,8 @@ void setSelection(int start, int length, boolean sendEvent, boolean doBlock) { setBlockSelectionOffset(start, end, sendEvent); } } else { + int oldStart = selection.x; + int oldLength = selection.y - selection.x; int charCount = content.getCharCount(); // called internally to remove selection after text is removed // therefore make sure redraw range is valid. @@ -9689,7 +9694,7 @@ void setSelection(int start, int length, boolean sendEvent, boolean doBlock) { if (redrawY - redrawX > 0) { internalRedrawRange(redrawX, redrawY - redrawX); } - if (sendEvent) { + if (sendEvent && (oldLength != end - start || (oldLength != 0 && oldStart != start))) { sendSelectionEvent(); } sendAccessibleTextCaretMoved(); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java index 6c6eecc772..867008aaa8 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java @@ -3621,6 +3621,74 @@ public void test_setSelectionII(){ } @Test +public void test_addSelectionListener() { + text.setText("0123456789"); + class TestSelectionListener extends SelectionAdapter { + public int counter; + public Point eventSelection = new Point(0, 0); + @Override + public void widgetSelected(SelectionEvent e) { + eventSelection.x = e.x; + eventSelection.y = e.y; + counter++; + } + } + final TestSelectionListener selectionListener = new TestSelectionListener(); + text.addSelectionListener(selectionListener); + + assertEquals(0, selectionListener.counter); + assertEquals(new Point(0, 0), selectionListener.eventSelection); + + text.invokeAction(ST.COLUMN_NEXT); + assertEquals(new Point(1, 1), text.getSelection()); + + text.invokeAction(ST.SELECT_COLUMN_NEXT); + assertEquals(1, selectionListener.counter); + assertEquals(new Point(1, 2), selectionListener.eventSelection); + + text.invokeAction(ST.SELECT_COLUMN_NEXT); + assertEquals(2, selectionListener.counter); + assertEquals(new Point(1, 3), selectionListener.eventSelection); + + // replace text while selection is non-empty: + text.replaceTextRange(0, 1, "a"); + assertEquals(2, selectionListener.counter); + assertEquals(new Point(1, 3), selectionListener.eventSelection); + + text.replaceTextRange(9, 1, "z"); + assertEquals(2, selectionListener.counter); + assertEquals(new Point(1, 3), selectionListener.eventSelection); + + text.replaceTextRange(0, 1, "ab"); + assertEquals(3, selectionListener.counter); + assertEquals(new Point(2, 4), selectionListener.eventSelection); + assertEquals(new Point(2, 4), text.getSelection()); + + text.invokeAction(ST.COLUMN_NEXT); + assertEquals(4, selectionListener.counter); + assertEquals(new Point(4, 4), selectionListener.eventSelection); + assertEquals(new Point(4, 4), text.getSelection()); + + // replace text while selection is empty: + text.replaceTextRange(0, 2, "a"); + assertEquals(4, selectionListener.counter); + assertEquals(new Point(3, 3), text.getSelection()); + + text.replaceTextRange(9, 1, "9"); + assertEquals(4, selectionListener.counter); + assertEquals(new Point(3, 3), text.getSelection()); + + text.replaceTextRange(0, 1, "0"); + assertEquals(4, selectionListener.counter); + assertEquals(new Point(3, 3), text.getSelection()); + + // replace text that overlaps empty selection: + text.replaceTextRange(0, 9, ""); + assertEquals(4, selectionListener.counter); + assertEquals(new Point(0, 0), text.getSelection()); +} + +@Test public void test_setSelectionBackgroundLorg_eclipse_swt_graphics_Color(){ text.setSelectionBackground(getColor(YELLOW)); assertTrue(":1a:", text.getSelectionBackground() == getColor(YELLOW)); |