diff options
author | Ivan Furnadjiev | 2014-01-30 10:58:42 +0000 |
---|---|---|
committer | Ivan Furnadjiev | 2014-02-03 13:31:58 +0000 |
commit | a42cd142074c173deeb0db2587decb7d87a65757 (patch) | |
tree | dbf49adc6c79e63a8ebbcdfac51d64ea8183417c | |
parent | 23dd22dee2da88a1a31f551fe571ee2f4a5a490c (diff) | |
download | org.eclipse.rap-a42cd142074c173deeb0db2587decb7d87a65757.tar.gz org.eclipse.rap-a42cd142074c173deeb0db2587decb7d87a65757.tar.xz org.eclipse.rap-a42cd142074c173deeb0db2587decb7d87a65757.zip |
Always use adapter method to set the text sent by the client
The adapter method is now responsible for setting the text only.
Selection is set in TextLCA after the text.
424118: [Text] Cursor reset to first character when typing fast
https://bugs.eclipse.org/bugs/show_bug.cgi?id=424118
Change-Id: I6ce0f31ad3ed7d275eb05103fb9ea9dfa0a24592
4 files changed, 62 insertions, 20 deletions
diff --git a/bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/internal/widgets/ITextAdapter.java b/bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/internal/widgets/ITextAdapter.java index f5e32b6c74..1b9b0f4e68 100644 --- a/bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/internal/widgets/ITextAdapter.java +++ b/bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/internal/widgets/ITextAdapter.java @@ -1,19 +1,19 @@ /******************************************************************************* - * Copyright (c) 2008 Innoopract Informationssysteme GmbH. + * Copyright (c) 2008, 2014 Innoopract Informationssysteme GmbH and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Innoopract Informationssysteme GmbH - initial API and implementation + * Innoopract Informationssysteme GmbH - initial API and implementation + * EclipseSource - ongoing development ******************************************************************************/ package org.eclipse.swt.internal.widgets; -import org.eclipse.swt.graphics.Point; - public interface ITextAdapter { - void setText( String text, Point selection ); + void setText( String text ); + } diff --git a/bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/widgets/Text.java b/bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/widgets/Text.java index 1a61e7c1d5..bd9cd2a3fe 100644 --- a/bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/widgets/Text.java +++ b/bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/widgets/Text.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2013 Innoopract Informationssysteme GmbH. + * Copyright (c) 2002, 2014 Innoopract Informationssysteme GmbH. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -162,8 +162,11 @@ public class Text extends Scrollable { if( ITextAdapter.class.equals( adapter ) ) { if( textAdapter == null ) { textAdapter = new ITextAdapter() { - public void setText( String text, Point selection ) { - Text.this.setText( text, selection ); + public void setText( String text ) { + if( internalSetText( text ) ) { + adjustSelection(); + notifyListeners( SWT.Modify, new Event() ); + } } }; } @@ -1094,15 +1097,21 @@ public class Text extends Scrollable { return result; } - private void setText( String text, Point selection ) { + private boolean internalSetText( String text ) { String verifiedText = verifyText( text, 0, this.text.length() ); if( verifiedText != null ) { - this.text = verifiedText; - if( selection != null ) { - setSelection( selection.x, selection.y ); + if( verifiedText.length() > textLimit ) { + this.text = verifiedText.substring( 0, textLimit ); + } else { + this.text = verifiedText; } - notifyListeners( SWT.Modify, new Event() ); } + return verifiedText != null; + } + + private void adjustSelection() { + selection.x = Math.min( selection.x, text.length() ); + selection.y = Math.min( selection.y, text.length() ); } /////////////////////////////////////// diff --git a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/textkit/TextLCAUtil.java b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/textkit/TextLCAUtil.java index e6b462b6ed..5306b545e4 100644 --- a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/textkit/TextLCAUtil.java +++ b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/textkit/TextLCAUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2013 Innoopract Informationssysteme GmbH and others. + * Copyright (c) 2002, 2014 Innoopract Informationssysteme GmbH and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -126,20 +126,18 @@ final class TextLCAUtil { // fire a VerifyEvent whose fields (text and doit) need to be evaluated // before actually setting the new value ProcessActionRunner.add( new Runnable() { - public void run() { - text.getAdapter( ITextAdapter.class ).setText( txt, selection ); - // since text is set in process action, preserved values have to be - // replaced WidgetAdapter adapter = WidgetUtil.getAdapter( text ); + getTextAdapter( text ).setText( txt ); adapter.preserve( PROP_TEXT, txt ); if( selection != null ) { + text.setSelection( selection ); adapter.preserve( PROP_SELECTION, selection ); } } } ); } else { - text.setText( txt ); + getTextAdapter( text ).setText( txt ); if( selection != null ) { text.setSelection( selection ); } @@ -204,4 +202,8 @@ final class TextLCAUtil { return isListening( text, SWT.Modify ) || isListening( text, SWT.Verify ); } + private static ITextAdapter getTextAdapter( Text text ) { + return text.getAdapter( ITextAdapter.class ); + } + } diff --git a/tests/org.eclipse.rap.rwt.test/src/org/eclipse/swt/widgets/Text_Test.java b/tests/org.eclipse.rap.rwt.test/src/org/eclipse/swt/widgets/Text_Test.java index 57698b4327..ae41c0b8a6 100644 --- a/tests/org.eclipse.rap.rwt.test/src/org/eclipse/swt/widgets/Text_Test.java +++ b/tests/org.eclipse.rap.rwt.test/src/org/eclipse/swt/widgets/Text_Test.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2013 Innoopract Informationssysteme GmbH and others. + * Copyright (c) 2007, 2014 Innoopract Informationssysteme GmbH and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -32,6 +32,7 @@ import org.eclipse.swt.events.VerifyListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.internal.widgets.ITextAdapter; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -499,6 +500,36 @@ public class Text_Test { } @Test + public void testSetText_resetSelection() { + text.setText( "some text" ); + text.setSelection( 2, 4 ); + + text.setText( "other text" ); + + assertEquals( new Point( 0, 0 ), text.getSelection() ); + } + + @Test + public void testAdapterSetText_doesNotResetSelection() { + text.setText( "some text" ); + text.setSelection( 2, 4 ); + + text.getAdapter( ITextAdapter.class ).setText( "other text" ); + + assertEquals( new Point( 2, 4 ), text.getSelection() ); + } + + @Test + public void testAdapterSetText_adjustsSelection() { + text.setText( "some text" ); + text.setSelection( 7, 9 ); + + text.getAdapter( ITextAdapter.class ).setText( "text" ); + + assertEquals( new Point( 4, 4 ), text.getSelection() ); + } + + @Test public void testMessage() { Text text = new Text( shell, SWT.SINGLE ); assertEquals( "", text.getMessage() ); |