001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.uif.field;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.datadictionary.parse.BeanTag;
020    import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021    import org.kuali.rice.krad.datadictionary.validator.ErrorReport;
022    import org.kuali.rice.krad.datadictionary.validator.Validator;
023    import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
024    import org.kuali.rice.krad.uif.component.Component;
025    import org.kuali.rice.krad.uif.element.Link;
026    import org.kuali.rice.krad.uif.view.View;
027    import org.kuali.rice.krad.uif.widget.LightBox;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * Field that encloses a link element
034     *
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     */
037    @BeanTag(name = "linkField-bean", parent = "Uif-LinkField")
038    public class LinkField extends FieldBase {
039        private static final long serialVersionUID = -1908504471910271148L;
040    
041        private Link link;
042    
043        public LinkField() {
044            super();
045        }
046    
047        /**
048         * The following initialization is performed:
049         *
050         * <ul>
051         * <li>Set the linkLabel if blank to the Field label</li>
052         * </ul>
053         *
054         * @see org.kuali.rice.krad.uif.component.ComponentBase#performInitialization(org.kuali.rice.krad.uif.view.View,
055         *      java.lang.Object)
056         */
057        @Override
058        public void performInitialization(View view, Object model) {
059            super.performInitialization(view, model);
060    
061            if (StringUtils.isBlank(getLinkText())) {
062                setLinkText(this.getLabel());
063            }
064        }
065    
066        /**
067         * PerformFinalize override - calls super, corrects the field's Label for attribute to point to this field's
068         * content
069         *
070         * @param view the view
071         * @param model the model
072         * @param parent the parent component
073         */
074        @Override
075        public void performFinalize(View view, Object model, Component parent) {
076            super.performFinalize(view, model, parent);
077    
078            //determine what id to use for the for attribute of the label, if present
079            if (this.getFieldLabel() != null && this.getLink() != null && StringUtils.isNotBlank(this.getLink().getId())) {
080                this.getFieldLabel().setLabelForComponentId(this.getLink().getId());
081            }
082        }
083    
084        /**
085         * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
086         */
087        @Override
088        public List<Component> getComponentsForLifecycle() {
089            List<Component> components = super.getComponentsForLifecycle();
090    
091            components.add(link);
092    
093            return components;
094        }
095    
096        /**
097         * Returns the <code>Link</code> field.
098         *
099         * @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    }