Coverage Report - org.apache.tapestry5.ajax.MultiZoneUpdate
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiZoneUpdate
93%
14/15
100%
2/2
0
 
 1  
 // Copyright 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.ajax;
 16  
 
 17  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 18  
 import org.apache.tapestry5.ioc.internal.util.Defense;
 19  
 
 20  
 import java.util.Map;
 21  
 
 22  
 /**
 23  
  * A mapping from <em>client-side zone ids</em> to objects that can render the content for that zone on the client. An
 24  
  * event handler method may instantiate an instance and chain together a series of calls to {@link #add(String,
 25  
  * Object)}, and return the final result.
 26  
  * <p/>
 27  
  * Remember that client-side element ids may not match server-side component ids, especially once Ajax is added to the
 28  
  * mix. Because of this, it is highly recommended that the client-side logic gather the actual component ids and include
 29  
  * those in the Ajax request, to ensure that the server generates updates that the client can process. Better yet, use
 30  
  * the Zone's id parameter to lock down the zone's id to a known, predictable value.
 31  
  *
 32  
  * @since 5.1.0.1
 33  
  */
 34  
 public class MultiZoneUpdate
 35  
 {
 36  
     private final MultiZoneUpdate parent;
 37  
 
 38  
     private final String zoneId;
 39  
 
 40  
     private final Object renderer;
 41  
 
 42  
     public MultiZoneUpdate(String zoneId, Object renderer)
 43  
     {
 44  2
         this(zoneId, renderer, null);
 45  2
     }
 46  
 
 47  
     private MultiZoneUpdate(String zoneId, Object renderer, MultiZoneUpdate parent)
 48  6
     {
 49  6
         this.zoneId = Defense.notBlank(zoneId, "zoneId");
 50  6
         this.renderer = Defense.notNull(renderer, "renderer");
 51  
 
 52  6
         this.parent = parent;
 53  6
     }
 54  
 
 55  
     /**
 56  
      * Returns a <strong>new</strong> MultiZoneUpdate reflecting the mapping from the indicated zone to an object that
 57  
      * will render the content for that zone.
 58  
      *
 59  
      * @param zoneId   client id of zone to update
 60  
      * @param renderer object that can provide the content for the zone
 61  
      * @return new MultiZoneUpdate
 62  
      */
 63  
     public MultiZoneUpdate add(String zoneId, Object renderer)
 64  
     {
 65  4
         return new MultiZoneUpdate(zoneId, renderer, this);
 66  
     }
 67  
 
 68  
     /**
 69  
      * Returns a mapping from client zone id to renderer object for that zone.
 70  
      *
 71  
      * @return string to renderer map
 72  
      */
 73  
     public Map<String, Object> getZoneToRenderMap()
 74  
     {
 75  2
         Map<String, Object> result = CollectionFactory.newMap();
 76  
 
 77  2
         MultiZoneUpdate cursor = this;
 78  
 
 79  8
         while (cursor != null)
 80  
         {
 81  6
             result.put(cursor.zoneId, cursor.renderer);
 82  
 
 83  6
             cursor = cursor.parent;
 84  
         }
 85  
 
 86  2
         return result;
 87  
     }
 88  
 
 89  
     @Override
 90  
     public String toString()
 91  
     {
 92  0
         return String.format("MultiZoneUpdate[%s]", getZoneToRenderMap());
 93  
     }
 94  
 }
 95