View Javadoc

1   /**
2    * Copyright 2005-2012 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.control;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.validator.ErrorReport;
20  import org.kuali.rice.krad.datadictionary.validator.TracerToken;
21  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22  import org.kuali.rice.krad.uif.component.Component;
23  import org.kuali.rice.krad.uif.element.ContentElementBase;
24  import org.kuali.rice.krad.uif.service.ExpressionEvaluatorService;
25  import org.kuali.rice.krad.uif.util.ExpressionUtils;
26  import org.kuali.rice.krad.uif.view.View;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * Base class for all <code>Control</code> implementations
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   * @see org.kuali.rice.krad.uif.control.Control
36   */
37  public abstract class ControlBase extends ContentElementBase implements Control {
38      private static final long serialVersionUID = -7898244978136312663L;
39  
40      private int tabIndex;
41  
42      private boolean disabled;
43      private String disabledExpression;
44      private String disabledReason;
45      private boolean evaluateDisabledOnKeyUp;
46      
47      private String disabledConditionJs;
48      private List<String> disabledConditionControlNames;
49  
50      private List<String> disabledWhenChangedPropertyNames;
51      private List<String> enabledWhenChangedPropertyNames;
52  
53      public ControlBase() {
54          super();
55  
56          disabled = false;
57          disabledWhenChangedPropertyNames = new ArrayList<String>();
58          enabledWhenChangedPropertyNames = new ArrayList<String>();
59      }
60  
61      /**
62       * Sets the disabledExpression, if any, evaluates it and sets the disabled property
63       *
64       * @param view - view instance to which the component belongs
65       * @param model - Top level object containing the data (could be the form or a
66       * @param parent
67       */
68      public void performApplyModel(View view, Object model, Component parent) {
69          super.performApplyModel(view, model, parent);
70          
71          disabledExpression = this.getPropertyExpression("disabled");
72          if(disabledExpression != null){
73              ExpressionEvaluatorService expressionEvaluatorService =
74                                  KRADServiceLocatorWeb.getExpressionEvaluatorService();
75              disabledExpression = expressionEvaluatorService.replaceBindingPrefixes(view, this,
76                      disabledExpression);
77              disabled = (Boolean) expressionEvaluatorService.evaluateExpression(model, this.getContext(),
78                      disabledExpression);
79          }
80      }
81  
82      /**
83       * Parses the disabled expressions, if any, to equivalent javascript and evaluates the disable/enable when
84       * changed property names.
85       *
86       * @param view - view instance that should be finalized for rendering
87       * @param model - top level object containing the data
88       * @param parent - parent component
89       */
90      public void performFinalize(View view, Object model, Component parent) {
91          super.performApplyModel(view, model, parent);
92  
93          if (StringUtils.isNotEmpty(disabledExpression) && !disabledExpression.equalsIgnoreCase("true")
94                  && !disabledExpression.equalsIgnoreCase("false")) {
95              disabledConditionControlNames = new ArrayList<String>();
96              disabledConditionJs = ExpressionUtils.parseExpression(disabledExpression,
97                      disabledConditionControlNames);
98          }
99  
100         List<String> adjustedDisablePropertyNames = new ArrayList<String>();
101         for (String propertyName : disabledWhenChangedPropertyNames) {
102             adjustedDisablePropertyNames.add(
103                     KRADServiceLocatorWeb.getExpressionEvaluatorService().replaceBindingPrefixes(view, this,
104                             propertyName));
105         }
106         disabledWhenChangedPropertyNames = adjustedDisablePropertyNames;
107 
108         List<String> adjustedEnablePropertyNames = new ArrayList<String>();
109         for (String propertyName : enabledWhenChangedPropertyNames) {
110             adjustedEnablePropertyNames.add(
111                     KRADServiceLocatorWeb.getExpressionEvaluatorService().replaceBindingPrefixes(view, this,
112                             propertyName));
113         }
114         enabledWhenChangedPropertyNames = adjustedEnablePropertyNames;
115     }
116 
117     /**
118      * @see org.kuali.rice.krad.uif.component.Component#getComponentTypeName()
119      */
120     @Override
121     public final String getComponentTypeName() {
122         return "control";
123     }
124 
125     /**
126      * @see Control#getTabIndex()
127      */
128     public int getTabIndex() {
129         return this.tabIndex;
130     }
131 
132     /**
133      * @see Control#setTabIndex(int)
134      */
135     public void setTabIndex(int tabIndex) {
136         this.tabIndex = tabIndex;
137     }
138 
139     /**
140      * @see Control#isDisabled()
141      */
142     public boolean isDisabled() {
143         return disabled;
144     }
145 
146     /**
147      * @see Control#setDisabled(boolean)
148      */
149     public void setDisabled(boolean disabled) {
150         this.disabled = disabled;
151     }
152 
153     /**
154      * @see Control#getDisabledReason()
155      */
156     public String getDisabledReason() {
157         return disabledReason;
158     }
159 
160     /**
161      * @see Control#setDisabledReason(java.lang.String)
162      */
163     public void setDisabledReason(String disabledReason) {
164         this.disabledReason = disabledReason;
165     }
166 
167     /**
168      * Returns js that will add data to this component by the element which matches its id.
169      *
170      * <p> This will return script for all the data elements since this component is implemented as a spring form:input
171      * tag
172      * that does not allow for the insertion of simple attributes. Therefore, the complex attributes script should
173      * include
174      * all the attributes since is it is inserted each time krad:template is used to display a control</p>
175      *
176      * @return jQuery data script for all data attributes
177      */
178     @Override
179     public String getComplexDataAttributesJs() {
180         /*TODO find out if all controls will need to override this. If not, uncomment and add the ones that need to the array
181         // classes which will exhibit the overriding behaviour
182         Class[] allowedcontrols = {TextAreaControl.class, TextControl.class, FileControl.class};
183         for (Class klass: allowedcontrols) {
184             if (klass.isAssignableFrom(this.getClass())) {
185                 return super.getAllDataAttributesJs();
186             }
187         }
188         return super.getComplexDataAttributesJs();*/
189         return super.getAllDataAttributesJs();
190     }
191 
192     /**
193      * @see org.kuali.rice.krad.uif.component.Component#completeValidation
194      */
195     @Override
196     public ArrayList<ErrorReport> completeValidation(TracerToken tracer){
197         ArrayList<ErrorReport> reports=new ArrayList<ErrorReport>();
198         tracer.addBean(this);
199 
200         reports.addAll(super.completeValidation(tracer.getCopy()));
201 
202         return reports;
203     }
204 
205 
206     /**
207      * Evaluate the disable condition on controls which disable it on each key up event
208      *
209      * @return true if evaluate on key up, false otherwise
210      */
211     public boolean isEvaluateDisabledOnKeyUp() {
212         return evaluateDisabledOnKeyUp;
213     }
214 
215     /**
216      * Set evaluateDisableOnKeyUp
217      *
218      * @param evaluateDisabledOnKeyUp
219      */
220     public void setEvaluateDisabledOnKeyUp(boolean evaluateDisabledOnKeyUp) {
221         this.evaluateDisabledOnKeyUp = evaluateDisabledOnKeyUp;
222     }
223 
224     /**
225      * Get the disable condition js derived from the springEL, cannot be set.
226      *
227      * @return the disableConditionJs javascript to be evaluated
228      */
229     public String getDisabledConditionJs() {
230         return disabledConditionJs;
231     }
232 
233     /**
234      * Control names to add handlers to for disable functionality, cannot be set
235      *
236      * @return control names to add handlers to for disable
237      */
238     public List<String> getDisabledConditionControlNames() {
239         return disabledConditionControlNames;
240     }
241 
242     /**
243      * Gets the property names of fields that when changed, will disable this component
244      *
245      * @return the property names to monitor for change to disable this component
246      */
247     public List<String> getDisabledWhenChangedPropertyNames() {
248         return disabledWhenChangedPropertyNames;
249     }
250 
251     /**
252      * Sets the property names of fields that when changed, will disable this component
253      *
254      * @param disabledWhenChangedPropertyNames
255      */
256     public void setDisabledWhenChangedPropertyNames(List<String> disabledWhenChangedPropertyNames) {
257         this.disabledWhenChangedPropertyNames = disabledWhenChangedPropertyNames;
258     }
259 
260     /**
261      * Gets the property names of fields that when changed, will enable this component
262      *
263      * @return the property names to monitor for change to enable this component
264      */
265     public List<String> getEnabledWhenChangedPropertyNames() {
266         return enabledWhenChangedPropertyNames;
267     }
268 
269     /**
270      * Sets the property names of fields that when changed, will enable this component
271      *
272      * @param enabledWhenChangedPropertyNames
273      */
274     public void setEnabledWhenChangedPropertyNames(List<String> enabledWhenChangedPropertyNames) {
275         this.enabledWhenChangedPropertyNames = enabledWhenChangedPropertyNames;
276     }
277 }