1 14 15 package com.sun.facelets.tag.jsf; 16 17 import javax.faces.component.EditableValueHolder; 18 import javax.faces.component.UIComponent; 19 import javax.faces.context.FacesContext; 20 import javax.faces.event.MethodExpressionValueChangeListener; 21 import javax.faces.event.ValueChangeEvent; 22 import javax.faces.validator.MethodExpressionValidator; 23 24 import com.sun.facelets.FaceletContext; 25 import com.sun.facelets.el.LegacyMethodBinding; 26 import com.sun.facelets.tag.TagAttribute; 27 import com.sun.facelets.tag.Metadata; 28 import com.sun.facelets.tag.MetaRule; 29 import com.sun.facelets.tag.MetadataTarget; 30 import com.sun.facelets.util.FacesAPI; 31 32 37 public final class EditableValueHolderRule extends MetaRule { 38 39 final static class LiteralValidatorMetadata extends Metadata { 40 41 private final String validatorId; 42 43 public LiteralValidatorMetadata(String validatorId) { 44 this.validatorId = validatorId; 45 } 46 47 public void applyMetadata(FaceletContext ctx, Object instance) { 48 ((EditableValueHolder) instance).addValidator(ctx.getFacesContext() 49 .getApplication().createValidator(this.validatorId)); 50 } 51 } 52 53 final static class ValueChangedExpressionMetadata extends Metadata { 54 private final TagAttribute attr; 55 56 public ValueChangedExpressionMetadata(TagAttribute attr) { 57 this.attr = attr; 58 } 59 60 public void applyMetadata(FaceletContext ctx, Object instance) { 61 ((EditableValueHolder) instance) 62 .addValueChangeListener(new MethodExpressionValueChangeListener( 63 this.attr.getMethodExpression(ctx, null, 64 VALUECHANGE_SIG))); 65 } 66 } 67 68 final static class ValueChangedBindingMetadata extends Metadata { 69 private final TagAttribute attr; 70 71 public ValueChangedBindingMetadata(TagAttribute attr) { 72 this.attr = attr; 73 } 74 75 public void applyMetadata(FaceletContext ctx, Object instance) { 76 ((EditableValueHolder) instance) 77 .setValueChangeListener(new LegacyMethodBinding(this.attr 78 .getMethodExpression(ctx, null, VALUECHANGE_SIG))); 79 } 80 } 81 82 final static class ValidatorExpressionMetadata extends Metadata { 83 private final TagAttribute attr; 84 85 public ValidatorExpressionMetadata(TagAttribute attr) { 86 this.attr = attr; 87 } 88 89 public void applyMetadata(FaceletContext ctx, Object instance) { 90 ((EditableValueHolder) instance) 91 .addValidator(new MethodExpressionValidator(this.attr 92 .getMethodExpression(ctx, null, VALIDATOR_SIG))); 93 } 94 } 95 96 final static class ValidatorBindingMetadata extends Metadata { 97 private final TagAttribute attr; 98 99 public ValidatorBindingMetadata(TagAttribute attr) { 100 this.attr = attr; 101 } 102 103 public void applyMetadata(FaceletContext ctx, Object instance) { 104 ((EditableValueHolder) instance) 105 .setValidator(new LegacyMethodBinding(this.attr 106 .getMethodExpression(ctx, null, VALIDATOR_SIG))); 107 } 108 } 109 110 private final static Class [] VALIDATOR_SIG = new Class [] { 111 FacesContext.class, UIComponent.class, Object .class }; 112 113 private final static Class [] VALUECHANGE_SIG = new Class [] { ValueChangeEvent.class }; 114 115 public final static EditableValueHolderRule Instance = new EditableValueHolderRule(); 116 117 public Metadata applyRule(String name, TagAttribute attribute, 118 MetadataTarget meta) { 119 120 if (meta.isTargetInstanceOf(EditableValueHolder.class)) { 121 122 boolean elSupport = FacesAPI.getComponentVersion(meta 123 .getTargetClass()) >= 12; 124 125 if ("validator".equals(name)) { 126 if (attribute.isLiteral()) { 127 return new LiteralValidatorMetadata(attribute.getValue()); 128 } else if (elSupport) { 129 return new ValidatorExpressionMetadata(attribute); 130 } else { 131 return new ValidatorBindingMetadata(attribute); 132 } 133 } 134 135 if ("valueChangeListener".equals(name)) { 136 if (elSupport) { 137 return new ValueChangedExpressionMetadata(attribute); 138 } else { 139 return new ValueChangedBindingMetadata(attribute); 140 } 141 } 142 143 } 144 return null; 145 } 146 147 } 148 | Popular Tags |