001/**
002 * Copyright 2005-2018 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 */
016package org.kuali.rice.krad.uif.field;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.datadictionary.parse.BeanTag;
020import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
022import org.kuali.rice.krad.datadictionary.validator.Validator;
023import org.kuali.rice.krad.uif.UifConstants;
024import org.kuali.rice.krad.uif.element.Link;
025import org.kuali.rice.krad.uif.util.LifecycleElement;
026import org.kuali.rice.krad.uif.widget.LightBox;
027
028/**
029 * Field that encloses a link element.
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033@BeanTag(name = "linkField", parent = "Uif-LinkField")
034public class LinkField extends FieldBase {
035    private static final long serialVersionUID = -1908504471910271148L;
036
037    private Link link;
038
039    private String sortAs;
040
041    public LinkField() {
042        super();
043    }
044
045    /**
046     * The following initialization is performed:
047     *
048     * <ul>
049     * <li>Set the linkLabel if blank to the Field label</li>
050     * </ul>
051     *
052     * {@inheritDoc}
053     */
054    @Override
055    public void performInitialization(Object model) {
056        super.performInitialization(model);
057
058        if (StringUtils.isBlank(getLinkText())) {
059            setLinkText(this.getLabel());
060        }
061    }
062
063    /**
064     * PerformFinalize override - calls super, corrects the field's Label for attribute to point to this field's
065     * content
066     *
067     * @param model the model
068     * @param parent the parent component
069     */
070    @Override
071    public void performFinalize(Object model, LifecycleElement parent) {
072        super.performFinalize(model, parent);
073
074        //determine what id to use for the for attribute of the label, if present
075        if (this.getFieldLabel() != null && this.getLink() != null && StringUtils.isNotBlank(this.getLink().getId())) {
076            this.getFieldLabel().setLabelForComponentId(this.getLink().getId());
077        }
078    }
079
080    /**
081     * Returns the <code>Link</code> field.
082     *
083     * @return The Link field
084     */
085    @BeanTagAttribute(type= BeanTagAttribute.AttributeType.DIRECTORBYTYPE)
086    public Link getLink() {
087        return link;
088    }
089
090    /**
091     * Setter for the <code>Link</code>  component.
092     *
093     * @param link
094     */
095    public void setLink(Link link) {
096        this.link = link;
097    }
098
099    /**
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}