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.TableRowRenderer;
026    import org.kuali.rice.kns.web.ui.Field;
027    
028    /**
029     * Represents a table row to display in an accounting view table.
030     */
031    public class AccountingLineTableRow implements RenderableElement {
032        private List<AccountingLineTableCell> cells;
033        private AccountingLineRenderingContext renderingContext;
034        
035        /**
036         * Constructs a AccountingLineTableRow
037         */
038        public AccountingLineTableRow() {
039            cells = new ArrayList<AccountingLineTableCell>();
040        }
041    
042        /**
043         * Gets the cells attribute. 
044         * @return Returns the cells.
045         */
046        public List<AccountingLineTableCell> getCells() {
047            return cells;
048        }
049    
050        /**
051         * Sets the cells attribute value.
052         * @param cells The cells to set.
053         */
054        public void setCells(List<AccountingLineTableCell> cells) {
055            this.cells = cells;
056        }
057        
058        /**
059         * Adds a new table cell to the row
060         * @param cell the cell to add to the row
061         */
062        public void addCell(AccountingLineTableCell cell) {
063            cells.add(cell);
064        }
065    
066        /**
067         * @see org.kuali.kfs.sys.document.web.RenderableElement#isHidden()
068         */
069        public boolean isHidden() {
070            for (AccountingLineTableCell cell : cells) {
071                if (!cell.isHidden()) {
072                    return false;
073                }
074            }
075            return true;
076        }
077    
078        /**
079         * This is not an action block
080         * @see org.kuali.kfs.sys.document.web.RenderableElement#isActionBlock()
081         */
082        public boolean isActionBlock() {
083            return false;
084        }
085    
086        /**
087         * @see org.kuali.kfs.sys.document.web.RenderableElement#isEmpty()
088         */
089        public boolean isEmpty() {
090            for (AccountingLineTableCell cell : cells) {
091                if (!cell.isEmpty()) {
092                    return false;
093                }
094            }
095            return true;
096        }
097    
098        /**
099         * @see org.kuali.kfs.sys.document.web.RenderableElement#renderElement(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
100         */
101        public void renderElement(PageContext pageContext, Tag parentTag, AccountingLineRenderingContext renderingContext) throws JspException {
102            TableRowRenderer renderer = new TableRowRenderer();
103            this.renderingContext = renderingContext;
104            renderer.setRow(this);
105            renderer.render(pageContext, parentTag);
106            renderer.clear();
107            this.renderingContext = null;
108        }
109        
110        /**
111         * Requests that the row renders all of its children cells
112         * @param pageContext the page contex to render to
113         * @param parentTag the tag requesting all this rendering
114         * @param accountingLine the accounting line to render
115         * @param accountingLineProperty the property from the form to the accounting line
116         * @throws JspException exception thrown when...something...goes, I don't know...wrong or somethin'
117         */
118        public void renderChildrenCells(PageContext pageContext, Tag parentTag) throws JspException {
119            for (AccountingLineTableCell cell : cells) {
120                cell.renderElement(pageContext, parentTag, renderingContext);
121            }
122        }
123        
124        /**
125         * Returns the number of children cells this row has
126         * @return the number of children cells this row has
127         */
128        public int getChildCellCount() {
129            return cells.size();
130        }
131        
132        /**
133         * Dutifully appends the names of any fields it knows about to the given List of field names
134         * @param fieldNames a List of field names to append other names to
135         */
136        public void appendFields(List<Field> fields) {
137            for (AccountingLineTableCell cell : cells) {
138                cell.appendFields(fields);
139            }
140        }
141    
142        /**
143         * @see org.kuali.kfs.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int[], int)
144         */
145        public void populateWithTabIndexIfRequested(int reallyHighIndex) {
146            for (AccountingLineTableCell cell : cells) {
147                cell.populateWithTabIndexIfRequested(reallyHighIndex);
148            }
149        }
150        
151        /**
152         * Determines whether each cell is safe to remove; if so, simply removes that cell
153         * @return true if the row can be safely removed; false otherwise
154         */
155        public boolean safeToRemove() {
156            for (AccountingLineTableCell cell : cells) {
157                if (!cell.safeToRemove()) return false;
158            }
159            return true;
160        }
161    }