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.element;
17  
18  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTags;
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.UifConstants;
25  import org.kuali.rice.krad.uif.component.Component;
26  import org.kuali.rice.krad.uif.util.ComponentFactory;
27  import org.kuali.rice.krad.uif.view.View;
28  import org.kuali.rice.krad.uif.widget.LightBox;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * Content element that renders a link
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  @BeanTags({@BeanTag(name = "link-bean", parent="Uif-Link"), @BeanTag(name = "createNewLink-bean", parent = "Uif-CreateNewLink")})
39  public class Link extends ContentElementBase {
40      private static final long serialVersionUID = 8989868231938336068L;
41  
42      private String linkText;
43      private String target;
44      private String href;
45  
46      private boolean openInLightbox;
47  
48      private LightBox lightBox;
49  
50      public Link() {
51          super();
52      }
53  
54      /**
55       * The following updates are done here:
56       *
57       * <ul>
58       * <li>Initialize the nested lightBox widget if open in lightbox is true</li>
59       * </ul>
60       *
61       * @see org.kuali.rice.krad.uif.component.Component#performApplyModel(org.kuali.rice.krad.uif.view.View, java.lang.Object,
62       *      org.kuali.rice.krad.uif.component.Component)
63       */
64      @Override
65      public void performApplyModel(View view, Object model, Component parent) {
66          super.performApplyModel(view, model, parent);
67  
68          if (openInLightbox && (lightBox == null)) {
69              lightBox = ComponentFactory.getLightBox();
70          }
71      }
72  
73      /**
74       * Special handling for lightbox links to add and onclick data attribute to be handled by a global handler
75       */
76      @Override
77      public void performFinalize(View view, Object model, Component parent) {
78          super.performFinalize(view, model, parent);
79  
80          if (lightBox != null && lightBox.isRender()){
81              this.addDataAttribute(UifConstants.DataAttributes.ONCLICK, "handleLightboxOpen(jQuery(this), " +
82                      lightBox.getTemplateOptionsJSString() + ", " + lightBox.isAddAppParms() + ", e);");
83              lightBox.setRender(false);
84          }
85      }
86  
87      /**
88       * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
89       */
90      @Override
91      public List<Component> getComponentsForLifecycle() {
92          List<Component> components = super.getComponentsForLifecycle();
93  
94          components.add(lightBox);
95  
96          return components;
97      }
98  
99      /**
100      * Returns the label of the link
101      *
102      * @return The link label
103      */
104     @BeanTagAttribute(name="linkText")
105     public String getLinkText() {
106         return linkText;
107     }
108 
109     /**
110      * Setter for the link label
111      *
112      * @param linkText
113      */
114     public void setLinkText(String linkText) {
115         this.linkText = linkText;
116     }
117 
118     /**
119      * Returns the target that will be used to specify where to open the href
120      *
121      * @return The target
122      */
123     @BeanTagAttribute(name="target")
124     public String getTarget() {
125         return target;
126     }
127 
128     /**
129      * Setter for the link target
130      *
131      * @param target
132      */
133     public void setTarget(String target) {
134         this.target = target;
135     }
136 
137     /**
138      * Returns the href text
139      *
140      * @return The href text
141      */
142     @BeanTagAttribute(name="href")
143     public String getHref() {
144         return href;
145     }
146 
147     /**
148      * Setter for the hrefText
149      *
150      * @param href
151      */
152     public void setHref(String href) {
153         this.href = href;
154     }
155 
156     /**
157      * Indicates whether the link URL should be opened in a lightbox
158      *
159      * <p>
160      * If set the target attribute is ignored and the URL is opened in a lightbox instead
161      * </p>
162      *
163      * @return true to open link in a lightbox, false if not (follow standard target attribute)
164      */
165     public boolean isOpenInLightbox() {
166         return openInLightbox;
167     }
168 
169     /**
170      * Setter that indicates whether the link should be opened in a lightbox
171      *
172      * @param openInLightbox
173      */
174     public void setOpenInLightbox(boolean openInLightbox) {
175         this.openInLightbox = openInLightbox;
176     }
177 
178     /**
179      * Returns the <code>LightBox</code> used to open the link in
180      *
181      * @return The <code>LightBox</code>
182      */
183     @BeanTagAttribute(name="lightBox",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
184     public LightBox getLightBox() {
185         return lightBox;
186     }
187 
188     /**
189      * Setter for the lightBox
190      *
191      * @param lightBox
192      */
193     public void setLightBox(LightBox lightBox) {
194         this.lightBox = lightBox;
195     }
196 
197     /**
198      * @see org.kuali.rice.krad.uif.component.Component#completeValidation
199      */
200     @Override
201     public void completeValidation(ValidationTrace tracer){
202         ArrayList<ErrorReport> reports=new ArrayList<ErrorReport>();
203         tracer.addBean(this);
204 
205         if(tracer.getValidationStage()== ValidationTrace.BUILD){
206 
207             // Checks that href is set
208             if(getHref()==null){
209                 if(!Validator.checkExpressions(this, "href")){
210                     String currentValues [] = {"href ="+getHref()};
211                     tracer.createError("Href must be set",currentValues);
212                 }
213             }
214 
215             // Checks that the text is set
216             if(getLinkText()==null){
217                 if(!Validator.checkExpressions(this, "linkText")){
218                     String currentValues [] = {"linkText = "+getLinkText()};
219                     tracer.createError("LinkText must be set",currentValues);
220                 }
221             }
222 
223         }
224 
225         super.completeValidation(tracer.getCopy());
226     }
227 
228     /**
229      * @see org.kuali.rice.krad.uif.component.ComponentBase#copy()
230      */
231     @Override
232     protected <T> void copyProperties(T component) {
233         super.copyProperties(component);
234         Link linkCopy = (Link) component;
235         linkCopy.setHref(this.href);
236 
237         if (this.lightBox != null) {
238             linkCopy.setLightBox((LightBox)this.lightBox.copy());
239         }
240 
241         linkCopy.setLinkText(this.linkText);
242         linkCopy.setOpenInLightbox(this.openInLightbox);
243         linkCopy.setTarget(this.target);
244     }
245 }