Search in sources :

Example 1 with Options

use of org.apache.empire.commons.Options in project empire-db by apache.

the class InputControl method formatValue.

/**
 * Returns the value formated as a string
 * this is a simple default implementation that does no type-secific formatting
 * Derived classes may override formatString an provide further formmatting
 * see TextInputControl for details
 *
 * @param value
 *            the value to be formatted
 * @param vi
 *            Meta-information about the value
 * @return the formatted value
 */
protected String formatValue(Object value, ValueInfo vi) {
    // For Enums use toString() to retrieve Value
    if ((value instanceof Enum<?>) && !hasFormatOption(vi, "nolookup")) {
        // Handle enum
        String text = ((Enum<?>) value).toString();
        if (text != null)
            return vi.getText(text);
        // Error
        InputControl.log.error("The enum '" + ((Enum<?>) value).name() + "' has no text!");
    }
    // Lookup and Print value
    Options options = vi.getOptions();
    if (options != null && !options.isEmpty() && !hasFormatOption(vi, "nolookup")) {
        // Check for Options
        OptionEntry entry = options.getEntry(value);
        if (entry != null)
            return vi.getText(entry.getText());
        // Error
        if (value != null)
            InputControl.log.error("The element '" + String.valueOf(value) + "' is not part of the supplied option list.");
    }
    // value
    if (value == null)
        value = getFormatOption(vi, InputControl.FORMAT_NULL, InputControl.FORMAT_NULL_ATTRIBUTE);
    // Convert to String
    String s = StringUtils.toString(value, "");
    if (hasFormatOption(vi, "noencode"))
        return s;
    // Encode Html
    return escapeHTML(s);
}
Also used : Options(org.apache.empire.commons.Options) OptionEntry(org.apache.empire.commons.OptionEntry)

Example 2 with Options

use of org.apache.empire.commons.Options in project empire-db by apache.

the class RadioInputControl method createInputComponents.

@Override
protected void createInputComponents(UIComponent parent, InputInfo ii, FacesContext context, List<UIComponent> compList) {
    if (!compList.isEmpty())
        throw new InvalidArgumentException("compList", compList);
    // create
    HtmlSelectOneRadio input = InputControlManager.createComponent(context, this.inputComponentClass);
    // setValueExpressionFlag
    Object value = ii.getValue(false);
    input.getAttributes().put(RadioInputControl.VALUE_EXPRESSION_FLAG, (value instanceof ValueExpression));
    // copy Attributes
    copyAttributes(parent, ii, input);
    // disabled
    boolean disabled = ii.isDisabled();
    input.setDisabled(disabled);
    // Options
    Options options = ii.getOptions();
    boolean addEmpty = getEmptyEntryRequired(ii, disabled) && !options.containsNull();
    String nullText = (addEmpty) ? getNullText(ii) : "";
    initOptions(input, ii.getTextResolver(), options, addEmpty, nullText);
    // add
    compList.add(input);
    // style
    addRemoveDisabledStyle(input, disabled);
    addRemoveInvalidStyle(input, ii.hasError());
    // Set Value
    setInputValue(input, ii);
}
Also used : Options(org.apache.empire.commons.Options) InvalidArgumentException(org.apache.empire.exceptions.InvalidArgumentException) ValueExpression(javax.el.ValueExpression) HtmlSelectOneRadio(javax.faces.component.html.HtmlSelectOneRadio)

Example 3 with Options

use of org.apache.empire.commons.Options in project empire-db by apache.

the class RadioInputControl method renderValue.

/* Value */
@Override
public void renderValue(ValueInfo vi, ResponseWriter writer) throws IOException {
    Object value = vi.getValue(true);
    String style = vi.getStyleClass("eCtlRadio") + " eInpDis";
    writer.startElement(HTML_TAG_DIV, null);
    writer.writeAttribute(HTML_ATTR_CLASS, style, null);
    writer.startElement(HTML_TAG_TABLE, null);
    writer.writeAttribute(HTML_ATTR_CLASS, style, null);
    writer.startElement(HTML_TAG_TR, null);
    Options o = vi.getOptions();
    for (OptionEntry e : o) {
        writer.startElement(HTML_TAG_TD, null);
        // input
        writer.startElement(HTML_TAG_INPUT, null);
        writer.writeAttribute(HTML_ATTR_TYPE, "radio", null);
        writer.writeAttribute(HTML_ATTR_DISABLED, "disabled", null);
        if (ObjectUtils.compareEqual(e.getValue(), value))
            writer.writeAttribute(HTML_ATTR_CHECKED, "checked", null);
        writer.endElement(HTML_TAG_INPUT);
        // label
        writer.startElement(HTML_TAG_LABEL, null);
        writer.writeAttribute(HTML_ATTR_CLASS, "eCtlRadio", null);
        String text = e.getText();
        text = vi.getTextResolver().resolveText(text);
        writer.writeText(text, null);
        writer.endElement(HTML_TAG_LABEL);
        // end
        writer.endElement(HTML_TAG_TD);
    }
    writer.endElement(HTML_TAG_TR);
    writer.endElement(HTML_TAG_TABLE);
    writer.endElement(HTML_TAG_DIV);
}
Also used : Options(org.apache.empire.commons.Options) OptionEntry(org.apache.empire.commons.OptionEntry)

Example 4 with Options

use of org.apache.empire.commons.Options in project empire-db by apache.

the class SelectInputControl method syncOptions.

