View Javadoc

1   /*
2    * Copyright 2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.nordija.tapestry.bayeux.delegate;
17  
18  import org.apache.tapestry.IBinding;
19  import org.apache.tapestry.IMarkupWriter;
20  import org.apache.tapestry.IRequestCycle;
21  import org.apache.tapestry.form.IFormComponent;
22  import org.apache.tapestry.form.validator.Required;
23  import org.apache.tapestry.valid.IValidator;
24  import org.apache.tapestry.valid.ValidationDelegate;
25  
26  import java.util.Collection;
27  import java.util.Map;
28  
29  /**
30   * <p>This validation delegate automatically marks required fields with a '!' mark and a 'required' css class to the
31   * marker.</p>
32   * <p>At the same time it overwrites the default marking of fields in error. Only one * is added and it uses a css class
33   * 'error' to mark the single star.</p>
34   *
35   * @author Jacob von Eyben www.nordija.com
36   * @version $Id: ClickOnce.java 93 2007-05-19 09:21:20Z jeyben $
37   * @since 2.0.0
38   */
39  public class RequiredValidationDelegate extends ValidationDelegate {
40  
41      public void writeSuffix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator) {
42          if (isInError()) {
43              writer.printRaw("&nbsp;");
44              writer.begin("span");
45              writer.attribute("class", "error");
46              writer.print("!");
47              writer.end();
48          } else if (isRequired(component)) {
49              writer.printRaw("&nbsp;");
50              writer.begin("span");
51              writer.attribute("class", "required");
52              writer.print("*");
53              writer.end();
54          }
55  
56      }
57  
58  
59      /**
60       * Determines if a {@link IFormComponent} has a required validator associated.
61       *
62       * @param component the {@link IFormComponent} to investigate for required validators.
63       * @return <code>true</code> if the {@link IFormComponent} is required, <code>false</code> otherwise.
64       */
65      private boolean isRequired(IFormComponent component) {
66          Map bindings = component.getBindings();
67          IBinding validationBinding = (IBinding) bindings.get("validators");
68          boolean required = false;
69          if (validationBinding != null) {
70              Object validationBindingObject = validationBinding.getObject();
71              if (validationBindingObject != null && validationBindingObject instanceof Collection) {
72                  Collection validators = (Collection) validationBindingObject;
73                  for (java.util.Iterator iterator = validators.iterator(); iterator.hasNext();) {
74                      Object aValidator = iterator.next();
75                      if (aValidator instanceof Required) {
76                          required = true;
77                          break;
78                      }
79                  }
80              }
81          }
82          return required;
83      }
84  }