View Javadoc
1   /**
2    * Copyright 2005-2014 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.element;
17  
18  import java.util.ArrayList;
19  import java.util.List;
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.datadictionary.parse.BeanTags;
25  import org.kuali.rice.krad.uif.component.Component;
26  import org.kuali.rice.krad.uif.component.ListAware;
27  import org.kuali.rice.krad.uif.container.Group;
28  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleRestriction;
29  import org.kuali.rice.krad.uif.util.LifecycleElement;
30  
31  /**
32   * Renders a toggle menu (aka sub menu, dropdown menu) of items.
33   *
34   * <p>The toggle menu component can be used to build context menus or full application menus. Essentially the
35   * component is configured by first setting the text that will appear as a link (optionally with a caret). When the
36   * user clicks the link, the items ({@link #getMenuItems()} will be presented.</p>
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  @BeanTags({@BeanTag(name = "dropdownToggleMenu-bean", parent = "Uif-DropdownToggleMenu"),
41          @BeanTag(name = "sidebarToggleMenu-bean", parent = "Uif-SidebarToggleMenu")})
42  public class ToggleMenu extends ContentElementBase implements ListAware {
43      private static final long serialVersionUID = -1759659012620124641L;
44  
45      private String toggleText;
46      private Message toggleMessage;
47  
48      private String toggleCaretClass;
49      private String iconClass;
50      private boolean renderToggleButton;
51      private boolean renderedInList;
52  
53      private List<Component> menuItems;
54      private Group menuGroup;
55  
56      public ToggleMenu() {
57          super();
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public void performInitialization(Object model) {
65          super.performInitialization(model);
66  
67          if ((this.menuItems != null) && !this.menuItems.isEmpty()) {
68              this.menuGroup.setItems(menuItems);
69          }
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public void performApplyModel(Object model, LifecycleElement parent) {
77          super.performApplyModel(model, parent);
78  
79          if (StringUtils.isNotBlank(toggleText) && StringUtils.isBlank(toggleMessage.getMessageText())) {
80              toggleMessage.setMessageText(toggleText);
81          }
82      }
83  
84      /**
85       * Text to display as the toggle menu toggle link
86       * 
87       * <p>
88       * This text will appear as a link for the user to click on, which then will bring up the toggle
89       * menu menu. This property is a shortcut for {@link #getToggleMessage()}
90       * {@link Message#setMessageText(String) .setMessageText}. This text is not required, in which
91       * case only the caret will render
92       * </p>
93       * 
94       * @return text to display for the toggle menu toggle link
95       */
96      @BeanTagAttribute(name = "toggleText")
97      public String getToggleText() {
98          return toggleText;
99      }
100 
101     /**
102      * @see ToggleMenu#getToggleText()
103      */
104     public void setToggleText(String toggleText) {
105         this.toggleText = toggleText;
106     }
107 
108     /**
109      * {@code Message} component that is associated with the toggle menu toggle text, can be used to adjust styling
110      * and so forth
111      *
112      * @return Message instance for toggle text
113      */
114     @BeanTagAttribute(name = "toggleMessage")
115     public Message getToggleMessage() {
116         return toggleMessage;
117     }
118 
119     /**
120      * @see ToggleMenu#getToggleMessage()
121      */
122     public void setToggleMessage(Message toggleMessage) {
123         this.toggleMessage = toggleMessage;
124     }
125 
126     /**
127      * Css class to use when rendering a caret icon which will appear to the right of the toggleText
128      *
129      * @return the caret icon class
130      */
131     @BeanTagAttribute(name = "toggleCaretClass")
132     public String getToggleCaretClass() {
133         return toggleCaretClass;
134     }
135 
136     /**
137      * @see org.kuali.rice.krad.uif.element.ToggleMenu#getToggleCaretClass()
138      */
139     public void setToggleCaretClass(String toggleCaretClass) {
140         this.toggleCaretClass = toggleCaretClass;
141     }
142 
143     /**
144      * Css class for an icon that will appear to the left of the toggleText
145      *
146      * @return the css class for an icon
147      */
148     @BeanTagAttribute(name = "iconClass")
149     public String getIconClass() {
150         return iconClass;
151     }
152 
153     /**
154      * @see org.kuali.rice.krad.uif.element.ToggleMenu#getIconClass()
155      */
156     public void setIconClass(String iconClass) {
157         this.iconClass = iconClass;
158     }
159 
160     /**
161      * Indicates whether a caret button should be rendered to the right of the toggle text (if present)
162      *
163      * @return boolean true if caret button should be rendered, false if not
164      */
165     @BeanTagAttribute(name = "renderToggleButton")
166     public boolean isRenderToggleButton() {
167         return renderToggleButton;
168     }
169 
170     /**
171      * @see ToggleMenu#isRenderToggleButton()
172      */
173     public void setRenderToggleButton(boolean renderToggleButton) {
174         this.renderToggleButton = renderToggleButton;
175     }
176 
177     /**
178      * @see org.kuali.rice.krad.uif.component.ListAware#setRenderedInList(boolean)
179      */
180     @BeanTagAttribute(name = "renderedInList")
181     public boolean isRenderedInList() {
182         return renderedInList;
183     }
184 
185     /**
186      * @see ToggleMenu#isRenderedInList()
187      */
188     public void setRenderedInList(boolean renderedInList) {
189         this.renderedInList = renderedInList;
190     }
191 
192     /**
193      * List of components that should be rendered for the toggle menu
194      *
195      * <p>
196      * Items for the menu are configured through this list. The order of the items within the list is
197      * the order they will appear in the toggle menu
198      * </p>
199      *
200      * @return List of menu items for the toggle menu
201      */
202     @ViewLifecycleRestriction
203     @BeanTagAttribute(name = "menuItems", type = BeanTagAttribute.AttributeType.LISTBEAN)
204     public List<Component> getMenuItems() {
205         return menuItems;
206     }
207 
208     /**
209      * @see ToggleMenu#getMenuItems()
210      */
211     public void setMenuItems(List<Component> menuItems) {
212         this.menuItems = menuItems;
213     }
214 
215     /**
216      * Group instance that is rendered when the toggle menu is toggled.
217      *
218      * <p>Note in most cases this group will be a simple list group. The component allows for the list group
219      * to be initialized in a base bean, then child beans can simply define the item using
220      * {@link ToggleMenu#getMenuItems()}</p>
221      *
222      * @return Group instance
223      */
224     @BeanTagAttribute(name = "menuGroup", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
225     public Group getMenuGroup() {
226         return menuGroup;
227     }
228 
229     /**
230      * @see ToggleMenu#getMenuGroup()
231      */
232     public void setMenuGroup(Group menuGroup) {
233         this.menuGroup = menuGroup;
234     }
235 }