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.UifConstants;
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", parent = "Uif-LinkField")
34  public class LinkField extends FieldBase {
35      private static final long serialVersionUID = -1908504471910271148L;
36  
37      private Link link;
38  
39      private String sortAs;
40  
41      public LinkField() {
42          super();
43      }
44  
45      /**
46       * The following initialization is performed:
47       *
48       * <ul>
49       * <li>Set the linkLabel if blank to the Field label</li>
50       * </ul>
51       *
52       * {@inheritDoc}
53       */
54      @Override
55      public void performInitialization(Object model) {
56          super.performInitialization(model);
57  
58          if (StringUtils.isBlank(getLinkText())) {
59              setLinkText(this.getLabel());
60          }
61      }
62  
63      /**
64       * PerformFinalize override - calls super, corrects the field's Label for attribute to point to this field's
65       * content
66       *
67       * @param model the model
68       * @param parent the parent component
69       */
70      @Override
71      public void performFinalize(Object model, LifecycleElement parent) {
72          super.performFinalize(model, parent);
73  
74          //determine what id to use for the for attribute of the label, if present
75          if (this.getFieldLabel() != null && this.getLink() != null && StringUtils.isNotBlank(this.getLink().getId())) {
76              this.getFieldLabel().setLabelForComponentId(this.getLink().getId());
77          }
78      }
79  
80      /**
81       * Returns the <code>Link</code> field.
82       *
83       * @return The Link field
84       */
85      @BeanTagAttribute(type= BeanTagAttribute.AttributeType.DIRECTORBYTYPE)
86      public Link getLink() {
87          return link;
88      }
89  
90      /**
91       * Setter for the <code>Link</code>  component.
92       *
93       * @param link
94       */
95      public void setLink(Link link) {
96          this.link = link;
97      }
98  
99      /**
100      * Returns the label of the <code>Link</code> field that will be used to render the label of the link.
101      *
102      * @return The link label
103      */
104     @BeanTagAttribute
105     public String getLinkText() {
106         return link.getLinkText();
107     }
108 
109     /**
110      * Setter for the link label. Sets the value on the <code>Link</code> field.
111      *
112      * @param linkLabel
113      */
114     public void setLinkText(String linkLabel) {
115         link.setLinkText(linkLabel);
116     }
117 
118     /**
119      * The id of the DialogGroup to use when the openInDialog property is true for this LinkField's link.
120      *
121      * <p>The DialogGroup should only contain an iframe for its items.  When not set, a default dialog
122      * will be used.</p>
123      *
124      * @return the id of the dialog to use for this link
125      */
126     @BeanTagAttribute
127     public String getLinkDialogId() {
128         return link.getLinkDialogId();
129     }
130 
131     /**
132      * @see LinkField#getLinkDialogId()
133      */
134     public void setLinkDialogId(String linkDialogId) {
135         link.setLinkDialogId(linkDialogId);
136     }
137 
138     /**
139      * Indicates whether the link's URL should be opened in a dialog.
140      *
141      * <p>
142      * If set the target attribute is ignored and the URL is opened in a dialog instead.
143      * </p>
144      *
145      * @return true to open link in a dialog, false if not (follow standard target attribute)
146      */
147     @BeanTagAttribute
148     public boolean isOpenInDialog() {
149         return link.isOpenInDialog();
150     }
151 
152     /**
153      * @see LinkField#isOpenInDialog()
154      */
155     public void setOpenInDialog(boolean openInDialog) {
156         link.setOpenInDialog(openInDialog);
157     }
158 
159 
160     /**
161      * Returns the target of the <code>Link</code> field that will be used to specify where to open the href.
162      *
163      * @return The target
164      */
165     @BeanTagAttribute
166     public String getTarget() {
167         return link.getTarget();
168     }
169 
170     /**
171      * Setter for the link target. Sets the value on the <code>Link</code> field.
172      *
173      * @param target
174      */
175     public void setTarget(String target) {
176         link.setTarget(target);
177     }
178 
179     /**
180      * Returns the href text of the <code>Link</code> field.
181      *
182      * @return The href text
183      */
184     @BeanTagAttribute
185     public String getHref() {
186         return link.getHref();
187     }
188 
189     /**
190      * Setter for the hrefText. Sets the value on the <code>Link</code> field.
191      *
192      * @param hrefText
193      */
194     public void setHref(String hrefText) {
195         link.setHref(hrefText);
196     }
197 
198     @BeanTagAttribute(name = "sortAs")
199     public String getSortAs() {
200         return sortAs;
201     }
202 
203     public void setSortAs(String sortAs) {
204         if (!(sortAs.equals(UifConstants.TableToolsValues.DATE) || sortAs.equals(UifConstants.TableToolsValues.NUMERIC) || sortAs.equals(UifConstants.TableToolsValues.STRING))) {
205             throw new IllegalArgumentException("invalid sortAs value of " + sortAs + ", allowed: " + UifConstants.TableToolsValues.DATE + "|" + UifConstants.TableToolsValues.NUMERIC + "|" + UifConstants.TableToolsValues.STRING);
206         }
207         this.sortAs = sortAs;
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public void completeValidation(ValidationTrace tracer) {
215         tracer.addBean(this);
216 
217         // Checks that the link is set
218         if (getLink() == null) {
219             if (Validator.checkExpressions(this, "link")) {
220                 String currentValues[] = {"link = " + getLink()};
221                 tracer.createError("Link should be set", currentValues);
222             }
223         }
224 
225         // Checks that the label is set
226         if (getLabel() == null) {
227             if (Validator.checkExpressions(this, "label")) {
228                 String currentValues[] = {"label =" + getLabel(), "link =" + getLink()};
229                 tracer.createWarning("Label is null, link should be used instead", currentValues);
230             }
231         }
232 
233         super.completeValidation(tracer.getCopy());
234     }
235 }