View Javadoc
1   /**
2    * Copyright 2005-2014 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.field;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21  import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
22  import org.kuali.rice.krad.datadictionary.validator.Validator;
23  import org.kuali.rice.krad.uif.component.Component;
24  import org.kuali.rice.krad.uif.element.Link;
25  import org.kuali.rice.krad.uif.util.LifecycleElement;
26  import org.kuali.rice.krad.uif.widget.LightBox;
27  
28  /**
29   * Field that encloses a link element
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  @BeanTag(name = "linkField-bean", parent = "Uif-LinkField")
34  public class LinkField extends FieldBase {
35      private static final long serialVersionUID = -1908504471910271148L;
36  
37      private Link link;
38  
39      public LinkField() {
40          super();
41      }
42  
43      /**
44       * The following initialization is performed:
45       *
46       * <ul>
47       * <li>Set the linkLabel if blank to the Field label</li>
48       * </ul>
49       *
50       * {@inheritDoc}
51       */
52      @Override
53      public void performInitialization(Object model) {
54          super.performInitialization(model);
55  
56          if (StringUtils.isBlank(getLinkText())) {
57              setLinkText(this.getLabel());
58          }
59      }
60  
61      /**
62       * PerformFinalize override - calls super, corrects the field's Label for attribute to point to this field's
63       * content
64       *
65       * @param view the view
66       * @param model the model
67       * @param parent the parent component
68       */
69      @Override
70      public void performFinalize(Object model, LifecycleElement parent) {
71          super.performFinalize(model, parent);
72  
73          //determine what id to use for the for attribute of the label, if present
74          if (this.getFieldLabel() != null && this.getLink() != null && StringUtils.isNotBlank(this.getLink().getId())) {
75              this.getFieldLabel().setLabelForComponentId(this.getLink().getId());
76          }
77      }
78  
79      /**
80       * Returns the <code>Link</code> field.
81       *
82       * @return The Link field
83       */
84      @BeanTagAttribute(name="link",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
85      public Link getLink() {
86          return link;
87      }
88  
89      /**
90       * Setter for the <code>Link</code>  component.
91       *
92       * @param link
93       */
94      public void setLink(Link link) {
95          this.link = link;
96      }
97  
98      /**
99       * Returns the label of the <code>Link</code> field that will be used to render the label of the link.
100      *
101      * @return The link label
102      */
103     @BeanTagAttribute(name="linkText")
104     public String getLinkText() {
105         return link.getLinkText();
106     }
107 
108     /**
109      * Setter for the link label. Sets the value on the <code>Link</code> field.
110      *
111      * @param linkLabel
112      */
113     public void setLinkText(String linkLabel) {
114         link.setLinkText(linkLabel);
115     }
116 
117     /**
118      * Returns the target of the <code>Link</code> field that will be used to specify where to open the href.
119      *
120      * @return The target
121      */
122     @BeanTagAttribute(name="target")
123     public String getTarget() {
124         return link.getTarget();
125     }
126 
127     /**
128      * Setter for the link target. Sets the value on the <code>Link</code> field.
129      *
130      * @param target
131      */
132     public void setTarget(String target) {
133         link.setTarget(target);
134     }
135 
136     /**
137      * Returns the href text of the <code>Link</code> field.
138      *
139      * @return The href text
140      */
141     @BeanTagAttribute(name="href")
142     public String getHref() {
143         return link.getHref();
144     }
145 
146     /**
147      * Setter for the hrefText. Sets the value on the <code>Link</code> field.
148      *
149      * @param hrefText
150      */
151     public void setHref(String hrefText) {
152         link.setHref(hrefText);
153     }
154 
155     /**
156      * Setter for the lightBox
157      *
158      * @param lightBox
159      */
160     public void setLightBox(LightBox lightBox) {
161         if (link != null) {
162             link.setLightBox(lightBox);
163         }
164     }
165 
166     /**
167      * Returns the <code>LightBox</code> used to open the link in
168      *
169      * @return The <code>LightBox</code>
170      */
171     @BeanTagAttribute(name="lightBox",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
172     public LightBox getLightBox() {
173         if (link != null) {
174             return link.getLightBox();
175         }
176 
177         return null;
178     }
179 
180     /**
181      * {@inheritDoc}
182      */
183     @Override
184     public void completeValidation(ValidationTrace tracer){
185         tracer.addBean(this);
186 
187         // Checks that the link is set
188         if(getLink()==null){
189             if(Validator.checkExpressions(this, "link")){
190                 String currentValues [] = {"link = "+getLink()};
191                 tracer.createError("Link should be set",currentValues);
192             }
193         }
194 
195         // Checks that the label is set
196         if(getLabel()==null){
197             if(Validator.checkExpressions(this, "label")){
198                 String currentValues [] = {"label ="+getLabel(),"link ="+getLink()};
199                 tracer.createWarning("Label is null, link should be used instead",currentValues);
200             }
201         }
202 
203         super.completeValidation(tracer.getCopy());
204     }
205 }