001    // Copyright 2011 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.corelib.mixins;
016    
017    import org.apache.tapestry5.BindingConstants;
018    import org.apache.tapestry5.ComponentResources;
019    import org.apache.tapestry5.EventConstants;
020    import org.apache.tapestry5.Link;
021    import org.apache.tapestry5.annotations.AfterRender;
022    import org.apache.tapestry5.annotations.Import;
023    import org.apache.tapestry5.annotations.InjectContainer;
024    import org.apache.tapestry5.annotations.Parameter;
025    import org.apache.tapestry5.corelib.components.Zone;
026    import org.apache.tapestry5.internal.util.CaptureResultCallback;
027    import org.apache.tapestry5.ioc.annotations.Inject;
028    import org.apache.tapestry5.json.JSONObject;
029    import org.apache.tapestry5.services.javascript.InitializationPriority;
030    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
031    
032    /**
033     * <p>
034     * This mixin periodically refreshs a @{link org.apache.tapestry5.corelib.components.Zone zone}
035     * by triggering an event on the server using ajax requests. 
036     * </p>
037     * 
038     * <b>Note: </b> This mixin is only meant for a @{link org.apache.tapestry5.corelib.components.Zone zone}
039     * @tapestrydoc
040     */
041    @Import(library = "zone-refresh.js")
042    public class ZoneRefresh
043    {
044       /**
045        *  Period between two consecutive refreshes (in seconds)  
046        */
047       @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
048       private int period;
049       
050       /**
051        * Context passed to the event
052        */
053       @Parameter
054       private Object[] context;
055       
056       @InjectContainer
057       private Zone zone;
058    
059       @Inject
060       private JavaScriptSupport javaScriptSupport;
061       
062       @Inject
063       private ComponentResources resources;
064       
065       public ZoneRefresh()
066       {
067       }
068       
069       //For testing purpose
070       ZoneRefresh(Object [] context, ComponentResources resources, JavaScriptSupport javaScriptSupport, Zone zone)
071       {
072          this.context = context;
073          this.resources = resources;
074          this.javaScriptSupport = javaScriptSupport;
075          this.zone = zone;
076       }
077    
078       @AfterRender
079       void addJavaScript()
080       {
081          JSONObject params = new JSONObject();
082          
083          params.put("period", period);
084          params.put("id", zone.getClientId());
085          params.put("URL", createEventLink());
086          
087          javaScriptSupport.addInitializerCall(InitializationPriority.LATE, "zoneRefresh", params);
088       }
089    
090       private Object createEventLink()
091       {
092          Link link = resources.createEventLink("zoneRefresh", context);
093          return link.toAbsoluteURI();
094       }
095       
096       Object onZoneRefresh()
097       {
098          CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>();
099          resources.triggerEvent(EventConstants.REFRESH, context, callback);
100          
101          if(callback.getResult() != null){
102             return callback.getResult();
103          }
104          
105          return zone.getBody();
106       }
107    
108    }