001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.document.web.renderers;
017    
018    import java.io.IOException;
019    
020    import javax.servlet.jsp.JspException;
021    import javax.servlet.jsp.JspWriter;
022    import javax.servlet.jsp.PageContext;
023    import javax.servlet.jsp.tagext.Tag;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.apache.struts.taglib.html.HiddenTag;
027    import org.kuali.kfs.sys.context.SpringContext;
028    import org.kuali.kfs.sys.document.web.HideShowBlock;
029    import org.kuali.rice.kns.service.KualiConfigurationService;
030    import org.kuali.rice.kns.web.taglib.html.KNSImageTag;
031    
032    /**
033     * Renders a hide show block
034     */
035    public class HideShowBlockRenderer implements Renderer {
036        private HideShowBlock hideShowBlock;
037        private HiddenTag tabStateTag = new HiddenTag();
038        private KNSImageTag showHideButton = new KNSImageTag();
039        
040        protected String riceImageURLProperty = "kr.externalizable.images.url";
041    
042        /**
043         * 
044         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#clear()
045         */
046        public void clear() {
047            hideShowBlock = null;
048            cleanTabStateTag();
049            cleanShowHideButton();
050        }
051        
052        /**
053         * Cleans the tab state hidden tag
054         */
055        protected void cleanTabStateTag() {
056            tabStateTag.setPageContext(null);
057            tabStateTag.setParent(null);
058            tabStateTag.setProperty(null);
059            tabStateTag.setValue(null);
060        }
061        
062        /**
063         * Cleans the show/hide button up
064         */
065        protected void cleanShowHideButton() {
066            showHideButton.setPageContext(null);
067            showHideButton.setParent(null);
068            showHideButton.setSrc(null);
069            showHideButton.setAlt(null);
070            showHideButton.setTitle(null);
071            showHideButton.setProperty(null);
072            showHideButton.setStyleClass(null);
073            showHideButton.setStyleId(null);
074            showHideButton.setOnclick(null);
075        }
076    
077        /**
078         * Renders the title row and forces the rendering of child content
079         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
080         */
081        public void render(PageContext pageContext, Tag parentTag) throws JspException {
082            JspWriter out = pageContext.getOut();
083            
084            try {
085                out.write(buildLabelButtonTableOpening());
086                renderTabStateTag(pageContext, parentTag);
087                if (!StringUtils.isBlank(hideShowBlock.getLabel())) {
088                    out.write(hideShowBlock.getLabel());
089                }
090                renderShowHideButton(pageContext, parentTag);
091                out.write(buildLabelButtonTableClosing());
092                out.write(buildInnerTableOpening());
093                hideShowBlock.renderChildRows(pageContext, parentTag);
094                out.write(buildInnerTableClosing());
095            } catch (IOException ioe) {
096                throw new JspException("Difficulty rendering Hide/Show block", ioe);
097            }
098        }
099        
100        /**
101         * @return the HTML for the opening of the button table
102         */
103        protected String buildLabelButtonTableOpening() {
104            return "<table class=\"datatable\" style=\"padding: 0px\"><tr><td class=\"tab-subhead\">";
105        }
106        
107        /**
108         * Renders a hidden tag which holds the current tab state
109         * @param pageContext the pageContext to render to
110         * @param parentTag the tag requesting all this rendering
111         * @throws JspException thrown if something goes wrong
112         */
113        protected void renderTabStateTag(PageContext pageContext, Tag parentTag) throws JspException {
114            tabStateTag.setPageContext(pageContext);
115            tabStateTag.setParent(parentTag);
116            tabStateTag.setProperty("tabStates("+hideShowBlock.getTabKey()+")");
117            tabStateTag.setValue(hideShowBlock.getTabState());
118            
119            tabStateTag.doStartTag();
120            tabStateTag.doEndTag();
121        }
122        
123        /**
124         * @return the HTML for the closing of the label button table
125         */
126        protected String buildLabelButtonTableClosing() {
127            return "</td></tr></table>";
128        }
129        
130        /**
131         * Renders the hide/show image button
132         * @param pageContext the pageContext to render to
133         * @param parentTag the tag requesting all this rendering
134         * @throws JspException thrown if something goes wrong
135         */
136        protected void renderShowHideButton(PageContext pageContext, Tag parentTag) throws JspException {
137            showHideButton.setPageContext(pageContext);
138            showHideButton.setParent(parentTag);
139            showHideButton.setProperty("methodToCall.toggleTab.tab"+hideShowBlock.getTabKey());
140            showHideButton.setStyleClass("tinybutton");
141            showHideButton.setStyleId("tab-"+hideShowBlock.getTabKey()+"-imageToggle");
142            showHideButton.setOnclick("javascript: return toggleTab(document, '"+hideShowBlock.getTabKey()+"'); ");
143            
144            String riceImageDir = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(riceImageURLProperty);
145            if (hideShowBlock.isShowing()) {
146                showHideButton.setSrc(riceImageDir+"tinybutton-hide.gif");
147                showHideButton.setAlt("Hide "+hideShowBlock.getFullLabel());
148                showHideButton.setTitle("Hide "+hideShowBlock.getFullLabel());
149            } else {
150                showHideButton.setSrc(riceImageDir+"tinybutton-show.gif");
151                showHideButton.setAlt("Show "+hideShowBlock.getFullLabel());
152                showHideButton.setTitle("Show "+hideShowBlock.getFullLabel());
153            }
154            
155            showHideButton.doStartTag();
156            showHideButton.doEndTag();
157        }
158        
159        /**
160         * Creates the HTML for the hiding/showing div and inner table to display children in
161         * @return the HTML for the opening of the inner table
162         */
163        protected String buildInnerTableOpening() {
164            StringBuilder opening = new StringBuilder();
165            opening.append("<div id=\"tab-"+hideShowBlock.getTabKey()+"-div\" style=\"display: ");
166            opening.append(hideShowBlock.isShowing() ? "block" : "none");
167            opening.append("\">");
168            
169            opening.append("<table class=\"datatable\" style=\"width: 100%;\">");
170            
171            return opening.toString();
172        }
173        
174        /**
175         * Creates the HTML to close the inner table and hide/show div
176         * @return the HTML for the closing of the inner table
177         */
178        protected String buildInnerTableClosing() {
179            return "</table></div>";
180        }
181        
182    
183        /**
184         * Gets the hideShowBlock attribute. 
185         * @return Returns the hideShowBlock.
186         */
187        public HideShowBlock getHideShowBlock() {
188            return hideShowBlock;
189        }
190    
191        /**
192         * Sets the hideShowBlock attribute value.
193         * @param hideShowBlock The hideShowBlock to set.
194         */
195        public void setHideShowBlock(HideShowBlock hideShowBlock) {
196            this.hideShowBlock = hideShowBlock;
197        }
198    
199    }