Merge remote-tracking branch

'origin/GP-2616-dragonmacher-patch-window-npe' (Closes #4604)
This commit is contained in:
Ryan Kurtz 2022-10-04 01:48:57 -04:00
commit 7641a386a1

View file

@ -336,6 +336,11 @@ public class TextFieldAutocompleter<T> {
* Build the completion window, parented to the attached field that last had focus.
*/
protected void buildCompletionWindow() {
if (focus == null) {
// shouldn't happen; likely some off focus issue
return;
}
completionWindow = new JWindow(WindowUtilities.windowForComponent(focus));
completionWindow.add(content);
content.setVisible(true);
@ -388,8 +393,10 @@ public class TextFieldAutocompleter<T> {
*/
private void doUpdateDisplayLocation() {
Point p = getCompletionWindowPosition();
if (p != null) {
completionWindow.setLocation(p);
}
}
/**
* Gets the prefix from the given text field, used to query the model.
@ -431,6 +438,9 @@ public class TextFieldAutocompleter<T> {
* preference.
*/
protected Dimension getDefaultCompletionWindowDimension() {
if (focus == null) {
return new Dimension(-1, -1);
}
return new Dimension(focus.getWidth(), -1);
}
@ -438,9 +448,13 @@ public class TextFieldAutocompleter<T> {
* A convenience function that returns the bottom on-screen position of the given field's caret.
*
* @param field the field, typically the one having focus
* @return the on-screen position of the caret's bottom.
* @return the on-screen position of the caret's bottom; null if the given field is null
*/
protected Point getCaretPositionOnScreen(JTextField field) {
if (focus == null) {
return null;
}
FontMetrics metrics = field.getFontMetrics(field.getFont());
Caret c = field.getCaret();
Point p = c.getMagicCaretPosition(); // returns a shared reference
@ -555,14 +569,18 @@ public class TextFieldAutocompleter<T> {
}
private void completionActivated(T sel) {
if (focus == null) {
// shouldn't happen; likely some off focus issue
return;
}
AutocompletionEvent<T> ev = new AutocompletionEvent<>(sel, focus);
if (!fireAutocompletionListeners(ev)) {
return;
}
try {
focus.getDocument()
.insertString(focus.getCaretPosition(), getCompletionText(sel),
null);
.insertString(focus.getCaretPosition(), getCompletionText(sel), null);
}
catch (BadLocationException e) {
throw new AssertionError("INTERNAL: Should not be here", e);