1 /**
2 * Copyright 2005-2012 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.rice.krad.uif.element;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.validator.ErrorReport;
20 import org.kuali.rice.krad.datadictionary.validator.RDValidator;
21 import org.kuali.rice.krad.datadictionary.validator.TracerToken;
22 import org.kuali.rice.krad.uif.component.Component;
23 import org.kuali.rice.krad.uif.util.ComponentFactory;
24 import org.kuali.rice.krad.uif.view.View;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 /**
30 * Content element that renders a HTML <code><IMG></code> tag
31 *
32 * @author Kuali Rice Team (rice.collab@kuali.org)
33 */
34 public class Image extends ContentElementBase {
35 private static final long serialVersionUID = -3911849875276940507L;
36
37 private String source;
38 private String altText;
39 private String height;
40 private String width;
41
42 private boolean captionHeaderPlacementAboveImage;
43
44 private String captionHeaderText;
45 private Header captionHeader;
46
47 private String cutlineText;
48 private Message cutlineMessage;
49
50 public Image() {
51 super();
52
53 altText = "";
54 }
55
56 /**
57 * The following initialization is performed:
58 *
59 * <ul>
60 * <li>Initializes the cutline message and caption header components if necessary</li>
61 * </ul>
62 *
63 * @see org.kuali.rice.krad.uif.component.ComponentBase#performInitialization(org.kuali.rice.krad.uif.view.View,
64 * java.lang.Object)
65 */
66 @Override
67 public void performInitialization(View view, Object model) {
68 super.performInitialization(view, model);
69
70 if ((StringUtils.isNotBlank(captionHeaderText) || (getPropertyExpression("captionHeaderText") != null)) && (
71 captionHeader == null)) {
72 captionHeader = ComponentFactory.getImageCaptionHeader();
73 view.assignComponentIds(captionHeader);
74 }
75
76 if ((StringUtils.isNotBlank(cutlineText) || (getPropertyExpression("cutlineText") != null)) && (cutlineMessage
77 == null)) {
78 cutlineMessage = ComponentFactory.getImageCutlineMessage();
79 view.assignComponentIds(cutlineMessage);
80 }
81 }
82
83 /**
84 * Performs the final lifecycle phase for this element.
85 *
86 * <p>
87 * Performs the following steps
88 * <ul>
89 * <li>Set the caption header text on the caption header</li>
90 * <li>Set the cutline text on the cutline message</li>
91 * </ul>
92 * </p>
93 *
94 * @see Component#performFinalize(org.kuali.rice.krad.uif.view.View, java.lang.Object, org.kuali.rice.krad.uif.component.Component)
95 */
96 @Override
97 public void performFinalize(View view, Object model, Component parent) {
98 super.performFinalize(view, model, parent);
99
100 if (StringUtils.isNotBlank(captionHeaderText)) {
101 captionHeader.setHeaderText(captionHeaderText);
102 }
103
104 if (StringUtils.isNotBlank(cutlineText)) {
105 cutlineMessage.setMessageText(cutlineText);
106 }
107 }
108
109 /**
110 * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
111 */
112 @Override
113 public List<Component> getComponentsForLifecycle() {
114 List<Component> components = super.getComponentsForLifecycle();
115
116 components.add(captionHeader);
117 components.add(cutlineMessage);
118
119 return components;
120 }
121
122 /**
123 * returns the URL of this image
124 *
125 * @return String containing the URL of this image.
126 */
127 public String getSource() {
128 return this.source;
129 }
130
131 /**
132 * Sets the URL of this image
133 *
134 * @param source - String representing the URL of this image
135 */
136 public void setSource(String source) {
137 this.source = source;
138 }
139
140 /**
141 * Provides alternate information for the image element
142 *
143 * <p>The altText property specifies an alternate text for an image. It is displayed by the browser
144 * if the image cannot be displayed. This is especially important for accessibility, because screen
145 * readers can't understand images, but rather will read aloud the alternative text assigned to them.
146 * <br>
147 * Some best practices:
148 * <ul>
149 * <li>spacer images, bullets, and icons should have the altText set to null or the empty string. This
150 * will prevent screen readers from announcing it.</li>
151 * <li>Make the altText message as short and succinct as possible</li>
152 * <li>Describe the content of the image and nothing more</li>
153 * </ul>
154 * </p>
155 *
156 * @return a String representing alternative information about this image
157 */
158 public String getAltText() {
159 return this.altText;
160 }
161
162 /**
163 * Sets the alternate text property for this image
164 *
165 * @param altText - a String containing the alternative information about the image
166 */
167 public void setAltText(String altText) {
168 this.altText = altText;
169 }
170
171 /**
172 * Returns the height style attribute of this image
173 *
174 * <p>
175 * The default unit of measure is pixels.<br>
176 * It is good practice to specify both the height and width attributes for an image.
177 * If these attributes are set, the space required for the image is reserved when the page is loaded.
178 * However, without these attributes, the browser does not know the size of the image. The effect will
179 * be that the page layout will change while the images load.
180 * </p>
181 *
182 * @return String containing of the height style attribute of this image
183 */
184 public String getHeight() {
185 return this.height;
186 }
187
188 /**
189 * Sets the height style attribute of the image.
190 *
191 * @param height - String containing the height of the image
192 */
193 public void setHeight(String height) {
194 this.height = height;
195 }
196
197 /**
198 * Returns the width style attribute of the image
199 *
200 * <p>
201 * The default unit of measure is pixels.<br>
202 * It is good practice to specify both the height and width attributes for an image.
203 * If these attributes are set, the space required for the image is reserved when the page is loaded.
204 * However, without these attributes, the browser does not know the size of the image. The effect will
205 * be that the page layout will change while the images load.
206 * <p>
207 *
208 * @return String containing the width of this image
209 */
210 public String getWidth() {
211 return width;
212 }
213
214 /**
215 * Sets the width style attribute of the image
216 *
217 * @param width - a String containing the width of this image
218 */
219 public void setWidth(String width) {
220 this.width = width;
221 }
222
223 /**
224 * Retrieves the caption text for this image
225 *
226 * <p>
227 * The caption text is a headline for the picture. It may be displayed either above or below the picture.
228 * </p>
229 *
230 * @return String containing the caption
231 */
232 public String getCaptionHeaderText() {
233 return captionHeaderText;
234 }
235
236 /**
237 * Sets the text displayed as of the caption for the picture
238 *
239 * @param captionHeaderText - String containing the caption text.
240 */
241 public void setCaptionHeaderText(String captionHeaderText) {
242 this.captionHeaderText = captionHeaderText;
243 }
244
245 /**
246 * Retrieves the {@link Header} component used to display the caption for this image
247 *
248 * @return Header component which wraps the caption text.
249 */
250 public Header getCaptionHeader() {
251 return captionHeader;
252 }
253
254 /**
255 * Sets the Header used to display the caption for this image
256 *
257 * @param captionHeader - Header component which wraps the caption text.
258 */
259 public void setCaptionHeader(Header captionHeader) {
260 this.captionHeader = captionHeader;
261 }
262
263 /**
264 * Retrieves the cutline text for this image
265 *
266 * <p>
267 * The cutline text give more detailed information about the picture. Generally it describes
268 * the who, what, where, when of this image.
269 * </p>
270 *
271 * @return String containing the cutline text.
272 */
273 public String getCutlineText() {
274 return cutlineText;
275 }
276
277 /**
278 * Sets the cutline text that describes this image
279 *
280 * @param cutlineText - String describing this image
281 */
282 public void setCutlineText(String cutlineText) {
283 this.cutlineText = cutlineText;
284 }
285
286 /**
287 * Gets the {@link Message} component used to display the cutline.
288 *
289 * <p>
290 * Wrapping the cutline text with a Message component allows styling of the cutline text.
291 * </p>
292 *
293 * @return Message component wrapping the cutline.
294 */
295 public Message getCutlineMessage() {
296 return cutlineMessage;
297 }
298
299 /**
300 * Sets the Message component used to display the cutline for this image
301 *
302 * @param cutlineMessage - Message
303 */
304 public void setCutlineMessage(Message cutlineMessage) {
305 this.cutlineMessage = cutlineMessage;
306 }
307
308 /**
309 * Specifies whether the image caption is to be displayed above or below the image
310 *
311 * @return true if the caption is to be displayed above the image. false if displayed below the image.
312 */
313 public boolean isCaptionHeaderPlacementAboveImage() {
314 return captionHeaderPlacementAboveImage;
315 }
316
317 /**
318 * Sets whether the image caption is to be displayed above or below the image
319 *
320 * @param captionHeaderPlacementAboveImage - true displays above image, false displays below image
321 */
322 public void setCaptionHeaderPlacementAboveImage(boolean captionHeaderPlacementAboveImage) {
323 this.captionHeaderPlacementAboveImage = captionHeaderPlacementAboveImage;
324 }
325
326 /**
327 * @see org.kuali.rice.krad.uif.component.Component#completeValidation
328 */
329 @Override
330 public ArrayList<ErrorReport> completeValidation(TracerToken tracer){
331 ArrayList<ErrorReport> reports=new ArrayList<ErrorReport>();
332 tracer.addBean(this);
333
334 // Checks that a source is set
335 if(getSource()==null){
336 if(!RDValidator.checkExpressions(this,"source")){
337 ErrorReport error = ErrorReport.createError("Source must be set",tracer);
338 error.addCurrentValue("source ="+getSource());
339 reports.add(error);
340 }
341 }
342
343 // Checks that alt text is set
344 if(getAltText().compareTo("")==0){
345 if(RDValidator.checkExpressions(this,"altText")){
346 ErrorReport error = ErrorReport.createWarning("Alt text should be set, violates accessibility standards if not set",tracer);
347 error.addCurrentValue("altText ="+getAltText());
348 reports.add(error);
349 }
350 }
351
352 reports.addAll(super.completeValidation(tracer.getCopy()));
353
354 return reports;
355 }
356 }