1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.cmdl;
21
22 import java.util.Iterator;
23 import java.util.TreeSet;
24
25 public class DefaultCompleter implements Completer {
26
27
28
29 private TreeSet completions = new TreeSet();
30 private Iterator possibleValues;
31
32
33
34 public DefaultCompleter()
35 {
36 }
37
38
39
40 public void add(String completion)
41 {
42 completions.add(completion);
43 }
44
45 public void remove(String completion)
46 {
47 completions.remove(completion);
48 }
49
50 public String[] getCompletions()
51 {
52 return (String[])completions.toArray(new String[0]);
53 }
54
55 /***
56 *
57 */
58 public String completer(String text, int state)
59 {
60 if (state == 0) {
61
62 possibleValues = completions.tailSet(text).iterator();
63 }
64
65 if (possibleValues.hasNext()) {
66 String nextKey = (String)possibleValues.next();
67 if (nextKey.startsWith(text))
68 return nextKey;
69 }
70
71 return null;
72 }
73
74 }