package cn.cerc.ui.fields;

import cn.cerc.core.Record;
import cn.cerc.ui.core.HtmlWriter;
import cn.cerc.ui.parts.UIComponent;

import java.util.LinkedHashMap;
import java.util.Map;

public class OptionField extends AbstractField {
    private String defaultValue;
    private int size;// 默认显示行数
    private Map<String, String> items = new LinkedHashMap<>();

    public OptionField(UIComponent owner, String name, String field) {
        super(owner, name, 0);
        this.setField(field);
    }

    public OptionField(UIComponent owner, String name, String field, int width) {
        super(owner, name, width);
        this.setField(field);
    }

    public OptionField put(String key, String text) {
        if (this.defaultValue == null) {
            defaultValue = key;
        }
        items.put(key, text);
        return this;
    }

    public OptionField copyValues(Map<String, String> items) {
        for (String key : items.keySet()) {
            this.put(key, items.get(key));
        }
        return this;
    }

    @Override
    public String getText(Record record) {
        if (record == null) {
            return null;
        }
        if (buildText != null) {
            HtmlWriter html = new HtmlWriter();
            buildText.outputText(record, html);
            return html.toString();
        }
        return record.getString(getField());
    }

    @Override
    public String getString() {
        String result = super.getString();
        if (result == null || "".equals(result)) {
            return this.defaultValue;
        }
        return result;
    }

    @Override
    public void output(HtmlWriter html) {
        Record record = dataSource != null ? dataSource.getDataSet().getCurrent() : null;
        String current = this.getText(record);
        html.println("<label for=\"%s\">%s</label>", this.getId(), this.getName() + "：");
        html.print("<select id=\"%s\" name=\"%s\"", this.getId(), this.getId());
        if (this.size > 0) {
            html.print(" size=\"%s\"", this.getSize());
        }
        if (this.isReadonly()) {
            html.print(" disabled");
        }
        if (this.getCssStyle() != null) {
            html.print(" style=\"%s\"", this.getCssStyle());
        }
        html.print(">");
        for (String key : items.keySet()) {
            String value = items.get(key);
            html.print("<option value=\"%s\"", key);
            if (key.equals(current)) {
                html.print(" selected");
            }
            html.print(">");
            html.println(String.format("%s</option>", value));
        }
        html.println("</select>");
        if (this.isShowStar()) {
            html.print("<font>*</font>");
        }
        html.print("<span></span>");
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

}
@@ -639,32 +639,145 @@
 (field);%0A    %7D%0A%0A
+    @Deprecated%0A    public OptionField add(String key, String text) %7B%0A        return this.put(key, text);%0A    %7D%0A%0A
     public Optio

Update OptionField.java