1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.xnap.gui.component;
20
21 import javax.swing.SwingUtilities;
22 import javax.swing.event.DocumentEvent;
23 import javax.swing.event.DocumentListener;
24 import javax.swing.text.JTextComponent;
25
26 import org.xnap.XNap;
27
28
29 public class ShortAutomaticCompletionMode extends CompletionMode
30 {
31
32
33
34
35 private DocumentListener docListener = new DocumentHandler();
36
37 /***
38 * @param textComponent
39 * @param wholeText
40 */
41 public ShortAutomaticCompletionMode(JTextComponent textComponent,
42 boolean wholeText)
43 {
44 super(textComponent, wholeText);
45 }
46
47 /***
48 * @param textComponent
49 */
50 public ShortAutomaticCompletionMode(JTextComponent textComponent)
51 {
52 super(textComponent);
53 }
54
55 /***
56 * @param textComponent
57 * @param model
58 */
59 public ShortAutomaticCompletionMode(JTextComponent textComponent,
60 CompletionModel model)
61 {
62 super(textComponent, model);
63 }
64
65 public ShortAutomaticCompletionMode(JTextComponent textComponent,
66 CompletionModel model, boolean wholeText)
67 {
68 super(textComponent, model, wholeText);
69 }
70
71
72
73 public String getName()
74 {
75 return XNap.tr("Short Automatic");
76 }
77
78 protected void complete(String prefix)
79 {
80 String comp = getModel().completeUniquePrefix(prefix);
81
82
83 if (comp.length() > prefix.length()) {
84 setText(comp, comp.length(), prefix.length());
85 }
86 }
87
88 protected void disable()
89 {
90 getTextComponent().getDocument().removeDocumentListener(docListener);
91 }
92
93 protected void enable()
94 {
95 getTextComponent().getDocument().addDocumentListener(docListener);
96 }
97
98 private class DocumentHandler extends DocumentAdapter
99 {
100 public void insertUpdate(DocumentEvent e)
101 {
102 if (e.getLength() == 1) {
103 final String text = getText();
104 Runnable r =
105 new Runnable() {
106 public void run()
107 {
108 complete(text);
109 }
110 };
111
112 SwingUtilities.invokeLater(r);
113 }
114 }
115 }
116 }