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    import java.util.List;
020    
021    import javax.servlet.jsp.JspException;
022    import javax.servlet.jsp.JspWriter;
023    import javax.servlet.jsp.PageContext;
024    import javax.servlet.jsp.tagext.Tag;
025    
026    import org.kuali.rice.kns.web.ui.Field;
027    
028    /**
029     * Renders a set of read only fields to a table cell
030     */
031    public class MultipleReadOnlyFieldsRenderer implements Renderer {
032        private List<Field> fields;
033        private ReadOnlyRenderer readOnlyRenderer = new ReadOnlyRenderer();
034    
035        /**
036         * 
037         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#clear()
038         */
039        public void clear() {
040            fields = null;
041        }
042    
043        /**
044         * 
045         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
046         */
047        public void render(PageContext pageContext, Tag parentTag) throws JspException {
048            try {
049                JspWriter out = pageContext.getOut();
050                if (fields != null && !fields.isEmpty()) {
051                    out.write(beginReadOnlyLayout());
052                    for (Field field : fields) {
053                        out.write(beginReadOnlyLabel());
054                        out.write(renderLabel(field));
055                        out.write(endReadOnlyLabel());
056                        out.write(beginReadOnlyValue());
057                        readOnlyRenderer.setField(field);
058                        if (field.getInquiryURL() != null) {
059                            readOnlyRenderer.setShouldRenderInquiry(true);
060                        }
061                        readOnlyRenderer.render(pageContext, parentTag);
062                        readOnlyRenderer.clear();
063                        out.write(endReadOnlyValue());
064                    }
065                    out.write(endReadOnlyLayout());
066                } else {
067                    out.write(renderEmptyCell());
068                }
069            }
070            catch (IOException ioe) {
071                throw new JspException("Could not render MultipleReadOnlyFields", ioe);
072            }
073        }
074        
075        /**
076         * @return the value to render for an empty cell
077         */
078        protected String renderEmptyCell() {
079            return "&nbsp;";
080        }
081        
082        protected String beginReadOnlyLayout() {
083            return "<table>";
084        }
085        
086        protected String beginReadOnlyLabel() {
087            return "<tr><td width=\"50%\">";
088        }
089        
090        protected String endReadOnlyLabel() {
091            return "</td>";
092        }
093        
094        protected String beginReadOnlyValue() {
095            return "<td width=\"50%\">";
096        }
097        
098        protected String endReadOnlyValue() {
099            return "</td></tr>";
100        }
101        
102        protected String renderLabel(Field field) {
103            return field.getFieldLabel();
104        }
105        
106        protected String endReadOnlyLayout() {
107            return "</table>";
108        }
109    
110        /**
111         * @return the current list of fields to render through this render pass
112         */
113        public List<Field> getFields() {
114            return fields;
115        }
116    
117        /**
118         * Associate fields with this render pass of the renderer
119         * @param fields the fields to render through this render pass
120         */
121        public void setFields(List<Field> fields) {
122            this.fields = fields;
123        }
124    
125    }