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.lifecycle.finalize;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.uif.UifConstants;
20  import org.kuali.rice.krad.uif.component.Component;
21  import org.kuali.rice.krad.uif.lifecycle.LifecycleElementState;
22  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
23  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
24  import org.kuali.rice.krad.web.form.UifFormBase;
25  
26  /**
27   * Add the focusId, jumpToId and jumpToName as dataAttributes to the component during the finalize phase.
28   *
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class AddFocusAndJumpDataAttributesTask extends ViewLifecycleTaskBase<Component> {
32  
33      /**
34       * Constructor.
35       *
36       * @param phase The finalize phase for the component.
37       */
38      public AddFocusAndJumpDataAttributesTask() {
39          super(Component.class);
40      }
41  
42      /**
43       * {@inheritDoc}
44       */
45      @Override
46      protected void performLifecycleTask() {
47          LifecycleElementState elementState = getElementState();
48          String phase = elementState.getViewPhase();
49          String viewPath = elementState.getViewPath();
50          if (!ViewLifecycle.isRefreshComponent(phase, viewPath)) {
51              return;
52          }
53  
54          Component component = (Component) getElementState().getElement();
55          Object model = ViewLifecycle.getModel();
56  
57          UifFormBase formBase = (UifFormBase) model;
58  
59          // If AutoFocus then set the focus_id to FIRST field, unless focus_id is also specified
60          if (((UifFormBase) model).getView().getCurrentPage().isAutoFocus() && StringUtils.isNotBlank(formBase.getFocusId())) {
61              component.addDataAttribute(UifConstants.ActionDataAttributes.FOCUS_ID, formBase.getFocusId());
62          } else if (((UifFormBase) model).getView().getCurrentPage().isAutoFocus()) {
63              component.addDataAttribute(UifConstants.ActionDataAttributes.FOCUS_ID, UifConstants.Order.FIRST.name());
64          }
65  
66          // Add jumpToId as a data attribute
67          if (StringUtils.isNotBlank(formBase.getJumpToId())) {
68              component.addDataAttribute(UifConstants.ActionDataAttributes.JUMP_TO_ID, formBase.getJumpToId());
69          }
70  
71          // Add jumpToName as a data attribute
72          if (StringUtils.isNotBlank(formBase.getJumpToName())) {
73              component.addDataAttribute(UifConstants.ActionDataAttributes.JUMP_TO_NAME, formBase.getJumpToName());
74          }
75      }
76  
77  }