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.JspWriter;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.Tag;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.ole.sys.OLEConstants;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.rice.core.api.config.property.ConfigurationService;
29  import org.kuali.rice.krad.util.KRADConstants;
30  
31  /**
32   * This renders a label (and not, as I was about to write labels a render).  It's main job
33   * is to render header cells on accounting lines.
34   */
35  public class LabelRenderer implements Renderer {
36      private boolean required = false;
37      private boolean readOnly = false;
38      private String label;
39      private String fullClassNameForHelp;
40      private String attributeEntryForHelp;
41      private String labelFor;
42  
43      /**
44       * Gets the attributeEntryForHelp attribute. 
45       * @return Returns the attributeEntryForHelp.
46       */
47      public String getAttributeEntryForHelp() {
48          return attributeEntryForHelp;
49      }
50  
51      /**
52       * Sets the attributeEntryForHelp attribute value.
53       * @param attributeEntryForHelp The attributeEntryForHelp to set.
54       */
55      public void setAttributeEntryForHelp(String attributeEntryForHelp) {
56          this.attributeEntryForHelp = attributeEntryForHelp;
57      }
58  
59      /**
60       * Gets the fullClassNameForHelp attribute. 
61       * @return Returns the fullClassNameForHelp.
62       */
63      public String getFullClassNameForHelp() {
64          return fullClassNameForHelp;
65      }
66  
67      /**
68       * Sets the fullClassNameForHelp attribute value.
69       * @param fullClassNameForHelp The fullClassNameForHelp to set.
70       */
71      public void setFullClassNameForHelp(String fullClassNameForHelp) {
72          this.fullClassNameForHelp = fullClassNameForHelp;
73      }
74  
75      /**
76       * Gets the label attribute. 
77       * @return Returns the label.
78       */
79      public String getLabel() {
80          return label;
81      }
82  
83      /**
84       * Sets the label attribute value.
85       * @param label The label to set.
86       */
87      public void setLabel(String label) {
88          this.label = label;
89      }
90  
91      /**
92       * Gets the required attribute. 
93       * @return Returns the required.
94       */
95      public boolean isRequired() {
96          return required;
97      }
98  
99      /**
100      * Sets the required attribute value.
101      * @param required The required to set.
102      */
103     public void setRequired(boolean required) {
104         this.required = required;
105     }
106 
107     /**
108      * Gets the readOnly attribute. 
109      * @return Returns the readOnly.
110      */
111     public boolean isReadOnly() {
112         return readOnly;
113     }
114 
115     /**
116      * Sets the readOnly attribute value.
117      * @param readOnly The readOnly to set.
118      */
119     public void setReadOnly(boolean readOnly) {
120         this.readOnly = readOnly;
121     }
122 
123     /**
124      * Gets the labelFor attribute. 
125      * @return Returns the labelFor.
126      */
127     public String getLabelFor() {
128         return labelFor;
129     }
130 
131     /**
132      * Sets the labelFor attribute value.
133      * @param labelFor The labelFor to set.
134      */
135     public void setLabelFor(String labelFor) {
136         this.labelFor = labelFor;
137     }
138 
139     /**
140      * 
141      * @see org.kuali.ole.sys.document.web.renderers.Renderer#clear()
142      */
143     public void clear() {
144         readOnly = false;
145         required = false;
146         label = null;
147         fullClassNameForHelp = null;
148         attributeEntryForHelp = null;
149         labelFor = null;
150     }
151 
152     private static String APPLICATION_URL;
153     
154     protected String getApplicationURL() {
155         if ( APPLICATION_URL == null ) {
156             APPLICATION_URL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(KRADConstants.APPLICATION_URL_KEY);
157         }
158         return APPLICATION_URL;
159     }
160     
161     /**
162      * 
163      * @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)
164      */
165     public void render(PageContext pageContext, Tag parentTag) throws JspException {
166         try {
167             JspWriter out = pageContext.getOut();
168             if (!StringUtils.isBlank(labelFor)) {
169                 out.write("<label for=\""+labelFor+"\">");
170             }
171             if (required) {
172                 out.write(OLEConstants.REQUIRED_FIELD_SYMBOL);
173                 out.write("&nbsp;");
174             }
175             if (!StringUtils.isBlank(fullClassNameForHelp) && !StringUtils.isBlank(attributeEntryForHelp)) {
176                 out.write("<a href=\"");
177                 out.write(getApplicationURL());
178                 out.write("/kr/help.do?methodToCall=getAttributeHelpText&amp;businessObjectClassName=");
179                 out.write(fullClassNameForHelp);
180                 out.write("&amp;attributeName=");
181                 out.write(attributeEntryForHelp);
182                 out.write("\" target=\"_blank\">");
183             }
184             out.write(label);
185             if (!StringUtils.isBlank(fullClassNameForHelp) && !StringUtils.isBlank(attributeEntryForHelp)) {
186                 out.write("</a>");
187             }
188             if (!StringUtils.isBlank(labelFor)) {
189                 out.write("</label>");
190             }
191         }
192         catch (IOException ioe) {
193             throw new JspException("Difficulty rendering label", ioe);
194         }
195     }
196 
197 }