public void syncOptions(UISelectOne input, TextResolver textResolver, InputInfo ii) {
    // get the options
    Options options = ii.getOptions();
    if (options == null) {
        // clear or not?
        if (ii.getValue(false) != null)
            log.warn("No options have been set for column {}", ii.getColumn().getName());
        else
            input.getChildren().clear();
        return;
    }
    Object currentValue = ii.getValue(true);
    boolean hasEmpty = isEmptyEntryRequired(input, options, ii, currentValue);
    // boolean isInsideUIData = ii.isInsideUIData();
    // Compare child-items with options
    Iterator<OptionEntry> ioe = options.iterator();
    OptionEntry oe = (ioe.hasNext() ? ioe.next() : null);
    List<UIComponent> childList = input.getChildren();
    Iterator<UIComponent> ico = childList.iterator();
    int lastIndex = 0;
    boolean emptyPresent = false;
    while (ico.hasNext()) {
        lastIndex++;
        UIComponent co = ico.next();
        if (!(co instanceof UISelectItem))
            continue;
        UISelectItem si = (UISelectItem) co;
        Object ov = si.getItemValue();
        if (ObjectUtils.isEmpty(ov) && hasEmpty) {
            emptyPresent = true;
            continue;
        }
        // skip inactive
        while (oe != null && !oe.isActive()) {
            // check for current
            if (ObjectUtils.compareEqual(oe.getValue(), currentValue))
                break;
            // next oe
            oe = (ioe.hasNext() ? ioe.next() : null);
        }
        if (oe == null) {
            // remove obsolete items
            lastIndex--;
            for (int index = childList.size() - 1; index >= lastIndex; index--) childList.remove(index);
            // done
            return;
        }
        if (ObjectUtils.compareEqual(ov, oe.getValue())) {
            // next
            String label = oe.getText();
            si.setItemLabel(textResolver.resolveText(label));
            oe = (ioe.hasNext() ? ioe.next() : null);
            continue;
        }
        // Not equal - do a full reload
        input.getChildren().clear();
        if (hasEmpty) {
            // add empty entry
            addSelectItem(input, textResolver, new OptionEntry("", getNullText(ii)));
        }
        for (OptionEntry opt : options) {
            // Option entries
            if (opt.isActive() || ObjectUtils.compareEqual(opt.getValue(), currentValue)) {
                // add active or current item
                addSelectItem(input, textResolver, opt);
            }
        }
        // done
        return;
    }
    // check empty entry
    if (hasEmpty && !emptyPresent) {
        // add missing empty entry
        addSelectItem(input, textResolver, new OptionEntry("", getNullText(ii)), 0);
    }
    // Are there any items left?
    while (oe != null) {
        // add missing item
        if (oe.isActive() || ObjectUtils.compareEqual(oe.getValue(), currentValue)) {
            // add item
            addSelectItem(input, textResolver, oe);
        }
        oe = (ioe.hasNext() ? ioe.next() : null);
    }
}
Also used : Options(org.apache.empire.commons.Options) OptionEntry(org.apache.empire.commons.OptionEntry) UIComponent(javax.faces.component.UIComponent) UISelectItem(javax.faces.component.UISelectItem)

Example 5 with Options

use of org.apache.empire.commons.Options in project empire-db by apache.

the class SelectInputControl method initOptions.

public void initOptions(UISelectOne input, TextResolver textResolver, InputInfo ii) {
    // get the options
    Options options = ii.getOptions();
    if (options == null) {
        // invalid options
        if (ii.getColumn() != null)
            log.warn("No options given for column {}", ii.getColumn().getName());
        else
            log.warn("No options given for select tag {}", input.getClientId());
        options = new Options();
    }
    // current
    Object currentValue = ii.getValue(true);
    if (isEmptyEntryRequired(input, options, ii, currentValue)) {
        // Empty entry
        addSelectItem(input, textResolver, new OptionEntry(null, getNullText(ii)));
    }
    if (options != null && options.size() > 0) {
        // Add options
        for (OptionEntry oe : options) {
            // Option entries
            if (oe.isActive() || ObjectUtils.compareEqual(oe.getValue(), currentValue)) {
                // add active or current item
                addSelectItem(input, textResolver, oe);
            } else if (log.isDebugEnabled()) {
                // not active, ignore this one
                log.debug("Select item {} is not active.", oe.getValue());
            }
        }
    }
}
Also used : Options(org.apache.empire.commons.Options) OptionEntry(org.apache.empire.commons.OptionEntry)

Aggregations

Options (org.apache.empire.commons.Options)17 OptionEntry (org.apache.empire.commons.OptionEntry)6 UIComponent (javax.faces.component.UIComponent)2 HtmlSelectOneRadio (javax.faces.component.html.HtmlSelectOneRadio)2 Column (org.apache.empire.data.Column)2 DBColumn (org.apache.empire.db.DBColumn)2 TextResolver (org.apache.empire.rest.app.TextResolver)2 JsoColumnMeta (org.apache.empire.rest.json.JsoColumnMeta)2 DateFormat (java.text.DateFormat)1 NumberFormat (java.text.NumberFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ValueExpression (javax.el.ValueExpression)1 UIInput (javax.faces.component.UIInput)1 UISelectItem (javax.faces.component.UISelectItem)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 ColumnExpr (org.apache.empire.data.ColumnExpr)1 DataType (org.apache.empire.data.DataType)1 RecordData (org.apache.empire.data.RecordData)1