View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.document.web.renderers;
17  
18  import java.io.IOException;
19  import java.text.MessageFormat;
20  
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.JspWriter;
23  import javax.servlet.jsp.PageContext;
24  import javax.servlet.jsp.tagext.Tag;
25  
26  import org.kuali.ole.sys.OLEKeyConstants;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.ole.sys.document.web.util.RendererUtil;
29  import org.kuali.rice.core.api.config.property.ConfigurationService;
30  import org.kuali.rice.krad.util.KRADConstants;
31  
32  /**
33   * Renders a quick field for an element
34   */
35  public class QuickFinderRenderer extends FieldRendererBase {
36      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(QuickFinderRenderer.class);
37      
38      private int tabIndex = -1;
39      
40      /**
41       * Gets the tabIndex attribute. 
42       * @return Returns the tabIndex.
43       */
44      public int getTabIndex() {
45          return tabIndex;
46      }
47  
48      /**
49       * Sets the tabIndex attribute value.
50       * @param tabIndex The tabIndex to set.
51       */
52      public void setTabIndex(int tabIndex) {
53          this.tabIndex = tabIndex;
54      }
55  
56      /**
57       * Renders the quick finder to the page context
58       * @see org.kuali.ole.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag, org.kuali.rice.krad.bo.BusinessObject)
59       */
60      public void render(PageContext pageContext, Tag parentTag) throws JspException {
61          JspWriter out = pageContext.getOut();
62          try {
63              out.write(buildQuickFinderHtml(pageContext));
64          } catch (IOException ioe) {
65              throw new JspException("Cannot render quick finder for field "+getField(), ioe);
66          }
67      }
68      
69      /**
70       * Creates the HTML for a quick finder icon
71       * @param businessObjectToRender the business object we're rendering
72       * @return the html for the quick finder
73       */
74      protected String buildQuickFinderHtml(PageContext pageContext) {
75          StringBuilder quickFinderHtml = new StringBuilder();
76          quickFinderHtml.append("&nbsp;<input type=\"image\" ");
77          //quickFinderHtml.append("tabindex=\"${tabindex}\" ");
78          quickFinderHtml.append("name=\"").append(buildQuickFinderName(pageContext)).append("\" ");
79          
80          quickFinderHtml.append("src=\"");
81          quickFinderHtml.append(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString("kr.externalizable.images.url"));
82          quickFinderHtml.append("searchicon.gif");
83          quickFinderHtml.append("\" ");
84          
85          quickFinderHtml.append("border=\"0\" ");
86          
87          quickFinderHtml.append("class=\"tinybutton\" ");
88          
89          quickFinderHtml.append("valign=\"middle\" ");
90          
91          quickFinderHtml.append("alt=\"");
92          quickFinderHtml.append(getAccessibleTitle());
93          quickFinderHtml.append("\" ");
94          
95          quickFinderHtml.append("title=\"");
96          quickFinderHtml.append(getAccessibleTitle());
97          quickFinderHtml.append("\" ");
98          
99          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(KRADConstants.METHOD_TO_CALL_BOPARM_LEFT_DEL);
119         nameBuf.append(getField().getQuickFinderClassNameImpl());
120         nameBuf.append(KRADConstants.METHOD_TO_CALL_BOPARM_RIGHT_DEL);
121         nameBuf.append(".");
122         
123         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM1_LEFT_DEL);
124         nameBuf.append(getField().getFieldConversions());
125         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM1_RIGHT_DEL);
126         nameBuf.append(".");
127         
128         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM2_LEFT_DEL);
129         nameBuf.append(getField().getLookupParameters());
130         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM2_RIGHT_DEL);
131         nameBuf.append(".");
132         
133         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM3_LEFT_DEL+KRADConstants.METHOD_TO_CALL_PARM3_RIGHT_DEL+"."); // hide return link
134         
135         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM4_LEFT_DEL+KRADConstants.METHOD_TO_CALL_PARM4_RIGHT_DEL+"."); // extra button source
136         
137         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM5_LEFT_DEL+KRADConstants.METHOD_TO_CALL_PARM5_RIGHT_DEL+"."); // extra button params
138         
139         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM7_LEFT_DEL+KRADConstants.METHOD_TO_CALL_PARM7_RIGHT_DEL+"."); // supress actions
140         
141         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM8_LEFT_DEL+KRADConstants.METHOD_TO_CALL_PARM8_RIGHT_DEL+"."); // read only fields
142         
143         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM10_LEFT_DEL);
144         nameBuf.append(getField().getReferencesToRefresh());
145         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM10_RIGHT_DEL+".");
146         
147         nameBuf.append(KRADConstants.METHOD_TO_CALL_PARM9_LEFT_DEL+KRADConstants.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.ole.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.ole.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.ole.sys.document.web.renderers.FieldRendererBase#setAccessibleTitle(java.lang.String)
177      */
178     @Override
179     public void setAccessibleTitle(String accessibleTitle) {
180         final String messagePattern = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEKeyConstants.LABEL_ACCOUNTING_LINE_QUICKFINDER_ACCESSIBLE_TITLE);
181         final String formattedAccessibleTitle = MessageFormat.format(messagePattern, accessibleTitle);
182         super.setAccessibleTitle(formattedAccessibleTitle);
183     }
184 
185 }