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

@ -36,7 +36,7 @@ import ghidra.util.task.SwingUpdateManager;
/** /**
* An autocompleter that may be attached to one or more {@link JTextField}. * An autocompleter that may be attached to one or more {@link JTextField}.
* *
* <p> * <p>
* Each autocompleter instance has one associated window (displaying the list of suggestions) and * Each autocompleter instance has one associated window (displaying the list of suggestions) and
* one associated model (generating the list of suggestions). Thus, the list can only be active on * one associated model (generating the list of suggestions). Thus, the list can only be active on
@ -44,7 +44,7 @@ import ghidra.util.task.SwingUpdateManager;
* for one autocompleter to be reused on many fields. Behavior is undefined when multiple * for one autocompleter to be reused on many fields. Behavior is undefined when multiple
* autocompleters are attached to the same text field. More likely, you should implement a composite * autocompleters are attached to the same text field. More likely, you should implement a composite
* model if you wish to present completions from multiple models on a single text field. * model if you wish to present completions from multiple models on a single text field.
* *
* <p> * <p>
* By default, the autocompleter is activated when the user presses CTRL-SPACE, at which point, the * By default, the autocompleter is activated when the user presses CTRL-SPACE, at which point, the
* model is queried for possible suggestions. The completer gives the model all the text preceding * model is queried for possible suggestions. The completer gives the model all the text preceding
@ -56,7 +56,7 @@ import ghidra.util.task.SwingUpdateManager;
* positioning behavior can be modified by overriding the {@link #getCompletionWindowPosition()} * positioning behavior can be modified by overriding the {@link #getCompletionWindowPosition()}
* method. As a convenience, the {@link #getCaretPositionOnScreen(JTextField)} method is available * method. As a convenience, the {@link #getCaretPositionOnScreen(JTextField)} method is available
* to compute the default position. * to compute the default position.
* *
* <p> * <p>
* Whether or not the list is currently displayed, when the user presses CTRL-SPACE, if only one * Whether or not the list is currently displayed, when the user presses CTRL-SPACE, if only one
* completion is possible, it is automatically activated. This logic is applied again and again, * completion is possible, it is automatically activated. This logic is applied again and again,
@ -65,20 +65,20 @@ import ghidra.util.task.SwingUpdateManager;
* by overriding the {@link #getCompletionCanDefault(Object) getCompletionCanDefault(T)} method. * by overriding the {@link #getCompletionCanDefault(Object) getCompletionCanDefault(T)} method.
* This same behavior can be activated by calling the {@link #startCompletion(JTextField)} method, * This same behavior can be activated by calling the {@link #startCompletion(JTextField)} method,
* which may be useful, e.g., to bind a different key sequence to start autocompletion. * which may be useful, e.g., to bind a different key sequence to start autocompletion.
* *
* <p> * <p>
* The appearance of each item in the suggestion list can be modified by overriding the various * The appearance of each item in the suggestion list can be modified by overriding the various
* {@code getCompletion...} methods. Note that it's possible for an item to be displayed one way, * {@code getCompletion...} methods. Note that it's possible for an item to be displayed one way,
* but cause the insertion of different text. In any case, it is best to ensure any modification * but cause the insertion of different text. In any case, it is best to ensure any modification
* produces an intuitive behavior. * produces an intuitive behavior.
* *
* <p> * <p>
* The simplest use case is to create a text field, create an autocompleter with a custom model, and * The simplest use case is to create a text field, create an autocompleter with a custom model, and
* then attach and show. * then attach and show.
* *
* <pre> * <pre>
* JTextField field = new JTextField(); * JTextField field = new JTextField();
* *
* AutocompletionModel<String> model = new AutocompletionModel<String>() { * AutocompletionModel<String> model = new AutocompletionModel<String>() {
* &#64;Override * &#64;Override
* public Collection<String> computeCompletions(String text) { * public Collection<String> computeCompletions(String text) {
@ -88,9 +88,9 @@ import ghidra.util.task.SwingUpdateManager;
* TextFieldAutocompleter<String> completer = new TextFieldAutocompleter<String>(model); * TextFieldAutocompleter<String> completer = new TextFieldAutocompleter<String>(model);
* completer.attachTo(field); * completer.attachTo(field);
* // ... Add the field to, e.g., a dialog, and show. * // ... Add the field to, e.g., a dialog, and show.
* *
* </pre> * </pre>
* *
* @param <T> the type of suggestions presented by this autocompleter. * @param <T> the type of suggestions presented by this autocompleter.
*/ */
public class TextFieldAutocompleter<T> { public class TextFieldAutocompleter<T> {
@ -267,7 +267,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Create a new autocompleter associated with the given model. * Create a new autocompleter associated with the given model.
* *
* @param model the model giving the suggestions. * @param model the model giving the suggestions.
*/ */
public TextFieldAutocompleter(AutocompletionModel<T> model) { public TextFieldAutocompleter(AutocompletionModel<T> model) {
@ -276,7 +276,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Recompute the display location and move with list window. * Recompute the display location and move with list window.
* *
* <p> * <p>
* This is useful, e.g., when the window containing the associated text field(s) moves. * This is useful, e.g., when the window containing the associated text field(s) moves.
*/ */
@ -288,7 +288,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Update the contents of the suggestion list. * Update the contents of the suggestion list.
* *
* <p> * <p>
* This entails taking the prefix, querying the model, and rendering the list. * This entails taking the prefix, querying the model, and rendering the list.
*/ */
@ -336,6 +336,11 @@ public class TextFieldAutocompleter<T> {
* Build the completion window, parented to the attached field that last had focus. * Build the completion window, parented to the attached field that last had focus.
*/ */
protected void buildCompletionWindow() { protected void buildCompletionWindow() {
if (focus == null) {
// shouldn't happen; likely some off focus issue
return;
}
completionWindow = new JWindow(WindowUtilities.windowForComponent(focus)); completionWindow = new JWindow(WindowUtilities.windowForComponent(focus));
completionWindow.add(content); completionWindow.add(content);
content.setVisible(true); content.setVisible(true);
@ -352,7 +357,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Show or hide the completion list window * Show or hide the completion list window
* *
* @param visible true to show, false to hide * @param visible true to show, false to hide
*/ */
public void setCompletionListVisible(boolean visible) { public void setCompletionListVisible(boolean visible) {
@ -373,10 +378,10 @@ public class TextFieldAutocompleter<T> {
/** /**
* Check if the completion list window is visible. * Check if the completion list window is visible.
* *
* <p> * <p>
* If it is visible, this implies that the user is actively using the autocompleter. * If it is visible, this implies that the user is actively using the autocompleter.
* *
* @return true if shown, false if hidden. * @return true if shown, false if hidden.
*/ */
public boolean isCompletionListVisible() { public boolean isCompletionListVisible() {
@ -388,12 +393,14 @@ public class TextFieldAutocompleter<T> {
*/ */
private void doUpdateDisplayLocation() { private void doUpdateDisplayLocation() {
Point p = getCompletionWindowPosition(); Point p = getCompletionWindowPosition();
completionWindow.setLocation(p); if (p != null) {
completionWindow.setLocation(p);
}
} }
/** /**
* Gets the prefix from the given text field, used to query the model. * Gets the prefix from the given text field, used to query the model.
* *
* @param field an attached field, usually the one with focus. * @param field an attached field, usually the one with focus.
* @return the prefix to use as the query. * @return the prefix to use as the query.
*/ */
@ -408,13 +415,13 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get the preferred location (on screen) of the completion list window. * Get the preferred location (on screen) of the completion list window.
* *
* <p> * <p>
* Typically, this is a location near the focused field. Ideally, it is positioned such that the * Typically, this is a location near the focused field. Ideally, it is positioned such that the
* displayed suggestions coincide with the applicable text in the focused field. For example, if * displayed suggestions coincide with the applicable text in the focused field. For example, if
* the suggestions display some portion of the prefix, the window could be positioned such that * the suggestions display some portion of the prefix, the window could be positioned such that
* the portion in the suggestion appears directly below the same portion in the field. * the portion in the suggestion appears directly below the same portion in the field.
* *
* @return the point giving the top-left corner of the completion window * @return the point giving the top-left corner of the completion window
*/ */
protected Point getCompletionWindowPosition() { protected Point getCompletionWindowPosition() {
@ -423,24 +430,31 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get the preferred dimensions of the completion list window. * Get the preferred dimensions of the completion list window.
* *
* <p> * <p>
* Typically, this is the width of the focused field. * Typically, this is the width of the focused field.
* *
* @return the dimension giving the preferred height and width. A value can be -1 to indicate no * @return the dimension giving the preferred height and width. A value can be -1 to indicate no
* preference. * preference.
*/ */
protected Dimension getDefaultCompletionWindowDimension() { protected Dimension getDefaultCompletionWindowDimension() {
if (focus == null) {
return new Dimension(-1, -1);
}
return new Dimension(focus.getWidth(), -1); return new Dimension(focus.getWidth(), -1);
} }
/** /**
* A convenience function that returns the bottom on-screen position of the given field's caret. * 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 * @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) { protected Point getCaretPositionOnScreen(JTextField field) {
if (focus == null) {
return null;
}
FontMetrics metrics = field.getFontMetrics(field.getFont()); FontMetrics metrics = field.getFontMetrics(field.getFont());
Caret c = field.getCaret(); Caret c = field.getCaret();
Point p = c.getMagicCaretPosition(); // returns a shared reference Point p = c.getMagicCaretPosition(); // returns a shared reference
@ -457,14 +471,14 @@ public class TextFieldAutocompleter<T> {
/** /**
* Builds the list cell renderer for the autocompletion list. * Builds the list cell renderer for the autocompletion list.
* *
* <p> * <p>
* A programmer may override this if the various {@code getCompletion...} methods prove * A programmer may override this if the various {@code getCompletion...} methods prove
* insufficient for customizing the display of the suggestions. Please remember that * insufficient for customizing the display of the suggestions. Please remember that
* {@link JLabel}s can render HTML, so {@link #getCompletionDisplay(Object) * {@link JLabel}s can render HTML, so {@link #getCompletionDisplay(Object)
* getCompletionDisplay(T)} is quite powerful with the default * getCompletionDisplay(T)} is quite powerful with the default
* {@link AutocompletionCellRenderer}. * {@link AutocompletionCellRenderer}.
* *
* @return a list cell renderer for the completion list. * @return a list cell renderer for the completion list.
*/ */
protected ListCellRenderer<? super T> buildListCellRenderer() { protected ListCellRenderer<? super T> buildListCellRenderer() {
@ -473,10 +487,10 @@ public class TextFieldAutocompleter<T> {
/** /**
* Attach the autocompleter to the given text field. * Attach the autocompleter to the given text field.
* *
* <p> * <p>
* If this method is never called, then the autocompleter can never appear. * If this method is never called, then the autocompleter can never appear.
* *
* @param field the field that will gain this autocompletion feature * @param field the field that will gain this autocompletion feature
* @return true, if this field is not already attached * @return true, if this field is not already attached
*/ */
@ -502,7 +516,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Deprive the given field of this autocompleter. * Deprive the given field of this autocompleter.
* *
* @param field the field that will lose this autocompletion feature * @param field the field that will lose this autocompletion feature
* @return true, if this field was actually attached * @return true, if this field was actually attached
*/ */
@ -520,7 +534,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Cause the currently-selected suggestion to be activated. * Cause the currently-selected suggestion to be activated.
* *
* <p> * <p>
* By default, this is called when the user presses ENTER or clicks a suggestion. * By default, this is called when the user presses ENTER or clicks a suggestion.
*/ */
@ -535,12 +549,12 @@ public class TextFieldAutocompleter<T> {
/** /**
* Fire the registered autocompletion listeners on the given event. * Fire the registered autocompletion listeners on the given event.
* *
* <p> * <p>
* Each registered listener is invoked in order of registration. If any listener consumes the * Each registered listener is invoked in order of registration. If any listener consumes the
* event, then later-registered listeners will not be notified of the event. If any listener * event, then later-registered listeners will not be notified of the event. If any listener
* cancels the event, then the suggested text will not be inserted. * cancels the event, then the suggested text will not be inserted.
* *
* @param ev the event * @param ev the event
* @return true, if no listener cancelled the event * @return true, if no listener cancelled the event
*/ */
@ -555,14 +569,18 @@ public class TextFieldAutocompleter<T> {
} }
private void completionActivated(T sel) { private void completionActivated(T sel) {
if (focus == null) {
// shouldn't happen; likely some off focus issue
return;
}
AutocompletionEvent<T> ev = new AutocompletionEvent<>(sel, focus); AutocompletionEvent<T> ev = new AutocompletionEvent<>(sel, focus);
if (!fireAutocompletionListeners(ev)) { if (!fireAutocompletionListeners(ev)) {
return; return;
} }
try { try {
focus.getDocument() focus.getDocument()
.insertString(focus.getCaretPosition(), getCompletionText(sel), .insertString(focus.getCaretPosition(), getCompletionText(sel), null);
null);
} }
catch (BadLocationException e) { catch (BadLocationException e) {
throw new AssertionError("INTERNAL: Should not be here", e); throw new AssertionError("INTERNAL: Should not be here", e);
@ -571,7 +589,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Register the given auto-completion listener * Register the given auto-completion listener
* *
* @param l the listener to register * @param l the listener to register
*/ */
public void addAutocompletionListener(AutocompletionListener<T> l) { public void addAutocompletionListener(AutocompletionListener<T> l) {
@ -580,7 +598,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Unregister the given auto-completion listener * Unregister the given auto-completion listener
* *
* @param l the listener to unregister * @param l the listener to unregister
*/ */
public void removeAutocompletionListener(AutocompletionListener<T> l) { public void removeAutocompletionListener(AutocompletionListener<T> l) {
@ -589,7 +607,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get all the registered auto-completion listeners * Get all the registered auto-completion listeners
* *
* @return an array of registered listeners * @return an array of registered listeners
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -599,7 +617,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get all registered listeners of the given type * Get all registered listeners of the given type
* *
* @param listenerType the type of listeners to get * @param listenerType the type of listeners to get
* @return an array of registered listeners * @return an array of registered listeners
*/ */
@ -613,7 +631,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get the text to insert when the given suggestion is activated * Get the text to insert when the given suggestion is activated
* *
* @param sel the activated suggestion * @param sel the activated suggestion
* @return the text to insert * @return the text to insert
*/ */
@ -623,7 +641,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get the (possibly HTML) text to display for the given suggestion in the list * Get the (possibly HTML) text to display for the given suggestion in the list
* *
* @param sel the suggestion to display * @param sel the suggestion to display
* @return the text or HTML representing the suggestion * @return the text or HTML representing the suggestion
*/ */
@ -633,7 +651,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get the foreground color to display for the given suggestion in the list * Get the foreground color to display for the given suggestion in the list
* *
* @param sel the suggestion to display * @param sel the suggestion to display
* @param isSelected true if the suggestion is currently selected * @param isSelected true if the suggestion is currently selected
* @param cellHasFocus true if the suggestion currently has focus * @param cellHasFocus true if the suggestion currently has focus
@ -645,7 +663,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get the background color to display for the given suggestion in the list * Get the background color to display for the given suggestion in the list
* *
* @param sel the suggestion to display * @param sel the suggestion to display
* @param isSelected true if the suggestion is currently selected * @param isSelected true if the suggestion is currently selected
* @param cellHasFocus true if the suggestion currently has focus * @param cellHasFocus true if the suggestion currently has focus
@ -657,7 +675,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get the icon to display with the given suggestion in the list * Get the icon to display with the given suggestion in the list
* *
* @param sel the suggestion to display * @param sel the suggestion to display
* @param isSelected true if the suggestion is currently selected * @param isSelected true if the suggestion is currently selected
* @param cellHasFocus true if the suggestion currently has focus * @param cellHasFocus true if the suggestion currently has focus
@ -669,7 +687,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get the font for the given suggestion in the list * Get the font for the given suggestion in the list
* *
* @param sel the suggestion to display * @param sel the suggestion to display
* @param isSelected true if the suggestion is currently selected * @param isSelected true if the suggestion is currently selected
* @param cellHasFocus true if the suggestion currently has focus * @param cellHasFocus true if the suggestion currently has focus
@ -684,7 +702,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Decide whether the given suggestion can be automatically activated. * Decide whether the given suggestion can be automatically activated.
* *
* <p> * <p>
* When autocompletion is started (via {@link #startCompletion(JTextField)}) or when the user * When autocompletion is started (via {@link #startCompletion(JTextField)}) or when the user
* presses CTRL-SPACE, if there is only a single suggestion, it is taken automatically, and the * presses CTRL-SPACE, if there is only a single suggestion, it is taken automatically, and the
@ -692,7 +710,7 @@ public class TextFieldAutocompleter<T> {
* it calls this method. If it returns false, the single suggestion is displayed in a 1-long * it calls this method. If it returns false, the single suggestion is displayed in a 1-long
* list instead. This is useful to prevent consequential actions from being automatically * list instead. This is useful to prevent consequential actions from being automatically
* activated by the autocompleter. * activated by the autocompleter.
* *
* @param sel the potentially auto-activated suggestion. * @param sel the potentially auto-activated suggestion.
* @return true to permit auto-activation, false to prevent it. * @return true to permit auto-activation, false to prevent it.
*/ */
@ -702,17 +720,17 @@ public class TextFieldAutocompleter<T> {
/** /**
* Starts the autocompleter on the given text field. * Starts the autocompleter on the given text field.
* *
* <p> * <p>
* First, this repeatedly attempts auto-activation. When there are many suggestions, or when * First, this repeatedly attempts auto-activation. When there are many suggestions, or when
* auto-activation is prevented (see {@link #getCompletionCanDefault(Object) * auto-activation is prevented (see {@link #getCompletionCanDefault(Object)
* getCompletionCanDefault(T)}), a list is displayed (usually below the caret) containing the * getCompletionCanDefault(T)}), a list is displayed (usually below the caret) containing the
* suggestions given the fields current contents. The list remains open until either the user * suggestions given the fields current contents. The list remains open until either the user
* cancels it (usually via ESC) or the user activates a suggestion. * cancels it (usually via ESC) or the user activates a suggestion.
* *
* <p> * <p>
* <b>NOTE:</b> The text field must already be attached. * <b>NOTE:</b> The text field must already be attached.
* *
* @param field the field on which to start autocompletion. * @param field the field on which to start autocompletion.
*/ */
public void startCompletion(JTextField field) { public void startCompletion(JTextField field) {
@ -765,7 +783,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Cause the suggestion at the given index to be selected * Cause the suggestion at the given index to be selected
* *
* @param index the index of the selection * @param index the index of the selection
*/ */
public void select(int index) { public void select(int index) {
@ -852,7 +870,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* Get the list of suggestions as ordered on screen * Get the list of suggestions as ordered on screen
* *
* @return an immutable copy of the list * @return an immutable copy of the list
*/ */
public List<T> getSuggestions() { public List<T> getSuggestions() {
@ -1012,7 +1030,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* A demonstration of the autocompleter on a single text field. * A demonstration of the autocompleter on a single text field.
* *
* <p> * <p>
* The autocompleter offers the tails from a list of strings that start with the text before the * The autocompleter offers the tails from a list of strings that start with the text before the
* caret. * caret.
@ -1056,7 +1074,7 @@ public class TextFieldAutocompleter<T> {
/** /**
* A demonstration of the autocompleter on two linked text fields. * A demonstration of the autocompleter on two linked text fields.
* *
* <p> * <p>
* This demo was designed to test whether the autocompleter and the {@link TextFieldLinker} * This demo was designed to test whether the autocompleter and the {@link TextFieldLinker}
* could be composed correctly. * could be composed correctly.