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      protected boolean treatWildcardsAndOperatorsAsLiteral = false;
44  
45      public LookupInputField() {
46          super();
47      }
48  
49      /**
50       * @return the treatWildcardsAndOperatorsAsLiteral
51       */
52      public boolean isTreatWildcardsAndOperatorsAsLiteral() {
53          return this.treatWildcardsAndOperatorsAsLiteral;
54      }
55  
56      /**
57       * @param treatWildcardsAndOperatorsAsLiteral the treatWildcardsAndOperatorsAsLiteral to set
58       */
59      public void setTreatWildcardsAndOperatorsAsLiteral(boolean treatWildcardsAndOperatorsAsLiteral) {
60          this.treatWildcardsAndOperatorsAsLiteral = treatWildcardsAndOperatorsAsLiteral;
61      }
62  
63      /**
64       * Override of InputField copy to setup properties necessary to make the field usable for inputting
65       * search criteria
66       *
67       * @param attributeDefinition - AttributeDefinition instance the property values should be copied from
68       * @see InputField#copyFromAttributeDefinition(org.kuali.rice.krad.datadictionary.AttributeDefinition)
69       */
70      @Override
71      public void copyFromAttributeDefinition(View view, AttributeDefinition attributeDefinition) {
72          // label
73          if (StringUtils.isEmpty(getLabel())) {
74              setLabel(attributeDefinition.getLabel());
75          }
76  
77          // short label
78          if (StringUtils.isEmpty(getShortLabel())) {
79              setShortLabel(attributeDefinition.getShortLabel());
80          }
81  
82          // security
83          if (getAttributeSecurity() == null) {
84              setAttributeSecurity(attributeDefinition.getAttributeSecurity());
85          }
86  
87          // options
88          if (getOptionsFinder() == null) {
89              setOptionsFinder(attributeDefinition.getOptionsFinder());
90          }
91  
92          // TODO: what about formatter?
93  
94          // use control from dictionary if not specified and convert for searching
95          if (getControl() == null) {
96              Control control = convertControlToLookupControl(attributeDefinition);
97              view.assignComponentIds(control);
98  
99              setControl(control);
100         }
101 
102         // overwrite maxLength to allow for wildcards and ranges
103         setMaxLength(100);
104 
105         // set default value for active field to true
106         if (StringUtils.isEmpty(getDefaultValue())) {
107             if ((StringUtils.equals(getPropertyName(), KRADPropertyConstants.ACTIVE))) {
108                 setDefaultValue(KRADConstants.YES_INDICATOR_VALUE);
109             }
110         }
111 
112         /*
113            * TODO delyea: FieldUtils.createAndPopulateFieldsForLookup used to allow for a set of property names to be passed in via the URL
114            * parameters of the lookup url to set fields as 'read only'
115            */
116 
117     }
118 
119     /**
120      * If control definition is defined on the given attribute definition, converts to an appropriate control for
121      * searching (if necessary) and returns a copy for setting on the field
122      *
123      * @param attributeDefinition - attribute definition instance to retrieve control from
124      * @return Control instance or null if not found
125      */
126     protected static Control convertControlToLookupControl(AttributeDefinition attributeDefinition) {
127         if (attributeDefinition.getControlField() == null) {
128             return null;
129         }
130 
131         Control newControl = null;
132 
133         // convert checkbox to radio with yes/no/both options
134         if (CheckboxControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
135             newControl = ComponentFactory.getRadioGroupControlHorizontal();
136             List<KeyValue> options = new ArrayList<KeyValue>();
137             options.add(new ConcreteKeyValue("Y", "Yes"));
138             options.add(new ConcreteKeyValue("N", "No"));
139             options.add(new ConcreteKeyValue("", "Both"));
140 
141             ((RadioGroupControl) newControl).setOptions(options);
142         }
143         // text areas get converted to simple text inputs
144         else if (TextAreaControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
145             newControl = ComponentFactory.getTextControl();
146         } else {
147             newControl = ComponentUtils.copy(attributeDefinition.getControlField(), "");
148         }
149 
150         return newControl;
151     }
152 }