1515import javax .swing .event .DocumentListener ;
1616import javax .swing .text .BadLocationException ;
1717
18+ /*
19+ This class determines if the word
20+ being typed matches the keywords that will be
21+ suggested to the user.
22+ */
1823public class JavaAutocomplete
1924 implements DocumentListener {
2025
@@ -28,48 +33,43 @@ public class JavaAutocomplete
2833 "synchronized" , "this" , "throw" , "throws" , "transient" , "true" ,
2934 "try" , "void" , "volatile" , "while" , "String" };
3035
31- static final String [] specialChars = {"{" , "(" };
36+ static final String [] bracketChars = {"{" , "(" };
3237
3338 UI ui ;
3439
3540 private ArrayList <String > words = new ArrayList <>();
36- private ArrayList <String > chars = new ArrayList <>();
41+ private ArrayList <String > brackets = new ArrayList <>();
3742
3843 private enum Mode {
39-
4044 INSERT , COMPLETION
4145 };
4246
4347 private Mode mode = Mode .INSERT ;
44-
4548 private final JTextArea textArea ;
46-
4749 private static final String COMMIT_ACTION = "commit" ;
48-
50+
4951 private boolean isKeyword ;
50-
5152 private int pos ;
5253 private String content ;
5354
5455 public JavaAutocomplete (UI ui ) {
5556
5657 this .ui = ui ;
5758 textArea = ui .getEditor ();
58-
59+
5960 InputMap im = textArea .getInputMap ();
6061 ActionMap am = textArea .getActionMap ();
6162 im .put (KeyStroke .getKeyStroke ("ENTER " ), COMMIT_ACTION );
6263 am .put (COMMIT_ACTION , new CommitAction ());
6364
64- //set the keywords
6565 for (String keyList : keywords ) {
6666 words .add (keyList );
6767 }
68- for (String character : specialChars ) {
69- chars .add (character );
68+ for (String bracket : bracketChars ) {
69+ brackets .add (bracket );
7070 }
7171 Collections .sort (words , null );
72- Collections .sort (chars , null );
72+ Collections .sort (brackets , null );
7373 }
7474
7575 @ Override
@@ -98,8 +98,8 @@ public void insertUpdate(DocumentEvent e) {
9898 }
9999 }
100100
101- if (chars .contains (s )) {
102- for (String str : chars ) {
101+ if (brackets .contains (s )) {
102+ for (String str : brackets ) {
103103 if (s .equals (str )) {
104104 switch (str ) {
105105 case "{" :
@@ -123,7 +123,6 @@ public void insertUpdate(DocumentEvent e) {
123123 }
124124
125125 String prefix = content .substring (start + 1 );
126-
127126 int n = Collections .binarySearch (words , prefix );
128127
129128 if (n < 0 && -n < words .size ()) {
@@ -142,6 +141,11 @@ public void insertUpdate(DocumentEvent e) {
142141 }
143142 }
144143
144+ /*
145+ This class handles the autocomplete
146+ suggestion generated when the user
147+ is typing a word that matches a keyword.
148+ */
145149 private class CompletionTask
146150 implements Runnable {
147151
@@ -159,7 +163,6 @@ public String getCompletion() {
159163
160164 @ Override
161165 public void run () {
162- System .out .println (isKeyword );
163166
164167 textArea .insert (completion , position );
165168
@@ -178,7 +181,11 @@ public void run() {
178181 }
179182 }
180183 }
181-
184+
185+ /*
186+ Handle when the enter button is pressed,
187+ particularly when it is in response to a keyword.
188+ */
182189 private class CommitAction
183190 extends AbstractAction {
184191
@@ -204,20 +211,15 @@ public void actionPerformed(ActionEvent e) {
204211 }
205212 }
206213
207- @ Override
208- public void removeUpdate (DocumentEvent e ) {
209- }
210-
211- @ Override
212- public void changedUpdate (DocumentEvent e ) {
213- }
214-
214+ /*
215+ User responses to autocomplete for brackets
216+ or parentheses will need extra attention.
217+ */
215218 private class HandleBracketEvent
216219 implements KeyListener {
217220
218221 @ Override
219222 public void keyTyped (KeyEvent e ) {
220- System .out .println ("Activated" );
221223
222224 switch (e .getKeyChar ()) {
223225 case '}' :
@@ -246,12 +248,15 @@ public void keyTyped(KeyEvent e) {
246248 }
247249
248250 @ Override
249- public void keyPressed (KeyEvent e ) {
250- }
251+ public void keyPressed (KeyEvent e ) {}
251252
252253 @ Override
253- public void keyReleased (KeyEvent e ) {
254- }
254+ public void keyReleased (KeyEvent e ) {}
255+ }
256+ @ Override
257+ public void removeUpdate (DocumentEvent e ) {}
258+
259+ @ Override
260+ public void changedUpdate (DocumentEvent e ) {}
255261
256- }
257262}
0 commit comments