IDEMPIERE-3049 String autocomplete NPE / refactor to apply best practices / drop the AD_Org_ID filter (doesn't make sense for non-org tables) / add ReQuery to context menu

This commit is contained in:
Carlos Ruiz 2017-08-30 14:26:08 +02:00
parent 205edcdb73
commit e287b987c2
2 changed files with 48 additions and 54 deletions

View File

@ -2361,54 +2361,42 @@ public class GridField
/** /**
* Returns a list containing all existing entries of this field * Returns a list containing all existing entries of this field
* with the actual AD_Org_ID and AD_Client_ID. * with the actual AD_Client_ID.
* @return List of existing entries for this field * @return List of existing entries for this field
*/ */
public List<String> getEntries() { public List<String> getEntries() {
/* TODO: consider caching the list to avoid repeating queries on every window open (twice, for find and for field) */
MColumn column = MColumn.get(Env.getCtx(), getAD_Column_ID());
MTable table = MTable.get(Env.getCtx(), column.getAD_Table_ID());
String tableName = table.getTableName();
String columnName = column.getColumnName();
ArrayList<String> list = new ArrayList<String>(); ArrayList<String> list = new ArrayList<String>();
PreparedStatement pstmt1; if (tableName != null && columnName != null) {
PreparedStatement pstmt2;
String sql = "";
try
{
String tableName = null;
String columnName = null;
int AD_Org_ID = Env.getAD_Org_ID(Env.getCtx());
int AD_Client_ID = Env.getAD_Client_ID(Env.getCtx()); int AD_Client_ID = Env.getAD_Client_ID(Env.getCtx());
sql = "SELECT t.TableName, c.ColumnName " + PreparedStatement pstmt = null;
" FROM AD_COLUMN c INNER JOIN AD_Table t ON (c.AD_Table_ID=t.AD_Table_ID)" + ResultSet rs = null;
" WHERE AD_Column_ID=?"; StringBuilder sql = new StringBuilder()
pstmt1 = DB.prepareStatement(sql, null); .append("SELECT DISTINCT ")
pstmt1.setInt(1, getAD_Column_ID()); .append(columnName)
ResultSet rs1 = pstmt1.executeQuery(); .append(" FROM ")
if (rs1.next()) .append(tableName)
{ .append(" WHERE AD_Client_ID=? AND ")
tableName = rs1.getString(1); .append(columnName)
columnName = rs1.getString(2); .append(" IS NOT NULL ORDER BY 1");
} try {
DB.close(rs1, pstmt1); pstmt = DB.prepareStatement(sql.toString(), null);
pstmt.setInt(1, AD_Client_ID);
if (tableName != null && columnName != null) {
sql = "SELECT DISTINCT " + columnName + " FROM " + tableName + " WHERE AD_Client_ID=? " rs = pstmt.executeQuery();
+ " AND AD_Org_ID=?"; while (rs.next()) {
pstmt2 = DB.prepareStatement(sql, null); list.add(rs.getString(1));
pstmt2.setInt(1, AD_Client_ID);
pstmt2.setInt(2, AD_Org_ID);
ResultSet rs2 = pstmt2.executeQuery();
while (rs2.next())
{
list.add(rs2.getString(1));
} }
DB.close(rs2, pstmt2); } catch (Exception e) {
log.log(Level.SEVERE, sql.toString(), e);
} finally {
DB.close(rs, pstmt);
} }
} }
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
return list; return list;
} }

View File

@ -145,7 +145,8 @@ public class WStringEditor extends WEditor implements ContextMenuListener
} }
else else
getComponent().setMultiline(false); getComponent().setMultiline(false);
getComponent().setRows(gridField.getNumLines() <= 0 || tableEditor ? 1 : gridField.getNumLines()); if (! gridField.isAutocomplete()) // avoid -> Combobox doesn't support multiple rows
getComponent().setRows(gridField.getNumLines() <= 0 || tableEditor ? 1 : gridField.getNumLines());
if (getComponent().getRows() > 1) if (getComponent().getRows() > 1)
ZKUpdateUtil.setHeight(getComponent(), "100%"); ZKUpdateUtil.setHeight(getComponent(), "100%");
@ -154,7 +155,7 @@ public class WStringEditor extends WEditor implements ContextMenuListener
if(!(this instanceof WPasswordEditor)){ // check password field if(!(this instanceof WPasswordEditor)){ // check password field
popupMenu = new WEditorPopupMenu(false, false, isShowPreference()); popupMenu = new WEditorPopupMenu(false, gridField.isAutocomplete(), isShowPreference());
addTextEditorMenu(popupMenu); addTextEditorMenu(popupMenu);
addChangeLogMenu(popupMenu); addChangeLogMenu(popupMenu);
} }
@ -289,23 +290,28 @@ public class WStringEditor extends WEditor implements ContextMenuListener
{ {
WFieldRecordInfo.start(gridField); WFieldRecordInfo.start(gridField);
} }
else if (WEditorPopupMenu.REQUERY_EVENT.equals(evt.getContextEvent()))
{
actionRefresh();
}
} }
@Override @Override
public void dynamicDisplay() { public void dynamicDisplay() {
super.dynamicDisplay(); super.dynamicDisplay();
//referesh auto complete list actionRefresh();
}
public void actionRefresh() {
//refresh auto complete list
if (gridField.isAutocomplete()) { if (gridField.isAutocomplete()) {
Combobox combo = (Combobox)getComponent(); Combobox combo = (Combobox)getComponent();
List<String> items = gridField.getEntries(); List<String> items = gridField.getEntries();
if (items.size() != combo.getItemCount()) combo.removeAllItems();
{ for(String s : items) {
combo.removeAllItems(); combo.appendItem(s);
for(String s : items) { }
combo.appendItem(s); }
}
}
}
} }
private AbstractADWindowContent findADWindowContent() { private AbstractADWindowContent findADWindowContent() {