Skip to content

Commit

Permalink
Merge pull request #2914 from jsfan3/patch-3
Browse files Browse the repository at this point in the history
Implementation of my RFE 2911
  • Loading branch information
shai-almog committed Sep 10, 2019
2 parents 10aa574 + 8c5eebd commit 0c46d92
Showing 1 changed file with 21 additions and 4 deletions.
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

0 comments on commit 0c46d92

Please sign in to comment.