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.text.MessageFormat;
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.kfs.sys.KFSKeyConstants;
027    import org.kuali.kfs.sys.context.SpringContext;
028    import org.kuali.kfs.sys.document.web.util.RendererUtil;
029    import org.kuali.rice.kns.service.KualiConfigurationService;
030    import org.kuali.rice.kns.util.KNSConstants;
031    
032    /**
033     * Renders a quick field for an element
034     */
035    public class QuickFinderRenderer extends FieldRendererBase {
036        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(QuickFinderRenderer.class);
037        
038        private int tabIndex = -1;
039        
040        /**
041         * Gets the tabIndex attribute. 
042         * @return Returns the tabIndex.
043         */
044        public int getTabIndex() {
045            return tabIndex;
046        }
047    
048        /**
049         * Sets the tabIndex attribute value.
050         * @param tabIndex The tabIndex to set.
051         */
052        public void setTabIndex(int tabIndex) {
053            this.tabIndex = tabIndex;
054        }
055    
056        /**
057         * Renders the quick finder to the page context
058         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag, org.kuali.rice.kns.bo.BusinessObject)
059         */
060        public void render(PageContext pageContext, Tag parentTag) throws JspException {
061            JspWriter out = pageContext.getOut();
062            try {
063                out.write(buildQuickFinderHtml(pageContext));
064            } catch (IOException ioe) {
065                throw new JspException("Cannot render quick finder for field "+getField(), ioe);
066            }
067        }
068        
069        /**
070         * Creates the HTML for a quick finder icon
071         * @param businessObjectToRender the business object we're rendering
072         * @return the html for the quick finder
073         */
074        protected String buildQuickFinderHtml(PageContext pageContext) {
075            StringBuilder quickFinderHtml = new StringBuilder();
076            quickFinderHtml.append("&nbsp;<input type=\"image\" ");
077            //quickFinderHtml.append("tabindex=\"${tabindex}\" ");
078            quickFinderHtml.append("name=\"").append(buildQuickFinderName(pageContext)).append("\" ");
079            
080            quickFinderHtml.append("src=\"");
081            quickFinderHtml.append(SpringContext.getBean(KualiConfigurationService.class).getPropertyString("kr.externalizable.images.url"));
082            quickFinderHtml.append("searchicon.gif");
083            quickFinderHtml.append("\" ");
084            
085            quickFinderHtml.append("border=\"0\" ");
086            
087            quickFinderHtml.append("class=\"tinybutton\" ");
088            
089            quickFinderHtml.append("valign=\"middle\" ");
090            
091            quickFinderHtml.append("alt=\"");
092            quickFinderHtml.append(getAccessibleTitle());
093            quickFinderHtml.append("\" ");
094            
095            quickFinderHtml.append("title=\"");
096            quickFinderHtml.append(getAccessibleTitle());
097            quickFinderHtml.append("\" ");
098            
099            if (tabIndex > -1) {
100                quickFinderHtml.append(" tabIndex=\"");
101                quickFinderHtml.append(getTabIndex());
102                quickFinderHtml.append("\"");
103            }
104            
105            quickFinderHtml.append("/> ");
106            
107            return quickFinderHtml.toString();
108        }
109        
110        /**
111         * Builds the (quite complex) name for the quick finder field
112         * @return the name of the quick finder field
113         */
114        protected String buildQuickFinderName(PageContext pageContext) {
115            StringBuilder nameBuf = new StringBuilder();
116            nameBuf.append("methodToCall.performLookup.");
117            
118            nameBuf.append(KNSConstants.METHOD_TO_CALL_BOPARM_LEFT_DEL);
119            nameBuf.append(getField().getQuickFinderClassNameImpl());
120            nameBuf.append(KNSConstants.METHOD_TO_CALL_BOPARM_RIGHT_DEL);
121            nameBuf.append(".");
122            
123            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM1_LEFT_DEL);
124            nameBuf.append(getField().getFieldConversions());
125            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM1_RIGHT_DEL);
126            nameBuf.append(".");
127            
128            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM2_LEFT_DEL);
129            nameBuf.append(getField().getLookupParameters());
130            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM2_RIGHT_DEL);
131            nameBuf.append(".");
132            
133            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM3_LEFT_DEL+KNSConstants.METHOD_TO_CALL_PARM3_RIGHT_DEL+"."); // hide return link
134            
135            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM4_LEFT_DEL+KNSConstants.METHOD_TO_CALL_PARM4_RIGHT_DEL+"."); // extra button source
136            
137            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM5_LEFT_DEL+KNSConstants.METHOD_TO_CALL_PARM5_RIGHT_DEL+"."); // extra button params
138            
139            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM7_LEFT_DEL+KNSConstants.METHOD_TO_CALL_PARM7_RIGHT_DEL+"."); // supress actions
140            
141            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM8_LEFT_DEL+KNSConstants.METHOD_TO_CALL_PARM8_RIGHT_DEL+"."); // read only fields
142            
143            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM10_LEFT_DEL);
144            nameBuf.append(getField().getReferencesToRefresh());
145            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM10_RIGHT_DEL+".");
146            
147            nameBuf.append(KNSConstants.METHOD_TO_CALL_PARM9_LEFT_DEL+KNSConstants.METHOD_TO_CALL_PARM9_RIGHT_DEL+"."); // auto-search
148            
149            nameBuf.append("anchor"); // anchor
150            
151            String name = nameBuf.toString();
152            RendererUtil.registerEditableProperty(pageContext, name);
153            return name;
154        }
155    
156        /**
157         * A quick finder for a quick finder?  I fear not
158         * @see org.kuali.kfs.sys.document.web.renderers.FieldRenderer#renderQuickfinder()
159         */
160        public boolean renderQuickfinder() {
161            return false;
162        }
163    
164        /**
165         * Clears the tab index
166         * @see org.kuali.kfs.sys.document.web.renderers.FieldRendererBase#clear()
167         */
168        @Override
169        public void clear() {
170            super.clear();
171            tabIndex = -1;
172        }
173    
174        /**
175         * Overridden to format into message automatically, so there's a "Search" in front of the field label name
176         * @see org.kuali.kfs.sys.document.web.renderers.FieldRendererBase#setAccessibleTitle(java.lang.String)
177         */
178        @Override
179        public void setAccessibleTitle(String accessibleTitle) {
180            final String messagePattern = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSKeyConstants.LABEL_ACCOUNTING_LINE_QUICKFINDER_ACCESSIBLE_TITLE);
181            final String formattedAccessibleTitle = MessageFormat.format(messagePattern, accessibleTitle);
182            super.setAccessibleTitle(formattedAccessibleTitle);
183        }
184    
185    }