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  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.PageContext;
22  import javax.servlet.jsp.tagext.Tag;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.rice.core.api.config.property.ConfigurationService;
27  import org.kuali.rice.kns.web.ui.Field;
28  import org.kuali.rice.krad.util.KRADConstants;
29  
30  /**
31   * Base class for all renderers which render fields
32   */
33  public abstract class FieldRendererBase implements FieldRenderer {
34      private Field field;
35      private String dynamicNameLabel;
36      private int arbitrarilyHighTabIndex = -1;
37      private String onBlur;
38      private boolean showError;
39      private String accessibleTitle;
40      private static String riceImageBase;
41  
42      /**
43       * Sets the field to render
44       * @see org.kuali.ole.sys.document.web.renderers.FieldRenderer#setField(org.kuali.rice.kns.web.ui.Field)
45       * 
46       * KRAD Conversion - Setting the field - No Use of data dictionary
47       */
48      public void setField(Field field) {
49          this.field = field;
50      }
51      
52      /**
53       * Returns the field to render
54       * @return the field to render
55       * 
56       * KRAD Conversion - Getting the field - No Use of data dictionary
57       */
58      public Field getField() {
59          return this.field;
60      }
61      
62      /**
63       * @return the name this field should have on the form
64       */
65      protected String getFieldName() {
66          if (!StringUtils.isBlank(field.getPropertyPrefix())) return field.getPropertyPrefix()+"."+field.getPropertyName();
67          return field.getPropertyName();
68      }
69  
70      /**
71       * Clears the field
72       * @see org.kuali.ole.sys.document.web.renderers.Renderer#clear()
73       */
74      public void clear() {
75          this.field = null;
76          this.arbitrarilyHighTabIndex = -1;
77          this.onBlur = null;
78      }
79      
80      /**
81       * Returns an accessible title for the field being rendered
82       * @return an accessible title for the field to render
83       */
84      protected String getAccessibleTitle() {
85          return accessibleTitle;
86      }
87      
88      /**
89       * Sets the accessible title of the current field 
90       * @param accessibleTitle the given the accessible title 
91       */
92      public void setAccessibleTitle(String accessibleTitle) {
93          this.accessibleTitle = accessibleTitle;
94      }
95      
96      /**
97       * Renders a quick finder for the field if one is warranted
98       * @param pageContext the page context to render to
99       * @param parentTag the parent tag requesting all of this rendering
100      * @param businessObjectToRender the business object that will be rendered
101      * @throws JspException thrown if something's off
102      */
103     protected void renderQuickFinderIfNecessary(PageContext pageContext, Tag parentTag) throws JspException {
104         if (!StringUtils.isBlank(getField().getQuickFinderClassNameImpl()) && renderQuickfinder()) {
105             QuickFinderRenderer renderer = new QuickFinderRenderer();
106             renderer.setField(getField());
107             renderer.setTabIndex(getQuickfinderTabIndex());
108             renderer.setAccessibleTitle(getField().getFieldLabel());
109             renderer.render(pageContext, parentTag);
110             renderer.clear();
111         }
112     }
113     
114     /**
115      * Writes the onblur call for the wrapped field
116      * @return a value for onblur=
117      */
118     protected String buildOnBlur() {
119         if (onBlur == null) {
120             StringBuilder onblur = new StringBuilder();
121             if (!StringUtils.isBlank(getField().getWebOnBlurHandler())) {
122                 onblur.append(getField().getWebOnBlurHandler());
123                 onblur.append("( this.name");
124                 if (!StringUtils.isBlank(getDynamicNameLabel())) {
125                     onblur.append(", '");
126                     onblur.append(getDynamicNameLabel());
127                     onblur.append("'");
128                 }
129                 onblur.append(" );");
130             }
131             onBlur = onblur.toString();
132         }
133         return onBlur;
134     }
135     
136     /**
137      * Overrides the onBlur setting for this renderer
138      * @param onBlur the onBlur value to set and return from buildOnBlur
139      */
140     public void overrideOnBlur(String onBlur) {
141         this.onBlur = onBlur;
142     }
143     
144     /**
145      * @return the dynamic name label field
146      */
147     protected String getDynamicNameLabel() {
148         return dynamicNameLabel;
149     }
150     
151     /** 
152      * @see org.kuali.ole.sys.document.web.renderers.FieldRenderer#setDynamicNameLabel(java.lang.String)
153      */
154     public void setDynamicNameLabel(String dynamicNameLabel) {
155         this.dynamicNameLabel = dynamicNameLabel;
156     }
157 
158     /**
159      * @see org.kuali.ole.sys.document.web.renderers.FieldRenderer#setArbitrarilyHighTabIndex(int)
160      */
161     public void setArbitrarilyHighTabIndex(int tabIndex) {
162         this.arbitrarilyHighTabIndex = tabIndex;   
163     }
164     
165     /**
166      * @return the tab index the quick finder should use - which, by default, is the arbitrarily high tab index
167      */
168     protected int getQuickfinderTabIndex() {
169         return arbitrarilyHighTabIndex;
170     }
171 
172     /**
173      * @see org.kuali.ole.sys.document.web.renderers.FieldRenderer#closeNoWrapSpan()
174      */
175     public void closeNoWrapSpan(PageContext pageContext, Tag parentTag) throws JspException {
176         try {
177             pageContext.getOut().write("</span>");
178         }
179         catch (IOException ioe) {
180             throw new JspException("Could not render closing of no-wrap span", ioe);
181         }
182     }
183 
184     /**
185      * @see org.kuali.ole.sys.document.web.renderers.FieldRenderer#openNoWrapSpan()
186      */
187     public void openNoWrapSpan(PageContext pageContext, Tag parentTag) throws JspException {
188         try {
189             pageContext.getOut().write("<span class=\"nowrap\">");
190         }
191         catch (IOException ioe) {
192             throw new JspException("Could not render opening of no-wrap span", ioe);
193         }
194     }
195 
196     /**
197      * Gets the showError attribute. 
198      * @return Returns the showError.
199      */
200     public boolean isShowError() {
201         return showError;
202     }
203 
204     /**
205      * Sets the showError attribute value.
206      * @param showError The showError to set.
207      */
208     public void setShowError(boolean showError) {
209         this.showError = showError;
210     }
211     
212     /**
213      * Renders the error icon
214      * @param pageContext the page context to render to
215      * @throws IOException thrown if the pageContext cannot be written to
216      */
217     protected void renderErrorIcon(PageContext pageContext) throws JspException {
218         try {
219             pageContext.getOut().write(getErrorIconImageTag());
220         }
221         catch (IOException ioe) {
222             throw new JspException("Could not render error icon", ioe);
223         }
224     }
225     
226     /**
227      * @return the tag for the error icon
228      */
229     protected String getErrorIconImageTag() {
230         return "<img src=\""+getErrorIconImageSrc()+"\" alt=\"error\" />";
231     }
232     
233     /**
234      * @return the source of the error icon
235      */
236     private String getErrorIconImageSrc() {
237         return getRiceImageBase()+"errormark.gif";
238     }
239     
240     /**
241      * @return the source of rice images
242      */
243     private String getRiceImageBase() {
244         if (riceImageBase == null) {
245             riceImageBase = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(KRADConstants.EXTERNALIZABLE_IMAGES_URL_KEY);
246         }
247         return riceImageBase;
248     }
249 }