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.ComponentResources;
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.annotations.SupportsInformalParameters;
024    import org.apache.tapestry5.ioc.annotations.Inject;
025    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
026    
027    /**
028     * closeable fieldset.
029     *
030     * @version $Id: FieldSet.java 674 2010-07-29 12:47:25Z homburgs $
031     */
032    @SupportsInformalParameters
033    @Import(library = {"../Chenillekit.js", "FieldSet.js"}, stylesheet = {"FieldSet.css"})
034    public class FieldSet
035    {
036            /**
037             * let the Fieldset initialy displayed as closed/open,
038             */
039            @Parameter(value = "false", required = false)
040            private boolean closed;
041    
042            /**
043             * The id used to generate a page-unique client-side identifier for the component. If a component renders multiple
044             * times, a suffix will be appended to the to id to ensure uniqueness. The uniqued value may be accessed via the
045             * {@link #getClientId() clientId property}.
046             */
047            @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
048            private String clientId;
049    
050            private String assignedClientId;
051    
052            @Environmental
053            private JavaScriptSupport javascriptSupport;
054    
055            @Inject
056            private ComponentResources resources;
057    
058            void setupRender()
059            {
060                    // By default, use the component id as the (base) client id. If the clientid
061                    // parameter is bound, then that is the value to use.
062                    // Often, these controlName and _clientId will end up as the same value. There are many
063                    // exceptions, including a form that renders inside a loop, or a form inside a component
064                    // that is used multiple times.
065                    assignedClientId = javascriptSupport.allocateClientId(clientId);
066            }
067    
068            void beginRender(MarkupWriter writer)
069            {
070                    writer.element("fieldset", "id", getClientId(), "class", "ck_fieldset");
071                    resources.renderInformalParameters(writer);
072            }
073    
074            void afterRender(MarkupWriter writer)
075            {
076                    writer.end();
077    
078                    javascriptSupport.addScript("var %s = new Ck.FieldSet('%s', %s);", getClientId(), getClientId(), closed);
079            }
080    
081            public String getClientId()
082            {
083                    return assignedClientId;
084            }
085    }