Skip to content

Instantly share code, notes, and snippets.

@jsfan3

jsfan3/Test.java Secret

Created Apr 24, 2019
Embed
What would you like to do?
Test of extending AutoCompleteTextField, after http://disq.us/p/21cvxbt
AutoCompleteTextField field = new AutoCompleteTextField(listModel) {
private Component popup;
private Container lay;
private Container parent;
@Override
protected boolean filter(String text) {
return getCityFilter(listModel, resultQuery, isFullAddressSearch).filter(text);
}
// https://github.com/codenameone/CodenameOne/issues/2757#event-2268199434
@Override
protected boolean shouldShowPopup() {
return isEditable();
}
@Override
public void setEditable(boolean isEditable) {
super.setEditable(isEditable);
if (!isEditable) {
this.removePopup();
}
}
/**
* The original deinitialize() of AutoCompleteTextField executes its
* own removePopup(), causing a NullPointerException if the popup
* was already removed by my removePopup().This overidde solves this
* issue.
*/
@Override
protected void deinitialize() {
if (popup != null && popup.getParent() == null && parent != null && parent.getParent() == null && lay != null) {
parent.add(popup);
lay.add(parent);
}
super.deinitialize();
}
@Override
protected void initComponent() {
if (popup != null && parent != null) {
parent.setVisible(true);
parent.setHidden(false);
popup.setVisible(true);
popup.setHidden(false);
getComponentForm().revalidateWithAnimationSafety();
}
super.initComponent();
}
/**
* Keep attention, flush animations, like explained here:
* http://disq.us/p/21cb38g
*/
private void removePopup() {
Form f = getComponentForm();
if (f != null) {
popup = ComponentSelector.$("AutoCompletePopup", f).asComponent();
if (popup != null) {
lay = f.getLayeredPane(super.getClass(), true);
parent = popup.getParent();
lay.getAnimationManager().flushAnimation(() -> {
popup.setVisible(false);
popup.setHidden(true);
parent.setVisible(false);
parent.setHidden(true);
lay.removeComponent(parent);
popup.remove();
f.revalidateWithAnimationSafety();
});
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment