support lookupField captionProperty

This commit is contained in:
Dmitry Abramov 2009-02-25 09:57:37 +00:00
parent e5a5351af8
commit 787e0470ff
4 changed files with 64 additions and 1 deletions

View File

@ -13,5 +13,16 @@ import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.CollectionDatasource;
public interface LookupField extends Field {
enum CaptionMode {
ITEM,
PROPERTY
}
CaptionMode getCaptionMode();
void setCaptionMode(CaptionMode captionMode);
String getCaptionProperty();
void setCaptionProperty(String captionProperty);
void setLookupDatasource(CollectionDatasource datasource);
}

View File

@ -11,6 +11,7 @@ package com.haulmont.cuba.gui.xml.layout.loaders;
import com.haulmont.cuba.gui.components.Field;
import com.haulmont.cuba.gui.components.LookupField;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.xml.layout.ComponentsFactory;
@ -23,6 +24,19 @@ public class LookupFieldLoader extends AbstractFieldLoader {
super(context, config, factory);
}
@Override
public Component loadComponent(ComponentsFactory factory, Element element) throws InstantiationException, IllegalAccessException {
final LookupField component = (LookupField) super.loadComponent(factory, element);
String captionProperty = element.attributeValue("captionProperty");
if (!StringUtils.isEmpty(captionProperty)) {
component.setCaptionMode(LookupField.CaptionMode.PROPERTY);
component.setCaptionProperty(captionProperty);
}
return component;
}
@Override
protected void loadDatasource(Field component, Element element) {
final String datasource = element.attributeValue("lookupDatasource");

View File

@ -45,7 +45,7 @@
</row>
<row>
<label value="Group"/>
<lookupField id="group" datasource="user" property="group" lookupDatasource="groups"/>
<lookupField id="group" datasource="user" property="group" lookupDatasource="groups" captionProperty="name"/>
</row>
</rows>
</grid>

View File

@ -27,6 +27,8 @@ public class LookupField
implements
com.haulmont.cuba.gui.components.LookupField, Component.Wrapper {
private CollectionDatasource lookupDatasource;
private CaptionMode captionMode = CaptionMode.ITEM;
private String captionProperty;
public LookupField() {
this.component = new Select();
@ -76,9 +78,45 @@ public class LookupField
setRequired(metaProperty.isMandatory());
}
public CaptionMode getCaptionMode() {
return captionMode;
}
public void setCaptionMode(CaptionMode captionMode) {
this.captionMode = captionMode;
switch (captionMode) {
case ITEM: {
component.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_ITEM);
break;
}
case PROPERTY: {
component.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
break;
}
default :{
throw new UnsupportedOperationException();
}
}
}
public String getCaptionProperty() {
return captionProperty;
}
public void setCaptionProperty(String captionProperty) {
this.captionProperty = captionProperty;
if (lookupDatasource != null) {
component.setItemCaptionPropertyId(lookupDatasource.getMetaClass().getProperty(captionProperty));
}
}
public void setLookupDatasource(CollectionDatasource datasource) {
lookupDatasource = datasource;
component.setContainerDataSource(new CollectionDatasourceWrapper(datasource, true));
if (captionProperty != null) {
component.setItemCaptionPropertyId(lookupDatasource.getMetaClass().getProperty(captionProperty));
}
}
@Override