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;
17  
18  import java.util.List;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.PageContext;
22  import javax.servlet.jsp.tagext.Tag;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewHideShowLinesDefinition;
26  import org.kuali.ole.sys.document.web.renderers.HideShowBlockRenderer;
27  import org.kuali.rice.kns.util.WebUtils;
28  import org.kuali.rice.kns.web.ui.Field;
29  
30  /**
31   * A renderable element that renders child elements within a div that can be hidden or displayed
32   */
33  public class HideShowBlock implements RenderableElement {
34      private List<AccountingLineTableRow> contentRows; 
35      private AccountingLineViewHideShowLinesDefinition definition;
36      private AccountingLineRenderingContext renderingContext;
37      private String tabKey;
38  
39      /**
40       * Has child table rows add any fields they know about to the List
41       * @see org.kuali.ole.sys.document.web.RenderableElement#appendFields(java.util.List)
42       * 
43       * KRAD Conversion: Customization of adding the fields - No use of data dictionary
44       */
45      public void appendFields(List<Field> fields) {
46         for (AccountingLineTableRow row : contentRows) {
47             row.appendFields(fields);
48         }
49      }
50  
51      /**
52       * This is not an action block
53       * @see org.kuali.ole.sys.document.web.RenderableElement#isActionBlock()
54       */
55      public boolean isActionBlock() {
56          return false;
57      }
58  
59      /**
60       * Checks if all of the child rows are empty or not; if one isn't empty, then this isn't empty
61       * @see org.kuali.ole.sys.document.web.RenderableElement#isEmpty()
62       */
63      public boolean isEmpty() {
64          for (AccountingLineTableRow row : contentRows) {
65              if (!row.isEmpty()) return false;
66          }
67          return true;
68      }
69  
70      /**
71       * Checks if all the child rows are hidden; if so, then no point in showing this...
72       * @see org.kuali.ole.sys.document.web.RenderableElement#isHidden()
73       */
74      public boolean isHidden() {
75          for (AccountingLineTableRow row : contentRows) {
76              if (!row.isHidden()) return false;
77          }
78          return true;
79      }
80  
81      /**
82       * Has child rows populate with the tab index
83       * @see org.kuali.ole.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int)
84       */
85      public void populateWithTabIndexIfRequested(int reallyHighIndex) {
86          for (AccountingLineTableRow row : contentRows) {
87              row.populateWithTabIndexIfRequested(reallyHighIndex);
88          }
89      }
90  
91      /**
92       * Uses a HideShowBlockRenderer to render this element
93       * @see org.kuali.ole.sys.document.web.RenderableElement#renderElement(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag, org.kuali.ole.sys.document.web.AccountingLineRenderingContext)
94       */
95      public void renderElement(PageContext pageContext, Tag parentTag, AccountingLineRenderingContext renderingContext) throws JspException {
96          this.renderingContext = renderingContext;
97          
98          HideShowBlockRenderer renderer = new HideShowBlockRenderer();
99          renderer.setHideShowBlock(this);
100         renderer.render(pageContext, parentTag);
101         
102         this.renderingContext = null;
103     }
104     
105     /**
106      * Forces all children rows to render themselves
107      * @param pageContext the pageContext to render to
108      * @param parentTag the tag requesting all this rendering
109      * @throws JspException thrown if something goes wrong
110      */
111     public void renderChildRows(PageContext pageContext, Tag parentTag) throws JspException {
112         for (AccountingLineTableRow row : contentRows) {
113             row.renderElement(pageContext, parentTag, renderingContext);
114         }
115     }
116 
117     /**
118      * Gets the contentRows attribute. 
119      * @return Returns the contentRows.
120      */
121     public List<AccountingLineTableRow> getContentRows() {
122         return contentRows;
123     }
124 
125     /**
126      * Sets the contentRows attribute value.
127      * @param contentRows The contentRows to set.
128      */
129     public void setContentRows(List<AccountingLineTableRow> contentRows) {
130         this.contentRows = contentRows;
131     }
132 
133     /**
134      * Sets the definition attribute value.
135      * @param definition The definition to set.
136      */
137     public void setDefinition(AccountingLineViewHideShowLinesDefinition definition) {
138         this.definition = definition;
139     }
140 
141     /** 
142      * @return the tab key for this hide/show block
143      */
144     public String getTabKey() {
145         if (tabKey == null) {
146             tabKey = WebUtils.generateTabKey(renderingContext.getGroupLabel()+definition.getLabel()) + "-" + renderingContext.getAccountingLinePropertyPath().replaceAll("\\.","-").replaceAll("\\[", "(").replaceAll("\\]", ")");
147         }
148         return tabKey;
149     }
150     
151     /**
152      * @return the current tab state
153      */
154     public String getTabState() {
155         String tabState = renderingContext.getTabState(getTabKey());
156         return StringUtils.isNotBlank(tabState) ? tabState : "CLOSE";
157     }
158     
159     /**
160      * Determines if this tab should currently be showing or not
161      * @return true if the tab shows its contents; false otherwise
162      */
163     public boolean isShowing() {
164         return getTabState().equals("OPEN");
165     }
166     
167     /**
168      * @return the label for this hide/show block
169      */
170     public String getLabel() {
171         return definition.getLabel();
172     }
173     
174     /**
175      * @return the concatenation of the group label and this block's label
176      */
177     public String getFullLabel() {
178         return renderingContext.getGroupLabel()+(!StringUtils.isBlank(definition.getLabel()) ? " "+definition.getLabel() : " Hide/Show Block");
179     }
180 }