Coverage Report - org.apache.tapestry5.corelib.internal.FormSupportImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
FormSupportImpl
100%
39/39
100%
12/12
0
 
 1  
 // Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.corelib.internal;
 16  
 
 17  
 import org.apache.tapestry5.ComponentAction;
 18  
 import org.apache.tapestry5.ComponentResources;
 19  
 import org.apache.tapestry5.Field;
 20  
 import org.apache.tapestry5.ioc.Locatable;
 21  
 import org.apache.tapestry5.ioc.Location;
 22  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 23  
 import org.apache.tapestry5.ioc.internal.util.Defense;
 24  
 import org.apache.tapestry5.ioc.internal.util.IdAllocator;
 25  
 import org.apache.tapestry5.services.ClientBehaviorSupport;
 26  
 
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * Provides support to components enclosed by a form when the form is rendering (allowing the components to registry
 31  
  * form submit callback commands), and also during form submission time.
 32  
  * <p/>
 33  
  * TODO: Most methods should only be invokable depending on whether the form is rendering or processing a submission.
 34  
  */
 35  
 public class FormSupportImpl implements InternalFormSupport, Locatable
 36  
 {
 37  
     private final ComponentResources resources;
 38  
 
 39  
     private final ClientBehaviorSupport clientBehaviorSupport;
 40  
 
 41  
     private final boolean clientValidationEnabled;
 42  
 
 43  
     private final IdAllocator idAllocator;
 44  
 
 45  
     private final String clientId;
 46  
 
 47  
     private final ComponentActionSink actionSink;
 48  
 
 49  
     private final String formValidationId;
 50  
 
 51  
     private List<Runnable> commands;
 52  
 
 53  
     private String encodingType;
 54  
 
 55  
     /**
 56  
      * Constructor used when processing a form submission.
 57  
      */
 58  
     public FormSupportImpl(ComponentResources resources, String formValidationId)
 59  
     {
 60  180
         this(resources, null, null, null, false, null, formValidationId);
 61  180
     }
 62  
 
 63  
     /**
 64  
      * Full constructor (specifically constructor for render time).
 65  
      */
 66  
     public FormSupportImpl(ComponentResources resources, String clientId, ComponentActionSink actionSink,
 67  
                            ClientBehaviorSupport clientBehaviorSupport,
 68  
                            boolean clientValidationEnabled, IdAllocator idAllocator, String formValidationId)
 69  542
     {
 70  542
         this.resources = resources;
 71  542
         this.clientId = clientId;
 72  542
         this.actionSink = actionSink;
 73  542
         this.clientBehaviorSupport = clientBehaviorSupport;
 74  542
         this.clientValidationEnabled = clientValidationEnabled;
 75  542
         this.idAllocator = idAllocator;
 76  542
         this.formValidationId = formValidationId;
 77  542
     }
 78  
 
 79  
     public Location getLocation()
 80  
     {
 81  2
         return resources.getLocation();
 82  
     }
 83  
 
 84  
     public String getFormComponentId()
 85  
     {
 86  6
         return resources.getCompleteId();
 87  
     }
 88  
 
 89  
     public String allocateControlName(String id)
 90  
     {
 91  952
         return idAllocator.allocateId(id);
 92  
     }
 93  
 
 94  
     public <T> void store(T component, ComponentAction<T> action)
 95  
     {
 96  2126
         actionSink.store(component, action);
 97  2126
     }
 98  
 
 99  
     public <T> void storeAndExecute(T component, ComponentAction<T> action)
 100  
     {
 101  1624
         actionSink.store(component, action);
 102  
 
 103  1624
         action.execute(component);
 104  1624
     }
 105  
 
 106  
     public void defer(Runnable command)
 107  
     {
 108  26
         if (commands == null) commands = CollectionFactory.newList();
 109  
 
 110  26
         commands.add(Defense.notNull(command, "command"));
 111  26
     }
 112  
 
 113  
     public void executeDeferred()
 114  
     {
 115  530
         if (commands == null) return;
 116  
 
 117  22
         for (Runnable r : commands)
 118  26
             r.run();
 119  
 
 120  22
         commands.clear();
 121  22
     }
 122  
 
 123  
     public String getClientId()
 124  
     {
 125  26
         return clientId;
 126  
     }
 127  
 
 128  
     public String getEncodingType()
 129  
     {
 130  358
         return encodingType;
 131  
     }
 132  
 
 133  
     public void setEncodingType(String encodingType)
 134  
     {
 135  12
         Defense.notBlank(encodingType, "encodingType");
 136  
 
 137  12
         if (this.encodingType != null && !this.encodingType.equals(encodingType))
 138  2
             throw new IllegalStateException(InternalMessages.conflictingEncodingType(this.encodingType, encodingType));
 139  
 
 140  10
         this.encodingType = encodingType;
 141  10
     }
 142  
 
 143  
     public void addValidation(Field field, String validationName, String message, Object constraint)
 144  
     {
 145  678
         if (clientValidationEnabled)
 146  476
             clientBehaviorSupport.addValidation(field, validationName, message, constraint);
 147  678
     }
 148  
 
 149  
     public boolean isClientValidationEnabled()
 150  
     {
 151  944
         return clientValidationEnabled;
 152  
     }
 153  
 
 154  
     public String getFormValidationId()
 155  
     {
 156  909
         return formValidationId;
 157  
     }
 158  
 }