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.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.uif.component.Component;
23
24 /**
25 * Renders a dropdown menu (context menu) of actions
26 *
27 * <p>
28 * The dropdown menu component can be used to build context menus or full application menus. Essentially the
29 * component is configured by first setting the text that will appear as a link (optionally with a caret). When the
30 * user clicks the link, the dropdown of actions ({@link #getMenuActions()} will be presented.
31 * </p>
32 *
33 * @author Kuali Rice Team (rice.collab@kuali.org)
34 */
35 public class DropdownMenu extends ContentElementBase {
36 private static final long serialVersionUID = -1759659012620124641L;
37
38 private String dropdownToggleText;
39 private Message dropdownToggle;
40
41 private boolean renderToggleCaret;
42 private boolean renderToggleButton;
43
44 private List<MenuAction> menuActions;
45
46 public DropdownMenu() {
47 super();
48
49 renderToggleCaret = true;
50 }
51
52 /**
53 * @see org.kuali.rice.krad.uif.component.Component#performApplyModel(org.kuali.rice.krad.uif.view.View, Object,
54 * org.kuali.rice.krad.uif.component.Component)
55 */
56 @Override
57 public void performApplyModel(Object model, Component parent) {
58 super.performApplyModel(model, parent);
59
60 if (StringUtils.isNotBlank(dropdownToggleText) && StringUtils.isBlank(dropdownToggle.getMessageText())) {
61 dropdownToggle.setMessageText(dropdownToggleText);
62 }
63 }
64
65 /**
66 * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
67 */
68 @Override
69 public List<Component> getComponentsForLifecycle() {
70 List<Component> components = super.getComponentsForLifecycle();
71
72 components.add(dropdownToggle);
73 components.addAll(menuActions);
74
75 return components;
76 }
77
78 /**
79 * Text to display as the dropdown toggle link
80 *
81 * <p>
82 * This text will appear as a link for the user to click on, which then will bring up the dropdown menu. This
83 * property is a shortcut for {@link #getDropdownToggle().setMessageText()}. This text is not required, in which
84 * case only the caret will render
85 * </p>
86 *
87 * @return text to display for the dropdown toggle link
88 */
89 public String getDropdownToggleText() {
90 return dropdownToggleText;
91 }
92
93 /**
94 * @see DropdownMenu#getDropdownToggleText()
95 */
96 public void setDropdownToggleText(String dropdownToggleText) {
97 this.dropdownToggleText = dropdownToggleText;
98 }
99
100 /**
101 * {@code Message} component that is associated with the dropdown toggle text, can be used to adjust styling
102 * and so forth
103 *
104 * @return Message instance for toggle text
105 */
106 public Message getDropdownToggle() {
107 return dropdownToggle;
108 }
109
110 /**
111 * @see DropdownMenu#getDropdownToggle()
112 */
113 public void setDropdownToggle(Message dropdownToggle) {
114 this.dropdownToggle = dropdownToggle;
115 }
116
117 /**
118 * Indicates whether a caret icon should be rendered to the right of the toggle text (if present)
119 *
120 * @return boolean true if caret should be rendered, false if not
121 */
122 public boolean isRenderToggleCaret() {
123 return renderToggleCaret;
124 }
125
126 /**
127 * @see DropdownMenu#isRenderToggleCaret()
128 */
129 public void setRenderToggleCaret(boolean renderToggleCaret) {
130 this.renderToggleCaret = renderToggleCaret;
131 }
132
133 /**
134 * Indicates whether a caret button should be rendered to the right of the toggle text (if present)
135 *
136 * @return boolean true if caret button should be rendered, false if not
137 */
138 public boolean isRenderToggleButton() {
139 return renderToggleButton;
140 }
141
142 /**
143 * @see DropdownMenu#isRenderToggleButton()
144 */
145 public void setRenderToggleButton(boolean renderToggleButton) {
146 this.renderToggleButton = renderToggleButton;
147 }
148
149 /**
150 * List of {@link MenuAction} instances that should be rendered for the dropdown
151 *
152 * <p>
153 * Actions for the menu are configured through this list. The order of the actions within the list is
154 * the order they will appear in the dropdown
155 * </p>
156 *
157 * @return List of menu actions for the dropdown
158 */
159 public List<MenuAction> getMenuActions() {
160 return menuActions;
161 }
162
163 /**
164 * @see DropdownMenu#getMenuActions()
165 */
166 public void setMenuActions(List<MenuAction> menuActions) {
167 this.menuActions = menuActions;
168 }
169
170 /**
171 * @see org.kuali.rice.krad.uif.component.ComponentBase#copy()
172 */
173 @Override
174 protected <T> void copyProperties(T component) {
175 super.copyProperties(component);
176
177 DropdownMenu dropdownCopy = (DropdownMenu) component;
178
179 if (this.dropdownToggle != null) {
180 dropdownCopy.setDropdownToggle((Message) this.dropdownToggle.copy());
181 }
182 dropdownCopy.setDropdownToggleText(this.dropdownToggleText);
183
184 dropdownCopy.setRenderToggleCaret(this.renderToggleCaret);
185 dropdownCopy.setRenderToggleButton(this.renderToggleButton);
186
187 if (this.menuActions != null) {
188 List<MenuAction> optionsCopy = new ArrayList<MenuAction>();
189
190 for (MenuAction action : this.menuActions) {
191 optionsCopy.add((MenuAction) action.copy());
192 }
193 dropdownCopy.setMenuActions(optionsCopy);
194 }
195 }
196 }