View Javadoc

1   /**
2    * Copyright 2005-2013 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.widget;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
23  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
24  import org.kuali.rice.krad.uif.UifConstants;
25  import org.kuali.rice.krad.uif.component.ClientSideState;
26  import org.kuali.rice.krad.uif.component.Component;
27  import org.kuali.rice.krad.uif.container.TabGroup;
28  import org.kuali.rice.krad.uif.view.View;
29  
30  /**
31   * Widget used for configuring tab options, use componentOptions for most options.
32   * See http://jqueryui.com/demos/tabs/ for usable options
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  @BeanTag(name = "tabs-bean", parent = "Uif-Tabs")
37  public class Tabs extends WidgetBase {
38      private static final long serialVersionUID = 2L;
39  
40      @ClientSideState(variableName = "activeTab")
41      private String defaultActiveTabId;
42  
43      private UifConstants.Position position = UifConstants.Position.TOP;
44  
45      public Tabs() {
46          super();
47      }
48  
49      /**
50       * The following is performed:
51       *
52       * <ul>
53       * <li>If the active tab id is configured, set the active plugin option</li>
54       * </ul>
55       */
56      @Override
57      public void performFinalize(View view, Object model, Component component) {
58          super.performFinalize(view, model, component);
59  
60          if (!(component instanceof TabGroup)) {
61              throw new RuntimeException("Parent for tabs widget should be tab group, not " + component.getClass());
62          }
63  
64          TabGroup tabGroup = (TabGroup) component;
65  
66          if (StringUtils.isNotBlank(defaultActiveTabId)) {
67              // need to find the index of the item to set the plugin active option
68              int index = 0;
69  
70              boolean found = false;
71              for (Component tabComponent : tabGroup.getItems()) {
72                  if (StringUtils.equals(defaultActiveTabId, tabComponent.getId())) {
73                      found = true;
74  
75                      break;
76                  }
77  
78                  index += 1;
79              }
80  
81              // if active tab index is set, add the plugin active option
82              if (found) {
83                  Map<String, String> oTemplateOptions = this.getTemplateOptions();
84                  
85                  if (oTemplateOptions == null) {
86                      setTemplateOptions(oTemplateOptions = new HashMap<String, String>());
87                  }
88                  
89                  oTemplateOptions.put(UifConstants.TabOptionKeys.ACTIVE, Integer.toString(index));
90              }
91          }
92      }
93  
94      /**
95       * Id for the group within the tab group that should be active (shown first), by default the first
96       * group is active
97       *
98       * @return id for the group within the tab group that should be initially active
99       */
100     @BeanTagAttribute(name = "defaultActiveTabId")
101     public String getDefaultActiveTabId() {
102         return defaultActiveTabId;
103     }
104 
105     /**
106      * Setter for the active group id
107      *
108      * @param defaultActiveTabId
109      */
110     public void setDefaultActiveTabId(String defaultActiveTabId) {
111         this.defaultActiveTabId = defaultActiveTabId;
112     }
113 
114     /**
115      * The position the tabs will appear related to the group, options are TOP, BOTTOM, RIGHT, or LEFT
116      *
117      * @return position for tabs
118      */
119     @BeanTagAttribute(name = "position")
120     public UifConstants.Position getPosition() {
121         return position;
122     }
123 
124     /**
125      * Setter for the tabs position
126      *
127      * @param position
128      */
129     public void setPosition(UifConstants.Position position) {
130         this.position = position;
131     }
132 
133     /**
134      * @see org.kuali.rice.krad.uif.component.ComponentBase#copy()
135      */
136     @Override
137     protected <T> void copyProperties(T component) {
138         super.copyProperties(component);
139         Tabs tabsCopy = (Tabs) component;
140         tabsCopy.setDefaultActiveTabId(this.getDefaultActiveTabId());
141         tabsCopy.setPosition(this.getPosition());
142     }
143 }