-
-
Save jsfan3/d8c9698afe63f8cf466679e6a1d79a34 to your computer and use it in GitHub Desktop.
Test of extending AutoCompleteTextField, after http://disq.us/p/21cvxbt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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