1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui.completion;
21
22 import javax.swing.SwingUtilities;
23 import javax.swing.event.DocumentEvent;
24 import javax.swing.event.DocumentListener;
25
26 /***
27 * This mode provides automatic completion similar to the one provided in
28 * KDE.
29 *
30 * It inserts the first match it finds into the text component after the
31 * cursor.
32 *
33 * @author Felix Berger
34 */
35 public class AutomaticCompletionMode extends ShortAutomaticCompletionMode
36 {
37
38 protected void complete(String prefix)
39 {
40 if (getModel().complete(prefix)) {
41 Object obj = getModel().getElementAt(0);
42 if (obj != null && obj.toString().length() > prefix.length()) {
43 setText(obj.toString(), obj.toString().length(), prefix.length());
44 }
45 }
46 }
47
48 private class DocumentHandler implements DocumentListener
49 {
50 public void insertUpdate(DocumentEvent e)
51 {
52 if (e.getLength() == 1) {
53 final String text = getText();
54 Runnable r = new Runnable()
55 {
56 public void run()
57 {
58 complete(text);
59 }
60 };
61 SwingUtilities.invokeLater(r);
62 }
63 }
64
65
66
67
68 public void removeUpdate(DocumentEvent e)
69 {
70
71 }
72
73
74
75
76 public void changedUpdate(DocumentEvent e)
77 {
78
79 }
80 }
81 }