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 javax.swing.JTextField;
22 import javax.swing.text.Document;
23 import javax.swing.text.JTextComponent;
24
25 import org.xnap.gui.event.PopupListener;
26 import org.xnap.gui.menu.TextFieldMenu;
27 import org.xnap.util.Preferences;
28 import org.xnap.util.PreferencesProvider;
29
30 /***
31 * Extends JTextField's functionality enabling basic completion for input.
32 *
33 * It provides a context menu for choosing the {@link CompletionMode}.
34 */
35 public class XNapTextField extends JTextField implements Completable
36 {
37
38 /***
39 * Holds the currently active completion mode.
40 */
41 private CompletionMode mode = null;
42
43 private TextFieldMenu menu;
44
45 public XNapTextField()
46 {
47 initialize(null);
48 }
49
50 public XNapTextField(CompletionModel model)
51 {
52 initialize(model);
53 }
54
55 public XNapTextField(Object[] completionData)
56 {
57 initialize(new DefaultCompletionModel(completionData));
58 }
59
60 /***
61 * @param columns
62 */
63 public XNapTextField(int columns)
64 {
65 super(columns);
66 initialize(null);
67 }
68
69 /***
70 * @param text
71 */
72 public XNapTextField(String text)
73 {
74 super(text);
75 initialize(null);
76 }
77
78 /***
79 * @param text
80 * @param columns
81 */
82 public XNapTextField(String text, int columns)
83 {
84 super(text, columns);
85 initialize(null);
86 }
87
88 /***
89 * @param doc
90 * @param text
91 * @param columns
92 */
93 public XNapTextField(Document doc, String text, int columns)
94 {
95 super(doc, text, columns);
96 initialize(null);
97 }
98
99 private void initialize(CompletionModel model)
100 {
101 menu = new TextFieldMenu((Completable)this);
102 addMouseListener(new PopupListener(menu));
103 if (model != null) {
104 mode.setModel(model);
105 }
106 }
107
108 public void setPreferences(String prefsKey, PreferencesProvider prefs)
109 {
110 menu.setPreferences(prefsKey, prefs);
111 }
112
113 public void setPreferences(String prefsKey)
114 {
115 setPreferences(prefsKey, Preferences.getInstance());
116 }
117
118 public CompletionModel getCompletionModel()
119 {
120 return mode.getModel();
121 }
122
123 public CompletionMode getCompletionMode()
124 {
125 return mode;
126 }
127
128 public void setCompletionModel(CompletionModel model)
129 {
130 mode.setModel(model);
131 }
132
133 /***
134 * @param m
135 */
136 private void setCompletionMode(CompletionMode newMode)
137 {
138 if (mode != null) {
139 if (mode.isEnabled()) {
140 mode.setEnabled(false);
141 newMode.setModel(mode.getModel());
142 mode = newMode;
143 mode.setEnabled(true);
144 }
145 else {
146 newMode.setModel(mode.getModel());
147 mode = newMode;
148 }
149 }
150 else {
151 mode = newMode;
152 mode.setEnabled(true);
153 }
154 }
155
156 public void setCompletionMode(String mode)
157 {
158 CompletionMode m = CompletionModeFactory.createCompletionMode(mode, this,
159 true);
160 setCompletionMode(m);
161 }
162
163 public JTextComponent getTextComponent()
164 {
165 return this;
166 }
167 }