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;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import javax.servlet.jsp.JspException;
022    import javax.servlet.jsp.PageContext;
023    import javax.servlet.jsp.tagext.Tag;
024    
025    import org.kuali.kfs.sys.document.web.renderers.TableCellRenderer;
026    import org.kuali.kfs.sys.document.web.renderers.TableHeaderRenderer;
027    import org.kuali.rice.kns.web.ui.Field;
028    
029    /**
030     * A class which represents a number of fields held in a table cell.
031     */
032    public class AccountingLineTableCell implements RenderableElement {
033        private int rowSpan = 1;
034        private int colSpan = 1;
035        private List<RenderableElement> renderableElements = new ArrayList<RenderableElement>();
036        private boolean rendersAsHeader = false;
037        private AccountingLineRenderingContext renderingContext;
038        private boolean neverEmpty;
039        private String extraStyle;
040        private String styleClassOverride;
041        
042        /**
043         * Gets the colSpan attribute. 
044         * @return Returns the colSpan.
045         */
046        public int getColSpan() {
047            return colSpan;
048        }
049        /**
050         * Sets the colSpan attribute value.
051         * @param colSpan The colSpan to set.
052         */
053        public void setColSpan(int colSpan) {
054            if (colSpan > 1) {
055                this.colSpan = colSpan;
056            }
057        }
058    
059        /**
060         * Gets the rowSpan attribute. 
061         * @return Returns the rowSpan.
062         */
063        public int getRowSpan() {
064            return rowSpan;
065        }
066        /**
067         * Sets the rowSpan attribute value.
068         * @param rowSpan The rowSpan to set.
069         */
070        public void setRowSpan(int rowSpan) {
071            if (rowSpan > 1) {
072                this.rowSpan = rowSpan;
073            }
074        }
075        
076        /**
077         * Gets the renderableElement attribute. 
078         * @return Returns the renderableElement.
079         */
080        public List<RenderableElement> getRenderableElement() {
081            return renderableElements;
082        }
083        
084        /**
085         * Sets the renderableElement attribute value.
086         * @param renderableElement The renderableElement to set.
087         */
088        public void setRenderableElement(List<RenderableElement> renderableElement) {
089            this.renderableElements = renderableElement;
090        }
091        
092        /**
093         * Adds a renderable element to the elements this cell can render
094         * @param element another element to render within this cell
095         */
096        public void addRenderableElement(RenderableElement element) {
097            renderableElements.add(element);
098        }
099        
100        /**
101         * @see org.kuali.kfs.sys.document.web.RenderableElement#isHidden()
102         */
103        public boolean isHidden() {
104            for (RenderableElement element : renderableElements) {
105                if (!element.isHidden()) {
106                    return false;
107                }
108            }
109            return true;
110        }
111        
112        /**
113         * This is not an action block
114         * @see org.kuali.kfs.sys.document.web.RenderableElement#isActionBlock()
115         */
116        public boolean isActionBlock() {
117            return false;
118        }
119        
120        /**
121         * @see org.kuali.kfs.sys.document.web.RenderableElement#isEmpty()
122         */
123        public boolean isEmpty() {
124            if (neverEmpty) return false;
125            for (RenderableElement element : renderableElements) {
126                if (!element.isEmpty()) {
127                    return false;
128                }
129            }
130            return true;
131        }
132        
133        /**
134         * Gets the rendersAsHeader attribute. 
135         * @return Returns the rendersAsHeader.
136         */
137        public boolean isRendersAsHeader() {
138            return rendersAsHeader;
139        }
140        /**
141         * Sets the rendersAsHeader attribute value.
142         * @param rendersAsHeader The rendersAsHeader to set.
143         */
144        public void setRendersAsHeader(boolean rendersAsHeader) {
145            this.rendersAsHeader = rendersAsHeader;
146        }
147        
148        /**
149         * Gets the extraStyle attribute. 
150         * @return Returns the extraStyle.
151         */
152        public String getExtraStyle() {
153            return extraStyle;
154        }
155        /**
156         * Sets the extraStyle attribute value.
157         * @param extraStyle The extraStyle to set.
158         */
159        public void setExtraStyle(String extraStyle) {
160            this.extraStyle = extraStyle;
161        }
162        
163        /**
164         * Gets the neverEmpty attribute. 
165         * @return Returns the neverEmpty.
166         */
167        public boolean isNeverEmpty() {
168            return neverEmpty;
169        }
170        /**
171         * Sets the neverEmpty attribute value.
172         * @param neverEmpty The neverEmpty to set.
173         */
174        public void setNeverEmpty(boolean neverEmpty) {
175            this.neverEmpty = neverEmpty;
176        }
177        
178        /**
179         * Gets the styleClassOverride attribute. 
180         * @return Returns the styleClassOverride.
181         */
182        public String getStyleClassOverride() {
183            return styleClassOverride;
184        }
185        /**
186         * Sets the styleClassOverride attribute value.
187         * @param styleClassOverride The styleClassOverride to set.
188         */
189        public void setStyleClassOverride(String styleClassOverride) {
190            this.styleClassOverride = styleClassOverride;
191        }
192        /**
193         * @see org.kuali.kfs.sys.document.web.RenderableElement#renderElement(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
194         */
195        public void renderElement(PageContext pageContext, Tag parentTag, AccountingLineRenderingContext renderingContext) throws JspException {
196            TableCellRenderer renderer;
197            if (isRendersAsHeader()) {
198                renderer = new TableHeaderRenderer();
199            } else {
200                renderer = new TableCellRenderer();
201            }
202            this.renderingContext = renderingContext;
203            renderer.setCell(this);
204            renderer.render(pageContext, parentTag);
205            renderer.clear();
206            this.renderingContext = null;
207        }
208        
209        /**
210         * Requests that this cell render all of its children cells
211         * @param pageContext the page context of the rendering
212         * @param parentTag the parent tag requesting all this rendering
213         * @param accountingLine the accounting line getting rendered
214         * @param accountingLineProperty the property to get from the form to the accounting line
215         * @throws JspException Oh.  Shoot.  Something went...wrong.
216         */
217        public void renderChildrenElements(PageContext pageContext, Tag parentTag) throws JspException {
218            for (RenderableElement element : renderableElements) {
219                element.renderElement(pageContext, parentTag, renderingContext);
220            }
221        }
222        
223        /**
224         * Determines if this cell has child elements
225         * @return True if this cell has child elements, false otherwise
226         */
227        public boolean hasChildElements() {
228            return renderableElements != null && renderableElements.size() > 0;
229        }
230        
231        /**
232         * Goes through fields, appending field names
233         * @see org.kuali.kfs.sys.document.web.RenderableElement#appendFieldNames(java.util.List)
234         */
235        public void appendFields(List<Field> fields) {
236            for (RenderableElement element : renderableElements) {
237                element.appendFields(fields);
238            }
239        }
240        
241        /**
242         * @see org.kuali.kfs.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int[], int)
243         */
244        public void populateWithTabIndexIfRequested(int reallyHighIndex) {
245            for (RenderableElement element : renderableElements) {
246                element.populateWithTabIndexIfRequested(reallyHighIndex);
247            }
248        }
249        
250        /**
251         * Determines if this field is safe to remove. If any child elements are instances of FieldTableJoining, then it assumes not
252         * @return true if this cell is safe to remove; false otherwise
253         */
254        public boolean safeToRemove() {
255            for (RenderableElement element : renderableElements) {
256                if (element instanceof FieldTableJoining) return false;
257            }
258            return true;
259        }
260    }