|
1 |
| package edu.rice.cs.drjava.ui; |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| import java.awt.event.*; |
|
40 |
| import java.awt.Component; |
|
41 |
| import java.awt.Container; |
|
42 |
| import java.awt.GridBagLayout; |
|
43 |
| import java.awt.GridBagConstraints; |
|
44 |
| import java.awt.Insets; |
|
45 |
| import java.awt.Color; |
|
46 |
| import java.awt.EventQueue; |
|
47 |
| import java.io.IOException; |
|
48 |
| import java.io.File; |
|
49 |
| import java.net.URL; |
|
50 |
| import java.util.*; |
|
51 |
| import javax.swing.*; |
|
52 |
| import javax.swing.event.*; |
|
53 |
| import javax.swing.border.EmptyBorder; |
|
54 |
| import java.awt.Dimension; |
|
55 |
| import javax.swing.text.JTextComponent; |
|
56 |
| import javax.swing.text.BadLocationException; |
|
57 |
| |
|
58 |
| import edu.rice.cs.drjava.DrJava; |
|
59 |
| import edu.rice.cs.drjava.model.SingleDisplayModel; |
|
60 |
| import edu.rice.cs.drjava.config.OptionConstants; |
|
61 |
| import edu.rice.cs.drjava.ui.predictive.PredictiveInputFrame; |
|
62 |
| import edu.rice.cs.drjava.ui.predictive.PredictiveInputModel; |
|
63 |
| import edu.rice.cs.drjava.model.DummyOpenDefDoc; |
|
64 |
| import edu.rice.cs.drjava.platform.PlatformFactory; |
|
65 |
| import edu.rice.cs.drjava.model.OpenDefinitionsDocument; |
|
66 |
| |
|
67 |
| import edu.rice.cs.plt.lambda.*; |
|
68 |
| import edu.rice.cs.plt.iter.*; |
|
69 |
| import edu.rice.cs.plt.collect.UnionSet; |
|
70 |
| |
|
71 |
| import edu.rice.cs.util.swing.SwingFrame; |
|
72 |
| import edu.rice.cs.util.swing.Utilities; |
|
73 |
| |
|
74 |
| import static edu.rice.cs.drjava.ui.MainFrameStatics.*; |
|
75 |
| import static edu.rice.cs.drjava.ui.predictive.PredictiveInputModel.*; |
|
76 |
| import static edu.rice.cs.drjava.ui.predictive.PredictiveInputFrame.FrameState; |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| public class AutoCompletePopup { |
|
82 |
| |
|
83 |
| final protected MainFrame _mainFrame; |
|
84 |
| |
|
85 |
| |
|
86 |
| JCheckBox _completeJavaAPICheckbox = new JCheckBox("Java API"); |
|
87 |
| |
|
88 |
| |
|
89 |
| protected FrameState _lastState = null; |
|
90 |
| |
|
91 |
| |
|
92 |
| final protected Set<AutoCompletePopupEntry> _allEntries; |
|
93 |
| |
|
94 |
| |
|
95 |
| final protected Set<AutoCompletePopupEntry> _docEntries; |
|
96 |
| |
|
97 |
| |
|
98 |
| final protected Set<AutoCompletePopupEntry> _apiEntries; |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
0
| public AutoCompletePopup(MainFrame mf) { this(mf, null); }
|
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
0
| public AutoCompletePopup(MainFrame mf, String frameState) {
|
|
109 |
0
| _mainFrame = mf;
|
|
110 |
0
| if (frameState!=null) _lastState = new FrameState(frameState);
|
|
111 |
0
| _docEntries = new HashSet<AutoCompletePopupEntry>();
|
|
112 |
0
| _apiEntries = new HashSet<AutoCompletePopupEntry>();
|
|
113 |
0
| _allEntries = new UnionSet<AutoCompletePopupEntry>(_apiEntries,
|
|
114 |
| new UnionSet<AutoCompletePopupEntry>(mf.getCompleteClassSet(), |
|
115 |
| _docEntries)); |
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
0
| public void show(final Component parent,
|
|
129 |
| final String title, |
|
130 |
| final String initial, |
|
131 |
| final int loc, |
|
132 |
| final Runnable canceledAction, |
|
133 |
| final Runnable3<AutoCompletePopupEntry,Integer,Integer> acceptedAction) { |
|
134 |
0
| show(parent, title, initial, loc, IterUtil.make("OK"),
|
|
135 |
| IterUtil.make(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)), 0, canceledAction, IterUtil.make(acceptedAction)); |
|
136 |
| } |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
0
| public void show(final Component parent,
|
|
150 |
| final String title, |
|
151 |
| final String initial, |
|
152 |
| final int loc, |
|
153 |
| final SizedIterable<String> actionNames, |
|
154 |
| final Runnable canceledAction, |
|
155 |
| final SizedIterable<Runnable3<AutoCompletePopupEntry,Integer,Integer>> acceptedActions) { |
|
156 |
0
| SizedIterable<KeyStroke> actionKeyStrokes =
|
|
157 |
| IterUtil.compose(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), |
|
158 |
| IterUtil.copy((KeyStroke)null, acceptedActions.size()-1)); |
|
159 |
0
| show(parent, title, initial, loc, actionNames, actionKeyStrokes,
|
|
160 |
| 0, canceledAction, acceptedActions); |
|
161 |
| } |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
0
| public void show(final Component parent,
|
|
177 |
| final String title, |
|
178 |
| final String initial, |
|
179 |
| final int loc, |
|
180 |
| final SizedIterable<String> actionNames, |
|
181 |
| final SizedIterable<KeyStroke> actionKeyStrokes, |
|
182 |
| final int oneMatchActionIndex, |
|
183 |
| final Runnable canceledAction, |
|
184 |
| final SizedIterable<Runnable3<AutoCompletePopupEntry,Integer,Integer>> acceptedActions) { |
|
185 |
0
| assert actionNames.size() == acceptedActions.size();
|
|
186 |
0
| assert actionNames.size() == actionKeyStrokes.size();
|
|
187 |
| |
|
188 |
0
| _completeJavaAPICheckbox.setSelected(DrJava.getConfig().getSetting(OptionConstants.DIALOG_COMPLETE_JAVAAPI));
|
|
189 |
0
| _completeJavaAPICheckbox.setEnabled(true);
|
|
190 |
| |
|
191 |
0
| new Thread() {
|
|
192 |
0
| public void run() {
|
|
193 |
0
| List<OpenDefinitionsDocument> docs = _mainFrame.getModel().getOpenDefinitionsDocuments();
|
|
194 |
0
| if ((docs == null) || (docs.size() == 0)) {
|
|
195 |
0
| Utilities.invokeAndWait(canceledAction);
|
|
196 |
0
| return;
|
|
197 |
| } |
|
198 |
| |
|
199 |
0
| AutoCompletePopupEntry currentEntry = null;
|
|
200 |
0
| _docEntries.clear();
|
|
201 |
0
| for(OpenDefinitionsDocument d: docs) {
|
|
202 |
0
| if (d.isUntitled()) continue;
|
|
203 |
0
| String str = d.toString();
|
|
204 |
0
| if (str.lastIndexOf('.')>=0) {
|
|
205 |
0
| str = str.substring(0, str.lastIndexOf('.'));
|
|
206 |
| } |
|
207 |
0
| GoToFileListEntry entry = new GoToFileListEntry(d, str);
|
|
208 |
0
| if (d.equals(_mainFrame.getModel().getActiveDocument())) currentEntry = entry;
|
|
209 |
0
| _docEntries.add(entry);
|
|
210 |
| } |
|
211 |
| |
|
212 |
0
| if (DrJava.getConfig().getSetting(OptionConstants.DIALOG_COMPLETE_JAVAAPI)) {
|
|
213 |
0
| addJavaAPI();
|
|
214 |
| } |
|
215 |
| |
|
216 |
0
| final PredictiveInputModel<AutoCompletePopupEntry> pim =
|
|
217 |
| new PredictiveInputModel<AutoCompletePopupEntry>(true, new PrefixStrategy<AutoCompletePopupEntry>(), _allEntries); |
|
218 |
0
| String mask = "";
|
|
219 |
0
| String s = initial;
|
|
220 |
| |
|
221 |
| |
|
222 |
0
| if ((loc<s.length()) && (!Character.isWhitespace(s.charAt(loc))) &&
|
|
223 |
| ("()[]{}<>.,:;/*+-!~&|%".indexOf(s.charAt(loc)) == -1)) { |
|
224 |
| |
|
225 |
0
| Utilities.invokeAndWait(canceledAction);
|
|
226 |
0
| return;
|
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
0
| int start = loc;
|
|
231 |
0
| while(start > 0) {
|
|
232 |
0
| if (!Character.isJavaIdentifierPart(s.charAt(start-1))) { break; }
|
|
233 |
0
| --start;
|
|
234 |
| } |
|
235 |
0
| while((start<s.length()) && (!Character.isJavaIdentifierStart(s.charAt(start))) && (start < loc)) {
|
|
236 |
0
| ++start;
|
|
237 |
| } |
|
238 |
| |
|
239 |
0
| int end = loc-1;
|
|
240 |
| |
|
241 |
0
| if ((start>=0) && (end < s.length())) {
|
|
242 |
0
| mask = s.substring(start, end + 1);
|
|
243 |
0
| pim.setMask(mask);
|
|
244 |
| } |
|
245 |
| |
|
246 |
0
| if ((pim.getMatchingItems().size() == 1) && (oneMatchActionIndex >= 0)) {
|
|
247 |
0
| if (pim.getCurrentItem() != null) {
|
|
248 |
| |
|
249 |
0
| final int finalStart = start;
|
|
250 |
0
| Utilities.invokeAndWait(new Runnable() {
|
|
251 |
0
| public void run() {
|
|
252 |
0
| Iterator<Runnable3<AutoCompletePopupEntry,Integer,Integer>> actionIt =
|
|
253 |
| acceptedActions.iterator(); |
|
254 |
0
| Runnable3<AutoCompletePopupEntry,Integer,Integer> action;
|
|
255 |
0
| int i = oneMatchActionIndex;
|
|
256 |
0
| do {
|
|
257 |
0
| action = actionIt.next();
|
|
258 |
0
| } while(i<0);
|
|
259 |
0
| action.run(pim.getCurrentItem(), finalStart, loc);
|
|
260 |
| } |
|
261 |
| }); |
|
262 |
0
| return;
|
|
263 |
| } |
|
264 |
| } |
|
265 |
| |
|
266 |
| |
|
267 |
0
| pim.setMask(mask);
|
|
268 |
0
| if (pim.getMatchingItems().size() == 0) {
|
|
269 |
| |
|
270 |
0
| mask = pim.getMask();
|
|
271 |
0
| while(mask.length() > 0) {
|
|
272 |
0
| mask = mask.substring(0, mask.length() - 1);
|
|
273 |
0
| pim.setMask(mask);
|
|
274 |
0
| if (pim.getMatchingItems().size() > 0) { break; }
|
|
275 |
| } |
|
276 |
| } |
|
277 |
0
| final PredictiveInputFrame<AutoCompletePopupEntry> completeWordDialog =
|
|
278 |
| createCompleteWordDialog(title, start, loc, actionNames, actionKeyStrokes, |
|
279 |
| canceledAction, acceptedActions); |
|
280 |
0
| final AutoCompletePopupEntry finalCurrentEntry = currentEntry;
|
|
281 |
0
| Utilities.invokeLater(new Runnable() {
|
|
282 |
0
| public void run() {
|
|
283 |
0
| completeWordDialog.setModel(true, pim);
|
|
284 |
0
| completeWordDialog.selectStrategy();
|
|
285 |
0
| if (finalCurrentEntry != null) {
|
|
286 |
0
| completeWordDialog.setCurrentItem(finalCurrentEntry);
|
|
287 |
| } |
|
288 |
0
| completeWordDialog.setLocationRelativeTo(parent);
|
|
289 |
| |
|
290 |
0
| if (_lastState != null) {
|
|
291 |
0
| completeWordDialog.setFrameState(_lastState);
|
|
292 |
| } |
|
293 |
| |
|
294 |
0
| completeWordDialog.setVisible(true);
|
|
295 |
| } |
|
296 |
| }); |
|
297 |
| } |
|
298 |
| }.start(); |
|
299 |
| } |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
0
| public FrameState getFrameState() { return _lastState; }
|
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
0
| public void setFrameState(FrameState ds) {
|
|
310 |
0
| _lastState = ds;
|
|
311 |
| } |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
0
| public void setFrameState(String s) {
|
|
317 |
0
| try { _lastState = new FrameState(s); }
|
|
318 |
0
| catch(IllegalArgumentException e) { _lastState = null; }
|
|
319 |
| } |
|
320 |
| |
|
321 |
0
| protected PredictiveInputFrame<AutoCompletePopupEntry>
|
|
322 |
| createCompleteWordDialog(final String title, |
|
323 |
| final int start, |
|
324 |
| final int loc, |
|
325 |
| final SizedIterable<String> actionNames, |
|
326 |
| final SizedIterable<KeyStroke> actionKeyStrokes, |
|
327 |
| final Runnable canceledAction, |
|
328 |
| final SizedIterable<Runnable3<AutoCompletePopupEntry,Integer,Integer>> acceptedActions) { |
|
329 |
0
| final SimpleBox<PredictiveInputFrame<AutoCompletePopupEntry>> dialogThunk =
|
|
330 |
| new SimpleBox<PredictiveInputFrame<AutoCompletePopupEntry>>(); |
|
331 |
| |
|
332 |
0
| _completeJavaAPICheckbox.addActionListener(new ActionListener() {
|
|
333 |
0
| public void actionPerformed(ActionEvent e) {
|
|
334 |
0
| String curMask = dialogThunk.value().getMask();
|
|
335 |
0
| DrJava.getConfig().setSetting(OptionConstants.DIALOG_COMPLETE_JAVAAPI, _completeJavaAPICheckbox.isSelected());
|
|
336 |
0
| if (_completeJavaAPICheckbox.isSelected()) addJavaAPI(); else removeJavaAPI();
|
|
337 |
0
| dialogThunk.value().setItems(true,_allEntries);
|
|
338 |
0
| dialogThunk.value().setMask(curMask);
|
|
339 |
0
| dialogThunk.value().resetFocus();
|
|
340 |
| } |
|
341 |
| }); |
|
342 |
0
| PlatformFactory.ONLY.setMnemonic(_completeJavaAPICheckbox,'j');
|
|
343 |
0
| PredictiveInputFrame.InfoSupplier<AutoCompletePopupEntry> info =
|
|
344 |
| new PredictiveInputFrame.InfoSupplier<AutoCompletePopupEntry>() { |
|
345 |
0
| public String value(AutoCompletePopupEntry entry) {
|
|
346 |
| |
|
347 |
0
| StringBuilder sb = new StringBuilder();
|
|
348 |
0
| sb.append(entry.getFullPackage());
|
|
349 |
0
| sb.append(entry.getClassName());
|
|
350 |
0
| return sb.toString();
|
|
351 |
| } |
|
352 |
| }; |
|
353 |
| |
|
354 |
0
| List<PredictiveInputFrame.CloseAction<AutoCompletePopupEntry>> actions
|
|
355 |
| = new ArrayList<PredictiveInputFrame.CloseAction<AutoCompletePopupEntry>>(); |
|
356 |
| |
|
357 |
0
| Iterator<String> nameIt = actionNames.iterator();
|
|
358 |
0
| Iterator<Runnable3<AutoCompletePopupEntry,Integer,Integer>> actionIt =
|
|
359 |
| acceptedActions.iterator(); |
|
360 |
0
| Iterator<KeyStroke> ksIt = actionKeyStrokes.iterator();
|
|
361 |
0
| for(int i=0; i<acceptedActions.size(); ++i) {
|
|
362 |
0
| final int acceptedActionIndex = i;
|
|
363 |
0
| final String name = nameIt.next();
|
|
364 |
0
| final Runnable3<AutoCompletePopupEntry,Integer,Integer> runnable = actionIt.next();
|
|
365 |
0
| final KeyStroke ks = ksIt.next();
|
|
366 |
| |
|
367 |
0
| PredictiveInputFrame.CloseAction<AutoCompletePopupEntry> okAction =
|
|
368 |
| new PredictiveInputFrame.CloseAction<AutoCompletePopupEntry>() { |
|
369 |
0
| public String getName() { return name; }
|
|
370 |
0
| public KeyStroke getKeyStroke() { return ks; }
|
|
371 |
0
| public String getToolTipText() { return "Complete the identifier"; }
|
|
372 |
0
| public Object value(final PredictiveInputFrame<AutoCompletePopupEntry> p) {
|
|
373 |
0
| _lastState = p.getFrameState();
|
|
374 |
0
| if (p.getItem() != null) {
|
|
375 |
0
| Utilities.invokeAndWait(new Runnable() {
|
|
376 |
0
| public void run() {
|
|
377 |
0
| runnable.run(p.getItem(), start, loc);
|
|
378 |
| } |
|
379 |
| }); |
|
380 |
| } |
|
381 |
| else { |
|
382 |
0
| Utilities.invokeAndWait(canceledAction);
|
|
383 |
| } |
|
384 |
0
| return null;
|
|
385 |
| } |
|
386 |
| }; |
|
387 |
0
| actions.add(okAction);
|
|
388 |
| } |
|
389 |
| |
|
390 |
0
| PredictiveInputFrame.CloseAction<AutoCompletePopupEntry> cancelAction =
|
|
391 |
| new PredictiveInputFrame.CloseAction<AutoCompletePopupEntry>() { |
|
392 |
0
| public String getName() { return "Cancel"; }
|
|
393 |
0
| public KeyStroke getKeyStroke() { return KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); }
|
|
394 |
0
| public String getToolTipText() { return null; }
|
|
395 |
0
| public Object value(PredictiveInputFrame<AutoCompletePopupEntry> p) {
|
|
396 |
0
| _lastState = p.getFrameState();
|
|
397 |
0
| Utilities.invokeAndWait(canceledAction);
|
|
398 |
0
| return null;
|
|
399 |
| } |
|
400 |
| }; |
|
401 |
0
| actions.add(cancelAction);
|
|
402 |
| |
|
403 |
| |
|
404 |
0
| java.util.ArrayList<MatchingStrategy<AutoCompletePopupEntry>> strategies =
|
|
405 |
| new java.util.ArrayList<MatchingStrategy<AutoCompletePopupEntry>>(); |
|
406 |
0
| strategies.add(new FragmentStrategy<AutoCompletePopupEntry>());
|
|
407 |
0
| strategies.add(new PrefixStrategy<AutoCompletePopupEntry>());
|
|
408 |
0
| strategies.add(new RegExStrategy<AutoCompletePopupEntry>());
|
|
409 |
| |
|
410 |
0
| GoToFileListEntry entry = new GoToFileListEntry(new DummyOpenDefDoc() {
|
|
411 |
0
| public String getPackageNameFromDocument() { return ""; }
|
|
412 |
| }, "dummyComplete"); |
|
413 |
0
| dialogThunk.set(new PredictiveInputFrame<AutoCompletePopupEntry>(null,
|
|
414 |
| title, |
|
415 |
| true, |
|
416 |
| true, |
|
417 |
| info, |
|
418 |
| strategies, |
|
419 |
| actions, |
|
420 |
| actions.size()-1, |
|
421 |
| entry) { |
|
422 |
0
| protected JComponent[] makeOptions() {
|
|
423 |
0
| return new JComponent[] { _completeJavaAPICheckbox };
|
|
424 |
| } |
|
425 |
| }); |
|
426 |
0
| dialogThunk.value().setSize(dialogThunk.value().getSize().width, 500);
|
|
427 |
0
| dialogThunk.value().setLocationRelativeTo(_mainFrame);
|
|
428 |
0
| return dialogThunk.value();
|
|
429 |
| } |
|
430 |
| |
|
431 |
0
| private void addJavaAPI() {
|
|
432 |
0
| Set<JavaAPIListEntry> apiSet = _mainFrame.getJavaAPISet();
|
|
433 |
0
| if (apiSet == null) {
|
|
434 |
0
| DrJava.getConfig().setSetting(OptionConstants.DIALOG_COMPLETE_JAVAAPI, Boolean.FALSE);
|
|
435 |
0
| _completeJavaAPICheckbox.setSelected(false);
|
|
436 |
0
| _completeJavaAPICheckbox.setEnabled(false);
|
|
437 |
| } |
|
438 |
| else { |
|
439 |
0
| _apiEntries.clear();
|
|
440 |
0
| _apiEntries.addAll(apiSet);
|
|
441 |
| } |
|
442 |
| } |
|
443 |
| |
|
444 |
0
| private void removeJavaAPI() {
|
|
445 |
0
| _apiEntries.clear();
|
|
446 |
| } |
|
447 |
| } |