package pt.ist.renderers; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.commons.collections.Predicate; import pt.ist.renderers.components.HtmlComponent; import pt.ist.renderers.components.HtmlFormComponent; import pt.ist.renderers.components.HtmlHiddenField; import pt.ist.renderers.components.HtmlInlineContainer; import pt.ist.renderers.components.HtmlMenu; import pt.ist.renderers.components.HtmlMenuOption; import pt.ist.renderers.components.HtmlSimpleValueComponent; import pt.ist.renderers.components.HtmlTextInput; import pt.ist.renderers.contexts.InputContext; import pt.ist.renderers.contexts.PresentationContext; import pt.ist.renderers.converters.EnumConverter; import pt.ist.renderers.layouts.Layout; import pt.ist.renderers.model.MetaObject; import pt.ist.renderers.model.MetaSlotKey; import pt.ist.renderers.utils.RenderUtils; import pt.utl.ist.fenix.tools.util.Pair; /** * This renderer presents an html menu with one option for each possible enum * value. Each option's label is searched in the bundle * ENUMERATION_RESOURCES using the enum's name. * *
* Example:
Choose a {@link java.lang.annotation.ElementType element type}:
*
*
* @author cfgi
*/
public class EnumInputRenderer extends InputRenderer {
private String defaultText;
private String defaultTextBundle;
private boolean defaultOptionHidden = false;
private String bundle;
private boolean key;
private String excludedValues;
private String includedValues;
private boolean sort;
private boolean readOnly;
private boolean disabled;
public String getBundle() {
return this.bundle;
}
/**
* The bundle used if key
is true
*
* @property
*/
public void setBundle(String bundle) {
this.bundle = bundle;
}
public String getDefaultText() {
return this.defaultText;
}
/**
* The text or key of the default menu title.
*
* @property
*/
public void setDefaultText(String defaultText) {
this.defaultText = defaultText;
}
public boolean isDefaultOptionHidden() {
return defaultOptionHidden;
}
/**
* Don't show the default option, that is, the options meaning no value
* selected.
*
* @property
*/
public void setDefaultOptionHidden(boolean defaultOptionHidden) {
this.defaultOptionHidden = defaultOptionHidden;
}
public String getExcludedValues() {
return excludedValues;
}
/**
* Excluded Values.
*
* @property
*/
public void setExcludedValues(String excludedValues) {
this.excludedValues = excludedValues;
}
public String getIncludedValues() {
return includedValues;
}
/**
* Excluded Values.
*
* @property
*/
public void setIncludedValues(String includedValues) {
this.includedValues = includedValues;
}
public boolean isKey() {
return this.key;
}
/**
* Indicates the the default text is a key to a resource bundle.
*
* @property
*/
public void setKey(boolean key) {
this.key = key;
}
@Override
protected Layout getLayout(final Object object, Class type) {
if (getReadOnly()) {
return new Layout() {
@Override
public HtmlComponent createComponent(Object targetObject, Class type) {
Enum enumerate = (Enum) targetObject;
HtmlInlineContainer container = new HtmlInlineContainer();
HtmlHiddenField value = new HtmlHiddenField();
value.setValue(enumerate.name());
value.setConverter(new EnumConverter());
container.addChild(value);
InputContext context = getInputContext();
((HtmlFormComponent) value).setTargetSlot((MetaSlotKey) context.getMetaObject().getKey());
HtmlTextInput component = new HtmlTextInput();
component.setValue(RenderUtils.getEnumString(enumerate));
container.addChild(component);
return container;
}
@Override
public void applyStyle(HtmlComponent component) {
HtmlInlineContainer block = (HtmlInlineContainer) component;
HtmlTextInput textInput = (HtmlTextInput) block.getChild(new Predicate() {
@Override
public boolean evaluate(Object elem) {
return !(elem instanceof HtmlHiddenField);
}
});
super.applyStyle(textInput);
textInput.setReadOnly(true);
textInput.setDisabled(getDisabled());
}
};
}
return new Layout() {
@Override
public HtmlComponent createComponent(Object targetObject, Class type) {
Enum enumerate = (Enum) targetObject;
if (!type.isEnum() && Enum.class.isAssignableFrom(type)) {
type = type.getEnclosingClass();
}
final Object bean = getRenderedObject();
Collection