
org.vaadin.viritin.fields.LabelField Maven / Gradle / Ivy
package org.vaadin.viritin.fields;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.Label;
import org.vaadin.viritin.label.RichText;
/**
* A field which represents the value always in a label, so it is not editable.
*
* The use case is a non editable value in a form, e.g. an id.
*
* The creation of the label text can be controlled via a
* {@link org.vaadin.viritin.fields.CaptionGenerator}.
*
* @author Daniel Nordhoff-Vergien
*
* @param
* the type of the entity
*/
public class LabelField extends CustomField {
private static final long serialVersionUID = -3079451926367430515L;
private final Class type;
private static class ToStringCaptionGenerator implements
CaptionGenerator {
private static final long serialVersionUID = 1149675718238329960L;
@Override
public String getCaption(T option) {
if (option == null) {
return "";
} else {
return option.toString();
}
}
}
public LabelField(Class type, String caption) {
super();
this.type = type;
setCaption(caption);
}
private CaptionGenerator captionGenerator = new ToStringCaptionGenerator<>();
private Label label = new RichText();
public LabelField(Class type) {
super();
this.type = type;
}
public LabelField() {
this.type = (Class) String.class;
}
public LabelField withFullWidth() {
setWidth("100%");
return this;
}
public LabelField withWidth(float width, Unit unit) {
setWidth(width, unit);
return this;
}
public LabelField withWidth(String width) {
setWidth(width);
return this;
}
public LabelField withCaption(String caption) {
setCaption(caption);
return this;
}
public LabelField withValue(T newFieldValue) {
setValue(newFieldValue);
return this;
}
@Override
protected Component initContent() {
updateLabel();
return label;
}
protected void updateLabel() {
String caption;
if (captionGenerator != null) {
caption = captionGenerator.getCaption(getValue());
} else {
caption = getValue().toString();
}
label.setValue(caption);
}
@Override
protected void setInternalValue(T newValue) {
super.setInternalValue(newValue);
updateLabel();
}
@Override
public Class extends T> getType() {
return type;
}
/**
* Sets the CaptionGenerator for creating the label value.
*
* @param captionGenerator the caption generator used to format the displayed property
*/
public void setCaptionGenerator(CaptionGenerator captionGenerator) {
this.captionGenerator = captionGenerator;
updateLabel();
}
public void setLabel(Label label) {
this.label = label;
}
public Label getLabel() {
return label;
}
}