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 java.awt.Point;
23 import java.awt.Rectangle;
24 import javax.swing.text.BadLocationException;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.xnap.commons.gui.util.GUIHelper;
28
29 /***
30 * This mode mimics KDE's automatic drop down mode. The longest common
31 * unique completion is inserted after the cursor while all ambiguous
32 * completions are presented to the user below the text field in a completion
33 * popup window.
34 *
35 * @author Felix Berger
36 */
37 public class AutomaticDropDownCompletionMode extends AutomaticCompletionMode
38 {
39 private CompletionPopup popup = new CompletionPopup();
40
41 private static Log logger = LogFactory.getLog(AutomaticCompletionMode.class);
42
43 protected void enable()
44 {
45 super.enable();
46 popup.enablePopup(getCompletion());
47 }
48
49 public void disable()
50 {
51 super.disable();
52 popup.disablePopup();
53 }
54
55 protected void complete(String prefix)
56 {
57 super.complete(prefix);
58 if (getModel().getSize() > 0) {
59 showPopup();
60 }
61 }
62
63 protected void showPopup()
64 {
65 if (!getTextComponent().isShowing()) {
66 return;
67 }
68
69 if (getCompletion().isWholeTextCompletion()) {
70 GUIHelper.showPopupMenu(popup, getTextComponent(), 0,
71 getTextComponent().getHeight());
72 }
73 else {
74 try {
75 int pos = getTextComponent().getCaretPosition() + 1;
76 Rectangle r = getTextComponent().modelToView(pos);
77 Point p = r.getLocation();
78 GUIHelper.showPopupMenu(popup, getTextComponent(),
79 p.x, p.y + r.height);
80 }
81 catch (BadLocationException ble) {
82 logger.debug("bad location", ble);
83 }
84 }
85 }
86
87 }
88
89