📄 modelform.java
字号:
altTargets.add(altTarget); } public void addAutoFieldsFromService(AutoFieldsService autoFieldsService, LocalDispatcher dispatcher) { autoFieldsServices.add(autoFieldsService); // read service def and auto-create fields ModelService modelService = null; try { modelService = dispatcher.getDispatchContext().getModelService(autoFieldsService.serviceName); } catch (GenericServiceException e) { String errmsg = "Error finding Service with name " + autoFieldsService.serviceName + " for auto-fields-service in a form widget"; Debug.logError(e, errmsg, module); throw new IllegalArgumentException(errmsg); } List modelParams = modelService.getInModelParamList(); Iterator modelParamIter = modelParams.iterator(); while (modelParamIter.hasNext()) { ModelParam modelParam = (ModelParam) modelParamIter.next(); // skip auto params that the service engine populates... if ("userLogin".equals(modelParam.name) || "locale".equals(modelParam.name)) { continue; } if (modelParam.formDisplay) { if (UtilValidate.isNotEmpty(modelParam.entityName) && UtilValidate.isNotEmpty(modelParam.fieldName)) { ModelEntity modelEntity = delegator.getModelEntity(modelParam.entityName); if (modelEntity != null) { ModelField modelField = modelEntity.getField(modelParam.fieldName); if (modelField != null) { // okay, populate using the entity field info... ModelFormField modelFormField = this.addFieldFromEntityField(modelEntity, modelField, autoFieldsService.defaultFieldType); if (UtilValidate.isNotEmpty(autoFieldsService.mapName)) { modelFormField.setMapName(autoFieldsService.mapName); } // continue to skip creating based on service param continue; } } } ModelFormField modelFormField = this.addFieldFromServiceParam(modelService, modelParam, autoFieldsService.defaultFieldType); if (UtilValidate.isNotEmpty(autoFieldsService.mapName)) { modelFormField.setMapName(autoFieldsService.mapName); } } } } public ModelFormField addFieldFromServiceParam(ModelService modelService, ModelParam modelParam, String defaultFieldType) { // create field def from service param def ModelFormField newFormField = new ModelFormField(this); newFormField.setName(modelParam.name); newFormField.setServiceName(modelService.name); newFormField.setAttributeName(modelParam.name); newFormField.setTitle(modelParam.formLabel); newFormField.induceFieldInfoFromServiceParam(modelService, modelParam, defaultFieldType); return this.addUpdateField(newFormField); } public void addAutoFieldsFromEntity(AutoFieldsEntity autoFieldsEntity, GenericDelegator delegator) { autoFieldsEntities.add(autoFieldsEntity); // read entity def and auto-create fields ModelEntity modelEntity = delegator.getModelEntity(autoFieldsEntity.entityName); if (modelEntity == null) { throw new IllegalArgumentException("Error finding Entity with name " + autoFieldsEntity.entityName + " for auto-fields-entity in a form widget"); } Iterator modelFieldIter = modelEntity.getFieldsIterator(); while (modelFieldIter.hasNext()) { ModelField modelField = (ModelField) modelFieldIter.next(); if (modelField.getIsAutoCreatedInternal()) { // don't ever auto-add these, should only be added if explicitly referenced continue; } ModelFormField modelFormField = this.addFieldFromEntityField(modelEntity, modelField, autoFieldsEntity.defaultFieldType); if (UtilValidate.isNotEmpty(autoFieldsEntity.mapName)) { modelFormField.setMapName(autoFieldsEntity.mapName); } } } public ModelFormField addFieldFromEntityField(ModelEntity modelEntity, ModelField modelField, String defaultFieldType) { // create field def from entity field def ModelFormField newFormField = new ModelFormField(this); newFormField.setName(modelField.getName()); newFormField.setEntityName(modelEntity.getEntityName()); newFormField.setFieldName(modelField.getName()); newFormField.induceFieldInfoFromEntityField(modelEntity, modelField, defaultFieldType); return this.addUpdateField(newFormField); } /** * Renders this form to a String, i.e. in a text format, as defined with the * FormStringRenderer implementation. * * @param buffer The StringBuffer that the form text will be written to * @param context Map containing the form context; the following are * reserved words in this context: parameters (Map), isError (Boolean), * itemIndex (Integer, for lists only, otherwise null), bshInterpreter, * formName (String, optional alternate name for form, defaults to the * value of the name attribute) * @param formStringRenderer An implementation of the FormStringRenderer * interface that is responsible for the actual text generation for * different form elements; implementing you own makes it possible to * use the same form definitions for many types of form UIs */ public void renderFormString(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer) { ModelFormAction.runSubActions(this.actions, context); // if this is a list form, don't useRequestParameters if ("list".equals(this.type) || "multi".equals(this.type)) { context.put("useRequestParameters", Boolean.FALSE); } // find the highest position number to get the max positions used int positions = 1; Iterator fieldIter = this.fieldList.iterator(); while (fieldIter.hasNext()) { ModelFormField modelFormField = (ModelFormField) fieldIter.next(); int curPos = modelFormField.getPosition(); if (curPos > positions) { positions = curPos; } ModelFormField.FieldInfo currentFieldInfo = modelFormField.getFieldInfo(); if (currentFieldInfo != null) { ModelFormField fieldInfoFormField = currentFieldInfo.getModelFormField(); if (fieldInfoFormField != null) { fieldInfoFormField.setModelForm(this); } } else { throw new IllegalArgumentException("Error rendering form, a field has no FieldInfo, ie no sub-element for the type of field for field named: " + modelFormField.getName()); } } if ("single".equals(this.type)) { this.renderSingleFormString(buffer, context, formStringRenderer, positions); } else if ("list".equals(this.type)) { this.renderListFormString(buffer, context, formStringRenderer, positions); } else if ("multi".equals(this.type)) { this.renderMultiFormString(buffer, context, formStringRenderer, positions); } else if ("upload".equals(this.type)) { this.renderSingleFormString(buffer, context, formStringRenderer, positions); } else { throw new IllegalArgumentException("The type " + this.getType() + " is not supported for form with name " + this.getName()); } } public void renderSingleFormString(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer, int positions) { List tempFieldList = FastList.newInstance(); tempFieldList.addAll(this.fieldList); // Check to see if there is a field, same name and same use-when (could come from extended form) for (int j = 0; j < tempFieldList.size(); j++) { ModelFormField modelFormField = (ModelFormField) tempFieldList.get(j); if (!modelFormField.isUseWhenEmpty()) { boolean shouldUse1 = modelFormField.shouldUse(context); for (int i = j+1; i < tempFieldList.size(); i++) { ModelFormField curField = (ModelFormField) tempFieldList.get(i); if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) { boolean shouldUse2 = curField.shouldUse(context); if (shouldUse1 == shouldUse2) { tempFieldList.remove(i--); } } else { continue; } } } } Set alreadyRendered = new TreeSet(); FieldGroup lastFieldGroup = null; // render form open if (!skipStart) formStringRenderer.renderFormOpen(buffer, context, this); // render all hidden & ignored fields this.renderHiddenIgnoredFields(buffer, context, formStringRenderer, alreadyRendered); // render formatting wrapper open // This should be covered by fieldGroup.renderStartString //formStringRenderer.renderFormatSingleWrapperOpen(buffer, context, this); // render each field row, except hidden & ignored rows Iterator fieldIter = tempFieldList.iterator(); ModelFormField lastFormField = null; ModelFormField currentFormField = null; ModelFormField nextFormField = null; if (fieldIter.hasNext()) { currentFormField = (ModelFormField) fieldIter.next(); } if (fieldIter.hasNext()) { nextFormField = (ModelFormField) fieldIter.next(); } FieldGroup currentFieldGroup = null; String currentFieldGroupName = null; String lastFieldGroupName = null; if (currentFormField != null) { currentFieldGroup = (FieldGroup)fieldGroupMap.get(currentFormField.getFieldName()); if (currentFieldGroup == null) { currentFieldGroup = defaultFieldGroup; } if (currentFieldGroup != null) { currentFieldGroupName = currentFieldGroup.getId(); } } boolean isFirstPass = true; boolean haveRenderedOpenFieldRow = false; while (currentFormField != null) { // do the check/get next stuff at the beginning so we can still use the continue stuff easily // don't do it on the first pass though... if (isFirstPass) { isFirstPass = false; List inbetweenList = getInbetweenList(lastFieldGroup, currentFieldGroup); Iterator iter = inbetweenList.iterator(); while (iter.hasNext()) { Object obj = iter.next(); if (obj instanceof ModelForm.Banner) { ((ModelForm.Banner) obj).renderString(buffer, context, formStringRenderer); } else { // no need to open and close an empty table, so skip that call formStringRenderer.renderFieldGroupOpen(buffer, context, (FieldGroup) obj); formStringRenderer.renderFieldGroupClose(buffer, context, (FieldGroup) obj); } } if (currentFieldGroup != null && (lastFieldGroup == null || !lastFieldGroupName.equals(currentFieldGroupName))) { currentFieldGroup.renderStartString(buffer, context, formStringRenderer); lastFieldGroup = currentFieldGroup; } } else { if (fieldIter.hasNext()) { // at least two loops left lastFormField = currentFormField; currentFormField = nextFormField; nextFormField = (ModelFormField) fieldIter.next(); } else if (nextFormField != null) { // okay, just one loop left lastFormField = currentFormField; currentFormField = nextFormField; nextFormField = null; } else { // at the end... lastFormField = currentFormField; currentFormField = null; // nextFormField is already null break; } currentFieldGroup = null; if (currentFormField != null) { currentFieldGroup = (FieldGroup) fieldGroupMap.get(currentFormField.getName()); } if (currentFieldGroup == null) { currentFieldGroup = defaultFieldGroup; } currentFieldGroupName = currentFieldGroup.getId();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -