Search in sources :

Example 1 with ModelFormFieldBuilder

use of org.apache.ofbiz.widget.model.ModelFormFieldBuilder in project ofbiz-framework by apache.

the class MacroFormRenderer method renderDateTimeField.

public void renderDateTimeField(Appendable writer, Map<String, Object> context, DateTimeField dateTimeField) throws IOException {
    ModelFormField modelFormField = dateTimeField.getModelFormField();
    String paramName = modelFormField.getParameterName(context);
    String defaultDateTimeString = dateTimeField.getDefaultDateTimeString(context);
    String className = "";
    String alert = "false";
    String name = "";
    String formattedMask = "";
    String event = modelFormField.getEvent();
    String action = modelFormField.getAction(context);
    if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
        className = modelFormField.getWidgetStyle();
        if (modelFormField.shouldBeRed(context)) {
            alert = "true";
        }
    }
    boolean useTimeDropDown = "time-dropdown".equals(dateTimeField.getInputMethod());
    String stepString = dateTimeField.getStep();
    int step = 1;
    StringBuilder timeValues = new StringBuilder();
    if (useTimeDropDown && UtilValidate.isNotEmpty(step)) {
        try {
            step = Integer.parseInt(stepString);
        } catch (IllegalArgumentException e) {
            Debug.logWarning("Invalid value for step property for field[" + paramName + "] with input-method=\"time-dropdown\" " + " Found Value [" + stepString + "]  " + e.getMessage(), module);
        }
        timeValues.append("[");
        for (int i = 0; i <= 59; ) {
            if (i != 0) {
                timeValues.append(", ");
            }
            timeValues.append(i);
            i += step;
        }
        timeValues.append("]");
    }
    Map<String, String> uiLabelMap = UtilGenerics.checkMap(context.get("uiLabelMap"));
    if (uiLabelMap == null) {
        Debug.logWarning("Could not find uiLabelMap in context", module);
    }
    String localizedInputTitle = "", localizedIconTitle = "";
    // whether the date field is short form, yyyy-mm-dd
    boolean shortDateInput = ("date".equals(dateTimeField.getType()) || useTimeDropDown ? true : false);
    if (useTimeDropDown) {
        name = UtilHttp.makeCompositeParam(paramName, "date");
    } else {
        name = paramName;
    }
    // the default values for a timestamp
    int size = 25;
    int maxlength = 30;
    if (shortDateInput) {
        size = maxlength = 10;
        if (uiLabelMap != null) {
            localizedInputTitle = uiLabelMap.get("CommonFormatDate");
        }
    } else if ("time".equals(dateTimeField.getType())) {
        size = maxlength = 8;
        if (uiLabelMap != null) {
            localizedInputTitle = uiLabelMap.get("CommonFormatTime");
        }
    } else {
        if (uiLabelMap != null) {
            localizedInputTitle = uiLabelMap.get("CommonFormatDateTime");
        }
    }
    /*
         * FIXME: Using a builder here is a hack. Replace the builder with appropriate code.
         */
    ModelFormFieldBuilder builder = new ModelFormFieldBuilder(modelFormField);
    boolean memEncodeOutput = modelFormField.getEncodeOutput();
    if (useTimeDropDown) {
        // If time-dropdown deactivate encodingOutput for found hour and minutes
        // FIXME: Encoding should be controlled by the renderer, not by the model.
        builder.setEncodeOutput(false);
    }
    // FIXME: modelFormField.getEntry ignores shortDateInput when converting Date objects to Strings.
    if (useTimeDropDown) {
        builder.setEncodeOutput(memEncodeOutput);
    }
    modelFormField = builder.build();
    String contextValue = modelFormField.getEntry(context, dateTimeField.getDefaultValue(context));
    String value = contextValue;
    if (UtilValidate.isNotEmpty(value)) {
        if (value.length() > maxlength) {
            value = value.substring(0, maxlength);
        }
    }
    String id = modelFormField.getCurrentContainerId(context);
    ModelForm modelForm = modelFormField.getModelForm();
    String formName = FormRenderer.getCurrentFormName(modelForm, context);
    String timeDropdown = dateTimeField.getInputMethod();
    String timeDropdownParamName = "";
    String classString = "";
    boolean isTwelveHour = false;
    String timeHourName = "";
    int hour2 = 0, hour1 = 0, minutes = 0;
    String timeMinutesName = "";
    String amSelected = "", pmSelected = "", ampmName = "";
    String compositeType = "";
    // search for a localized label for the icon
    if (uiLabelMap != null) {
        localizedIconTitle = uiLabelMap.get("CommonViewCalendar");
    }
    if (!"time".equals(dateTimeField.getType())) {
        String tempParamName;
        if (useTimeDropDown) {
            tempParamName = UtilHttp.makeCompositeParam(paramName, "date");
        } else {
            tempParamName = paramName;
        }
        timeDropdownParamName = tempParamName;
        defaultDateTimeString = UtilHttp.encodeBlanks(modelFormField.getEntry(context, defaultDateTimeString));
    }
    // dropdowns
    if (useTimeDropDown) {
        className = modelFormField.getWidgetStyle();
        classString = (className != null ? className : "");
        isTwelveHour = "12".equals(dateTimeField.getClock());
        // set the Calendar to the default time of the form or now()
        Calendar cal = null;
        try {
            Timestamp defaultTimestamp = Timestamp.valueOf(contextValue);
            cal = Calendar.getInstance();
            cal.setTime(defaultTimestamp);
        } catch (IllegalArgumentException e) {
            Debug.logWarning("Form widget field [" + paramName + "] with input-method=\"time-dropdown\" was not able to understand the default time [" + defaultDateTimeString + "]. The parsing error was: " + e.getMessage(), module);
        }
        timeHourName = UtilHttp.makeCompositeParam(paramName, "hour");
        if (cal != null) {
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            hour2 = hour;
            if (hour == 0) {
                hour = 12;
            }
            if (hour > 12) {
                hour -= 12;
            }
            hour1 = hour;
            minutes = cal.get(Calendar.MINUTE);
        }
        timeMinutesName = UtilHttp.makeCompositeParam(paramName, "minutes");
        compositeType = UtilHttp.makeCompositeParam(paramName, "compositeType");
        // if 12 hour clock, write the AM/PM selector
        if (isTwelveHour) {
            amSelected = ((cal != null && cal.get(Calendar.AM_PM) == Calendar.AM) ? "selected" : "");
            pmSelected = ((cal != null && cal.get(Calendar.AM_PM) == Calendar.PM) ? "selected" : "");
            ampmName = UtilHttp.makeCompositeParam(paramName, "ampm");
        }
    }
    // check for required field style on single forms
    if ("single".equals(modelFormField.getModelForm().getType()) && modelFormField.getRequiredField()) {
        String requiredStyle = modelFormField.getRequiredFieldStyle();
        if (UtilValidate.isEmpty(requiredStyle)) {
            requiredStyle = "required";
        }
        if (UtilValidate.isEmpty(className)) {
            className = requiredStyle;
        } else {
            className = requiredStyle + " " + className;
        }
    }
    String mask = dateTimeField.getMask();
    if ("Y".equals(mask)) {
        if ("date".equals(dateTimeField.getType())) {
            formattedMask = "9999-99-99";
        } else if ("time".equals(dateTimeField.getType())) {
            formattedMask = "99:99:99";
        } else if ("timestamp".equals(dateTimeField.getType())) {
            formattedMask = "9999-99-99 99:99:99";
        }
    }
    String tabindex = modelFormField.getTabindex();
    StringWriter sr = new StringWriter();
    sr.append("<@renderDateTimeField ");
    sr.append("name=\"");
    sr.append(name);
    sr.append("\" className=\"");
    sr.append(className);
    sr.append("\" alert=\"");
    sr.append(alert);
    sr.append("\" value=\"");
    sr.append(value);
    sr.append("\" title=\"");
    sr.append(localizedInputTitle);
    sr.append("\" size=\"");
    sr.append(Integer.toString(size));
    sr.append("\" maxlength=\"");
    sr.append(Integer.toString(maxlength));
    sr.append("\" step=\"");
    sr.append(Integer.toString(step));
    sr.append("\" timeValues=\"");
    sr.append(timeValues.toString());
    sr.append("\" id=\"");
    sr.append(id);
    sr.append("\" event=\"");
    sr.append(event);
    sr.append("\" action=\"");
    sr.append(action);
    sr.append("\" dateType=\"");
    sr.append(dateTimeField.getType());
    sr.append("\" shortDateInput=");
    sr.append(Boolean.toString(shortDateInput));
    sr.append(" timeDropdownParamName=\"");
    sr.append(timeDropdownParamName);
    sr.append("\" defaultDateTimeString=\"");
    sr.append(defaultDateTimeString);
    sr.append("\" localizedIconTitle=\"");
    sr.append(localizedIconTitle);
    sr.append("\" timeDropdown=\"");
    sr.append(timeDropdown);
    sr.append("\" timeHourName=\"");
    sr.append(timeHourName);
    sr.append("\" classString=\"");
    sr.append(classString);
    sr.append("\" hour1=");
    sr.append(Integer.toString(hour1));
    sr.append(" hour2=");
    sr.append(Integer.toString(hour2));
    sr.append(" timeMinutesName=\"");
    sr.append(timeMinutesName);
    sr.append("\" minutes=");
    sr.append(Integer.toString(minutes));
    sr.append(" isTwelveHour=");
    sr.append(Boolean.toString(isTwelveHour));
    sr.append(" ampmName=\"");
    sr.append(ampmName);
    sr.append("\" amSelected=\"");
    sr.append(amSelected);
    sr.append("\" pmSelected=\"");
    sr.append(pmSelected);
    sr.append("\" compositeType=\"");
    sr.append(compositeType);
    sr.append("\" formName=\"");
    sr.append(formName);
    sr.append("\" mask=\"");
    sr.append(formattedMask);
    sr.append("\" tabindex=\"");
    sr.append(tabindex);
    sr.append("\" />");
    executeMacro(writer, sr.toString());
    this.addAsterisks(writer, context, modelFormField);
    this.appendTooltip(writer, context, modelFormField);
}
Also used : StringWriter(java.io.StringWriter) ModelFormField(org.apache.ofbiz.widget.model.ModelFormField) Calendar(com.ibm.icu.util.Calendar) ModelFormFieldBuilder(org.apache.ofbiz.widget.model.ModelFormFieldBuilder) Timestamp(java.sql.Timestamp) ModelForm(org.apache.ofbiz.widget.model.ModelForm)

Aggregations

Calendar (com.ibm.icu.util.Calendar)1 StringWriter (java.io.StringWriter)1 Timestamp (java.sql.Timestamp)1 ModelForm (org.apache.ofbiz.widget.model.ModelForm)1 ModelFormField (org.apache.ofbiz.widget.model.ModelFormField)1 ModelFormFieldBuilder (org.apache.ofbiz.widget.model.ModelFormFieldBuilder)1