001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.form;
016:
017: import org.apache.commons.lang.ArrayUtils;
018: import org.araneaframework.backend.util.BeanMapper;
019: import org.araneaframework.core.AraneaRuntimeException;
020: import org.araneaframework.uilib.form.reader.BeanFormReader;
021: import org.araneaframework.uilib.form.reader.BeanFormWriter;
022:
023: public class BeanFormWidget extends FormWidget {
024: private static final String[] primitiveTypes = new String[] {
025: "int", "long", "short", "double", "float", "boolean",
026: "byte", "char" };
027: private BeanMapper beanMapper;
028: private Class beanClass;
029:
030: public BeanFormWidget(Class beanClass) {
031: this .beanClass = beanClass;
032: this .beanMapper = new BeanMapper(beanClass);
033: }
034:
035: private Data inferDataType(String fieldId, boolean mandatory) {
036: if (!beanMapper.isReadable(fieldId))
037: throw new AraneaRuntimeException(
038: "Could not infer type for bean field '" + fieldId
039: + "'!");
040:
041: Class type = beanMapper.getFieldType(fieldId);
042:
043: if (type.isPrimitive()) {
044: if (!mandatory)
045: throw new AraneaRuntimeException(
046: "Form element '"
047: + fieldId
048: + "' corresponding to JavaBean's primitive-typed field was not specified as mandatory.");
049:
050: switch (ArrayUtils.indexOf(primitiveTypes, type.getName())) {
051: case 0: {
052: type = Integer.class;
053: break;
054: }
055: case 1: {
056: type = Long.class;
057: break;
058: }
059: case 2: {
060: type = Short.class;
061: break;
062: }
063: case 3: {
064: type = Double.class;
065: break;
066: }
067: case 4: {
068: type = Float.class;
069: break;
070: }
071: case 5: {
072: type = Boolean.class;
073: break;
074: }
075: case 6: {
076: type = Byte.class;
077: break;
078: }
079: case 7: {
080: type = Character.class;
081: break;
082: }
083: default:
084: throw new AraneaRuntimeException(
085: "Could not infer type for bean field '"
086: + fieldId + "'!");
087: }
088: }
089:
090: return new Data(type);
091: }
092:
093: public BeanFormWidget addBeanSubForm(String id) {
094: if (!beanMapper.isReadable(id))
095: throw new AraneaRuntimeException(
096: "Could not infer type for bean subform '" + id
097: + "'!");
098:
099: BeanFormWidget result = new BeanFormWidget(beanMapper
100: .getFieldType(id));
101: addElement(id, result);
102: return result;
103: }
104:
105: public FormElement addBeanElement(String elementName,
106: String labelId, Control control, boolean mandatory) {
107: return super .addElement(elementName, labelId, control,
108: inferDataType(elementName, mandatory), mandatory);
109: }
110:
111: public FormElement addBeanElement(String elementName,
112: String labelId, Control control, Object initialValue,
113: boolean mandatory) {
114: return super .addElement(elementName, labelId, control,
115: inferDataType(elementName, mandatory), initialValue,
116: mandatory);
117: }
118:
119: public Object writeToBean(Object bean) {
120: BeanFormReader reader = new BeanFormReader(this );
121: reader.readFormBean(bean);
122: return bean;
123: }
124:
125: public void readFromBean(Object bean) {
126: BeanFormWriter writer = new BeanFormWriter(beanClass);
127: writer.writeFormBean(this , bean);
128: }
129:
130: public Class getBeanClass() {
131: return beanClass;
132: }
133: }
|