View Javadoc

1   /**
2    * Copyright 2005-2011 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.core.api.util.ConcreteKeyValue;
20  import org.kuali.rice.core.api.util.KeyValue;
21  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
22  import org.kuali.rice.krad.uif.control.CheckboxControl;
23  import org.kuali.rice.krad.uif.control.Control;
24  import org.kuali.rice.krad.uif.control.RadioGroupControl;
25  import org.kuali.rice.krad.uif.control.TextAreaControl;
26  import org.kuali.rice.krad.uif.util.ComponentFactory;
27  import org.kuali.rice.krad.uif.util.ComponentUtils;
28  import org.kuali.rice.krad.uif.view.View;
29  import org.kuali.rice.krad.util.KRADConstants;
30  import org.kuali.rice.krad.util.KRADPropertyConstants;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  /**
36   * Custom <code>InputField</code> for search fields within a lookup view
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class LookupInputField extends InputField {
41      private static final long serialVersionUID = -8294275596836322699L;
42  
43      private boolean treatWildcardsAndOperatorsAsLiteral;
44      private boolean addAllOption;
45  
46      public LookupInputField() {
47          super();
48  
49          treatWildcardsAndOperatorsAsLiteral = false;
50          addAllOption = false;
51      }
52  
53      /**
54       * Override of InputField copy to setup properties necessary to make the field usable for inputting
55       * search criteria
56       *
57       * @param attributeDefinition - AttributeDefinition instance the property values should be copied from
58       * @see InputField#copyFromAttributeDefinition(org.kuali.rice.krad.datadictionary.AttributeDefinition)
59       */
60      @Override
61      public void copyFromAttributeDefinition(View view, AttributeDefinition attributeDefinition) {
62          // label
63          if (StringUtils.isEmpty(getLabel())) {
64              setLabel(attributeDefinition.getLabel());
65          }
66  
67          // short label
68          if (StringUtils.isEmpty(getShortLabel())) {
69              setShortLabel(attributeDefinition.getShortLabel());
70          }
71  
72          // security
73          if (getAttributeSecurity() == null) {
74              setAttributeSecurity(attributeDefinition.getAttributeSecurity());
75          }
76  
77          // options
78          if (getOptionsFinder() == null) {
79              setOptionsFinder(attributeDefinition.getOptionsFinder());
80          }
81  
82          // TODO: what about formatter?
83  
84          // use control from dictionary if not specified and convert for searching
85          if (getControl() == null) {
86              Control control = convertControlToLookupControl(attributeDefinition);
87              view.assignComponentIds(control);
88  
89              setControl(control);
90          }
91  
92          // overwrite maxLength to allow for wildcards and ranges
93          setMaxLength(100);
94  
95          // set default value for active field to true
96          if (StringUtils.isEmpty(getDefaultValue())) {
97              if ((StringUtils.equals(getPropertyName(), KRADPropertyConstants.ACTIVE))) {
98                  setDefaultValue(KRADConstants.YES_INDICATOR_VALUE);
99              }
100         }
101 
102         /*
103            * TODO delyea: FieldUtils.createAndPopulateFieldsForLookup used to allow for a set of property names to be passed in via the URL
104            * parameters of the lookup url to set fields as 'read only'
105            */
106 
107     }
108 
109     /**
110      * If control definition is defined on the given attribute definition, converts to an appropriate control for
111      * searching (if necessary) and returns a copy for setting on the field
112      *
113      * @param attributeDefinition - attribute definition instance to retrieve control from
114      * @return Control instance or null if not found
115      */
116     protected static Control convertControlToLookupControl(AttributeDefinition attributeDefinition) {
117         if (attributeDefinition.getControlField() == null) {
118             return null;
119         }
120 
121         Control newControl = null;
122 
123         // convert checkbox to radio with yes/no/both options
124         if (CheckboxControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
125             newControl = ComponentFactory.getRadioGroupControlHorizontal();
126             List<KeyValue> options = new ArrayList<KeyValue>();
127             options.add(new ConcreteKeyValue("Y", "Yes"));
128             options.add(new ConcreteKeyValue("N", "No"));
129             options.add(new ConcreteKeyValue("", "Both"));
130 
131             ((RadioGroupControl) newControl).setOptions(options);
132         }
133         // text areas get converted to simple text inputs
134         else if (TextAreaControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
135             newControl = ComponentFactory.getTextControl();
136         } else {
137             newControl = ComponentUtils.copy(attributeDefinition.getControlField(), "");
138         }
139 
140         return newControl;
141     }
142 
143     /**
144      * @return the treatWildcardsAndOperatorsAsLiteral
145      */
146     public boolean isTreatWildcardsAndOperatorsAsLiteral() {
147         return this.treatWildcardsAndOperatorsAsLiteral;
148     }
149 
150     /**
151      * @param treatWildcardsAndOperatorsAsLiteral the treatWildcardsAndOperatorsAsLiteral to set
152      */
153     public void setTreatWildcardsAndOperatorsAsLiteral(boolean treatWildcardsAndOperatorsAsLiteral) {
154         this.treatWildcardsAndOperatorsAsLiteral = treatWildcardsAndOperatorsAsLiteral;
155     }
156 
157     /**
158      * Indicates whether the option for all values (blank key, 'All' label) should be added to the lookup
159      * field, note this is only supported for {@link org.kuali.rice.krad.uif.control.MultiValueControl} instance
160      *
161      * @return boolean true if all option should be added, false if not
162      */
163     public boolean isAddAllOption() {
164         return addAllOption;
165     }
166 
167     /**
168      * Setter for the add all option indicator
169      *
170      * @param addAllOption
171      */
172     public void setAddAllOption(boolean addAllOption) {
173         this.addAllOption = addAllOption;
174     }
175 }