/
TextArea.java
2372 lines (2112 loc) · 77.2 KB
/
TextArea.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores
* CA 94065 USA or visit www.oracle.com if you need additional information or
* have any questions.
*/
package com.codename1.ui;
import com.codename1.cloud.BindTarget;
import com.codename1.compat.java.util.Objects;
import com.codename1.impl.CodenameOneImplementation;
import com.codename1.io.Log;
import com.codename1.ui.TextSelection.Span;
import com.codename1.ui.TextSelection.Spans;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.events.ActionSource;
import com.codename1.ui.events.DataChangedListener;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.geom.Rectangle;
import com.codename1.ui.plaf.LookAndFeel;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.EventDispatcher;
import com.codename1.ui.util.UITimer;
import java.io.IOException;
import java.util.ArrayList;
/**
* <p>An optionally multi-line editable region that can display text and allow a user to edit it.
* By default the text area will grow based on its content.<br>
* {@code TextArea} is useful both for text input and for displaying multi-line data, it is used internally
* by components such as {@link com.codename1.components.SpanLabel} &
* {@link com.codename1.components.SpanButton}.</p>
*
* <p>
* {@code TextArea} & {@link com.codename1.ui.TextField} are very similar, we discuss the main differences
* between the two {@link com.codename1.ui.TextField here}. In fact they are so similar that our sample code
* below was written for {@link com.codename1.ui.TextField} but should be interchangeable with {@code TextArea}.
* </p>
*
* <script src="https://gist.github.com/codenameone/fb63dd5d6efdb95932be.js"></script>
* <img src="https://www.codenameone.com/img/developer-guide/components-text-component.png" alt="Text field input sample" />
*
* @author Chen Fishbein
*/
public class TextArea extends Component implements ActionSource, TextHolder {
private EventDispatcher listeners = new EventDispatcher();
private ActionListener doneListener;
private static int defaultValign = TOP;
/**
* Indicates the default vertical alignment for a text field, only applies to single line text fields
* @return the defaultValign
*/
public static int getDefaultValign() {
return defaultValign;
}
/**
* Indicates the default vertical alignment for a text field, only applies to single line text fields
* @param aDefaultValign the defaultValign to set
*/
public static void setDefaultValign(int aDefaultValign) {
defaultValign = aDefaultValign;
}
private int valign = defaultValign;
private static int defaultMaxSize = 124;
private static boolean autoDegradeMaxSize = false;
private static boolean hadSuccessfulEdit = false;
private int linesToScroll = 1;
/**
* Indicates the enter key to be used for editing the text area and by the
* text field
*/
private static final char ENTER_KEY = '\n';
/**
* Unsupported characters is a string that contains characters that cause issues
* when rendering on some problematic fonts. The rendering engine can thus remove them
* when drawing.
*/
private String unsupportedChars = "\t\r";
/**
* By default text area uses charWidth since its much faster on some devices
* than string width. However, with some fonts and especially some languages (such
* as Arabic, Korean etc.) the width of the string drawn might not equal the summary
* of the width of the chars. Hence for portability to those languages/fonts this
* flag must be set to true.
*/
private static boolean useStringWidth;
/**
* Allows any type of input into a text field, if a constraint is not supported
* by an underlying implementation this will be the default.
*/
public static final int ANY = 0;
/**
* The user is allowed to enter an e-mail address.
*/
public static final int EMAILADDR = 1;
/**
* The user is allowed to enter only an integer value.
*/
public static final int NUMERIC = 2;
/**
* The user is allowed to enter a phone number.
*/
public static final int PHONENUMBER = 3;
/**
* The user is allowed to enter a URL.
*/
public static final int URL = 4;
/**
* The user is allowed to enter numeric values with optional decimal
* fractions, for example "-123", "0.123", or ".5".
*/
public static final int DECIMAL = 5;
/**
* Indicates that the text entered is confidential data that should be
* obscured whenever possible.
*/
public static final int PASSWORD = 0x10000;
/**
* Indicates that editing is currently disallowed.
*/
public static final int UNEDITABLE = 0x20000;
/**
* Indicates that the text entered is sensitive data that the
* implementation must never store into a dictionary or table for use
* in predictive, auto-completing, or other accelerated input schemes.
*/
public static final int SENSITIVE = 0x40000;
/**
* Indicates that the text entered does not consist of words that are
* likely to be found in dictionaries typically used by predictive input
* schemes.
*/
public static final int NON_PREDICTIVE= 0x80000;
/**
* This flag is a hint to the implementation that during text editing,
* the initial letter of each word should be capitalized.
*/
public static final int INITIAL_CAPS_WORD = 0x100000;
/**
* This flag is a hint to the implementation that during text editing,
* the initial letter of each sentence should be capitalized.
*/
public static final int INITIAL_CAPS_SENTENCE = 0x200000;
//private int modifierFlag = 0x00000;
/**
* This flag is a hint to the implementation that this field contains
* a username.
*/
public static final int USERNAME = 0x400000;
/**
* This flag is a hint to the implementation that the text in this
* field should be upper case
*/
public static final int UPPERCASE = 0x800000;
/**
* Input constraint which should be one of ANY, NUMERIC,
* PHONENUMBER, URL or EMAIL
*/
private int constraint = INITIAL_CAPS_SENTENCE;
private String text="";
private boolean editable = true;
private int maxSize = defaultMaxSize ; //maximum size (number of characters) that can be stored in this TextField.
private int rows = 1;
private int columns = 3;
private int growLimit = -1;
private boolean endsWith3Points = false;
/**
* This flag indicates that the text area should try to act as a label and try to fix more accurately within it's bounds
* this might make it slower as a result
*/
private boolean actAsLabel;
// problematic maxSize = 20; //maximum size (number of characters) that can be stored in this TextField.
private ArrayList rowStrings;
private int widthForRowCalculations = -1;
private int rowsGap = 2;
private boolean triggerClose;
private EventDispatcher actionListeners = null;
private EventDispatcher bindListeners = null;
private EventDispatcher closeListeners = null;
private String lastTextValue = "";
/**
* Indicates that the text area should "grow" in height based on the content beyond the
* limits indicate by the rows variable
*/
private boolean growByContent = true;
/**
* Indicates the widest character in the alphabet, this is useful for detecting
* linebreaks internally. In CJK languages the widest char is different than W
* hence this functionality is exposed to developers.
*/
private static char widestChar = 'W';
/**
* Indicates whether this is a single line text area, in which case "growing" won't
* work as expected.
*/
private boolean singleLineTextArea;
private int currentRowWidth;
private Label hintLabel;
/**
* Creates an area with the given rows and columns
*
* @param rows the number of rows
* @param columns - the number of columns
* @throws IllegalArgumentException if rows <= 0 or columns <= 1
*/
public TextArea(int rows, int columns){
this("", defaultMaxSize, rows, columns, ANY);
}
/**
* Creates an area with the given rows, columns and constraint
*
* @param rows the number of rows
* @param columns - the number of columns
* @param constraint one of ANY, EMAILADDR, NUMERIC, PHONENUMBER, URL, DECIMAL
* it can be bitwised or'd with one of PASSWORD, UNEDITABLE, SENSITIVE, NON_PREDICTIVE,
* INITIAL_CAPS_SENTENCE, INITIAL_CAPS_WORD. E.g. ANY | PASSWORD.
* @throws IllegalArgumentException if rows <= 0 or columns <= 1
*/
public TextArea(int rows, int columns, int constraint){
this("", defaultMaxSize, rows, columns, constraint);
}
/**
* Creates an area with the given text, rows and columns
*
* @param text the text to be displayed; if text is null, the empty
* string "" will be displayed
* @param rows the number of rows
* @param columns - the number of columns
* @throws IllegalArgumentException if rows <= 0 or columns <= 1
*/
public TextArea(String text, int rows, int columns){
this(text,defaultMaxSize, rows, columns, ANY); //String , maxSize, constraints= 0 (ANY)
}
/**
* Creates an area with the given text, rows, columns and constraint
*
* @param text the text to be displayed; if text is null, the empty
* string "" will be displayed
* @param rows the number of rows
* @param columns - the number of columns
* @param constraint one of ANY, EMAILADDR, NUMERIC, PHONENUMBER, URL, DECIMAL
* it can be bitwised or'd with one of PASSWORD, UNEDITABLE, SENSITIVE, NON_PREDICTIVE,
* INITIAL_CAPS_SENTENCE, INITIAL_CAPS_WORD. E.g. ANY | PASSWORD.
* @throws IllegalArgumentException if rows <= 0 or columns <= 1
*/
public TextArea(String text, int rows, int columns, int constraint){
this(text,defaultMaxSize, rows, columns, constraint);
}
/**
* Creates an area with the given text and maximum size, this constructor
* will create a single line text area similar to a text field!
*
* @param text the text to be displayed; if text is null, the empty
* string "" will be displayed
* @param maxSize text area maximum size
*/
public TextArea(String text, int maxSize){
this(text,maxSize, 1, 3, ANY);
}
/**
* Creates an area with the given text, this constructor
* will create a single line text area similar to a text field!
*
* @param text the text to be displayed; if text is null, the empty
* string "" will be displayed
*/
public TextArea(String text) {
this(text, Math.max(defaultMaxSize, nl(text)), 1, numCols(text), ANY);
}
/**
* To work around race conditions in UI bindings (on Android at least), we want to
* send action events early. Even the focusLost() event isn't early enough to ensure
* that the action event is sent before an action event in a button that would trigger
* focus lost. We add this form press listener to the form when we add the textarea
* and remove it when we remove the textarea.
*
* Reference bug https://github.com/codenameone/CodenameOne/issues/2472
*/
private final ActionListener formPressListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Form f = getComponentForm();
if (f != null) {
if (isEditing() && f.getComponentAt(evt.getX(), evt.getY()) != TextArea.this) {
fireActionEvent();
setSuppressActionEvent(true);
}
}
}
};
@Override
protected void initComponent() {
super.initComponent();
Form f = getComponentForm();
if (f != null) {
// To be able to send action events early.
// https://github.com/codenameone/CodenameOne/issues/2472
f.addPointerPressedListener(formPressListener);
}
}
@Override
protected void deinitialize() {
Form f = getComponentForm();
if (f != null) {
// For sending action events early
// https://github.com/codenameone/CodenameOne/issues/2472
f.removePointerPressedListener(formPressListener);
}
super.deinitialize();
}
private static int numCols(String t) {
if(t == null) {
return 3;
}
int s = t.length();
if(s < 3) {
return 3;
}
if(s > 80) {
return 80;
}
return s;
}
private static int nl(String t) {
if(t == null) return 0;
return t.length();
}
/**
* Creates an empty text area, this constructor
* will create a single line text area similar to a text field!
*/
public TextArea() {
this("");
}
/**
* Creates an area with the given text, maximum size, rows, columns and constraint
*
* @param text the text to be displayed; if text is null, the empty
* string "" will be displayed
* @param maxSize text area maximum size
* @param rows the number of rows
* @param columns - the number of columns
* @param constraint one of ANY, EMAILADDR, NUMERIC, PHONENUMBER, URL, DECIMAL
* it can be bitwised or'd with one of PASSWORD, UNEDITABLE, SENSITIVE, NON_PREDICTIVE,
* INITIAL_CAPS_SENTENCE, INITIAL_CAPS_WORD. E.g. ANY | PASSWORD.
* @throws IllegalArgumentException if rows <= 0 or columns <= 1
*/
private TextArea(String text, int maxSize, int rows, int columns, int constraint){
setUIID("TextArea");
setPreferredTabIndex(0);
this.maxSize = maxSize;
setText(text);
setConstraint(constraint);
if(rows <= 0){
throw new IllegalArgumentException("rows must be positive");
}
if(columns <= 1 && rows != 1){
throw new IllegalArgumentException("columns must be larger than 1");
}
this.rows = rows;
this.columns = columns;
setCursor(Component.TEXT_CURSOR);
}
/**
* {@inheritDoc}
*/
protected void initLaf(UIManager uim) {
super.initLaf(uim);
setSelectCommandText(uim.localize("edit", "Edit"));
LookAndFeel laf = uim.getLookAndFeel();
setSmoothScrolling(laf.isDefaultSmoothScrolling());
}
/**
* Sets the constraint which provides a hint to the virtual keyboard input, notice this <b>doesn't</b>
* limit input type in any way!
*
* @param constraint one of ANY, EMAILADDR, NUMERIC, PHONENUMBER, URL, DECIMAL
* it can be bitwised or'd with one of PASSWORD, UNEDITABLE, SENSITIVE, NON_PREDICTIVE,
* INITIAL_CAPS_SENTENCE, INITIAL_CAPS_WORD. E.g. ANY | PASSWORD.
*/
public void setConstraint(int constraint) {
this.constraint = constraint;
}
/**
* Returns the editing constraint value
*
* @return the editing constraint value
* @see #setConstraint
*/
public int getConstraint() {
return constraint;
}
/**
* {@inheritDoc}
*/
public void setWidth(int width) {
if(width != getWidth()) {
rowStrings = null;
if(growByContent) {
setShouldCalcPreferredSize(true);
}
}
super.setWidth(width);
}
/**
* Sets the text within this text area
*
* @param t new value for the text area
*/
public void setText(String t) {
String old = this.text;
if (t != null ? !t.equals(old) : old != null) {
// If we've previously suppressed action events,
// we need to unsuppress them upon the text changing again.
setSuppressActionEvent(false);
}
this.text = (t != null) ? t : "";
setShouldCalcPreferredSize(true);
if(maxSize < text.length()) {
maxSize = text.length() + 1;
}
synchronized(this) {
//zero the ArrayList in order to initialize it on the next paint
rowStrings=null;
}
if (!Objects.equals(text, old)) {
fireDataChanged(DataChangedListener.CHANGED, -1);
}
// while native editing we don't need the cursor animations
if(Display.getInstance().isNativeInputSupported() && Display.getInstance().isTextEditing(this)) {
if (!text.equals(old)) {
Display.impl.updateNativeEditorText(this, text);
}
return;
}
repaint();
}
/**
* Returns the text in the text area
*
* @return the text in the text area
*/
public String getText() {
return text;
}
/**
* Convenience method for numeric text fields, returns the value as a number or invalid if the value in the
* text field isn't a number
* @param invalid in case the text isn't an integer this number will be returned
* @return the int value of the text field
*/
public int getAsInt(int invalid) {
try {
return Integer.parseInt(text);
} catch(NumberFormatException e) {
return invalid;
}
}
/**
* Convenience method for numeric text fields, returns the value as a number or invalid if the value in the
* text field isn't a number
* @param invalid in case the text isn't a long this number will be returned
* @return the long value of the text field
*/
public long getAsLong(long invalid) {
try {
return Long.parseLong(text);
} catch(NumberFormatException e) {
return invalid;
}
}
/**
* Convenience method for numeric text fields, returns the value as a number or invalid if the value in the
* text field isn't a number
* @param invalid in case the text isn't an double this number will be returned
* @return the double value of the text field
*/
public double getAsDouble(double invalid) {
try {
return Double.parseDouble(text);
} catch(NumberFormatException e) {
return invalid;
}
}
/**
* Returns true if this area is editable
*
* @return true if this area is editable
*/
public boolean isEditable() {
return editable;
}
@Override
public int getPreferredTabIndex() {
if (isEditable()) {
return super.getPreferredTabIndex();
}
return -1;
}
private void updateCursor() {
setCursor(isEditable() || isTextSelectionEnabled() ? TEXT_CURSOR : DEFAULT_CURSOR);
}
/**
* Sets this text area to be editable or readonly
*
* @param b true is text are is editable; otherwise false
*/
public void setEditable(boolean b) {
editable = b;
updateCursor();
}
/**
* Returns the maximum size for the text area
*
* @return the maximum size for the text area
*/
public int getMaxSize() {
return maxSize;
}
/**
* Sets the maximum size of the text area
*
* @param maxSize the maximum size of the text area
*/
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
/**
* {@inheritDoc}
*/
public void keyPressed(int keyCode) {
super.keyPressed(keyCode);
setSuppressActionEvent(false);
int action = com.codename1.ui.Display.getInstance().getGameAction(keyCode);
// this works around a bug where fire is also a softkey on devices such as newer Nokia
// series 40's (e.g. the Nokia emulator). It closes its native text box on fire then
// as a result of a Nokia bug we get the key released of that closing and assume the
// users wants to edit the text... When means the only way to exit the native text box
// is via the cancel option (after pressing OK once).
triggerClose = action == Display.GAME_FIRE;
//scroll the TextArea
Rectangle rect = new Rectangle(getScrollX(), getScrollY(), getWidth(), getHeight());
Font textFont = getStyle().getFont();
if(action == Display.GAME_DOWN){
if((getScrollY() + getHeight()) <(rowsGap + getStyle().getFont().getHeight()) * getLines()) {
rect.setY(rect.getY() + (textFont.getHeight() + rowsGap) * linesToScroll);
scrollRectToVisible(rect, this);
} else {
setHandlesInput(false);
}
} else {
if(action == Display.GAME_UP){
if(getScrollY() > 0) {
rect.setY(Math.max(0, rect.getY() - (textFont.getHeight() + rowsGap) * linesToScroll));
scrollRectToVisible(rect, this);
} else {
setHandlesInput(false);
}
}
}
if(action == Display.GAME_RIGHT || action == Display.GAME_LEFT){
setHandlesInput(false);
}
}
/**
* {@inheritDoc}
*/
protected void fireClicked() {
onClick();
}
/**
* {@inheritDoc}
*/
protected boolean isSelectableInteraction() {
return editable;
}
private boolean isTypedKey(int code) {
if (true) return code > 0;
return (code >= 48 && code <= 90) // 0-9a-z
||
(code >= 96 && code <= 111) // number pad and arithmetic
||
(code >= 186 && code <= 192) // punctuation
||
(code >= 219 && code <= 222); // brackets & quotes
}
/**
* {@inheritDoc}
*/
public void keyReleased(int keyCode) {
int action = com.codename1.ui.Display.getInstance().getGameAction(keyCode);
if(isEditable()){
// this works around a bug where fire is also a softkey on devices such as newer Nokia
// series 40's
if (triggerClose && (action == Display.GAME_FIRE || isEnterKey(keyCode))) {
triggerClose = false;
onClick();
return;
}
Display d = Display.getInstance();
if(action == 0 && isTypedKey(keyCode)) {
//registerAsInputDevice();
Display.getInstance().editString(this, getMaxSize(), getConstraint(), getText(), keyCode);
}
}
}
/**
* {@inheritDoc}
*/
public boolean isScrollableY() {
return isFocusable() && getScrollDimension().getHeight() > getHeight();
}
void deinitializeImpl() {
super.deinitializeImpl();
Display.getInstance().stopEditing(this);
}
void onClick(){
if(isEditable()) {
editString();
}
}
void editString() {
if(autoDegradeMaxSize && (!hadSuccessfulEdit) && (maxSize > 1024)) {
try {
//registerAsInputDevice();
Display.getInstance().editString(this, getMaxSize(), getConstraint(), getText());
} catch(IllegalArgumentException err) {
maxSize -= 1024;
setDefaultMaxSize(maxSize);
editString();
}
} else {
//registerAsInputDevice();
Display.getInstance().editString(this, getMaxSize(), getConstraint(), getText());
}
}
/**
* {@inheritDoc}
*/
public void pointerHover(int[] x, int[] y) {
if (!Display.getInstance().isDesktop()) {
requestFocus();
}
}
/**
* {@inheritDoc}
*/
public void pointerHoverReleased(int[] x, int[] y) {
if (!Display.getInstance().isDesktop()) {
requestFocus();
}
}
boolean showLightweightVKB() {
return false;
}
/**
* {@inheritDoc}
*/
public void pointerReleased(int x, int y) {
// prevent a drag operation from going into edit mode
if (isDragActivated()) {
super.pointerReleased(x, y);
} else {
super.pointerReleased(x, y);
if (isEditable() && isEnabled() && !isCellRenderer()) {
if(Display.impl.isNativeInputImmediate()) {
editString();
return;
}
if (Display.getInstance().isTouchScreenDevice()) {
if(showLightweightVKB() || !(Display.getInstance().getDefaultVirtualKeyboard() instanceof Dialog)) {
if (!Display.getInstance().isVirtualKeyboardShowing()) {
Display.getInstance().setShowVirtualKeyboard(true);
}
} else {
onClick();
}
} else {
onClick();
}
}
}
}
/**
* {@inheritDoc}
*/
void focusGainedInternal() {
setSuppressActionEvent(false);
super.focusGainedInternal();
setHandlesInput(isScrollableY());
}
/**
* {@inheritDoc}
*/
void focusLostInternal() {
super.focusLostInternal();
setHandlesInput(false);
if (isEditing()) {
fireActionEvent();
setSuppressActionEvent(true);
}
}
/**
* Returns the number of columns in the text area
*
* @return the number of columns in the text area
*/
public int getColumns() {
return columns;
}
/**
* Returns the number of actual rows in the text area taking into consideration
* growsByContent
*
* @return the number of rows in the text area
*/
public int getActualRows() {
if(growByContent) {
if(growLimit > -1) {
return Math.min(Math.max(rows, getLines()), growLimit);
}
return Math.max(rows, getLines());
}
return rows;
}
/**
* Returns the number of rows in the text area
*
* @return the number of rows in the text area
*/
public int getRows() {
return rows;
}
/**
* Sets the number of columns in the text area
*
* @param columns number of columns
*/
public void setColumns(int columns) {
setShouldCalcPreferredSize(true);
this.columns = columns;
}
/**
* Sets the number of rows in the text area
*
* @param rows number of rows
*/
public void setRows(int rows) {
setShouldCalcPreferredSize(true);
this.rows = rows;
}
void initComponentImpl() {
getRowStrings();
super.initComponentImpl();
}
private ArrayList getRowStrings() {
if(rowStrings == null || widthForRowCalculations != getWidth() - getUnselectedStyle().getHorizontalPadding()){
initRowString();
setShouldCalcPreferredSize(true);
}
return rowStrings;
}
/**
* Returns the number of text lines in the TextArea
*
* @return the number of text lines in the TextArea
*/
public int getLines(){
int retVal;
ArrayList v = getRowStrings();
retVal = v.size();
return retVal;
}
/**
* Returns the text in the given row of the text box
*
* @param line the line number in the text box
* @return the text of the line
*/
public String getTextAt(int line){
ArrayList rowsV = getRowStrings();
int size = rowsV.size();
if(size == 0){
return "";
}
if(line >= size){
return (String)rowsV.get(size-1);
}
return (String)rowsV.get(line);
}
private int indexOf(char[] t, char c, int offset, int length) {
int tlen = t.length;
for(int iter = offset ; iter < tlen && iter < offset+length; iter++) {
if(t[iter] == c) {
return iter;
}
}
return -1;
}
/**
* Override this to modify the text for rendering in cases of invalid characters
* for display, this method allows the developer to replace such characters e.g.:
* replace "\\t" with 4 spaces
*
* @param text the text to process
* @return the given string as a processed char array ready for rendering
*/
protected char[] preprocess(String text) {
return text.toCharArray();
}
private int updateRowWidth(char c, Font font) {
currentRowWidth += font.charWidth(c);
return currentRowWidth;
}
private int updateRowWidth(String str, Font font) {
currentRowWidth += font.stringWidth(str);
return currentRowWidth;
}
private boolean fastCharWidthCheck(char[] chrs, int off, int length, int width, int charWidth, Font f) {
if(length * charWidth < width) {
return true;
}
length = Math.min(chrs.length, length);
return f.charsWidth(chrs, off, length) < width;
}
private void initRowString() {
if(!Display.getInstance().isEdt()) {
if(rowStrings == null) {
rowStrings = new ArrayList();
rowStrings.add(getText());
return;
}
}
Style style = getUnselectedStyle();
rowStrings= new ArrayList();
widthForRowCalculations = getWidth() - style.getHorizontalPadding();
// single line text area is essentially a text field, we call the method
// to allow subclasses to override it
if (isSingleLineTextArea()) {
rowStrings.add(getText());
return;
}
if (widthForRowCalculations <= 0) {
rowStrings.add(getText());
setShouldCalcPreferredSize(true);
return;
}
if(text == null || text.equals("")){
return;
}
Font font = style.getFont();
if(actAsLabel && text.length() <= columns && text.indexOf('\n') < 0) {
int w = font.stringWidth(text);
if(w <= getWidth()) {
if(rowStrings == null) {
rowStrings = new ArrayList();
rowStrings.add(getText());
return;
} else {
rowStrings.clear();
rowStrings.add(getText());
return;
}
}