001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008-2010 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.tapestry.core.components;
016    
017    import org.apache.tapestry5.BindingConstants;
018    import org.apache.tapestry5.ClientElement;
019    import org.apache.tapestry5.MarkupWriter;
020    import org.apache.tapestry5.annotations.Environmental;
021    import org.apache.tapestry5.annotations.Import;
022    import org.apache.tapestry5.annotations.Parameter;
023    import org.apache.tapestry5.json.JSONObject;
024    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
025    
026    /**
027     * A component for creating a slide show from a generic group of HTML
028     * elements on the client side.
029     *
030     * @version $Id: SlideShow.java 674 2010-07-29 12:47:25Z homburgs $
031     */
032    @Import(library = {"../Chenillekit.js", "SlideShow.js"}, stylesheet = "SlideShow.css")
033    public class SlideShow implements ClientElement
034    {
035            /**
036             * Sets the amount of time (in seconds) each slide will be displayed.
037             */
038            @Parameter(value = "2")
039            private int interval;
040    
041            /**
042             * The slide transition object.
043             */
044            @Parameter(defaultPrefix = BindingConstants.LITERAL, value = "Ck.SlideShow.Tx.Crossfade")
045            private String transition;
046    
047            // (in development)
048            @Parameter(value = "true")
049            private boolean controls;
050    
051            /**
052             * Determines if the slide show will start over after displaying the final slide.
053             */
054            @Parameter(value = "false")
055            private boolean loop;
056    
057            /**
058             * Determines if the slide show will pause when the mouse hovers over it.
059             */
060            @Parameter(value = "true")
061            private boolean pauseOnHover;
062    
063            /**
064             * calculates the component size by the size of the biggest image.
065             */
066            @Parameter(value = "true")
067            private boolean calculateElementSize;
068    
069            @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
070            private String clientId;
071    
072            @Environmental
073            private JavaScriptSupport javascriptSupport;
074    
075            private String assignedClientId;
076    
077            void setupRender()
078            {
079                    assignedClientId = javascriptSupport.allocateClientId(clientId);
080            }
081    
082            void beginRender(final MarkupWriter writer)
083            {
084                    JSONObject jsConfig = new JSONObject();
085                    jsConfig.put("interval", interval);
086                    jsConfig.put("transition", transition);
087                    jsConfig.put("controls", controls);
088                    jsConfig.put("loop", loop);
089                    jsConfig.put("pauseOnHover", pauseOnHover);
090                    jsConfig.put("calculateElementSize", calculateElementSize);
091                    javascriptSupport.addScript("new Ck.SlideShow('%s', %s);", getClientId(), jsConfig);
092            }
093    
094            public String getClientId()
095            {
096                    return assignedClientId;
097            }
098    
099    }