IDEMPIERE-5692 Add custom column editor support to WListbox (#1803)

This commit is contained in:
hengsin 2023-04-28 17:44:44 +08:00 committed by GitHub
parent fdbbf093fb
commit 72a546bcad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 2 deletions

View File

@ -35,8 +35,10 @@ import java.util.Set;
import org.adempiere.webui.AdempiereWebUI; import org.adempiere.webui.AdempiereWebUI;
import org.adempiere.webui.ClientInfo; import org.adempiere.webui.ClientInfo;
import org.adempiere.webui.apps.AEnv; import org.adempiere.webui.apps.AEnv;
import org.adempiere.webui.editor.WEditor;
import org.adempiere.webui.event.TableValueChangeEvent; import org.adempiere.webui.event.TableValueChangeEvent;
import org.adempiere.webui.event.TableValueChangeListener; import org.adempiere.webui.event.TableValueChangeListener;
import org.adempiere.webui.event.ValueChangeEvent;
import org.adempiere.webui.util.ZKUpdateUtil; import org.adempiere.webui.util.ZKUpdateUtil;
import org.compiere.minigrid.IDColumn; import org.compiere.minigrid.IDColumn;
import org.compiere.minigrid.SelectableIDColumn; import org.compiere.minigrid.SelectableIDColumn;
@ -53,6 +55,7 @@ import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events; import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Decimalbox; import org.zkoss.zul.Decimalbox;
import org.zkoss.zul.Html;
import org.zkoss.zul.Image; import org.zkoss.zul.Image;
import org.zkoss.zul.ListModel; import org.zkoss.zul.ListModel;
import org.zkoss.zul.Listbox; import org.zkoss.zul.Listbox;
@ -60,6 +63,7 @@ import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem; import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer; import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.ListitemRendererExt; import org.zkoss.zul.ListitemRendererExt;
import org.zkoss.zul.impl.InputElement;
/** /**
* Renderer for {@link org.adempiere.webui.component.ListItem} * Renderer for {@link org.adempiere.webui.component.ListItem}
@ -117,7 +121,7 @@ public class WListItemRenderer implements ListitemRenderer<Object>, EventListene
* @param columnIndex The index of the column for which details are to be retrieved. * @param columnIndex The index of the column for which details are to be retrieved.
* @return The details of the column at the specified index. * @return The details of the column at the specified index.
*/ */
private WTableColumn getColumn(int columnIndex) public WTableColumn getColumn(int columnIndex)
{ {
try try
{ {
@ -201,14 +205,61 @@ public class WListItemRenderer implements ListitemRenderer<Object>, EventListene
int rowIndex, int columnIndex) int rowIndex, int columnIndex)
{ {
ListCell listcell = new ListCell(); ListCell listcell = new ListCell();
WTableColumn column = null;
if (m_tableColumns.size() > columnIndex) { if (m_tableColumns.size() > columnIndex) {
WTableColumn column = getColumn(columnIndex); column = getColumn(columnIndex);
if (column != null && column.getHeaderValue() != null) { if (column != null && column.getHeaderValue() != null) {
listcell.setWidgetAttribute(AdempiereWebUI.WIDGET_INSTANCE_NAME, column.getHeaderValue().toString()); listcell.setWidgetAttribute(AdempiereWebUI.WIDGET_INSTANCE_NAME, column.getHeaderValue().toString());
} }
} }
boolean isCellEditable = table != null ? table.isCellEditable(rowIndex, columnIndex) : false; boolean isCellEditable = table != null ? table.isCellEditable(rowIndex, columnIndex) : false;
//use custom editor configure through WTableColumn (if any)
if (column != null && column.getEditorProvider() != null)
{
WEditor editor = column.getEditorProvider().apply(new WTableColumn.EditorProviderParameters(table, rowIndex, columnIndex, value));
if (editor != null)
{
editor.setValue(value);
if (isCellEditable)
{
editor.setReadWrite(true);
editor.setTableEditor(true);
editor.addValueChangeListener(evt -> onValueChange(evt));
listcell.appendChild(editor.getComponent());
}
else
{
editor.setReadWrite(false);
Component displayComponent = editor.getDisplayComponent();
if (displayComponent != null && displayComponent instanceof Html)
{
String text = editor.getDisplayTextForGridView(value);
((Html)displayComponent).setContent(text);
listcell.appendChild(displayComponent);
}
else if (displayComponent != null && displayComponent instanceof Label)
{
String text = editor.getDisplay();
((Label)displayComponent).setValue(text);
listcell.appendChild(displayComponent);
}
else if (displayComponent != null && displayComponent instanceof InputElement)
{
String text = editor.getDisplay();
((InputElement)displayComponent).setText(text);
((InputElement)displayComponent).setReadonly(true);
listcell.appendChild(displayComponent);
}
else
{
listcell.appendChild(editor.getComponent());
}
}
return listcell;
}
}
if (value != null) if (value != null)
{ {
if (value instanceof Boolean) if (value instanceof Boolean)
@ -378,6 +429,24 @@ public class WListItemRenderer implements ListitemRenderer<Object>, EventListene
return listcell; return listcell;
} }
/**
* Handle value change from custom editor created by {@link WTableColumn#getEditorProvider()}.
* @param evt
*/
private void onValueChange(ValueChangeEvent evt) {
WEditor editor = (WEditor) evt.getSource();
Component component = editor.getComponent();
int row = getRowPosition(component);
int col = getColumnPosition(component);
WTableColumn tableColumn = getColumn(col);
TableValueChangeEvent vcEvent = new TableValueChangeEvent(component,
tableColumn.getHeaderValue().toString(),
row, col,
evt.getOldValue(), evt.getNewValue());
fireTableValueChange(vcEvent);
}
/** /**
* Update header of a Column. * Update header of a Column.

View File

@ -17,6 +17,10 @@
package org.adempiere.webui.component; package org.adempiere.webui.component;
import java.util.function.Function;
import org.adempiere.webui.editor.WEditor;
/** /**
* Model for {@link WListbox} column * Model for {@link WListbox} column
* @author Andrew Kimball * @author Andrew Kimball
@ -47,6 +51,8 @@ public class WTableColumn
private int AD_Reference_ID; private int AD_Reference_ID;
private Function<EditorProviderParameters, WEditor> editorProvider;
/** /**
* Default constructor * Default constructor
*/ */
@ -224,4 +230,42 @@ public class WTableColumn
public void setAD_Reference_ID(int AD_Reference_ID) { public void setAD_Reference_ID(int AD_Reference_ID) {
this.AD_Reference_ID=AD_Reference_ID; this.AD_Reference_ID=AD_Reference_ID;
} }
/**
* Set optional editor provider for column
* @param provider Function&lt;EditorProviderParameters, WEditor&gt;
*/
public void setEditorProvider(Function<EditorProviderParameters, WEditor> provider) {
this.editorProvider = provider;
}
/**
* @return Function&lt;EditorProviderParameters, WEditor&gt; instance of null
*/
public Function<EditorProviderParameters, WEditor> getEditorProvider() {
return editorProvider;
}
/**
* Parameters for editor provider
*/
public static class EditorProviderParameters {
public WListbox listbox;
public int rowIndex;
public int columnIndex;
public Object value;
/**
* @param listbox
* @param rowIndex
* @param columnIndex
* @param value
*/
public EditorProviderParameters(WListbox listbox, int rowIndex, int columnIndex, Object value) {
this.listbox = listbox;
this.rowIndex = rowIndex;
this.columnIndex = columnIndex;
this.value = value;
}
}
} }