001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.element;
017
018import java.util.List;
019
020import org.apache.commons.lang.StringUtils;
021import org.kuali.rice.krad.datadictionary.parse.BeanTag;
022import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
023import org.kuali.rice.krad.datadictionary.parse.BeanTags;
024import org.kuali.rice.krad.uif.component.Component;
025import org.kuali.rice.krad.uif.component.ListAware;
026import org.kuali.rice.krad.uif.container.Group;
027import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleRestriction;
028import org.kuali.rice.krad.uif.util.LifecycleElement;
029
030/**
031 * Renders a toggle menu (aka sub menu, dropdown menu) of items.
032 *
033 * <p>The toggle menu component can be used to build context menus or full application menus. Essentially the
034 * component is configured by first setting the text that will appear as a link (optionally with a caret). When the
035 * user clicks the link, the items ({@link #getMenuItems()} will be presented.</p>
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039@BeanTags({@BeanTag(name = "dropdownMenu", parent = "Uif-DropdownToggleMenu"),
040        @BeanTag(name = "sidebarMenu", parent = "Uif-SidebarToggleMenu")})
041public class ToggleMenu extends ContentElementBase implements ListAware {
042    private static final long serialVersionUID = -1759659012620124641L;
043
044    private String toggleText;
045    private Message toggleMessage;
046
047    private String toggleCaretClass;
048    private String iconClass;
049    private boolean renderToggleButton;
050    private boolean renderedInList;
051
052    private List<Component> menuItems;
053    private Group menuGroup;
054
055    public ToggleMenu() {
056        super();
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    public void performInitialization(Object model) {
064        super.performInitialization(model);
065
066        if ((this.menuItems != null) && !this.menuItems.isEmpty()) {
067            this.menuGroup.setItems(menuItems);
068        }
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public void performApplyModel(Object model, LifecycleElement parent) {
076        super.performApplyModel(model, parent);
077
078        if (StringUtils.isNotBlank(toggleText) && StringUtils.isBlank(toggleMessage.getMessageText())) {
079            toggleMessage.setMessageText(toggleText);
080        }
081    }
082
083    /**
084     * Text to display as the toggle menu toggle link
085     * 
086     * <p>
087     * This text will appear as a link for the user to click on, which then will bring up the toggle
088     * menu menu. This property is a shortcut for {@link #getToggleMessage()}
089     * {@link Message#setMessageText(String) .setMessageText}. This text is not required, in which
090     * case only the caret will render
091     * </p>
092     * 
093     * @return text to display for the toggle menu toggle link
094     */
095    @BeanTagAttribute
096    public String getToggleText() {
097        return toggleText;
098    }
099
100    /**
101     * @see ToggleMenu#getToggleText()
102     */
103    public void setToggleText(String toggleText) {
104        this.toggleText = toggleText;
105    }
106
107    /**
108     * {@code Message} component that is associated with the toggle menu toggle text, can be used to adjust styling
109     * and so forth
110     *
111     * @return Message instance for toggle text
112     */
113    @BeanTagAttribute
114    public Message getToggleMessage() {
115        return toggleMessage;
116    }
117
118    /**
119     * @see ToggleMenu#getToggleMessage()
120     */
121    public void setToggleMessage(Message toggleMessage) {
122        this.toggleMessage = toggleMessage;
123    }
124
125    /**
126     * Css class to use when rendering a caret icon which will appear to the right of the toggleText
127     *
128     * @return the caret icon class
129     */
130    @BeanTagAttribute
131    public String getToggleCaretClass() {
132        return toggleCaretClass;
133    }
134
135    /**
136     * @see org.kuali.rice.krad.uif.element.ToggleMenu#getToggleCaretClass()
137     */
138    public void setToggleCaretClass(String toggleCaretClass) {
139        this.toggleCaretClass = toggleCaretClass;
140    }
141
142    /**
143     * Css class for an icon that will appear to the left of the toggleText
144     *
145     * @return the css class for an icon
146     */
147    @BeanTagAttribute
148    public String getIconClass() {
149        return iconClass;
150    }
151
152    /**
153     * @see org.kuali.rice.krad.uif.element.ToggleMenu#getIconClass()
154     */
155    public void setIconClass(String iconClass) {
156        this.iconClass = iconClass;
157    }
158
159    /**
160     * Indicates whether a caret button should be rendered to the right of the toggle text (if present)
161     *
162     * @return boolean true if caret button should be rendered, false if not
163     */
164    @BeanTagAttribute
165    public boolean isRenderToggleButton() {
166        return renderToggleButton;
167    }
168
169    /**
170     * @see ToggleMenu#isRenderToggleButton()
171     */
172    public void setRenderToggleButton(boolean renderToggleButton) {
173        this.renderToggleButton = renderToggleButton;
174    }
175
176    /**
177     * @see org.kuali.rice.krad.uif.component.ListAware#setRenderedInList(boolean)
178     */
179    @BeanTagAttribute
180    public boolean isRenderedInList() {
181        return renderedInList;
182    }
183
184    /**
185     * @see ToggleMenu#isRenderedInList()
186     */
187    public void setRenderedInList(boolean renderedInList) {
188        this.renderedInList = renderedInList;
189    }
190
191    /**
192     * List of components that should be rendered for the toggle menu.
193     *
194     * <p>Items for the menu are configured through this list. The order of the items within the list is
195     * the order they will appear in the toggle menu</p>
196     *
197     * @return List of menu items for the toggle menu
198     */
199    @ViewLifecycleRestriction
200    @BeanTagAttribute
201    public List<Component> getMenuItems() {
202        return menuItems;
203    }
204
205    /**
206     * @see ToggleMenu#getMenuItems()
207     */
208    public void setMenuItems(List<Component> menuItems) {
209        this.menuItems = menuItems;
210    }
211
212    /**
213     * Group instance that is rendered when the toggle menu is toggled.
214     *
215     * <p>Note in most cases this group will be a simple list group. The component allows for the list group
216     * to be initialized in a base bean, then child beans can simply define the item using
217     * {@link ToggleMenu#getMenuItems()}</p>
218     *
219     * @return Group instance
220     */
221    @BeanTagAttribute
222    public Group getMenuGroup() {
223        return menuGroup;
224    }
225
226    /**
227     * @see ToggleMenu#getMenuGroup()
228     */
229    public void setMenuGroup(Group menuGroup) {
230        this.menuGroup = menuGroup;
231    }
232}