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.RelationalOperator; 015 import org.springframework.rules.constraint.StringLengthConstraint; 016 import org.springframework.rules.constraint.property.PropertyConstraint; 017 import org.springframework.rules.constraint.property.PropertyValueConstraint; 018 019 /** 020 * Dialog showing a number of fields to manipulate the 021 * {@link StringLengthConstraint} and see it in action on the value field. 022 * 023 * @author Jan Hoskens 024 * 025 */ 026 public class StringLenghtConstraintDialog extends TitledApplicationDialog { 027 028 public class StringLengthValue implements PropertyConstraintProvider { 029 030 private int length = 4; 031 032 private RelationalOperator relationalOperator = RelationalOperator.LESS_THAN_EQUAL_TO; 033 034 private int minLength = 2; 035 036 private int maxLength = 4; 037 038 private boolean rangeConstraint = false; 039 040 private String value; 041 042 public int getLength() { 043 return length; 044 } 045 046 public void setLength(int length) { 047 this.length = length; 048 } 049 050 public RelationalOperator getRelationalOperator() { 051 return relationalOperator; 052 } 053 054 public void setRelationalOperator(RelationalOperator relationalOperator) { 055 this.relationalOperator = relationalOperator; 056 } 057 058 public int getMinLength() { 059 return minLength; 060 } 061 062 public void setMinLength(int minLength) { 063 this.minLength = minLength; 064 } 065 066 public int getMaxLength() { 067 return maxLength; 068 } 069 070 public void setMaxLength(int maxLength) { 071 this.maxLength = maxLength; 072 } 073 074 public void setRangeConstraint(boolean rangeConstraint) { 075 this.rangeConstraint = rangeConstraint; 076 } 077 078 public boolean isRangeConstraint() { 079 return rangeConstraint; 080 } 081 082 public String getValue() { 083 return value; 084 } 085 086 public void setValue(String value) { 087 this.value = value; 088 } 089 090 public PropertyConstraint getPropertyConstraint(String propertyName) { 091 if (isRangeConstraint()) { 092 return new PropertyValueConstraint("value", new StringLengthConstraint(minLength, maxLength, 093 "rangeConstraint")); 094 } 095 096 return new PropertyValueConstraint("value", new StringLengthConstraint(relationalOperator, length)); 097 } 098 099 } 100 101 public class StringLengthConstraintForm extends AbstractForm { 102 103 public StringLengthConstraintForm() { 104 super(FormModelHelper.createFormModel(new StringLengthValue(), false, "stringLengthValue")); 105 } 106 107 protected JComponent createFormControl() { 108 TableFormBuilder builder = new TableFormBuilder(getBindingFactory()); 109 builder.add("relationalOperator"); 110 builder.row(); 111 builder.add("length"); 112 builder.row(); 113 builder.add("minLength"); 114 builder.row(); 115 builder.add("maxLength"); 116 builder.row(); 117 builder.add("rangeConstraint"); 118 builder.row(); 119 builder.add("value"); 120 newSingleLineResultsReporter(StringLenghtConstraintDialog.this); 121 JPanel panel = new JPanel(); 122 panel.add(builder.getForm()); 123 panel.add(createValidateCommand().createButton()); 124 return panel; 125 } 126 127 private ActionCommand createValidateCommand() { 128 ActionCommand validateCommand = new ActionCommand("validateCommand") { 129 130 protected void doExecuteCommand() { 131 getFormModel().validate(); 132 } 133 }; 134 ((CommandConfigurer) ApplicationServicesLocator.services().getService(CommandConfigurer.class)) 135 .configure(validateCommand); 136 return validateCommand; 137 } 138 139 } 140 141 protected JComponent createTitledDialogContentPane() { 142 143 return new StringLengthConstraintForm().getControl(); 144 } 145 146 protected boolean onFinish() { 147 return true; 148 } 149 150 }