001 package org.springframework.richclient.samples.showcase.validation;
002
003 import javax.swing.JComponent;
004 import javax.swing.JPanel;
005
006 import org.springframework.richclient.application.ApplicationServicesLocator;
007 import org.springframework.richclient.command.ActionCommand;
008 import org.springframework.richclient.command.config.CommandConfigurer;
009 import org.springframework.richclient.dialog.TitledApplicationDialog;
010 import org.springframework.richclient.form.AbstractForm;
011 import org.springframework.richclient.form.FormModelHelper;
012 import org.springframework.richclient.form.builder.TableFormBuilder;
013 import org.springframework.rules.PropertyConstraintProvider;
014 import org.springframework.rules.constraint.RegexpConstraint;
015 import org.springframework.rules.constraint.property.PropertyConstraint;
016 import org.springframework.rules.constraint.property.PropertyValueConstraint;
017 import org.springframework.util.StringUtils;
018
019 /**
020 * Show a basic dialog with a regular expression input field and a value input
021 * field. When entering a new regular expression, user can evaluate expression.
022 *
023 * @author Jan Hoskens
024 *
025 */
026 public class BasicRegExpConstraintDialog extends TitledApplicationDialog {
027
028 /**
029 * This object is used as formObject. It contains a value to set, the
030 * regular expression that should be used while validating that value and
031 * the logic to get the {@link PropertyConstraint} for the value based on
032 * the regexp.
033 */
034 private class RegExpValue implements PropertyConstraintProvider {
035 private String regExp = "";
036
037 private String value = "";
038
039 public String getRegExp() {
040 return regExp;
041 }
042
043 public void setRegExp(String regExp) {
044 this.regExp = regExp;
045 }
046
047 public String getValue() {
048 return value;
049 }
050
051 public void setValue(String value) {
052 this.value = value;
053 }
054
055 public PropertyConstraint getPropertyConstraint(String propertyName) {
056 if (StringUtils.hasText(regExp))
057 {
058 if ((propertyName == null) || ("value".equals(propertyName))) {
059 return new PropertyValueConstraint("value", new RegexpConstraint(regExp, "regExpViolated"));
060 }
061 }
062 return null;
063 }
064
065 }
066
067 private class RegExpForm extends AbstractForm {
068
069 public RegExpForm() {
070 super(FormModelHelper.createFormModel(new RegExpValue(), false));
071 }
072
073 protected JComponent createFormControl() {
074 TableFormBuilder builder = new TableFormBuilder(getBindingFactory());
075 builder.add("regExp");
076 builder.row();
077 builder.add("value");
078 newSingleLineResultsReporter(BasicRegExpConstraintDialog.this);
079 JPanel panel = new JPanel();
080 panel.add(builder.getForm());
081 panel.add(createValidateCommand().createButton());
082 return panel;
083 }
084
085 private ActionCommand createValidateCommand() {
086 ActionCommand validateCommand = new ActionCommand("validateCommand") {
087
088 protected void doExecuteCommand() {
089 getFormModel().validate();
090 }
091 };
092 ((CommandConfigurer) ApplicationServicesLocator.services().getService(CommandConfigurer.class))
093 .configure(validateCommand);
094 return validateCommand;
095 }
096 }
097
098 protected JComponent createTitledDialogContentPane() {
099 return new RegExpForm().getControl();
100 }
101
102 protected boolean onFinish() {
103 return true;
104 }
105
106 }