com.tcdng.unify.web.ui.widget.container.Form Maven / Gradle / Ivy
/*
* Copyright 2018-2023 The Code Department.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.tcdng.unify.web.ui.widget.container;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import com.tcdng.unify.core.UnifyException;
import com.tcdng.unify.core.annotation.Component;
import com.tcdng.unify.core.annotation.UplAttribute;
import com.tcdng.unify.core.annotation.UplAttributes;
import com.tcdng.unify.core.data.ValueStore;
import com.tcdng.unify.web.ui.widget.AbstractContainer;
import com.tcdng.unify.web.ui.widget.Section;
import com.tcdng.unify.web.ui.widget.Widget;
/**
* Represents a user interface form.
*
* @author The Code Department
* @since 1.0
*/
@Component("ui-form")
@UplAttributes({ @UplAttribute(name = "columns", type = int.class, defaultVal = "1"),
@UplAttribute(name = "section", type = Section[].class, mandatory = true),
@UplAttribute(name = "requiredSymbol", type = String.class, defaultVal = "*"),
@UplAttribute(name = "captionSuffix", type = String.class, defaultVal = ":") })
public class Form extends AbstractContainer {
private FormSection[] formSections;
private String dataGroupId;
public Form() {
super(false);
}
@Override
public void onPageConstruct() throws UnifyException {
super.onPageConstruct();
Section[] sections = getUplAttribute(Section[].class, "section");
formSections = new FormSection[sections.length];
for (int i = 0; i < sections.length; i++) {
formSections[i] = new FormSection(sections[i]);
}
dataGroupId = getPrefixedId("data_");
}
@Override
public List getShallowReferencedLongNames(String attribute) throws UnifyException {
return getWidgetInfo().getShallowNames();
}
@SuppressWarnings("unchecked")
@Override
public void cascadeValueStore() throws UnifyException {
super.cascadeValueStore();
ValueStore valueStore = getChildBindingValueStore();
for (FormSection formSection : formSections) {
if (formSection.isBinding()) {
Object newValue = null;
if (valueStore != null) {
newValue = valueStore.retrieve(formSection.getBinding());
}
if (formSection.oldValue != newValue) {
formSection.valueStore = null;
formSection.valueStoreList = null;
if (newValue != null) {
if (newValue.getClass().isArray()) {
List valueList = new ArrayList();
int len = Array.getLength(newValue);
for (int i = 0; i < len; i++) {
valueList.add(createArrayValueStore((Object[]) newValue, i));
}
formSection.valueStoreList = valueList;
} else if (newValue instanceof List) {
List