Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of my RFE 2911 #2914

Merged
merged 1 commit into from Sep 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions CodenameOne/src/com/codename1/ui/AutoCompleteTextField.java
Expand Up @@ -64,6 +64,11 @@ public class AutoCompleteTextField extends TextField {
private String pickedText;
private int minimumLength;

public static final int POPUP_POSITION_AUTO = 0;
public static final int POPUP_POSITION_OVER = 1;
public static final int POPUP_POSITION_UNDER = 2;
private int popupPosition = POPUP_POSITION_AUTO;

/**
* The number of elements shown for the auto complete popup
*/
Expand Down Expand Up @@ -239,7 +244,7 @@ private boolean filterImpl(String text) {
Form f = getComponentForm();

if (popup.getComponentCount() > 0) {
int popupHeight = calcPopuupHeight((List)popup.getComponentAt(0));
int popupHeight = calcPopupHeight((List)popup.getComponentAt(0));
popup.setHeight(popupHeight);
dontCalcSize = false;
popup.forceRevalidate();
Expand Down Expand Up @@ -377,7 +382,7 @@ public void actionPerformed(ActionEvent evt) {
}
popup.getAllStyles().setMargin(LEFT, Math.max(0, getAbsoluteX()));

int popupHeight = calcPopuupHeight(l);
int popupHeight = calcPopupHeight(l);

popup.setPreferredW(getWidth());
popup.setHeight(popupHeight);
Expand Down Expand Up @@ -435,8 +440,20 @@ public int getMinimumElementsShownInPopup() {
public void setMinimumElementsShownInPopup(int minimumElementsShownInPopup) {
this.minimumElementsShownInPopup = minimumElementsShownInPopup;
}

/**
* Set the autocomplete popup position in respect of the text field;
* POPUP_POSITION_AUTO is the default and it means that the popup is placed
* according to the available space.
*
* @param popupPosition on of POPUP_POSITION_AUTO, POPUP_POSITION_OVER,
* POPUP_POSITION_UNDER
*/
public void setPopupPosition(int popupPosition) {
this.popupPosition = popupPosition;
}

private int calcPopuupHeight(List l) {
private int calcPopupHeight(List l) {
int y = getAbsoluteY();
int topMargin;
int popupHeight;
Expand All @@ -450,7 +467,7 @@ private int calcPopuupHeight(List l) {
items = ((FilterProxyListModel)l.getModel()).getUnderlying().getSize();
}
int listHeight = items * l.getElementSize(false, true).getHeight();
if(y < f.getContentPane().getHeight()/2){
if(popupPosition == POPUP_POSITION_UNDER || popupPosition == POPUP_POSITION_AUTO && y < f.getContentPane().getHeight()/2){
topMargin = y - f.getTitleArea().getHeight() + getHeight();
popupHeight = Math.min(listHeight, f.getContentPane().getHeight()/2);
}else{
Expand Down