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 static org.xnap.commons.gui.completion.CompletionModeFactory.I18N;
23 import java.awt.event.InputEvent;
24 import java.awt.event.KeyAdapter;
25 import java.awt.event.KeyEvent;
26 import java.awt.event.KeyListener;
27 import javax.swing.KeyStroke;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.xnap.commons.gui.shortcut.DefaultShortcut;
31 import org.xnap.commons.gui.shortcut.ShortcutManager;
32
33
34 /***
35 * This mode provides manual completion similar to the one offered by KDE.
36 *
37 * Completion has to be triggered manually using a shortcut which can be
38 * configured in the {@link org.xnap.commons.gui.shortcut.ShortcutManager}
39 * framework.
40 *
41 * @author Felix Berger
42 */
43 public class ManualCompletionMode extends AbstractCompletionMode
44 {
45 private static Log logger = LogFactory.getLog(ManualCompletionMode.class);
46
47 private static DefaultShortcut manual = new DefaultShortcut
48 (KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK),
49 I18N.tr("Completion"), I18N.tr("Trigger Manual Completion"));
50
51
52 static {
53 ShortcutManager.getInstance().add(manual);
54 }
55
56 private KeyListener listener = new KeyHandler();
57
58 public void disable()
59 {
60 getTextComponent().removeKeyListener(listener);
61 }
62
63
64
65
66 protected void enable()
67 {
68 getTextComponent().addKeyListener(listener);
69 }
70
71 private class KeyHandler extends KeyAdapter
72 {
73 public void keyPressed(KeyEvent e)
74 {
75 if (manual.getKeyStroke() != null
76 && manual.getKeyStroke().equals
77 (KeyStroke.getKeyStrokeForEvent(e))) {
78 String prefix = getText();
79 String completion = getModel().completeUniquePrefix(prefix);
80
81 if (completion.length() > prefix.length()) {
82 setText(completion, completion.length(), prefix.length());
83 }
84 e.consume();
85 }
86 }
87 }
88 }