Author: ehillenius Date: Tue Jun 5 08:27:38 2007 New Revision: 544518 URL: http://svn.apache.org/viewvc?view=rev&rev=544518 Log: sorted members
Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java?view=diff&rev=544518&r1=544517&r2=544518 ============================================================================== --- incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java (original) +++ incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java Tue Jun 5 08:27:38 2007 @@ -78,6 +78,26 @@ public abstract class FormComponent extends WebMarkupContainer implements IFormVisitorParticipant { /** + * Visitor for traversing form components + */ + public static abstract class AbstractVisitor implements IVisitor + { + /** + * @see org.apache.wicket.markup.html.form.FormComponent.IVisitor#formComponent(org.apache.wicket.markup.html.form.FormComponent) + */ + public Object formComponent(IFormVisitorParticipant component) + { + if (component instanceof FormComponent) + { + onFormComponent((FormComponent)component); + } + return Component.IVisitor.CONTINUE_TRAVERSAL; + } + + protected abstract void onFormComponent(FormComponent formComponent); + } + + /** * Typesafe interface to code that is called when visiting a form component. */ public static interface IVisitor @@ -93,23 +113,156 @@ } /** - * Visitor for traversing form components + * [EMAIL PROTECTED] IErrorMessageSource} used for error messags against this form + * components. + * + * @author ivaynberg */ - public static abstract class AbstractVisitor implements IVisitor + private class MessageSource implements IErrorMessageSource { + /** - * @see org.apache.wicket.markup.html.form.FormComponent.IVisitor#formComponent(org.apache.wicket.markup.html.form.FormComponent) + * @see org.apache.wicket.validation.IErrorMessageSource#getMessage(java.lang.String) */ - public Object formComponent(IFormVisitorParticipant component) + public String getMessage(String key) { - if (component instanceof FormComponent) + final FormComponent formComponent = FormComponent.this; + + // retrieve prefix that will be used to construct message keys + String prefix = formComponent.getValidatorKeyPrefix(); + if (Strings.isEmpty(prefix)) { - onFormComponent((FormComponent)component); + prefix = ""; } - return Component.IVisitor.CONTINUE_TRAVERSAL; + + final Localizer localizer = formComponent.getLocalizer(); + + String resource = prefix + getId() + "." + key; + + // First use the parent for resolving so that + // form1.textfield1.Required can be used. + + // Note: It is important that the default value of "" is provided + // to getString() not to throw a MissingResourceException or to + // return a default string like "[Warning: String ..." + String message = localizer.getString(resource, formComponent.getParent(), ""); + + // If not found, than ... + if (Strings.isEmpty(message)) + { + // Try a variation of the resource key + + resource = prefix + key; + + message = localizer.getString(resource, formComponent.getParent(), ""); + } + + if (Strings.isEmpty(message)) + { + // If still empty then use default + + resource = prefix + getId() + "." + key; + + // Note: It is important that the default value of "" is + // provided + // to getString() not to throw a MissingResourceException or to + // return a default string like "[Warning: String ..." + message = localizer.getString(resource, formComponent, ""); + + // If not found, than ... + if (Strings.isEmpty(message)) + { + // Try a variation of the resource key + + resource = prefix + key; + + message = localizer.getString(resource, formComponent, ""); + } + } + + // convert empty string to null in case our default value of "" was + // returned from localizer + if (Strings.isEmpty(message)) + { + message = null; + } + return message; } - protected abstract void onFormComponent(FormComponent formComponent); + /** + * @see org.apache.wicket.validation.IErrorMessageSource#substitute(java.lang.String, + * java.util.Map) + */ + public String substitute(String string, Map vars) throws IllegalStateException + { + return new MapVariableInterpolator(string, addDefaultVars(vars), true).toString(); + } + + /** + * Creates a new params map that additionaly contains the default input, + * name, label parameters + * + * @param params + * original params map + * @return new params map + */ + private Map addDefaultVars(Map params) + { + // create and fill the new params map + final HashMap fullParams; + if (params == null) + { + fullParams = new HashMap(6); + } + else + { + fullParams = new HashMap(params.size() + 6); + fullParams.putAll(params); + } + + // add the input param if not already present + if (!fullParams.containsKey("input")) + { + fullParams.put("input", FormComponent.this.getInput()); + } + + // add the name param if not already present + if (!fullParams.containsKey("name")) + { + fullParams.put("name", FormComponent.this.getId()); + } + + // add the label param if not already present + if (!fullParams.containsKey("label")) + { + fullParams.put("label", getLabel()); + } + return fullParams; + } + + + /** + * @return value of label param for this form component + */ + private Object getLabel() + { + final FormComponent fc = FormComponent.this; + Object label = null; + + // first try the label model ... + if (fc.getLabel() != null) + { + label = fc.getLabel().getObject(); + } + // ... then try a resource of format [form-component-id] with + // default of '[form-component-id]' + if (label == null) + { + + label = fc.getLocalizer().getString(fc.getId(), fc.getParent(), fc.getId()); + } + return label; + } } /** @@ -133,15 +286,40 @@ } /** - * The value separator + * Adapter that makes this component appear as [EMAIL PROTECTED] IValidatable} + * + * @author ivaynberg */ - public static String VALUE_SEPARATOR = ";"; + private class ValidatableAdapter implements IValidatable + { + + /** + * @see org.apache.wicket.validation.IValidatable#error(org.apache.wicket.validation.IValidationError) + */ + public void error(IValidationError error) + { + FormComponent.this.error(error); + } + + /** + * @see org.apache.wicket.validation.IValidatable#getValue() + */ + public Object getValue() + { + return FormComponent.this.getConvertedInput(); + } + + public boolean isValid() + { + return FormComponent.this.isValid(); + } + + } /** - * Make empty strings null values boolean. Used by AbstractTextComponent - * subclass. + * The value separator */ - protected static final short FLAG_CONVERT_EMPTY_INPUT_STRING_TO_NULL = FLAG_RESERVED1; + public static String VALUE_SEPARATOR = ";"; private static final String[] EMPTY_STRING_ARRAY = new String[] { "" }; @@ -154,51 +332,117 @@ /** Whether or not this component's value is required (non-empty) */ private static final short FLAG_REQUIRED = FLAG_RESERVED3; - private static final String NO_RAW_INPUT = "[-NO-RAW-INPUT-]"; + private static final String NO_RAW_INPUT = "[-NO-RAW-INPUT-]"; private static final long serialVersionUID = 1L; - private transient Object convertedInput; - /** - * The value will be made available to the validator property by means of - * ${label}. It does not have any specific meaning to FormComponent itself. - */ - private IModel labelModel = null; - - /** - * Raw Input entered by the user or NO_RAW_INPUT if nothing is filled in. + * Make empty strings null values boolean. Used by AbstractTextComponent + * subclass. */ - private String rawInput = NO_RAW_INPUT; + protected static final short FLAG_CONVERT_EMPTY_INPUT_STRING_TO_NULL = FLAG_RESERVED1; /** - * Type that the raw input string will be converted to + * Visits any form components inside component if it is a container, or + * component itself if it is itself a form component + * + * @param component + * starting point of the traversal + * + * @param visitor + * The visitor to call */ - private Class type; + public static final void visitFormComponentsPostOrder(Component component, + final FormComponent.IVisitor visitor) + { + if (visitor == null) + { + throw new IllegalArgumentException("Argument `visitor` cannot be null"); + } - /** - * The list of validators for this form component as either an IValidator - * instance or an array of IValidator instances. - */ - private Object validators = null; - /** - * @see org.apache.wicket.Component#Component(String) - */ - public FormComponent(final String id) - { - super(id); - // the form decides whether form components are versioned or not - // see Form.setVersioned - setVersioned(false); + visitFormComponentsPostOrderHelper(component, visitor); } - /** - * @see org.apache.wicket.Component#Component(String, IModel) - */ - public FormComponent(final String id, IModel model) - { + private static final Object visitFormComponentsPostOrderHelper(Component component, + final FormComponent.IVisitor visitor) + { + if (component instanceof MarkupContainer) + { + final MarkupContainer container = (MarkupContainer)component; + if (container.size() > 0) + { + boolean visitChildren = true; + if (container instanceof IFormVisitorParticipant) + { + visitChildren = ((IFormVisitorParticipant)container).processChildren(); + } + if (visitChildren) + { + final Iterator children = container.iterator(); + while (children.hasNext()) + { + final Component child = (Component)children.next(); + Object value = visitFormComponentsPostOrderHelper(child, visitor); + if (value == Component.IVisitor.STOP_TRAVERSAL) + { + return value; + } + } + } + } + } + + if (component instanceof FormComponent) + { + final FormComponent fc = (FormComponent)component; + return visitor.formComponent(fc); + } + + return null; + } + + private transient Object convertedInput; + + /** + * The value will be made available to the validator property by means of + * ${label}. It does not have any specific meaning to FormComponent itself. + */ + private IModel labelModel = null; + + /** + * Raw Input entered by the user or NO_RAW_INPUT if nothing is filled in. + */ + private String rawInput = NO_RAW_INPUT; + + /** + * Type that the raw input string will be converted to + */ + private Class type; + + /** + * The list of validators for this form component as either an IValidator + * instance or an array of IValidator instances. + */ + private Object validators = null; + + /** + * @see org.apache.wicket.Component#Component(String) + */ + public FormComponent(final String id) + { + super(id); + // the form decides whether form components are versioned or not + // see Form.setVersioned + setVersioned(false); + } + + /** + * @see org.apache.wicket.Component#Component(String, IModel) + */ + public FormComponent(final String id, IModel model) + { super(id, model); // the form decides whether form components are versioned or not // see Form.setVersioned @@ -312,15 +556,6 @@ } /** - * @see org.apache.wicket.Component#getBehaviors(java.lang.Class) - */ - protected List getBehaviors(Class type) - { - // List - return super.getBehaviors(type); - } - - /** * @return The parent form for this form component */ public Form getForm() @@ -410,6 +645,7 @@ return inputName.toString(); } + /** * The value will be made available to the validator property by means of * ${label}. It does not have any specific meaning to FormComponent itself. @@ -441,7 +677,6 @@ return type; } - /** * @see Form#getValidatorKeyPrefix() * @return prefix used when constructing validator key messages @@ -586,6 +821,7 @@ return getFlag(FLAG_REQUIRED); } + /** * Gets whether this component is 'valid'. Valid in this context means that * no validation errors were reported the last time the form component was @@ -616,6 +852,14 @@ } /** + * @see org.apache.wicket.markup.html.form.IFormVisitorParticipant#processChildren(boolean) + */ + public boolean processChildren() + { + return true; + } + + /** * This method will retrieve the request parameter, validate it, and if * valid update the model. These are the same steps as would be performed by * the form. @@ -655,7 +899,6 @@ return this; } - /** * Sets the value for a form component this value will be split the string * with [EMAIL PROTECTED] FormComponent#VALUE_SEPARATOR} and calls @@ -757,6 +1000,7 @@ setModelObject(getConvertedInput()); } + /** * Called to indicate that the user input is valid. */ @@ -787,6 +1031,77 @@ } /** + * @param validator + * The validator to add to the validators Object (which may be an + * array of IValidators or a single instance, for efficiency) + */ + private void validators_add(final IValidator validator) + { + if (this.validators == null) + { + this.validators = validator; + } + else + { + // Get current list size + final int size = validators_size(); + + // Create array that holds size + 1 elements + final IValidator[] validators = new IValidator[size + 1]; + + // Loop through existing validators copying them + for (int i = 0; i < size; i++) + { + validators[i] = validators_get(i); + } + + // Add new validator to the end + validators[size] = validator; + + // Save new validator list + this.validators = validators; + } + } + + /** + * Gets validator from validators Object (which may be an array of + * IValidators or a single instance, for efficiency) at the given index + * + * @param index + * The index of the validator to get + * @return The validator + */ + private IValidator validators_get(int index) + { + if (this.validators == null) + { + throw new IndexOutOfBoundsException(); + } + if (this.validators instanceof IValidator[]) + { + return ((IValidator[])validators)[index]; + } + return (IValidator)validators; + } + + /** + * @return The number of validators in the validators Object (which may be + * an array of IValidators or a single instance, for efficiency) + */ + private int validators_size() + { + if (this.validators == null) + { + return 0; + } + if (this.validators instanceof IValidator[]) + { + return ((IValidator[])validators).length; + } + return 1; + } + + /** * Converts and validates the conversion of the raw input string into the * object specified by [EMAIL PROTECTED] FormComponent#getType()} and records any * errors. Converted value is available through @@ -883,6 +1198,14 @@ return value != null && value.length > 0 && value[0] != null ? value[0].trim() : null; } + /** + * @see org.apache.wicket.Component#getBehaviors(java.lang.Class) + */ + protected List getBehaviors(Class type) + { + // List + return super.getBehaviors(type); + } /** * @return Value to return when model value is needed @@ -990,6 +1313,13 @@ super.onComponentTag(tag); } + protected void onDetach() + { + super.onDetach(); + convertedInput = null; + } + + /** * Called by [EMAIL PROTECTED] #onComponentTag(ComponentTag)} when the component is * disabled. By default, this method will add a disabled="disabled" @@ -1004,12 +1334,6 @@ tag.put("disabled", "disabled"); } - protected void onDetach() - { - super.onDetach(); - convertedInput = null; - } - /** * Handle invalidation */ @@ -1072,330 +1396,6 @@ throw new WicketRuntimeException("Exception '" + e + "' occurred during validation " + validator.getClass().getName() + " on component " + this.getPath(), e); } - } - - /** - * @param validator - * The validator to add to the validators Object (which may be an - * array of IValidators or a single instance, for efficiency) - */ - private void validators_add(final IValidator validator) - { - if (this.validators == null) - { - this.validators = validator; - } - else - { - // Get current list size - final int size = validators_size(); - - // Create array that holds size + 1 elements - final IValidator[] validators = new IValidator[size + 1]; - - // Loop through existing validators copying them - for (int i = 0; i < size; i++) - { - validators[i] = validators_get(i); - } - - // Add new validator to the end - validators[size] = validator; - - // Save new validator list - this.validators = validators; - } - } - - /** - * Gets validator from validators Object (which may be an array of - * IValidators or a single instance, for efficiency) at the given index - * - * @param index - * The index of the validator to get - * @return The validator - */ - private IValidator validators_get(int index) - { - if (this.validators == null) - { - throw new IndexOutOfBoundsException(); - } - if (this.validators instanceof IValidator[]) - { - return ((IValidator[])validators)[index]; - } - return (IValidator)validators; - } - - - /** - * @return The number of validators in the validators Object (which may be - * an array of IValidators or a single instance, for efficiency) - */ - private int validators_size() - { - if (this.validators == null) - { - return 0; - } - if (this.validators instanceof IValidator[]) - { - return ((IValidator[])validators).length; - } - return 1; - } - - /** - * Adapter that makes this component appear as [EMAIL PROTECTED] IValidatable} - * - * @author ivaynberg - */ - private class ValidatableAdapter implements IValidatable - { - - /** - * @see org.apache.wicket.validation.IValidatable#error(org.apache.wicket.validation.IValidationError) - */ - public void error(IValidationError error) - { - FormComponent.this.error(error); - } - - /** - * @see org.apache.wicket.validation.IValidatable#getValue() - */ - public Object getValue() - { - return FormComponent.this.getConvertedInput(); - } - - public boolean isValid() - { - return FormComponent.this.isValid(); - } - - } - - /** - * [EMAIL PROTECTED] IErrorMessageSource} used for error messags against this form - * components. - * - * @author ivaynberg - */ - private class MessageSource implements IErrorMessageSource - { - - /** - * @see org.apache.wicket.validation.IErrorMessageSource#getMessage(java.lang.String) - */ - public String getMessage(String key) - { - final FormComponent formComponent = FormComponent.this; - - // retrieve prefix that will be used to construct message keys - String prefix = formComponent.getValidatorKeyPrefix(); - if (Strings.isEmpty(prefix)) - { - prefix = ""; - } - - final Localizer localizer = formComponent.getLocalizer(); - - String resource = prefix + getId() + "." + key; - - // First use the parent for resolving so that - // form1.textfield1.Required can be used. - - // Note: It is important that the default value of "" is provided - // to getString() not to throw a MissingResourceException or to - // return a default string like "[Warning: String ..." - String message = localizer.getString(resource, formComponent.getParent(), ""); - - // If not found, than ... - if (Strings.isEmpty(message)) - { - // Try a variation of the resource key - - resource = prefix + key; - - message = localizer.getString(resource, formComponent.getParent(), ""); - } - - if (Strings.isEmpty(message)) - { - // If still empty then use default - - resource = prefix + getId() + "." + key; - - // Note: It is important that the default value of "" is - // provided - // to getString() not to throw a MissingResourceException or to - // return a default string like "[Warning: String ..." - message = localizer.getString(resource, formComponent, ""); - - // If not found, than ... - if (Strings.isEmpty(message)) - { - // Try a variation of the resource key - - resource = prefix + key; - - message = localizer.getString(resource, formComponent, ""); - } - } - - // convert empty string to null in case our default value of "" was - // returned from localizer - if (Strings.isEmpty(message)) - { - message = null; - } - return message; - } - - /** - * Creates a new params map that additionaly contains the default input, - * name, label parameters - * - * @param params - * original params map - * @return new params map - */ - private Map addDefaultVars(Map params) - { - // create and fill the new params map - final HashMap fullParams; - if (params == null) - { - fullParams = new HashMap(6); - } - else - { - fullParams = new HashMap(params.size() + 6); - fullParams.putAll(params); - } - - // add the input param if not already present - if (!fullParams.containsKey("input")) - { - fullParams.put("input", FormComponent.this.getInput()); - } - - // add the name param if not already present - if (!fullParams.containsKey("name")) - { - fullParams.put("name", FormComponent.this.getId()); - } - - // add the label param if not already present - if (!fullParams.containsKey("label")) - { - fullParams.put("label", getLabel()); - } - return fullParams; - } - - /** - * @return value of label param for this form component - */ - private Object getLabel() - { - final FormComponent fc = FormComponent.this; - Object label = null; - - // first try the label model ... - if (fc.getLabel() != null) - { - label = fc.getLabel().getObject(); - } - // ... then try a resource of format [form-component-id] with - // default of '[form-component-id]' - if (label == null) - { - - label = fc.getLocalizer().getString(fc.getId(), fc.getParent(), fc.getId()); - } - return label; - } - - - /** - * @see org.apache.wicket.validation.IErrorMessageSource#substitute(java.lang.String, - * java.util.Map) - */ - public String substitute(String string, Map vars) throws IllegalStateException - { - return new MapVariableInterpolator(string, addDefaultVars(vars), true).toString(); - } - } - - /** - * @see org.apache.wicket.markup.html.form.IFormVisitorParticipant#processChildren(boolean) - */ - public boolean processChildren() - { - return true; - } - - /** - * Visits any form components inside component if it is a container, or - * component itself if it is itself a form component - * - * @param component - * starting point of the traversal - * - * @param visitor - * The visitor to call - */ - public static final void visitFormComponentsPostOrder(Component component, - final FormComponent.IVisitor visitor) - { - if (visitor == null) - { - throw new IllegalArgumentException("Argument `visitor` cannot be null"); - } - - - visitFormComponentsPostOrderHelper(component, visitor); - } - - private static final Object visitFormComponentsPostOrderHelper(Component component, - final FormComponent.IVisitor visitor) - { - if (component instanceof MarkupContainer) - { - final MarkupContainer container = (MarkupContainer)component; - if (container.size() > 0) - { - boolean visitChildren = true; - if (container instanceof IFormVisitorParticipant) - { - visitChildren = ((IFormVisitorParticipant)container).processChildren(); - } - if (visitChildren) - { - final Iterator children = container.iterator(); - while (children.hasNext()) - { - final Component child = (Component)children.next(); - Object value = visitFormComponentsPostOrderHelper(child, visitor); - if (value == Component.IVisitor.STOP_TRAVERSAL) - { - return value; - } - } - } - } - } - - if (component instanceof FormComponent) - { - final FormComponent fc = (FormComponent)component; - return visitor.formComponent(fc); - } - - return null; } }