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.element;
017    
018    import org.kuali.rice.krad.datadictionary.parse.BeanTag;
019    import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
020    import org.kuali.rice.krad.datadictionary.parse.BeanTags;
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.util.ComponentFactory;
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     * Content element that renders a link
034     *
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     */
037    @BeanTags({@BeanTag(name = "link-bean", parent="Uif-Link"), @BeanTag(name = "createNewLink-bean", parent = "Uif-CreateNewLink")})
038    public class Link extends ContentElementBase {
039        private static final long serialVersionUID = 8989868231938336068L;
040    
041        private String linkText;
042        private String target;
043        private String href;
044    
045        private boolean openInLightbox;
046    
047        private LightBox lightBox;
048    
049        public Link() {
050            super();
051        }
052    
053        /**
054         * The following updates are done here:
055         *
056         * <ul>
057         * <li>Initialize the nested lightBox widget if open in lightbox is true</li>
058         * </ul>
059         *
060         * @see org.kuali.rice.krad.uif.component.Component#performApplyModel(org.kuali.rice.krad.uif.view.View, java.lang.Object,
061         *      org.kuali.rice.krad.uif.component.Component)
062         */
063        @Override
064        public void performApplyModel(View view, Object model, Component parent) {
065            super.performApplyModel(view, model, parent);
066    
067            if (openInLightbox && (lightBox == null)) {
068                lightBox = ComponentFactory.getLightBox();
069            }
070        }
071    
072        /**
073         * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
074         */
075        @Override
076        public List<Component> getComponentsForLifecycle() {
077            List<Component> components = super.getComponentsForLifecycle();
078    
079            components.add(lightBox);
080    
081            return components;
082        }
083    
084        /**
085         * Returns the label of the link
086         *
087         * @return The link label
088         */
089        @BeanTagAttribute(name="linkText")
090        public String getLinkText() {
091            return linkText;
092        }
093    
094        /**
095         * Setter for the link label
096         *
097         * @param linkText
098         */
099        public void setLinkText(String linkText) {
100            this.linkText = linkText;
101        }
102    
103        /**
104         * Returns the target that will be used to specify where to open the href
105         *
106         * @return The target
107         */
108        @BeanTagAttribute(name="target")
109        public String getTarget() {
110            return target;
111        }
112    
113        /**
114         * Setter for the link target
115         *
116         * @param target
117         */
118        public void setTarget(String target) {
119            this.target = target;
120        }
121    
122        /**
123         * Returns the href text
124         *
125         * @return The href text
126         */
127        @BeanTagAttribute(name="href")
128        public String getHref() {
129            return href;
130        }
131    
132        /**
133         * Setter for the hrefText
134         *
135         * @param href
136         */
137        public void setHref(String href) {
138            this.href = href;
139        }
140    
141        /**
142         * Indicates whether the link URL should be opened in a lightbox
143         *
144         * <p>
145         * If set the target attribute is ignored and the URL is opened in a lightbox instead
146         * </p>
147         *
148         * @return boolean true to open link in a lightbox, false if not (follow standard target attribute)
149         */
150        public boolean isOpenInLightbox() {
151            return openInLightbox;
152        }
153    
154        /**
155         * Setter that indicates whether the link should be opened in a lightbox
156         *
157         * @param openInLightbox
158         */
159        public void setOpenInLightbox(boolean openInLightbox) {
160            this.openInLightbox = openInLightbox;
161        }
162    
163        /**
164         * Returns the <code>LightBox</code> used to open the link in
165         *
166         * @return The <code>LightBox</code>
167         */
168        @BeanTagAttribute(name="lightBox",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
169        public LightBox getLightBox() {
170            return lightBox;
171        }
172    
173        /**
174         * Setter for the lightBox
175         *
176         * @param lightBox
177         */
178        public void setLightBox(LightBox lightBox) {
179            this.lightBox = lightBox;
180        }
181    
182        /**
183         * @see org.kuali.rice.krad.uif.component.Component#completeValidation
184         */
185        @Override
186        public void completeValidation(ValidationTrace tracer){
187            ArrayList<ErrorReport> reports=new ArrayList<ErrorReport>();
188            tracer.addBean(this);
189    
190            if(tracer.getValidationStage()== ValidationTrace.BUILD){
191    
192                // Checks that href is set
193                if(getHref()==null){
194                    if(!Validator.checkExpressions(this, "href")){
195                        String currentValues [] = {"href ="+getHref()};
196                        tracer.createError("Href must be set",currentValues);
197                    }
198                }
199    
200                // Checks that the text is set
201                if(getLinkText()==null){
202                    if(!Validator.checkExpressions(this, "linkText")){
203                        String currentValues [] = {"linkText = "+getLinkText()};
204                        tracer.createError("LinkText must be set",currentValues);
205                    }
206                }
207    
208            }
209    
210            super.completeValidation(tracer.getCopy());
211        }
212    }