com.talk2object.plum.view.web.component.input.WebRadioField Maven / Gradle / Ivy
The newest version!
package com.talk2object.plum.view.web.component.input;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jsoup.nodes.Element;
import com.talk2object.plum.input.value.image.Image;
import com.talk2object.plum.view._abstract.components.listbox.option.Options;
import com.talk2object.plum.view._abstract.components.listbox.option.ViewOption;
import com.talk2object.plum.view._abstract.components.listbox.option.converter.OptionConverter;
import com.talk2object.plum.view.component.Component;
import com.talk2object.plum.view.component.listbox.RadioField;
import com.talk2object.plum.view.event.AbstractViewAction;
import com.talk2object.plum.view.event.ViewAction;
import com.talk2object.plum.view.event.WebEvent;
import com.talk2object.plum.view.web.WebFileUtils;
import com.talk2object.plum.view.web.component.AbstractTemplateWebComponent;
import com.talk2object.plum.view.web.component.ComponentElements;
public class WebRadioField extends AbstractTemplateWebComponent {
public static final String VALUE_CHANGE = "valueChange";
public static final String IMAGE_NUMBER_SEPERATOR = "-";
private final String INPUT = "input";
private static Element componentHtmlTemplate;
private Element inputElement;
private Element itemOuterElement;
private List options;
private int selectedIndex = -1;
public WebRadioField(String id, Component component,
Element templateTemplate) {
super(id, component, templateTemplate);
}
private RadioField getRadioBox() {
return (RadioField) getComponent();
}
public void createHtmlElement() {
// ===create local state
RadioField radioBox = getRadioBox();
createViewOptions(radioBox);
selectedIndex = radioBox.getSelectedIndex();
ComponentElements componentElements = findAndCloneElement(
getComponentHtmlTemplate(true), false);
htmlOuterElement = componentElements.getOuter();
htmlInnerElement = componentElements.getInner();
itemOuterElement = findElementByClass("item-outer");
htmlInnerElement.empty();
int i = 0;
for (ViewOption option : options) {
Element itemElement = itemOuterElement.clone();
Element inputElement = findElementByClass(itemElement, "item-input");
Element imageElement = findElementByClass(itemElement, "item-image");
Element textElement = findElementByClass(itemElement, "item-text");
inputElement.attr("name", getFullInputName(INPUT));
inputElement.attr("value",
getFullInputName(String.format("%s_%d", INPUT, i)));
if (option.getImage() != null)
imageElement.attr(
"src",
WebFileUtils.generateUrl(getId(), INPUT
+ IMAGE_NUMBER_SEPERATOR + i));
else
imageElement.remove();
if (option.getText() != null)
textElement.text(option.getText());
else
textElement.remove();
htmlInnerElement.appendChild(itemElement);
i++;
}
// radio box will not contains other elements.
htmlInnerElement = null;
}
private void createViewOptions(RadioField radioField) {
options = new ArrayList();
Options inputOptions = radioField.getOptions();
OptionConverter optionConverter = inputOptions.getOptionConveter();
// create options
for (int i = 0; i < inputOptions.size(); i++) {
Object obj = inputOptions.get(i);
String value = optionConverter.toText(obj);
String text = optionConverter.toText(obj);
Image image = optionConverter.toImage(obj);
ViewOption option = new ViewOption(value, text, image);
options.add(option);
}
}
@Override
public void writeFile(HttpServletRequest req, HttpServletResponse response,
String shortName) throws IOException {
int index = Integer.valueOf(shortName.split(IMAGE_NUMBER_SEPERATOR)[1]);
Image image = options.get(index).getImage();
writeFile(req, response, image.getFile());
}
protected ViewAction onValueChange(final Object newValue) {
return new AbstractViewAction(WebEvent.VALUE_SET.name()) {
@Override
public void execute(Component view) {
// getRadioBox().setValue(newValue);
RadioField radioBox = (RadioField) view;
radioBox.setSelectedIndex(selectedIndex);
}
};
}
@Override
public ViewAction processEvent(WebEvent event,
Map parameters) {
if (event == WebEvent.VALUE_SET) {
Object newValue = (Object) parameters.get(INPUT);
return onValueChange(newValue);
} else
throw new RuntimeException("unknow event:" + event);
}
@Override
public Element getComponentHtmlTemplate() {
return componentHtmlTemplate;
}
@Override
public void setComponentHtmlTemplate(Element element) {
componentHtmlTemplate = element;
}
@Override
public String getComponentType() {
return "radiobox";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy