Ghidra is a software reverse engineering (SRE) framework
GP-2616 - fixed NPE in text field
dragonmacher5201b9563ac8
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/autocomplete/TextFieldAutocompleter.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/autocomplete/TextFieldAutocompleter.javaindex fef873e6a6..d45552dfef 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/autocomplete/TextFieldAutocompleter.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/autocomplete/TextFieldAutocompleter.java@@ -36,7 +36,7 @@ import ghidra.util.task.SwingUpdateManager;/*** An autocompleter that may be attached to one or more {@link JTextField}.- *+ ** <p>* 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@@ -44,7 +44,7 @@ import ghidra.util.task.SwingUpdateManager;* 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* model if you wish to present completions from multiple models on a single text field.- *+ ** <p>* 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@@ -56,7 +56,7 @@ import ghidra.util.task.SwingUpdateManager;* positioning behavior can be modified by overriding the {@link #getCompletionWindowPosition()}* method. As a convenience, the {@link #getCaretPositionOnScreen(JTextField)} method is available* to compute the default position.- *+ ** <p>* 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,@@ -65,20 +65,20 @@ import ghidra.util.task.SwingUpdateManager;* by overriding the {@link #getCompletionCanDefault(Object) getCompletionCanDefault(T)} 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.- *+ ** <p>* 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,* but cause the insertion of different text. In any case, it is best to ensure any modification* produces an intuitive behavior.- *+ ** <p>* The simplest use case is to create a text field, create an autocompleter with a custom model, and* then attach and show.- *+ ** <pre>* JTextField field = new JTextField();- *+ ** AutocompletionModel<String> model = new AutocompletionModel<String>() {* @Override* public Collection<String> computeCompletions(String text) {@@ -88,9 +88,9 @@ import ghidra.util.task.SwingUpdateManager;* TextFieldAutocompleter<String> completer = new TextFieldAutocompleter<String>(model);* completer.attachTo(field);* // ... Add the field to, e.g., a dialog, and show.- *+ ** </pre>- *+ ** @param <T> the type of suggestions presented by this autocompleter.*/public class TextFieldAutocompleter<T> {@@ -267,7 +267,7 @@ public class TextFieldAutocompleter<T> {/*** Create a new autocompleter associated with the given model.- *+ ** @param model the model giving the suggestions.*/public TextFieldAutocompleter(AutocompletionModel<T> model) {@@ -276,7 +276,7 @@ public class TextFieldAutocompleter<T> {/*** Recompute the display location and move with list window.- *+ ** <p>* 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.- *+ ** <p>* 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.*/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);@@ -352,7 +357,7 @@ public class TextFieldAutocompleter<T> {/*** Show or hide the completion list window- *+ ** @param visible true to show, false to hide*/public void setCompletionListVisible(boolean visible) {@@ -373,10 +378,10 @@ public class TextFieldAutocompleter<T> {/*** Check if the completion list window is visible.- *+ ** <p>* If it is visible, this implies that the user is actively using the autocompleter.- *+ ** @return true if shown, false if hidden.*/public boolean isCompletionListVisible() {@@ -388,12 +393,14 @@ public class TextFieldAutocompleter<T> {*/private void doUpdateDisplayLocation() {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.- *+ ** @param field an attached field, usually the one with focus.* @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.- *+ ** <p>* 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* 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.- *+ ** @return the point giving the top-left corner of the completion window*/protected Point getCompletionWindowPosition() {@@ -423,24 +430,31 @@ public class TextFieldAutocompleter<T> {/*** Get the preferred dimensions of the completion list window.- *+ ** <p>* 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* preference.*/protected Dimension getDefaultCompletionWindowDimension() {+ if (focus == null) {+ return new Dimension(-1, -1);+ }return new Dimension(focus.getWidth(), -1);}/*** 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@@ -457,14 +471,14 @@ public class TextFieldAutocompleter<T> {/*** Builds the list cell renderer for the autocompletion list.- *+ ** <p>* A programmer may override this if the various {@code getCompletion...} methods prove* insufficient for customizing the display of the suggestions. Please remember that* {@link JLabel}s can render HTML, so {@link #getCompletionDisplay(Object)* getCompletionDisplay(T)} is quite powerful with the default* {@link AutocompletionCellRenderer}.- *+ ** @return a list cell renderer for the completion list.*/protected ListCellRenderer<? super T> buildListCellRenderer() {@@ -473,10 +487,10 @@ public class TextFieldAutocompleter<T> {/*** Attach the autocompleter to the given text field.- *+ ** <p>* If this method is never called, then the autocompleter can never appear.- *+ ** @param field the field that will gain this autocompletion feature* @return true, if this field is not already attached*/@@ -502,7 +516,7 @@ public class TextFieldAutocompleter<T> {/*** Deprive the given field of this autocompleter.- *+ ** @param field the field that will lose this autocompletion feature* @return true, if this field was actually attached*/@@ -520,7 +534,7 @@ public class TextFieldAutocompleter<T> {/*** Cause the currently-selected suggestion to be activated.- *+ ** <p>* 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.- *+ ** <p>* 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* cancels the event, then the suggested text will not be inserted.- *+ ** @param ev the event* @return true, if no listener cancelled the event*/@@ -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);@@ -571,7 +589,7 @@ public class TextFieldAutocompleter<T> {/*** Register the given auto-completion listener- *+ ** @param l the listener to register*/public void addAutocompletionListener(AutocompletionListener<T> l) {@@ -580,7 +598,7 @@ public class TextFieldAutocompleter<T> {/*** Unregister the given auto-completion listener- *+ ** @param l the listener to unregister*/public void removeAutocompletionListener(AutocompletionListener<T> l) {@@ -589,7 +607,7 @@ public class TextFieldAutocompleter<T> {/*** Get all the registered auto-completion listeners- *+ ** @return an array of registered listeners*/@SuppressWarnings("unchecked")@@ -599,7 +617,7 @@ public class TextFieldAutocompleter<T> {/*** Get all registered listeners of the given type- *+ ** @param listenerType the type of listeners to get* @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- *+ ** @param sel the activated suggestion* @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- *+ ** @param sel the suggestion to display* @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- *+ ** @param sel the suggestion to display* @param isSelected true if the suggestion is currently selected* @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- *+ ** @param sel the suggestion to display* @param isSelected true if the suggestion is currently selected* @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- *+ ** @param sel the suggestion to display* @param isSelected true if the suggestion is currently selected* @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- *+ ** @param sel the suggestion to display* @param isSelected true if the suggestion is currently selected* @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.- *+ ** <p>* 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@@ -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* list instead. This is useful to prevent consequential actions from being automatically* activated by the autocompleter.- *+ ** @param sel the potentially auto-activated suggestion.* @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.- *+ ** <p>* First, this repeatedly attempts auto-activation. When there are many suggestions, or when* auto-activation is prevented (see {@link #getCompletionCanDefault(Object)* 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* cancels it (usually via ESC) or the user activates a suggestion.- *+ ** <p>* <b>NOTE:</b> The text field must already be attached.- *+ ** @param field the field on which to start autocompletion.*/public void startCompletion(JTextField field) {@@ -765,7 +783,7 @@ public class TextFieldAutocompleter<T> {/*** Cause the suggestion at the given index to be selected- *+ ** @param index the index of the selection*/public void select(int index) {@@ -852,7 +870,7 @@ public class TextFieldAutocompleter<T> {/*** Get the list of suggestions as ordered on screen- *+ ** @return an immutable copy of the list*/public List<T> getSuggestions() {@@ -1012,7 +1030,7 @@ public class TextFieldAutocompleter<T> {/*** A demonstration of the autocompleter on a single text field.- *+ ** <p>* The autocompleter offers the tails from a list of strings that start with the text before the* caret.@@ -1056,7 +1074,7 @@ public class TextFieldAutocompleter<T> {/*** A demonstration of the autocompleter on two linked text fields.- *+ ** <p>* This demo was designed to test whether the autocompleter and the {@link TextFieldLinker}* could be composed correctly.