Skip to content

Commit

Permalink
Fix #22: AssistanceIconPanel/RegexAwareComboBox issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbylight committed Nov 8, 2021
1 parent e12a327 commit b7726e3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
46 changes: 40 additions & 6 deletions RSTAUI/src/main/java/org/fife/rsta/ui/AssistanceIconPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
public class AssistanceIconPanel extends DecorativeIconPanel
implements PropertyChangeListener {

private ComponentListener listener;
private JComponent listenedToComponent;

/**
* The tool tip text for the light bulb icon. It is assumed that access
* to this field is single-threaded (on the EDT).
Expand Down Expand Up @@ -66,25 +69,27 @@ public AssistanceIconPanel(JComponent comp, int iconWidth) {

private void init(JComponent comp) {

listenedToComponent = comp;

// null can be passed to make a "filler" icon panel for alignment
// purposes.
if (comp!=null) {
if (listenedToComponent != null) {

ComponentListener listener = new ComponentListener();
listener = new ComponentListener();

if (comp instanceof JComboBox) {
JComboBox<?> combo = (JComboBox<?>)comp;
if (listenedToComponent instanceof JComboBox) {
JComboBox<?> combo = (JComboBox<?>)listenedToComponent;
Component c = combo.getEditor().getEditorComponent();
if (c instanceof JTextComponent) { // Always true
JTextComponent tc = (JTextComponent)c;
tc.addFocusListener(listener);
}
}
else { // Usually a JTextComponent
comp.addFocusListener(listener);
listenedToComponent.addFocusListener(listener);
}

comp.addPropertyChangeListener(
listenedToComponent.addPropertyChangeListener(
ContentAssistable.ASSISTANCE_IMAGE, this);

}
Expand Down Expand Up @@ -139,6 +144,35 @@ public void setAssistanceEnabled(Image img) {
}


@Override
public void updateUI() {

// Since we actually listen to a child component of combo boxes,
// we must stop listening to the prior child component...
if (listenedToComponent instanceof JComboBox) {
JComboBox<?> combo = (JComboBox<?>)listenedToComponent;
Component c = combo.getEditor().getEditorComponent();
if (c instanceof JTextComponent) { // Always true
JTextComponent tc = (JTextComponent)c;
tc.removeFocusListener(listener);
}
}

super.updateUI();

// And start listening to the new one, since this might have
// changed because of the UI change.
if (listenedToComponent instanceof JComboBox) {
JComboBox<?> combo = (JComboBox<?>)listenedToComponent;
Component c = combo.getEditor().getEditorComponent();
if (c instanceof JTextComponent) { // Always true
JTextComponent tc = (JTextComponent)c;
tc.addFocusListener(listener);
}
}
}


/**
* Listens for events in the text component we're annotating.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,22 @@ public void setAutoCompleteEnabled(boolean enabled) {
}


/**
* Sets the image to display by this text field when content
* assistance is available.
*
* @param image The image. If this is {@code null}, a default image
* (a light bulb) is used). This should be kept small, around
* 8x8 for standard resolution monitors.
* @see #getContentAssistImage()
*/
public void setContentAssistImage(Image image) {
contentAssistImage = image;
}
/**
* Sets the image to display by this text field when content
* assistance is available.
*
* @param image The image. If this is {@code null}, a default image
* (a light bulb) is used). This should be kept small, around
* 8x8 for standard resolution monitors.
* @see #getContentAssistImage()
*/
public void setContentAssistImage(Image image) {
if (image != contentAssistImage) {
Image old = contentAssistImage;
contentAssistImage = image;
firePropertyChange(ContentAssistable.ASSISTANCE_IMAGE, old, image);
}
}


/**
Expand Down

0 comments on commit b7726e3

Please sign in to comment.