Skip to content

Commit 09f9384

Browse files
committed
added helper classes for ease of use in table and form component
1 parent bb73c2a commit 09f9384

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [0.5.0] - 2019-02-23
88

9+
## Added
10+
- `SoftReferenceFormFieldGenerator` to render a soft reference into a CUBA 7 form component
11+
- `SoftReferenceInstanceNameTableColumnGenerator` to render a soft reference with an instance name as a link into a table component
12+
913
## Changed
1014
- fixed entity import / export of entities containing a soft reference via entity inspector. The soft reference in the EntityLoadInfo output format is used.
1115
- `EntitySoftReferenceDatatype` uses EntityLoadInfo output format: `example$Customer-2fdc4906-fa89-11e7-8c3f-9a214cf093ae`
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package de.diedavids.cuba.entitysoftreference.web;
2+
3+
import com.haulmont.cuba.core.global.Messages;
4+
import com.haulmont.cuba.gui.components.ComponentGenerationContext;
5+
import com.haulmont.cuba.gui.components.Field;
6+
import com.haulmont.cuba.gui.components.Form;
7+
import com.haulmont.cuba.gui.components.UiComponentsGenerator;
8+
import com.haulmont.cuba.gui.components.data.value.ContainerValueSource;
9+
import com.haulmont.cuba.gui.model.InstanceContainer;
10+
import org.springframework.stereotype.Component;
11+
12+
import javax.inject.Inject;
13+
14+
/**
15+
* Helper class that generates a entity soft reference into a form component
16+
*
17+
* Usage:
18+
*
19+
* Inside a Editor the EntitySoftReferenceFormFieldGenerator bean can be injected. In the initialization
20+
* phase of the editor, the bean can be used to add the soft reference form field into the form
21+
*
22+
* @Inject
23+
* private EntitySoftReferenceFormFieldGenerator entitySoftReferenceFormFieldGenerator;
24+
*
25+
* @Subscribe
26+
* protected void onInit(InitEvent event) {
27+
*
28+
* entitySoftReferenceFormFieldGenerator.initSoftReferenceFormField(form, "mySoftReferenceFieldName", instanceContainer);
29+
*
30+
* }
31+
*
32+
*/
33+
@Component(SoftReferenceFormFieldGenerator.NAME)
34+
public class SoftReferenceFormFieldGenerator {
35+
36+
public static final String NAME = "ddcser_SoftReferenceFormFieldGenerator";
37+
38+
@Inject
39+
protected UiComponentsGenerator uiComponentsGenerator;
40+
41+
@Inject
42+
protected Messages messages;
43+
44+
45+
/**
46+
* initialized the soft reference as a form field
47+
* @param form the destination form component instance
48+
* @param container the instance container
49+
* @param property the soft reference property
50+
*/
51+
public void initSoftReferenceFormField(Form form, InstanceContainer container, String property) {
52+
53+
Field field = generateSoftReferenceField(property, container);
54+
55+
setCaption(property, container, field);
56+
setValueSource(property, container, field);
57+
58+
form.add(field);
59+
}
60+
61+
private void setValueSource(String property, InstanceContainer container, Field field) {
62+
field.setValueSource(new ContainerValueSource<>(container, property));
63+
}
64+
65+
private void setCaption(String property, InstanceContainer container, Field field) {
66+
String propertyCaption = messages.getTools().getPropertyCaption(container.getEntityMetaClass(), property);
67+
field.setCaption(propertyCaption);
68+
}
69+
70+
private Field generateSoftReferenceField(String property, InstanceContainer container) {
71+
return (Field) uiComponentsGenerator.generate(new ComponentGenerationContext(container.getEntityMetaClass(), property));
72+
}
73+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package de.diedavids.cuba.entitysoftreference.web;
2+
3+
import com.haulmont.cuba.core.entity.Entity;
4+
import com.haulmont.cuba.core.global.MetadataTools;
5+
import com.haulmont.cuba.gui.ScreenBuilders;
6+
import com.haulmont.cuba.gui.UiComponents;
7+
import com.haulmont.cuba.gui.components.Component;
8+
import com.haulmont.cuba.gui.components.LinkButton;
9+
import com.haulmont.cuba.gui.components.Table;
10+
import com.haulmont.cuba.gui.screen.FrameOwner;
11+
import com.haulmont.cuba.gui.screen.Screen;
12+
13+
/**
14+
* Helper class that generates an entity soft reference as a generated table column
15+
*
16+
* Usage:
17+
*
18+
* Within a controller a table can add a generated column with a new instance of a
19+
* EntitySoftReferenceTableColumnGenerator.
20+
*
21+
* @Subscribe
22+
* protected void onInit(InitEvent event) {
23+
* myTable.addGeneratedColumn("myEntitySoftReferenceColumnName",
24+
* new SoftReferenceInstanceNameTableColumnGenerator(
25+
* "myEntitySoftReferenceColumnName",
26+
* uiComponents,
27+
* metadataTools,
28+
* screenBuilders,
29+
* this
30+
* ));
31+
* }
32+
*/
33+
public class SoftReferenceInstanceNameTableColumnGenerator implements Table.ColumnGenerator {
34+
35+
private final String property;
36+
37+
private final UiComponents uiComponents;
38+
private final MetadataTools metadataTools;
39+
private final ScreenBuilders screenBuilders;
40+
private final FrameOwner frameOwner;
41+
42+
/**
43+
* Creates a new SoftReferenceInstanceNameTableColumnGenerator
44+
* @param property the entity soft reference property name
45+
* @param uiComponents instance of CUBAs UiComponents bean
46+
* @param metadataTools instance of CUBAs metadataTools bean
47+
* @param screenBuilders instance of CUBAs screenBuilders bean
48+
* @param frameOwner the frame owner that renders the frame
49+
*/
50+
public SoftReferenceInstanceNameTableColumnGenerator(String property, UiComponents uiComponents, MetadataTools metadataTools, ScreenBuilders screenBuilders, FrameOwner frameOwner) {
51+
this.property = property;
52+
this.uiComponents = uiComponents;
53+
this.metadataTools = metadataTools;
54+
this.screenBuilders = screenBuilders;
55+
this.frameOwner = frameOwner;
56+
}
57+
58+
59+
@Override
60+
public Component generateCell(Entity entity) {
61+
62+
Entity softReference = getSoftReference(entity);
63+
return softReferenceLinkButton(softReference);
64+
}
65+
66+
private LinkButton softReferenceLinkButton(Entity entitySoftReference) {
67+
LinkButton linkButton = uiComponents.create(LinkButton.class);
68+
linkButton.setCaption(metadataTools.getInstanceName(entitySoftReference));
69+
70+
linkButton.addClickListener(clickEvent -> {
71+
softReferenceEditor(entitySoftReference).show();
72+
});
73+
return linkButton;
74+
}
75+
76+
private Entity getSoftReference(Entity entity) {
77+
return (Entity) entity.getValue(property);
78+
}
79+
80+
private Screen softReferenceEditor(Entity softReference) {
81+
return screenBuilders.editor((Class<Entity>) softReference.getClass(), frameOwner)
82+
.editEntity(softReference)
83+
.build();
84+
}
85+
}

0 commit comments

Comments
 (0)