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.Point;
22 import java.awt.Rectangle;
23
24 import javax.swing.event.DocumentEvent;
25 import javax.swing.event.DocumentListener;
26 import javax.swing.text.BadLocationException;
27 import javax.swing.text.JTextComponent;
28
29 import org.apache.log4j.Logger;
30 import org.xnap.XNap;
31 import org.xnap.gui.util.GUIHelper;
32
33
34 /***
35 * Uses a {@link org.xnap.gui.component.CompletionPopup} to present
36 * its completions to the user.
37 */
38 public class DropDownListCompletionMode extends CompletionMode
39 {
40 protected CompletionPopup popup = null;
41
42 protected DocumentListener listener = new DocumentHandler();
43
44 private static Logger logger = Logger.getLogger(DropDownListCompletionMode.class);
45
46 /***
47 * @param textComponent
48 * @param wholeText
49 */
50 public DropDownListCompletionMode(JTextComponent textComponent,
51 boolean wholeText)
52 {
53 super(textComponent, wholeText);
54 }
55
56 /***
57 * @param textComponent
58 */
59 public DropDownListCompletionMode(JTextComponent textComponent)
60 {
61 super(textComponent);
62 }
63
64 /***
65 * @param textComponent
66 * @param model
67 * @param wholeText
68 */
69 public DropDownListCompletionMode(JTextComponent textComponent,
70 CompletionModel model, boolean wholeText)
71 {
72 super(textComponent, model, wholeText);
73 }
74
75 public DropDownListCompletionMode(JTextComponent textComponent,
76 CompletionModel model)
77 {
78 super(textComponent, model);
79 }
80
81
82
83
84 public String getName()
85 {
86 return XNap.tr("Dropdown List");
87 }
88
89 protected void enable()
90 {
91 if (popup == null) {
92 popup = new CompletionPopup(this);
93 popup.enablePopup();
94 }
95 else {
96 popup.enablePopup();
97 }
98 getTextComponent().getDocument().addDocumentListener(listener);
99 }
100
101 protected void disable()
102 {
103 if (popup != null) {
104 popup.disablePopup();
105 }
106 getTextComponent().getDocument().removeDocumentListener(listener);
107 }
108
109 protected void showPopup()
110 {
111 if (!getTextComponent().isVisible()) {
112 return;
113 }
114
115 if (isWholeTextCompletion()) {
116 GUIHelper.showPopupMenu(popup, getTextComponent(), 0,
117 getTextComponent().getHeight());
118 }
119 else {
120 try {
121 int pos = getTextComponent().getCaretPosition() + 1;
122 Rectangle r = getTextComponent().modelToView(pos);
123 Point p = r.getLocation();
124 GUIHelper.showPopupMenu(popup, getTextComponent(),
125 p.x, p.y + r.height);
126 }
127 catch (BadLocationException ble) {
128 logger.debug("bad location", ble);
129 }
130 }
131
132
133
134 getTextComponent().requestFocus();
135 }
136
137 private class DocumentHandler extends DocumentAdapter
138 {
139 public void insertUpdate(DocumentEvent e)
140 {
141 if (e.getLength() == 1 && getModel().complete(getText())) {
142 showPopup();
143 }
144 else {
145 popup.setVisible(false);
146 }
147 }
148
149 public void removeUpdate(DocumentEvent e)
150 {
151 if (e.getLength() == 1 && getModel().complete(getText())) {
152 showPopup();
153 }
154 else {
155 popup.setVisible(false);
156 }
157 }
158 }
159 }