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     */
040    @Import(library = "zone-refresh.js")
041    public class ZoneRefresh
042    {
043       /**
044        *  Period between two consecutive refreshes (in seconds)  
045        */
046       @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
047       private int period;
048       
049       /**
050        * Context passed to the event
051        */
052       @Parameter
053       private Object[] context;
054       
055       @InjectContainer
056       private Zone zone;
057    
058       @Inject
059       private JavaScriptSupport javaScriptSupport;
060       
061       @Inject
062       private ComponentResources resources;
063       
064       public ZoneRefresh()
065       {
066       }
067       
068       //For testing purpose
069       ZoneRefresh(Object [] context, ComponentResources resources, JavaScriptSupport javaScriptSupport, Zone zone)
070       {
071          this.context = context;
072          this.resources = resources;
073          this.javaScriptSupport = javaScriptSupport;
074          this.zone = zone;
075       }
076    
077       @AfterRender
078       void addJavaScript()
079       {
080          JSONObject params = new JSONObject();
081          
082          params.put("period", period);
083          params.put("id", zone.getClientId());
084          params.put("URL", createEventLink());
085          
086          javaScriptSupport.addInitializerCall(InitializationPriority.LATE, "zoneRefresh", params);
087       }
088    
089       private Object createEventLink()
090       {
091          Link link = resources.createEventLink("zoneRefresh", context);
092          return link.toAbsoluteURI();
093       }
094       
095       Object onZoneRefresh()
096       {
097          CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>();
098          resources.triggerEvent(EventConstants.REFRESH, context, callback);
099          
100          if(callback.getResult() != null){
101             return callback.getResult();
102          }
103          
104          return zone.getBody();
105       }
106    
107    }