-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathAutoCompletion.java
More file actions
1770 lines (1488 loc) · 51.6 KB
/
AutoCompletion.java
File metadata and controls
1770 lines (1488 loc) · 51.6 KB
1
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 12/21/2008
*
* AutoCompletion.java - Handles auto-completion for a text component.
*
* This library is distributed under a modified BSD license. See the included
* AutoComplete.License.txt file for details.
*/
package org.fife.ui.autocomplete;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import sun.management.snmp.jvminstr.JvmThreadInstanceEntryImpl;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.event.*;
import javax.swing.text.*;
/**
* Adds auto-completion to a text component. Provides a popup window with a
* list of auto-complete choices on a given keystroke, such as Crtrl+Space.<p>
*
* Depending on the {@link CompletionProvider} installed, the following
* auto-completion features may be enabled:
*
* <ul>
* <li>An auto-complete choices list made visible via e.g. Ctrl+Space</li>
* <li>A "description" window displayed alongside the choices list that
* provides documentation on the currently selected completion choice
* (as seen in Eclipse and NetBeans).</li>
* <li>Parameter assistance. If this is enabled, if the user enters a
* "parameterized" completion, such as a method or a function, then they
* will receive a tool tip describing the arguments they have to enter to
* the completion. Also, the arguments can be navigated via tab and
* shift+tab (a la Eclipse and NetBeans).</li>
* </ul>
*
* @author Robert Futrell
* @version 1.0
*/
/* This class handles intercepting window and hierarchy events from the text
* component, so the popup window is only visible when it should be visible. It
* also handles communication between the CompletionProvider and the actual
* popup Window.
*/
public class AutoCompletion {
/**
* The text component we're providing completion for.
*/
private JTextComponent textComponent;
/**
* The parent window of {@link #textComponent}.
*/
private Window parentWindow;
/**
* The popup window containing completion choices.
*/
private AutoCompletePopupWindow popupWindow;
/**
* The preferred size of the completion choices window. This field exists
* because the user will likely set the preferred size of the window before
* it is actually created.
*/
private Dimension preferredChoicesWindowSize;
/**
* The preferred size of the optional description window. This field only
* exists because the user may (and usually will) set the size of the
* description window before it exists (it must be parented to a Window).
*/
private Dimension preferredDescWindowSize;
/**
* Manages any parameterized completions that are inserted.
*/
private ParameterizedCompletionContext pcc;
/**
* Provides the completion options relevant to the current caret position.
*/
private CompletionProvider provider;
/**
* The renderer to use for the completion choices. If this is
* <code>null</code>, then a default renderer is used.
*/
private ListCellRenderer renderer;
/**
* The handler to use when an external URL is clicked in the help
* documentation.
*/
private ExternalURLHandler externalURLHandler;
/**
* An optional redirector that converts URL's to some other location before
* being handed over to <code>externalURLHandler</code>.
*/
private static LinkRedirector linkRedirector;
/**
* Whether the description window should be displayed along with the
* completion choice window.
*/
private boolean showDescWindow;
/**
* Whether auto-complete is enabled.
*/
private boolean autoCompleteEnabled;
/**
* Whether the auto-activation of auto-complete (after a delay, after the
* user types an appropriate character) is enabled.
*/
private boolean autoActivationEnabled;
/**
* Whether or not, when there is only a single auto-complete option that
* matches the text at the current text position, that text should be
* auto-inserted, instead of the completion window displaying.
*/
private boolean autoCompleteSingleChoices;
/**
* Whether parameter assistance is enabled.
*/
private boolean parameterAssistanceEnabled;
/**
* A renderer used for {@link Completion}s in the optional parameter choices
* popup window (displayed when a {@link ParameterizedCompletion} is
* code-completed). If this isn't set, a default renderer is used.
*/
private ListCellRenderer paramChoicesRenderer;
/**
* The keystroke that triggers the completion window.
*/
private KeyStroke trigger;
/**
* The keystroke to show method parameter tooltip
*/
private KeyStroke paramTrigger;
/**
* The previous key in the text component's <code>InputMap</code> for the
* trigger key.
*/
private Object oldTriggerKey;
/**
* The action previously assigned to {@link #trigger}, so we can reset it if
* the user disables auto-completion.
*/
private Action oldTriggerAction;
/**
* The previous key in the text component's <code>InputMap</code> for the
* paramTrigger key.
*/
private Object oldParamTriggerKey;
/**
* The action previously assigned to {@link #paramTrigger}, so we can reset it if
* the user disables auto-completion.
*/
private Action oldParamTriggerAction;
/**
* List of tooltip windows in case user presses Ctrl-P. If multiple ParameterizedCompletion is returned from the
* provider, we show multiple tool windows. This holds reference, so we can remove it.
*/
private List<ParameterizedCompletionDescriptionToolTip> parameterTooltipWindows = new ArrayList<ParameterizedCompletionDescriptionToolTip>();
private ParameterTooltipListener parameterTooltipListener;
/**
* The previous key in the text component's <code>InputMap</code> for the
* parameter completion trigger key.
*/
private Object oldParenKey;
/**
* The action previously assigned to the parameter completion key, so we can
* reset it when we uninstall.
*/
private Action oldParenAction;
/**
* Listens for events in the parent window that affect the visibility of the
* popup windows.
*/
private ParentWindowListener parentWindowListener;
/**
* Listens for events from the text component that affect the visibility of
* the popup windows.
*/
private TextComponentListener textComponentListener;
/**
* Listens for events in the text component that cause the popup windows to
* automatically activate.
*/
private AutoActivationListener autoActivationListener;
/**
* Listens for LAF changes so the auto-complete windows automatically update
* themselves accordingly.
*/
private LookAndFeelChangeListener lafListener;
/**
* Listens for events from the popup window.
*/
private PopupWindowListener popupWindowListener;
/**
* All listeners registered on this component.
*/
private EventListenerList listeners;
/**
* Whether or not the popup should be hidden when user types a space (or any
* character that resets the completion list to "all completions"). Defaults
* to true.
*/
private boolean hideOnNoText;
/**
* Whether or not the popup should be hidden when the CompletionProvider
* changes. If set to false, caller has to ensure refresh of the popup
* content. Defaults to true.
*/
private boolean hideOnCompletionProviderChange;
/**
* The key used in the input map for the AutoComplete action.
*/
private static final String PARAM_TRIGGER_KEY = "AutoComplete";
/**
* The key used in the input map for the AutoComplete action.
*/
private static final String SHOW_PARAM_TRIGGER_KEY = "AutoCompleteParams";
/**
* Key used in the input map for the parameter completion action.
*/
private static final String PARAM_COMPLETE_KEY = "AutoCompletion.FunctionStart";
/**
* Stores how to render auto-completion-specific highlights in text
* components.
*/
private static final AutoCompletionStyleContext styleContext = new AutoCompletionStyleContext();
/**
* Whether debug messages should be printed to stdout as AutoCompletion
* runs.
*/
private static final boolean DEBUG = initDebug();
/**
* Constructor.
*
* @param provider The completion provider. This cannot be <code>null</code>
*/
public AutoCompletion(CompletionProvider provider) {
setChoicesWindowSize(350, 200);
setDescriptionWindowSize(350, 250);
setCompletionProvider(provider);
setTriggerKey(getDefaultTriggerKey());
setParamTrigger(getDefaultParamTriggerKey());
setAutoCompleteEnabled(true);
setAutoCompleteSingleChoices(true);
setAutoActivationEnabled(false);
setShowDescWindow(false);
setHideOnCompletionProviderChange(true);
setHideOnNoText(true);
parentWindowListener = new ParentWindowListener();
textComponentListener = new TextComponentListener();
autoActivationListener = new AutoActivationListener();
lafListener = new LookAndFeelChangeListener();
popupWindowListener = new PopupWindowListener();
listeners = new EventListenerList();
}
/**
* Adds a listener interested in popup window events from this instance.
*
* @param l The listener to add.
* @see #removeAutoCompletionListener(AutoCompletionListener)
*/
public void addAutoCompletionListener(AutoCompletionListener l) {
listeners.add(AutoCompletionListener.class, l);
}
/**
* Displays the popup window. Hosting applications can call this method to
* programmatically begin an auto-completion operation.
*/
public void doCompletion() {
refreshPopupWindow();
}
/**
* Fires an {@link AutoCompletionEvent} of the specified type.
*
* @param type The type of event to fire.
*/
protected void fireAutoCompletionEvent(AutoCompletionEvent.Type type) {
// Guaranteed to return a non-null array
Object[] listeners = this.listeners.getListenerList();
AutoCompletionEvent e = null;
// Process the listeners last to first, notifying those that are
// interested in this event
for (int i=listeners.length-2; i>=0; i-=2) {
if (listeners[i] == AutoCompletionListener.class) {
if (e==null) {
e = new AutoCompletionEvent(this, type);
}
((AutoCompletionListener)listeners[i+1]).autoCompleteUpdate(e);
}
}
}
/**
* Returns the delay between when the user types a character and when the
* code completion popup should automatically appear (if applicable).
*
* @return The delay, in milliseconds.
* @see #setAutoActivationDelay(int)
*/
public int getAutoActivationDelay() {
return autoActivationListener.timer.getDelay();
}
/**
* Returns whether, if a single auto-complete choice is available, it should
* be automatically inserted, without displaying the popup menu.
*
* @return Whether to auto-complete single choices.
* @see #setAutoCompleteSingleChoices(boolean)
*/
public boolean getAutoCompleteSingleChoices() {
return autoCompleteSingleChoices;
}
/**
* Returns the completion provider.
*
* @return The completion provider.
*/
public CompletionProvider getCompletionProvider() {
return provider;
}
/**
* Returns whether debug is enabled for AutoCompletion.
*
* @return Whether debug is enabled.
*/
static boolean getDebug() {
return DEBUG;
}
/**
* Returns the default auto-complete "trigger key" for this OS. For Windows,
* for example, it is Ctrl+Space.
*
* @return The default auto-complete trigger key.
*/
public static KeyStroke getDefaultTriggerKey() {
// Default to CTRL, even on Mac, since Ctrl+Space activates Spotlight
int mask = InputEvent.CTRL_MASK;
return KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, mask);
}
public static KeyStroke getDefaultParamTriggerKey() {
int mask = InputEvent.CTRL_MASK;
return KeyStroke.getKeyStroke(KeyEvent.VK_P, mask);
}
/**
* Returns the handler to use when an external URL is clicked in the
* description window.
*
* @return The handler.
* @see #setExternalURLHandler(ExternalURLHandler)
* @see #getLinkRedirector()
*/
public ExternalURLHandler getExternalURLHandler() {
return externalURLHandler;
}
int getLineOfCaret() {
Document doc = textComponent.getDocument();
Element root = doc.getDefaultRootElement();
return root.getElementIndex(textComponent.getCaretPosition());
}
/**
* Returns the link redirector, if any.
*
* @return The link redirector, or <code>null</code> if none.
* @see #setLinkRedirector(LinkRedirector)
*/
public static LinkRedirector getLinkRedirector() {
return linkRedirector;
}
/**
* Returns the default list cell renderer used when a completion provider
* does not supply its own.
*
* @return The default list cell renderer.
* @see #setListCellRenderer(ListCellRenderer)
*/
public ListCellRenderer getListCellRenderer() {
return renderer;
}
/**
* Returns the renderer to use for {@link Completion}s in the optional
* parameter choices popup window (displayed when a
* {@link ParameterizedCompletion} is code-completed). If this returns
* <code>null</code>, a default renderer is used.
*
* @return The renderer to use.
* @see #setParamChoicesRenderer(ListCellRenderer)
* @see #isParameterAssistanceEnabled()
*/
public ListCellRenderer getParamChoicesRenderer() {
return paramChoicesRenderer;
}
/**
* Returns the text to replace with in the document. This is a "last-chance"
* hook for subclasses to make special modifications to the completion text
* inserted. The default implementation simply returns
* <tt>c.getReplacementText()</tt>. You usually will not need to modify this
* method.
*
* @param c The completion being inserted.
* @param doc The document being modified.
* @param start The start of the text being replaced.
* @param len The length of the text being replaced.
* @return The text to replace with.
*/
protected String getReplacementText(Completion c, Document doc, int start,
int len) {
return c.getReplacementText();
}
/**
* Returns whether the "description window" should be shown alongside the
* completion window.
*
* @return Whether the description window should be shown.
* @see #setShowDescWindow(boolean)
*/
public boolean getShowDescWindow() {
return showDescWindow;
}
/**
* Returns the style context describing how auto-completion related
* highlights in the editor are rendered.
*
* @return The style context.
*/
public static AutoCompletionStyleContext getStyleContext() {
return styleContext;
}
/**
* Returns the text component for which auto-completion is enabled.
*
* @return The text component, or <code>null</code> if this
* {@link AutoCompletion} is not installed on any text component.
* @see #install(JTextComponent)
*/
public JTextComponent getTextComponent() {
return textComponent;
}
/**
* Returns the orientation of the text component we're installed to.
*
* @return The orientation of the text component, or <code>null</code> if we
* are not installed on one.
*/
ComponentOrientation getTextComponentOrientation() {
return textComponent == null ? null : textComponent
.getComponentOrientation();
}
/**
* Returns the "trigger key" used for auto-complete.
*
* @return The trigger key.
* @see #setTriggerKey(KeyStroke)
*/
public KeyStroke getTriggerKey() {
return trigger;
}
/**
* Hides any child windows being displayed by the auto-completion system.
*
* @return Whether any windows were visible.
*/
public boolean hideChildWindows() {
// return hidePopupWindow() || hideToolTipWindow();
boolean res = hidePopupWindow();
res |= hideParameterCompletionPopups();
return res;
}
/**
* Hides and disposes of any parameter completion-related popups.
*
* @return Whether any such windows were visible (and thus hidden).
*/
private boolean hideParameterCompletionPopups() {
if (pcc != null) {
pcc.deactivate();
pcc = null;
return true;
}
return false;
}
/**
* Hides the popup window, if it is visible.
*
* @return Whether the popup window was visible.
*/
protected boolean hidePopupWindow() {
if (popupWindow != null) {
if (popupWindow.isVisible()) {
setPopupVisible(false);
return true;
}
}
hideParameterTooltips();
return false;
}
/**
* Determines whether debug should be enabled for the AutoCompletion
* library. This method checks a system property, but takes care of
* {@link SecurityException}s in case we're in an applet or WebStart.
*
* @return Whether debug should be enabled.
*/
private static final boolean initDebug() {
boolean debug = false;
try {
debug = Boolean.getBoolean("AutoCompletion.debug");
} catch (SecurityException se) { // We're in an applet or WebStart.
debug = false;
}
return debug;
}
/**
* Inserts a completion. Any time a code completion event occurs, the actual
* text insertion happens through this method.
*
* @param c A completion to insert. This cannot be <code>null</code>.
*/
protected final void insertCompletion(Completion c) {
insertCompletion(c, false);
}
/**
* Inserts a completion. Any time a code completion event occurs, the actual
* text insertion happens through this method.
*
* @param c A completion to insert. This cannot be <code>null</code>.
* @param typedParamListStartChar Whether the parameterized completion start
* character was typed (typically <code>'('</code>).
*/
protected void insertCompletion(Completion c,
boolean typedParamListStartChar) {
JTextComponent textComp = getTextComponent();
String alreadyEntered = c.getAlreadyEntered(textComp);
hidePopupWindow();
Caret caret = textComp.getCaret();
int dot = caret.getDot();
int len = alreadyEntered.length();
int start = dot - len;
String replacement = getReplacementText(c, textComp.getDocument(),
start, len);
caret.setDot(start);
caret.moveDot(dot);
textComp.replaceSelection(replacement);
if (isParameterAssistanceEnabled()
&& (c instanceof ParameterizedCompletion)) {
ParameterizedCompletion pc = (ParameterizedCompletion) c;
startParameterizedCompletionAssistance(pc, typedParamListStartChar);
}
}
/**
* Installs this auto-completion on a text component. If this
* {@link AutoCompletion} is already installed on another text component,
* it is uninstalled first.
*
* @param c The text component.
* @see #uninstall()
*/
public void install(JTextComponent c) {
if (textComponent != null) {
uninstall();
}
this.textComponent = c;
installTriggerKey(getTriggerKey());
installParamTriggerKey(getParamTrigger());
// Install the function completion key, if there is one.
// NOTE: We cannot do this if the start char is ' ' (e.g. just a space
// between the function name and parameters) because it overrides
// RSTA's special space action. It seems KeyStorke.getKeyStroke(' ')
// hoses ctrl+space, shift+space, etc., even though I think it
// shouldn't...
char start = provider.getParameterListStart();
if (start != 0 && start != ' ') {
InputMap im = c.getInputMap();
ActionMap am = c.getActionMap();
KeyStroke ks = KeyStroke.getKeyStroke(start);
oldParenKey = im.get(ks);
im.put(ks, PARAM_COMPLETE_KEY);
oldParenAction = am.get(PARAM_COMPLETE_KEY);
am.put(PARAM_COMPLETE_KEY, new ParameterizedCompletionStartAction(
start));
}
textComponentListener.addTo(this.textComponent);
// In case textComponent is already in a window...
textComponentListener.hierarchyChanged(null);
if (isAutoActivationEnabled()) {
autoActivationListener.addTo(this.textComponent);
}
UIManager.addPropertyChangeListener(lafListener);
updateUI(); // In case there have been changes since we uninstalled
}
/**
* Installs a "trigger key" action onto the current text component.
*
* @param ks The keystroke that should trigger the action.
* @see #uninstallTriggerKey()
*/
private void installTriggerKey(KeyStroke ks) {
InputMap im = textComponent.getInputMap();
oldTriggerKey = im.get(ks);
im.put(ks, PARAM_TRIGGER_KEY);
ActionMap am = textComponent.getActionMap();
oldTriggerAction = am.get(PARAM_TRIGGER_KEY);
am.put(PARAM_TRIGGER_KEY, createAutoCompleteAction());
}
/**
* Installs a "trigger key" action onto the current text component.
*
* @param ks The keystroke that should trigger the action.
* @see #uninstallTriggerKey()
*/
private void installParamTriggerKey(KeyStroke ks) {
InputMap im = textComponent.getInputMap();
oldParamTriggerKey = im.get(ks);
im.put(ks, SHOW_PARAM_TRIGGER_KEY);
ActionMap am = textComponent.getActionMap();
oldParamTriggerAction = am.get(SHOW_PARAM_TRIGGER_KEY);
am.put(SHOW_PARAM_TRIGGER_KEY, showParameterTooltip());
}
private void hideParameterTooltips() {
if (parameterTooltipWindows.size() > 0) {
for (ParameterizedCompletionDescriptionToolTip tip : parameterTooltipWindows) {
tip.setVisible(false);
}
parameterTooltipWindows.clear();
}
if (parameterTooltipListener != null) {
parameterTooltipListener.uninstall(textComponent);
}
}
private Action showParameterTooltip() {
return new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
hideParameterTooltips();
if (parameterTooltipListener == null) {
parameterTooltipListener = new ParameterTooltipListener();
}
parameterTooltipListener.install(textComponent);
List<ParameterizedCompletion> completions = provider.getParameterizedCompletions(textComponent);
if (completions != null && completions.size() > 0) {
for (int i = 0;i < completions.size();i++) {
ParameterizedCompletion pc = completions.get(i);
ParameterizedCompletionDescriptionToolTip tip = new ParameterizedCompletionDescriptionToolTip(parentWindow, new ParameterizedCompletionContext(parentWindow, AutoCompletion.this, pc), AutoCompletion.this, pc);
try {
int dot = textComponent.getCaretPosition();
Rectangle r = textComponent.modelToView(dot);
Point p = new Point(r.x, r.y);
SwingUtilities.convertPointToScreen(p, textComponent);
r.x = p.x;
r.y = p.y - (24 * (completions.size() - (i + 1)));
tip.setLocationRelativeTo(r);
tip.setVisible(true);
parameterTooltipWindows.add(tip);
}
catch (Exception ex) {
}
}
}
}
};
}
/**
* Creates and returns the action to call when the user presses the
* auto-completion trigger key (e.g. ctrl+space). This is a hook for
* subclasses that want to provide their own behavior in this scenario.
* The default implementation returns an {@link AutoCompleteAction}.
*
* @return The action to use.
* @see AutoCompleteAction
*/
protected Action createAutoCompleteAction() {
return new AutoCompleteAction();
}
/**
* Returns whether auto-activation is enabled (that is, whether the
* completion popup will automatically appear after a delay when the user
* types an appropriate character). Note that this parameter will be ignored
* if auto-completion is disabled.
*
* @return Whether auto-activation is enabled.
* @see #setAutoActivationEnabled(boolean)
* @see #getAutoActivationDelay()
* @see #isAutoCompleteEnabled()
*/
public boolean isAutoActivationEnabled() {
return autoActivationEnabled;
}
/**
* Returns whether auto-completion is enabled.
*
* @return Whether auto-completion is enabled.
* @see #setAutoCompleteEnabled(boolean)
*/
public boolean isAutoCompleteEnabled() {
return autoCompleteEnabled;
}
/**
* Whether or not the popup should be hidden when the CompletionProvider
* changes. If set to false, caller has to ensure refresh of the popup
* content.
*
* @return Whether the popup should be hidden when the completion provider
* changes.
* @see #setHideOnCompletionProviderChange(boolean)
*/
protected boolean isHideOnCompletionProviderChange() {
return hideOnCompletionProviderChange;
}
/**
* Whether or not the popup should be hidden when user types a space (or
* any character that resets the completion list to "all completions").
*
* @return Whether the popup should be hidden when the completion list is
* reset to show all completions.
* @see #setHideOnNoText(boolean)
*/
protected boolean isHideOnNoText() {
return hideOnNoText;
}
/**
* Returns whether parameter assistance is enabled.
*
* @return Whether parameter assistance is enabled.
* @see #setParameterAssistanceEnabled(boolean)
*/
public boolean isParameterAssistanceEnabled() {
return parameterAssistanceEnabled;
}
/**
* Returns whether the completion popup window is visible.
*
* @return Whether the completion popup window is visible.
*/
public boolean isPopupVisible() {
return popupWindow != null && popupWindow.isVisible();
}
/**
* Refreshes the popup window. First, this method gets the possible
* completions for the current caret position. If there are none, and the
* popup is visible, it is hidden. If there are some completions and the
* popup is hidden, it is made visible and made to display the completions.
* If there are some completions and the popup is visible, its list is
* updated to the current set of completions.
*
* @return The current line number of the caret.
*/
protected int refreshPopupWindow() {
// A return value of null => don't suggest completions
String text = provider.getAlreadyEnteredText(textComponent);
if (text == null && !isPopupVisible()) {
return getLineOfCaret();
}
hideParameterTooltips();
// If the popup is currently visible, and they type a space (or any
// character that resets the completion list to "all completions"),
// the popup window should be hidden instead of being reset to show
// everything.
int textLen = text == null ? 0 : text.length();
if (textLen == 0 && isHideOnNoText()) {
if (isPopupVisible()) {
hidePopupWindow();
return getLineOfCaret();
}
}
final List<Completion> completions = provider
.getCompletions(textComponent);
int count = completions==null ? 0 : completions.size();
if (count > 1 || (count == 1 && (isPopupVisible() || textLen == 0))
|| (count == 1 && !getAutoCompleteSingleChoices())) {
if (popupWindow == null) {
popupWindow = new AutoCompletePopupWindow(parentWindow, this);
popupWindowListener.install(popupWindow);
// Completion is usually done for code, which is always done
// LTR, so make completion stuff RTL only if text component is
// also RTL.
popupWindow
.applyComponentOrientation(getTextComponentOrientation());
if (renderer != null) {
popupWindow.setListCellRenderer(renderer);
}
if (preferredChoicesWindowSize != null) {
popupWindow.setSize(preferredChoicesWindowSize);
}
if (preferredDescWindowSize != null) {
popupWindow
.setDescriptionWindowSize(preferredDescWindowSize);
}
}
popupWindow.setCompletions(completions);
if (!popupWindow.isVisible()) {
Rectangle r = null;
try {
r = textComponent.modelToView(textComponent
.getCaretPosition());
} catch (BadLocationException ble) {
ble.printStackTrace();
return -1;
}
Point p = new Point(r.x, r.y);
SwingUtilities.convertPointToScreen(p, textComponent);
r.x = p.x;
r.y = p.y;
popupWindow.setLocationRelativeTo(r);
setPopupVisible(true);
}
}
else if (count == 1) { // !isPopupVisible && autoCompleteSingleChoices
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
insertCompletion(completions.get(0));
}
});
}
else {
hidePopupWindow();
}
return getLineOfCaret();
}
/**
* Removes a listener interested in popup window events from this instance.
*
* @param l The listener to remove.
* @see #addAutoCompletionListener(AutoCompletionListener)
*/
public void removeAutoCompletionListener(AutoCompletionListener l) {
listeners.remove(AutoCompletionListener.class, l);
}
/**
* Sets the delay between when the user types a character and when the code
* completion popup should automatically appear (if applicable).
*
* @param ms The delay, in milliseconds. This should be greater than zero.
* @see #getAutoActivationDelay()
*/
public void setAutoActivationDelay(int ms) {
ms = Math.max(0, ms);
autoActivationListener.timer.stop();
autoActivationListener.timer.setInitialDelay(ms);
}
/**
* Toggles whether auto-activation is enabled. Note that auto-activation
* also depends on auto-completion itself being enabled.
*