11package com .annimon .ownlang .modules .forms ;
22
3+ import com .annimon .ownlang .lib .Arguments ;
34import static com .annimon .ownlang .lib .Converters .*;
5+ import com .annimon .ownlang .lib .Function ;
6+ import com .annimon .ownlang .lib .MapValue ;
7+ import com .annimon .ownlang .lib .NumberValue ;
8+ import com .annimon .ownlang .lib .StringValue ;
9+ import com .annimon .ownlang .lib .Value ;
10+ import com .annimon .ownlang .lib .ValueUtils ;
11+ import javax .swing .event .CaretEvent ;
12+ import javax .swing .event .DocumentEvent ;
13+ import javax .swing .event .DocumentListener ;
414import javax .swing .text .JTextComponent ;
515
616public class JTextComponentValue extends JComponentValue {
717
818 private final JTextComponent textComponent ;
919
1020 public JTextComponentValue (int functionsCount , JTextComponent textComponent ) {
11- super (functionsCount + 20 , textComponent );
21+ super (functionsCount + 21 , textComponent );
1222 this .textComponent = textComponent ;
1323 init ();
1424 }
1525
1626 private void init () {
27+ set ("addCaretListener" , this ::addCaretListener );
28+ set ("addDocumentListener" , this ::addDocumentListener );
1729 set ("copy" , voidToVoid (textComponent ::copy ));
1830 set ("cut" , voidToVoid (textComponent ::cut ));
1931 set ("getCaretPosition" , voidToInt (textComponent ::getCaretPosition ));
@@ -35,4 +47,46 @@ private void init() {
3547 set ("setSelectionEnd" , intToVoid (textComponent ::setSelectionEnd ));
3648 set ("setText" , stringToVoid (textComponent ::setText ));
3749 }
50+
51+ private Value addCaretListener (Value [] args ) {
52+ Arguments .check (1 , args .length );
53+ final Function action = ValueUtils .consumeFunction (args [0 ], 0 );
54+ textComponent .addCaretListener ((CaretEvent e ) -> {
55+ final MapValue map = new MapValue (2 );
56+ map .set ("getDot" , NumberValue .of (e .getDot ()));
57+ map .set ("getMark" , NumberValue .of (e .getMark ()));
58+ action .execute (map );
59+ });
60+ return NumberValue .ZERO ;
61+ }
62+
63+ private Value addDocumentListener (Value [] args ) {
64+ Arguments .check (1 , args .length );
65+ final Function action = ValueUtils .consumeFunction (args [0 ], 0 );
66+ textComponent .getDocument ().addDocumentListener (new DocumentListener () {
67+ @ Override
68+ public void insertUpdate (DocumentEvent e ) {
69+ handleDocumentEvent (DocumentEvent .EventType .INSERT , e );
70+ }
71+
72+ @ Override
73+ public void removeUpdate (DocumentEvent e ) {
74+ handleDocumentEvent (DocumentEvent .EventType .REMOVE , e );
75+ }
76+
77+ @ Override
78+ public void changedUpdate (DocumentEvent e ) {
79+ handleDocumentEvent (DocumentEvent .EventType .CHANGE , e );
80+ }
81+
82+ private void handleDocumentEvent (DocumentEvent .EventType type , final DocumentEvent e ) {
83+ final MapValue map = new MapValue (3 );
84+ map .set ("getLength" , NumberValue .of (e .getLength ()));
85+ map .set ("getOffset" , NumberValue .of (e .getOffset ()));
86+ map .set ("getType" , new StringValue (e .getType ().toString ()));
87+ action .execute (new StringValue (type .toString ()), map );
88+ }
89+ });
90+ return NumberValue .ZERO ;
91+ }
3892}
0 commit comments