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 java.awt.event.InputEvent;
22 import java.awt.event.KeyAdapter;
23 import java.awt.event.KeyEvent;
24 import java.awt.event.KeyListener;
25
26 import javax.swing.KeyStroke;
27 import javax.swing.text.JTextComponent;
28
29 import org.apache.log4j.Logger;
30 import org.xnap.XNap;
31 import org.xnap.gui.shortcut.DefaultShortcut;
32 import org.xnap.gui.shortcut.ShortcutManager;
33
34
35 public class ManualCompletionMode extends CompletionMode
36 {
37 private static Logger logger = Logger.getLogger(ManualCompletionMode.class);
38
39 private static DefaultShortcut manual = new DefaultShortcut
40 (KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK),
41 XNap.tr("Completion"), XNap.tr("Trigger Manual Completion"),
42 "manuallyComplete");
43
44 static {
45 ShortcutManager.getInstance().add(manual);
46 }
47
48 private KeyListener listener = new KeyHandler();
49
50 /***
51 * @param textComponent
52 * @param wholeText
53 */
54 public ManualCompletionMode(JTextComponent textComponent, boolean wholeText)
55 {
56 super(textComponent, wholeText);
57 }
58
59 /***
60 * @param textComponent
61 */
62 public ManualCompletionMode(JTextComponent textComponent)
63 {
64 super(textComponent);
65 }
66
67 /***
68 * @param textComponent
69 * @param model
70 */
71 public ManualCompletionMode(JTextComponent textComponent,
72 CompletionModel model)
73 {
74 super(textComponent, model);
75 }
76
77 /***
78 * @param textComponent
79 * @param model
80 * @param wholeText
81 */
82 public ManualCompletionMode(JTextComponent textComponent,
83 CompletionModel model, boolean wholeText)
84 {
85 super(textComponent, model, wholeText);
86 }
87
88 public String getName()
89 {
90 return XNap.tr("Manual");
91 }
92
93 protected void disable()
94 {
95 getTextComponent().removeKeyListener(listener);
96 }
97
98
99
100
101 protected void enable()
102 {
103 getTextComponent().addKeyListener(listener);
104 }
105
106 private class KeyHandler extends KeyAdapter
107 {
108 public void keyPressed(KeyEvent e)
109 {
110 if (manual.getKeyStroke() != null
111 && manual.getKeyStroke().equals
112 (KeyStroke.getKeyStrokeForEvent(e))) {
113 String prefix = getText();
114 String completion = getModel().completeUniquePrefix(prefix);
115
116 if (completion.length() > prefix.length()) {
117 setText(completion, completion.length(), prefix.length());
118 }
119 e.consume();
120 }
121 }
122 }
123 }