View Javadoc
1   /**
2    * Copyright 2005-2015 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.parse.BeanTag;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21  import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
22  import org.kuali.rice.krad.uif.UifConstants;
23  import org.kuali.rice.krad.uif.component.Component;
24  import org.kuali.rice.krad.uif.element.ContentElementBase;
25  import org.kuali.rice.krad.uif.view.ExpressionEvaluator;
26  import org.kuali.rice.krad.uif.util.ExpressionUtils;
27  import org.kuali.rice.krad.uif.view.View;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * Base class for all <code>Control</code> implementations
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   * @see org.kuali.rice.krad.uif.control.Control
37   */
38  @BeanTag(name = "controlBase-bean", parent = "Uif-ControlBase")
39  public abstract class ControlBase extends ContentElementBase implements Control {
40      private static final long serialVersionUID = -7898244978136312663L;
41  
42      private int tabIndex;
43  
44      private boolean disabled;
45      private String disabledExpression;
46      private String disabledReason;
47      private boolean evaluateDisabledOnKeyUp;
48  
49      private String disabledConditionJs;
50      private List<String> disabledConditionControlNames;
51  
52      private List<String> disabledWhenChangedPropertyNames;
53      private List<String> enabledWhenChangedPropertyNames;
54  
55      public ControlBase() {
56          super();
57  
58          disabled = false;
59          disabledWhenChangedPropertyNames = new ArrayList<String>();
60          enabledWhenChangedPropertyNames = new ArrayList<String>();
61      }
62  
63      /**
64       * Sets the disabledExpression, if any, evaluates it and sets the disabled property
65       *
66       * @param view view instance to which the component belongs
67       * @param model top level object containing the data (could be the form or a
68       * top level business object, dto)
69       * @param parent
70       */
71      public void performApplyModel(View view, Object model, Component parent) {
72          super.performApplyModel(view, model, parent);
73  
74          disabledExpression = this.getPropertyExpression("disabled");
75          if(disabledExpression != null){
76              ExpressionEvaluator expressionEvaluator =
77                      view.getViewHelperService().getExpressionEvaluator();
78  
79              disabledExpression = expressionEvaluator.replaceBindingPrefixes(view, this,
80                      disabledExpression);
81              disabled = (Boolean) expressionEvaluator.evaluateExpression(this.getContext(), disabledExpression);
82          }
83      }
84  
85      /**
86       * Parses the disabled expressions, if any, to equivalent javascript and evaluates the disable/enable when
87       * changed property names.
88       *
89       * @param view view instance that should be finalized for rendering
90       * @param model top level object containing the data
91       * @param parent parent component
92       */
93      public void performFinalize(View view, Object model, Component parent) {
94          super.performApplyModel(view, model, parent);
95  
96          ExpressionEvaluator expressionEvaluator =
97                  view.getViewHelperService().getExpressionEvaluator();
98  
99          if (StringUtils.isNotEmpty(disabledExpression) && !disabledExpression.equalsIgnoreCase("true")
100                 && !disabledExpression.equalsIgnoreCase("false")) {
101             disabledConditionControlNames = new ArrayList<String>();
102             disabledConditionJs = ExpressionUtils.parseExpression(disabledExpression,
103                     disabledConditionControlNames);
104         }
105 
106         List<String> adjustedDisablePropertyNames = new ArrayList<String>();
107         for (String propertyName : disabledWhenChangedPropertyNames) {
108             adjustedDisablePropertyNames.add(expressionEvaluator.replaceBindingPrefixes(view, this,
109                     propertyName));
110         }
111         disabledWhenChangedPropertyNames = adjustedDisablePropertyNames;
112 
113         List<String> adjustedEnablePropertyNames = new ArrayList<String>();
114         for (String propertyName : enabledWhenChangedPropertyNames) {
115             adjustedEnablePropertyNames.add(expressionEvaluator.replaceBindingPrefixes(view, this,
116                     propertyName));
117         }
118         enabledWhenChangedPropertyNames = adjustedEnablePropertyNames;
119 
120         // add control role
121         this.addDataAttribute(UifConstants.DataAttributes.ROLE, UifConstants.RoleTypes.CONTROL);
122     }
123 
124     /**
125      * @see org.kuali.rice.krad.uif.component.Component#getComponentTypeName()
126      */
127     @Override
128     public final String getComponentTypeName() {
129         return "control";
130     }
131 
132     /**
133      * @see org.kuali.rice.krad.uif.control.Control#getTabIndex()
134      */
135     @BeanTagAttribute(name="tabIndex")
136     public int getTabIndex() {
137         return this.tabIndex;
138     }
139 
140     /**
141      * @see org.kuali.rice.krad.uif.control.Control#setTabIndex(int)
142      */
143     public void setTabIndex(int tabIndex) {
144         this.tabIndex = tabIndex;
145     }
146 
147     /**
148      * @see org.kuali.rice.krad.uif.control.Control#isDisabled()
149      */
150     @BeanTagAttribute(name="disabled")
151     public boolean isDisabled() {
152         return disabled;
153     }
154 
155     /**
156      * @see org.kuali.rice.krad.uif.control.Control#setDisabled(boolean)
157      */
158     public void setDisabled(boolean disabled) {
159         this.disabled = disabled;
160     }
161 
162     /**
163      * @see org.kuali.rice.krad.uif.control.Control#getDisabledReason()
164      */
165     @BeanTagAttribute(name="disabledReason")
166     public String getDisabledReason() {
167         return disabledReason;
168     }
169 
170     /**
171      * @see org.kuali.rice.krad.uif.control.Control#setDisabledReason(java.lang.String)
172      */
173     public void setDisabledReason(String disabledReason) {
174         this.disabledReason = disabledReason;
175     }
176 
177     /**
178      * @see org.kuali.rice.krad.uif.component.Component#completeValidation
179      */
180     @Override
181     public void completeValidation(ValidationTrace tracer){
182         tracer.addBean(this);
183 
184         super.completeValidation(tracer.getCopy());
185     }
186 
187 
188     /**
189      * Evaluate the disable condition on controls which disable it on each key up event
190      *
191      * @return true if evaluate on key up, false otherwise
192      */
193     @BeanTagAttribute(name="evaluateDisabledOnKeyUp")
194     public boolean isEvaluateDisabledOnKeyUp() {
195         return evaluateDisabledOnKeyUp;
196     }
197 
198     /**
199      * Set evaluateDisableOnKeyUp
200      *
201      * @param evaluateDisabledOnKeyUp
202      */
203     public void setEvaluateDisabledOnKeyUp(boolean evaluateDisabledOnKeyUp) {
204         this.evaluateDisabledOnKeyUp = evaluateDisabledOnKeyUp;
205     }
206 
207     /**
208      * Get the disable condition js derived from the springEL, cannot be set.
209      *
210      * @return the disableConditionJs javascript to be evaluated
211      */
212     public String getDisabledConditionJs() {
213         return disabledConditionJs;
214     }
215 
216     /**
217      * Control names to add handlers to for disable functionality, cannot be set
218      *
219      * @return control names to add handlers to for disable
220      */
221     public List<String> getDisabledConditionControlNames() {
222         return disabledConditionControlNames;
223     }
224 
225     /**
226      * Gets the property names of fields that when changed, will disable this component
227      *
228      * @return the property names to monitor for change to disable this component
229      */
230     @BeanTagAttribute(name="disabledWhenChangedPropertyNames",type= BeanTagAttribute.AttributeType.LISTVALUE)
231     public List<String> getDisabledWhenChangedPropertyNames() {
232         return disabledWhenChangedPropertyNames;
233     }
234 
235     /**
236      * Sets the property names of fields that when changed, will disable this component
237      *
238      * @param disabledWhenChangedPropertyNames
239      */
240     public void setDisabledWhenChangedPropertyNames(List<String> disabledWhenChangedPropertyNames) {
241         this.disabledWhenChangedPropertyNames = disabledWhenChangedPropertyNames;
242     }
243 
244     /**
245      * Gets the property names of fields that when changed, will enable this component
246      *
247      * @return the property names to monitor for change to enable this component
248      */
249     @BeanTagAttribute(name="ensabledConditionControlNames",type= BeanTagAttribute.AttributeType.LISTVALUE)
250     public List<String> getEnabledWhenChangedPropertyNames() {
251         return enabledWhenChangedPropertyNames;
252     }
253 
254     /**
255      * Sets the property names of fields that when changed, will enable this component
256      *
257      * @param enabledWhenChangedPropertyNames
258      */
259     public void setEnabledWhenChangedPropertyNames(List<String> enabledWhenChangedPropertyNames) {
260         this.enabledWhenChangedPropertyNames = enabledWhenChangedPropertyNames;
261     }
262 
263     /**
264      * Sets the disabled expression
265      *
266      * @param disabledExpression
267      */
268     protected void setDisabledExpression(String disabledExpression) {
269         this.disabledExpression = disabledExpression;
270     }
271 
272     /**
273      * Sets the disabled condition javascript
274      *
275      * @param disabledConditionJs
276      */
277     protected void setDisabledConditionJs(String disabledConditionJs) {
278         this.disabledConditionJs = disabledConditionJs;
279     }
280 
281     /**
282      * Sets the disabled condition control names
283      *
284      * @param disabledConditionControlNames
285      */
286     protected void setDisabledConditionControlNames(List<String> disabledConditionControlNames) {
287         this.disabledConditionControlNames = disabledConditionControlNames;
288     }
289 
290     /**
291      * @see org.kuali.rice.krad.uif.component.ComponentBase#copy()
292      */
293     @Override
294     protected <T> void copyProperties(T component) {
295         super.copyProperties(component);
296         ControlBase controlBaseCopy = (ControlBase) component;
297         controlBaseCopy.setTabIndex(this.tabIndex);
298         controlBaseCopy.setDisabled(this.disabled);
299         controlBaseCopy.setDisabledExpression(this.disabledExpression);
300         controlBaseCopy.setDisabledReason(this.disabledReason);
301         controlBaseCopy.setEvaluateDisabledOnKeyUp(this.evaluateDisabledOnKeyUp);
302         controlBaseCopy.setDisabledConditionJs(this.disabledConditionJs);
303 
304         if (disabledConditionControlNames != null) {
305             controlBaseCopy.setDisabledConditionControlNames(new ArrayList<String>(this.disabledConditionControlNames));
306         }
307 
308         if (disabledWhenChangedPropertyNames != null) {
309             controlBaseCopy.setDisabledWhenChangedPropertyNames(new ArrayList<String>(this.disabledWhenChangedPropertyNames));
310         }
311 
312         if (enabledWhenChangedPropertyNames != null) {
313             controlBaseCopy.setEnabledWhenChangedPropertyNames(new ArrayList<String>(this.enabledWhenChangedPropertyNames));
314         }
315     }
316 }