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.rice.kns.service.KualiConfigurationService;
029    import org.kuali.rice.kns.web.taglib.html.KNSImageTag;
030    
031    /**
032     * Renders the header of an accounting line table
033     */
034    public class AccountingLineTableHeaderRenderer implements Renderer {
035        private int cellCount;
036        private boolean hideDetails;
037        private String accountingLineImportInstructionsUrl;
038        private KNSImageTag showHideTag = new KNSImageTag();
039        private HiddenTag hideStateTag = new HiddenTag();
040        
041        /**
042         * Constructs a AccountingLineTableHeaderRenderer, updating the tags used by this renderer to keep constant properties
043         */
044        public AccountingLineTableHeaderRenderer() {
045            hideStateTag.setName("KualiForm");
046            hideStateTag.setProperty("hideDetails");
047            
048            showHideTag.setStyleClass("tinybutton");
049        }
050    
051        /**
052         * Clears out the mutable, changing qualities of this renderer so it can be repooled
053         */
054        public void clear() {
055            cellCount = 0;
056            hideDetails = false;
057            accountingLineImportInstructionsUrl = null;
058            
059            showHideTag.setPageContext(null);
060            showHideTag.setParent(null);
061            showHideTag.setProperty(null);
062            showHideTag.setAlt(null);
063            showHideTag.setTitle(null);
064            showHideTag.setSrc(null);
065            
066            hideStateTag.setPageContext(null);
067            hideStateTag.setParent(null);
068        }
069    
070        /**
071         * Renders the header for the accounting line table to the screen
072         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
073         */
074        public void render(PageContext pageContext, Tag parentTag) throws JspException {
075            JspWriter out = pageContext.getOut();
076            
077            try {
078                out.write(buildDivStart());
079                out.write(buildTableStart());
080                out.write(buildSubheadingWithDetailToggleRowBeginning());
081                renderHideDetails(pageContext, parentTag);
082                out.write(buildSubheadingWithDetailToggleRowEnding());
083            }
084            catch (IOException ioe) {
085                throw new JspException("Difficulty rendering AccountingLineTableHeader", ioe);
086            }
087        }
088        
089        /**
090         * Builds the beginning of the tab-container div
091         * @return the beginning of the tab-container div in HTML
092         */
093        protected String buildDivStart() {
094            return "<div class=\"tab-container\" align=\"center\">\n";
095        }
096    
097        /**
098         * Builds the very start of the table
099         * @return the very start of the table expressed as HTML
100         */
101        protected String buildTableStart() {
102            return "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"datatable\">\n";
103        }
104        
105        /**
106         * Builds the start of the subheading row of the table
107         * @return the start of the subheading row of the table expressed as HTML
108         */
109        protected String buildSubheadingWithDetailToggleRowBeginning() {
110            StringBuilder row = new StringBuilder();
111            row.append("\t<tr>\n");
112            row.append("\t\t<td colspan=\"");
113            row.append(cellCount);
114            row.append("\" class=\"subhead\">\n");
115            row.append("\t\t\t<span class=\"subhead-left\">");
116            row.append(buildSubHeading());
117            row.append("</span>\n");
118            row.append("\t\t\t<span class=\"subhead-right\">\n");
119            
120            return row.toString();
121        }
122        
123        /**
124         * Builds the subheading for the table
125         * @return the subheading for the table, expressed as HTML
126         */
127        protected String buildSubHeading() {
128            if (StringUtils.isBlank(accountingLineImportInstructionsUrl)) return "&nbsp;";
129            
130            StringBuilder subheading = new StringBuilder();
131    
132            subheading.append("Accounting Lines <a href=\"");
133            subheading.append(accountingLineImportInstructionsUrl);
134            subheading.append("\" target=\"helpWindow\">");
135            subheading.append("<img src=\"");
136            subheading.append(SpringContext.getBean(KualiConfigurationService.class).getPropertyString("kr.externalizable.images.url"));
137            subheading.append("my_cp_inf.gif\" title=\"Accounting Lines Help\" src=\"Accounting Lines Help\" hspace=\"5\" border=\"0\" align=\"middle\" />");
138            subheading.append("</a>");
139            
140            return subheading.toString();
141        }
142        
143        /**
144         * Renders the show/hide button  
145         * @param pageContext the page context to render to
146         * @param parentTag the tag requesting all this rendering
147         * @throws JspException thrown under terrible circumstances when the rendering failed and had to be left behind like so much refuse
148         */
149        protected void renderHideDetails(PageContext pageContext, Tag parentTag) throws JspException {
150            hideStateTag.setPageContext(pageContext);
151            hideStateTag.setParent(parentTag);
152    
153            hideStateTag.doStartTag();
154            hideStateTag.doEndTag();
155            
156            String toggle = hideDetails ? "show" : "hide";
157            
158            showHideTag.setPageContext(pageContext);
159            showHideTag.setParent(parentTag);
160            showHideTag.setProperty("methodToCall."+toggle+"Details");
161            showHideTag.setSrc(SpringContext.getBean(KualiConfigurationService.class).getPropertyString("kr.externalizable.images.url")+"det-"+toggle+".gif");
162            showHideTag.setAlt(toggle+" transaction details");
163            showHideTag.setTitle(toggle+" transaction details");
164            
165            showHideTag.doStartTag();
166            showHideTag.doEndTag();
167        }
168        
169        /**
170         * Builds the ending of the toggle row
171         * @return the ending of the toggle row expressed as HTML
172         */
173        protected String buildSubheadingWithDetailToggleRowEnding() {
174            StringBuilder row = new StringBuilder();
175            row.append("\t\t\t</span>\n");
176            row.append("\t\t</td>\n");
177            row.append("\t</tr>\n");
178            return row.toString();
179        }
180    
181        /**
182         * Gets the accountingLineImportInstructionsUrl attribute. 
183         * @return Returns the accountingLineImportInstructionsUrl.
184         */
185        public String getAccountingLineImportInstructionsUrl() {
186            return accountingLineImportInstructionsUrl;
187        }
188    
189        /**
190         * Sets the accountingLineImportInstructionsUrl attribute value.
191         * @param accountingLineImportInstructionsUrl The accountingLineImportInstructionsUrl to set.
192         */
193        public void setAccountingLineImportInstructionsUrl(String accountingLineImportInstructionsUrl) {
194            this.accountingLineImportInstructionsUrl = accountingLineImportInstructionsUrl;
195        }
196    
197        /**
198         * Gets the cellCount attribute. 
199         * @return Returns the cellCount.
200         */
201        public int getCellCount() {
202            return cellCount;
203        }
204    
205        /**
206         * Sets the cellCount attribute value.
207         * @param cellCount The cellCount to set.
208         */
209        public void setCellCount(int cellCount) {
210            this.cellCount = cellCount;
211        }
212    
213        /**
214         * Gets the hideDetails attribute. 
215         * @return Returns the hideDetails.
216         */
217        public boolean getHideDetails() {
218            return hideDetails;
219        }
220    
221        /**
222         * Sets the hideDetails attribute value.
223         * @param hideDetails The hideDetails to set.
224         */
225        public void setHideDetails(boolean hideDetails) {
226            this.hideDetails = hideDetails;
227        }
228    }