View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.document.web.renderers;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.JspWriter;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.Tag;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.struts.taglib.html.HiddenTag;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.ole.sys.document.web.HideShowBlock;
29  import org.kuali.rice.core.api.config.property.ConfigurationService;
30  import org.kuali.rice.kns.web.taglib.html.KNSImageTag;
31  
32  /**
33   * Renders a hide show block
34   */
35  public class HideShowBlockRenderer implements Renderer {
36      private HideShowBlock hideShowBlock;
37      private HiddenTag tabStateTag = new HiddenTag();
38      private KNSImageTag showHideButton = new KNSImageTag();
39      
40      protected String riceImageURLProperty = "kr.externalizable.images.url";
41  
42      /**
43       * 
44       * @see org.kuali.ole.sys.document.web.renderers.Renderer#clear()
45       */
46      public void clear() {
47          hideShowBlock = null;
48          cleanTabStateTag();
49          cleanShowHideButton();
50      }
51      
52      /**
53       * Cleans the tab state hidden tag
54       */
55      protected void cleanTabStateTag() {
56          tabStateTag.setPageContext(null);
57          tabStateTag.setParent(null);
58          tabStateTag.setProperty(null);
59          tabStateTag.setValue(null);
60      }
61      
62      /**
63       * Cleans the show/hide button up
64       */
65      protected void cleanShowHideButton() {
66          showHideButton.setPageContext(null);
67          showHideButton.setParent(null);
68          showHideButton.setSrc(null);
69          showHideButton.setAlt(null);
70          showHideButton.setTitle(null);
71          showHideButton.setProperty(null);
72          showHideButton.setStyleClass(null);
73          showHideButton.setStyleId(null);
74          showHideButton.setOnclick(null);
75      }
76  
77      /**
78       * Renders the title row and forces the rendering of child content
79       * @see org.kuali.ole.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
80       */
81      public void render(PageContext pageContext, Tag parentTag) throws JspException {
82          JspWriter out = pageContext.getOut();
83          
84          try {
85              out.write(buildLabelButtonTableOpening());
86              renderTabStateTag(pageContext, parentTag);
87              if (!StringUtils.isBlank(hideShowBlock.getLabel())) {
88                  out.write(hideShowBlock.getLabel());
89              }
90              renderShowHideButton(pageContext, parentTag);
91              out.write(buildLabelButtonTableClosing());
92              out.write(buildInnerTableOpening());
93              hideShowBlock.renderChildRows(pageContext, parentTag);
94              out.write(buildInnerTableClosing());
95          } catch (IOException ioe) {
96              throw new JspException("Difficulty rendering Hide/Show block", ioe);
97          }
98      }
99      
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(ConfigurationService.class).getPropertyValueAsString(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 }