From 75be677f2eb9ca2341491c5f94674dd36c6e5946 Mon Sep 17 00:00:00 2001 From: hengsin Date: Mon, 13 Nov 2023 21:37:22 +0800 Subject: [PATCH] IDEMPIERE-5924 WSearchEditor - add API to allow form to turn on the auto complete feature (#2105) --- .../adempiere/webui/editor/WSearchEditor.java | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WSearchEditor.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WSearchEditor.java index a9397c96a6..19fb703ef7 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WSearchEditor.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WSearchEditor.java @@ -105,6 +105,7 @@ public class WSearchEditor extends WEditor implements ContextMenuListener, Value /** ADWindow instance that own this editor */ protected ADWindow adwindow; + private EventListener autoCompleteListener; /** * @@ -252,18 +253,7 @@ public class WSearchEditor extends WEditor implements ContextMenuListener, Value getComponent().getCombobox().setPlaceholder(gridField.getPlaceholder()); if (gridField != null && gridField.isAutocomplete()) { - setTableAndKeyColumn(); - listModel = new InfoListSubModel(lookup, gridField, m_tableName, m_keyColumnName); - int maxRows = MSysConfig.getIntValue(MSysConfig.ZK_SEARCH_AUTO_COMPLETE_MAX_ROWS, DEFAULT_MAX_AUTO_COMPLETE_ROWS, Env.getAD_Client_ID(Env.getCtx())); - getComponent().getCombobox().setModel(listModel.getSubModel(null, maxRows)); - - getComponent().getCombobox().addEventListener(Events.ON_CHANGING, (EventListener)(e) -> { - if (!e.isChangingBySelectBack()) { - listModel.setWhereClause(getWhereClause()); - String s = e.getValue(); - getComponent().getCombobox().setModel(listModel.getSubModel(s, maxRows)); - } - }); + enableAutoComplete(); } else { getComponent().getCombobox().setAutodrop(false); } @@ -271,6 +261,44 @@ public class WSearchEditor extends WEditor implements ContextMenuListener, Value return; } + /** + * Turn on auto complete for editor + */ + public void enableAutoComplete() { + if (autoCompleteListener != null) + return; + + if (m_tableName == null) + setTableAndKeyColumn(); + listModel = new InfoListSubModel(lookup, gridField, m_tableName, m_keyColumnName); + int maxRows = MSysConfig.getIntValue(MSysConfig.ZK_SEARCH_AUTO_COMPLETE_MAX_ROWS, DEFAULT_MAX_AUTO_COMPLETE_ROWS, Env.getAD_Client_ID(Env.getCtx())); + getComponent().getCombobox().setModel(listModel.getSubModel(null, maxRows)); + + autoCompleteListener = e -> { + if (!e.isChangingBySelectBack()) { + listModel.setWhereClause(getWhereClause()); + String s = e.getValue(); + getComponent().getCombobox().setModel(listModel.getSubModel(s, maxRows)); + } + }; + + getComponent().getCombobox().addEventListener(Events.ON_CHANGING, autoCompleteListener); + getComponent().getCombobox().setAutodrop(true); + } + + /** + * Turn off auto complete for editor + */ + public void disableAutoComplete() { + if (autoCompleteListener != null) { + getComponent().getCombobox().removeEventListener(Events.ON_CHANGING, autoCompleteListener); + getComponent().getCombobox().setAutodrop(false); + getComponent().getCombobox().setModel(null); + listModel = null; + autoCompleteListener = null; + } + } + @Override public void setValue(Object value) {