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.event.DocumentEvent;
25 import javax.swing.event.DocumentListener;
26 import javax.swing.text.BadLocationException;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.xnap.commons.gui.util.GUIHelper;
30
31
32 /***
33 * Uses a {@link org.xnap.commons.gui.completion.CompletionPopup} to present
34 * its completions to the user.
35 *
36 * @author Felix Berger
37 */
38 public class DropDownListCompletionMode extends AbstractCompletionMode
39 {
40 protected CompletionPopup popup = new CompletionPopup();
41
42 protected DocumentListener listener = new DocumentHandler();
43
44 private static Log logger = LogFactory.getLog(DropDownListCompletionMode.class);
45
46 protected void enable()
47 {
48 popup.enablePopup(getCompletion());
49 getTextComponent().getDocument().addDocumentListener(listener);
50 }
51
52 public void disable()
53 {
54 popup.disablePopup();
55 getTextComponent().getDocument().removeDocumentListener(listener);
56 }
57
58 protected void showPopup()
59 {
60 if (!getTextComponent().isShowing()) {
61 return;
62 }
63
64 if (getCompletion().isWholeTextCompletion()) {
65 GUIHelper.showPopupMenu(popup, getTextComponent(), 0,
66 getTextComponent().getHeight());
67 }
68 else {
69 try {
70 int pos = getTextComponent().getCaretPosition() + 1;
71 Rectangle r = getTextComponent().modelToView(pos);
72 Point p = r.getLocation();
73 GUIHelper.showPopupMenu(popup, getTextComponent(),
74 p.x, p.y + r.height);
75 }
76 catch (BadLocationException ble) {
77 logger.debug("bad location", ble);
78 }
79 }
80 }
81
82 private class DocumentHandler implements DocumentListener
83 {
84 public void insertUpdate(DocumentEvent e)
85 {
86 if (e.getLength() == 1 && getModel().complete(getText())) {
87 showPopup();
88 }
89 else {
90 popup.setVisible(false);
91 }
92 }
93
94 public void removeUpdate(DocumentEvent e)
95 {
96 if (e.getLength() == 1 && getModel().complete(getText())) {
97 showPopup();
98 }
99 else {
100 popup.setVisible(false);
101 }
102 }
103
104
105
106
107 public void changedUpdate(DocumentEvent e)
108 {
109 }
110 }
111 }