@@ -179,6 +179,7 @@ public class TestCodenameOneImplementation extends CodenameOneImplementation {
179179 private MediaRecorderBuilder lastMediaRecorderBuilder ;
180180 private VideoCaptureConstraints lastVideoConstraints ;
181181 private final List <AudioCaptureFrame > audioCaptureFrames = new ArrayList <AudioCaptureFrame >();
182+ private TextArea activeTextEditor ;
182183
183184
184185 public TestCodenameOneImplementation () {
@@ -1050,17 +1051,18 @@ public int getDisplayHeight() {
10501051
10511052 @ Override
10521053 public void editString (com .codename1 .ui .Component cmp , int maxSize , int constraint , String text , int initiatingKeycode ) {
1053- if (cmp instanceof TextField ) {
1054- TextField field = (TextField ) cmp ;
1055- if (shouldInsertCharacter (field .isEditable (), initiatingKeycode )) {
1056- field .insertChars (String .valueOf ((char ) initiatingKeycode ));
1057- return ;
1058- }
1059- field .setText (text );
1060- return ;
1061- }
10621054 if (cmp instanceof TextArea ) {
10631055 TextArea area = (TextArea ) cmp ;
1056+ activeTextEditor = area ;
1057+ if (cmp instanceof TextField ) {
1058+ TextField field = (TextField ) cmp ;
1059+ if (shouldInsertCharacter (field .isEditable (), initiatingKeycode )) {
1060+ insertCharacter (field , (char ) initiatingKeycode , maxSize );
1061+ return ;
1062+ }
1063+ field .setText (text );
1064+ return ;
1065+ }
10641066 if (shouldInsertCharacter (area .isEditable (), initiatingKeycode )) {
10651067 insertCharacter (area , (char ) initiatingKeycode , maxSize );
10661068 return ;
@@ -1109,13 +1111,94 @@ private void insertCharacter(TextArea area, char character, int maxSize) {
11091111 if (cursor < 0 || cursor > current .length ()) {
11101112 cursor = current .length ();
11111113 }
1114+ char adjustedCharacter = applyAutoCapitalization (area , character , current , cursor );
11121115 StringBuilder sb = new StringBuilder (current .length () + 1 );
11131116 sb .append (current , 0 , cursor );
1114- sb .append (character );
1117+ sb .append (adjustedCharacter );
11151118 if (cursor < current .length ()) {
11161119 sb .append (current .substring (cursor ));
11171120 }
11181121 area .setText (sb .toString ());
1122+ if (area instanceof TextField ) {
1123+ ((TextField ) area ).setCursorPosition (cursor + 1 );
1124+ }
1125+ }
1126+
1127+ private TextArea getActiveEditingArea () {
1128+ Component editing = getEditingText ();
1129+ if (editing instanceof TextArea ) {
1130+ return (TextArea ) editing ;
1131+ }
1132+ Display display = Display .getInstance ();
1133+ if (display == null ) {
1134+ return null ;
1135+ }
1136+ Form current = display .getCurrent ();
1137+ if (current == null ) {
1138+ return null ;
1139+ }
1140+ Component focused = current .getFocused ();
1141+ if (focused instanceof TextArea ) {
1142+ return (TextArea ) focused ;
1143+ }
1144+ if (current != null ) {
1145+ TextArea firstTextArea = findFirstTextArea (current .getContentPane ());
1146+ if (firstTextArea != null ) {
1147+ activeTextEditor = firstTextArea ;
1148+ return firstTextArea ;
1149+ }
1150+ }
1151+ if (activeTextEditor != null ) {
1152+ return activeTextEditor ;
1153+ }
1154+ return null ;
1155+ }
1156+
1157+ private TextArea findFirstTextArea (Container container ) {
1158+ if (container == null ) {
1159+ return null ;
1160+ }
1161+ int componentCount = container .getComponentCount ();
1162+ for (int i = 0 ; i < componentCount ; i ++) {
1163+ Component child = container .getComponentAt (i );
1164+ if (child instanceof TextArea ) {
1165+ return (TextArea ) child ;
1166+ }
1167+ if (child instanceof Container ) {
1168+ TextArea nested = findFirstTextArea ((Container ) child );
1169+ if (nested != null ) {
1170+ return nested ;
1171+ }
1172+ }
1173+ }
1174+ return null ;
1175+ }
1176+
1177+ private char applyAutoCapitalization (TextArea area , char character , String currentText , int cursorPosition ) {
1178+ if (!Character .isLetter (character )) {
1179+ return character ;
1180+ }
1181+ int constraint = area .getConstraint ();
1182+ boolean initialCapsSentence = (constraint & TextArea .INITIAL_CAPS_SENTENCE ) == TextArea .INITIAL_CAPS_SENTENCE ;
1183+ boolean initialCapsWord = (constraint & TextArea .INITIAL_CAPS_WORD ) == TextArea .INITIAL_CAPS_WORD ;
1184+ if (!initialCapsSentence && !initialCapsWord ) {
1185+ return character ;
1186+ }
1187+ int index = cursorPosition - 1 ;
1188+ if (initialCapsSentence ) {
1189+ while (index >= 0 && Character .isWhitespace (currentText .charAt (index ))) {
1190+ index --;
1191+ }
1192+ if (index < 0 || currentText .charAt (index ) == '.' || currentText .charAt (index ) == '!' || currentText .charAt (index ) == '?' ) {
1193+ return Character .toUpperCase (character );
1194+ }
1195+ }
1196+ if (initialCapsWord ) {
1197+ if (index < 0 || Character .isWhitespace (currentText .charAt (index ))) {
1198+ return Character .toUpperCase (character );
1199+ }
1200+ }
1201+ return character ;
11191202 }
11201203
11211204 @ Override
@@ -1125,6 +1208,7 @@ public boolean isAsyncEditMode() {
11251208
11261209 @ Override
11271210 public void stopTextEditing () {
1211+ activeTextEditor = null ;
11281212 hideTextEditor ();
11291213 }
11301214
@@ -1133,7 +1217,11 @@ public void dispatchKeyPress(final int keyCode) {
11331217 if (display == null ) {
11341218 return ;
11351219 }
1220+ final TextArea editing = getActiveEditingArea ();
11361221 final boolean reenter = beginAllowingEditDuringKey (keyCode );
1222+ if (editing != null && shouldInsertCharacter (editing .isEditable (), keyCode )) {
1223+ insertCharacter (editing , (char ) keyCode , editing .getMaxSize ());
1224+ }
11371225 display .keyPressed (keyCode );
11381226 display .keyReleased (keyCode );
11391227 if (reenter ) {
@@ -1166,16 +1254,15 @@ public void tapComponent(Component component) {
11661254 }
11671255
11681256 private boolean beginAllowingEditDuringKey (int keyCode ) {
1169- Component editing = getEditingText ();
1170- if (!( editing instanceof TextArea ) ) {
1257+ TextArea area = getActiveEditingArea ();
1258+ if (area == null ) {
11711259 return false ;
11721260 }
1173- TextArea area = (TextArea ) editing ;
11741261 if (!shouldInsertCharacter (area .isEditable (), keyCode )) {
11751262 return false ;
11761263 }
1177- if (editing instanceof TextField ) {
1178- TextField tf = (TextField ) editing ;
1264+ if (area instanceof TextField ) {
1265+ TextField tf = (TextField ) area ;
11791266 if (!tf .isQwertyInput ()) {
11801267 return false ;
11811268 }
0 commit comments