GXT的表单验证和复杂的多选框操作

今天为老板做了一个功能类似于手机定时闹钟的界面,写的很顺手与大家分享.
演示了如何在GXT通过正则表达式进行验证和复杂的多选框操作


public class Point implements IsWidget, EntryPoint {
/*
 * 数据字典.
 */
public enum Repeat {
    ONCE(0, "仅一次"), MON(2, "周一"), TUE(3, "周二"), WED(4, "周三"), THU(5, "周四"), FRI(
            6, "周五"), SAT(7, "周六"), SUN(1, "周日");
    private int index;
    private String name;

    private Repeat(int index, String name) {
        this.index = index;
        this.name = name;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 根据索引获取对象.
     * 
     * @param index
     * @return
     */
    public static Repeat get(int index) {
        for (Repeat r : Repeat.values()) {
            if (r.getIndex() == index) {
                return r;
            }
        }
        return null;
    }
}

@Override
public void onModuleLoad() {
    RootPanel.get().add(asWidget());
}

@Override
public Widget asWidget() {
    Dialog dialog = new Dialog();
    dialog.setSize("640", "260");
    dialog.setHeadingHtml("Test");

    final FramedPanel panel = new FramedPanel();
    panel.setHeaderVisible(false);
    panel.setBodyStyle("background: none; padding: 10px");
    dialog.add(panel);

    VerticalLayoutContainer p = new VerticalLayoutContainer();
    panel.add(p);

    // 时间,生成一个客户端当前时间
    TextField timeField = new TextField();
    timeField.setAllowBlank(false);
    Date now = new Date();
    // 注意DateTimeFormat包路径
    com.google.gwt.i18n.client.DateTimeFormat fmt = DateTimeFormat
            .getFormat("HH:mm");
    timeField.setValue(fmt.format(now));
    timeField.addValidator(new Validator<String>() {
        @Override
        public List<EditorError> validate(Editor<String> editor,
                String value) {
            List<EditorError> list = new ArrayList<EditorError>();
            // 正则表达式,注意:需要在*.gwt.xml中引入<inherits
            // name="com.google.gwt.regexp.RegExp" />
            RegExp regExp = RegExp
                    .compile("([0-2]\\d|2[0-3]):([0-5][0-9])$");
            if (!regExp.test(value)) {
                EditorError editorError = new DefaultEditorError(editor,
                        "格式不正确!", value);
                list.add(editorError);
                return list;
            }
            return list;
        }
    });
    p.add(new FieldLabel(timeField, "时间"), new VerticalLayoutData(1, -1));

    // 多选框,当选中'仅一次'的时候将其与复选框置为不可用,当选中特定日期时将'仅一次'置为不可用
    CheckBox check;
    final HorizontalPanel hp = new HorizontalPanel();
    ValueChangeHandler<Boolean> repeatHandler = new ValueChangeHandler<Boolean>() {
        @Override
        public void onValueChange(ValueChangeEvent<Boolean> event) {
            CheckBox check = (CheckBox) event.getSource();

            if (check.getItemId().equals("0")) {
                if (check.getValue()) {
                    for (int i = 1; i <= 7; i++) {
                        CheckBox box = (CheckBox) hp.getWidget(i);
                        box.setValue(false);
                        box.setEnabled(false);
                    }
                } else {
                    for (int i = 1; i <= 7; i++) {
                        CheckBox box = (CheckBox) hp.getWidget(i);
                        box.setEnabled(true);
                    }
                }
            } else {
                if (check.getValue()) {
                    CheckBox box = (CheckBox) hp.getWidget(0);
                    box.setValue(false);
                    box.setEnabled(false);
                } else {
                    boolean b = false;
                    for (int i = 1; i <= 7; i++) {
                        CheckBox box = (CheckBox) hp.getWidget(i);
                        if (box.getValue()) {
                            b = true;
                            continue;
                        }
                    }

                    if (!b) {
                        CheckBox box = (CheckBox) hp.getWidget(0);
                        box.setValue(false);
                        box.setEnabled(true);
                    }
                }
            }
        }
    };
    for (int i = 0, end = Repeat.values().length; i < end; i++) {
        check = new CheckBox();
        check.setItemId(String.valueOf(Repeat.values()[i].getIndex()));
        check.setBoxLabel(Repeat.values()[i].getName());

        check.addValueChangeHandler(repeatHandler);
        hp.add(check);
    }
    p.add(new FieldLabel(hp, "重复"));

    return dialog;
}

}

效果图:

你可能感兴趣的:(正则表达式,checkbox,复选框,gxt)