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