001 /**
002 * Copyright 2010 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015
016 package org.kuali.student.common.ui.client.widgets.menus;
017
018 import java.util.ArrayList;
019 import java.util.Collections;
020 import java.util.List;
021
022 import com.google.gwt.event.dom.client.ClickHandler;
023 import com.google.gwt.event.shared.HandlerManager;
024 import com.google.gwt.event.shared.HandlerRegistration;
025 import com.google.gwt.event.shared.GwtEvent.Type;
026 import com.google.gwt.user.client.ui.Image;
027
028 /**
029 * The data object used to populate interactive ui menus.
030 *
031 * @author Kuali Student Team
032 *
033 */
034 public class KSMenuItemData {
035 private String label;
036 private String styleName;
037 private ClickHandler clickHandler;
038 private List<KSMenuItemData> subItems = new ArrayList<KSMenuItemData>();
039 private KSMenuItemData parent = null;
040 private boolean selected = false;
041 private Image shownIcon = null;
042
043 private HandlerManager manager = new HandlerManager(this);
044
045 public KSMenuItemData(String label) {
046 super();
047 this.label = label;
048 }
049
050 public KSMenuItemData(String label, ClickHandler clickHandler) {
051 super();
052 this.label = label;
053 this.clickHandler = clickHandler;
054 }
055
056 public KSMenuItemData(String label, Image icon, ClickHandler clickHandler) {
057 super();
058 this.label = label;
059 this.shownIcon = icon;
060 this.clickHandler = clickHandler;
061 }
062
063 /**
064 * Get the text used for this menu item
065 *
066 * @return the "label" for this menu item
067 */
068 public String getLabel() {
069 return label;
070 }
071 /**
072 * Set the label to be used in the menu for this menu item
073 *
074 * @param label the "label" of this menu item
075 */
076 public void setLabel(String label) {
077 this.label = label;
078 manager.fireEvent(new MenuChangeEvent());
079 }
080 /**
081 * Get the ClickHandler for this menu item.
082 *
083 * @return ClickHandler which controls what this menu item does when selected
084 */
085 public ClickHandler getClickHandler() {
086 return clickHandler;
087 }
088 /**
089 * Set the click handler for this menu item (what the menu item does when selected).
090 *
091 * @param clickHandler a ClickHandler for this menu item.
092 */
093 public void setClickHandler(ClickHandler clickHandler) {
094 this.clickHandler = clickHandler;
095 }
096
097 /**
098 * Adds a KSMenuItemData to the list of children for this menu "category".
099 *
100 * @param item a KSMenuItemData that is a child of this KSMenuItemData
101 */
102 public void addSubItem(KSMenuItemData item) {
103 subItems.add(item);
104 item.setParent(this);
105 }
106
107 /**
108 * Gets the list of sub items (children) in this KSMenuItemData
109 *
110 * @return the list of sub items in this KSMenuItemData
111 */
112 public List<KSMenuItemData> getSubItems() {
113 return Collections.unmodifiableList(subItems);
114 }
115
116 /**
117 * Set the parent of this KSMenuItemData
118 *
119 * @param parent the KSMenuItemData which is the parent KSMenuItemData (category)
120 */
121 public void setParent(KSMenuItemData parent) {
122 this.parent = parent;
123 }
124
125 /**
126 * Gets the parent of this KSMenuItemData
127 *
128 * @return the paren of this KSMenuItemData
129 */
130 public KSMenuItemData getParent() {
131 return parent;
132 }
133
134 public boolean isSelected() {
135 return selected;
136 }
137
138 public void setSelected(boolean selected) {
139 this.selected = selected;
140 if(selected == true){
141 manager.fireEvent(new MenuSelectEvent());
142 }
143 }
144
145 public void setSelected(boolean selected, boolean fireClick) {
146 this.selected = selected;
147 if(selected == true){
148 MenuSelectEvent e = new MenuSelectEvent();
149 e.setFireClickEvent(fireClick);
150 manager.fireEvent(e);
151 }
152 }
153
154 public void unhandledSetSelected(boolean selected){
155 this.selected = selected;
156 }
157
158 public Image getShownIcon() {
159 return shownIcon;
160 }
161
162 public void setShownIcon(Image shownIcon) {
163 this.shownIcon = shownIcon;
164 manager.fireEvent(new MenuChangeEvent());
165 }
166
167 @SuppressWarnings("unchecked")
168 public HandlerRegistration addMenuEventHandler(Type type, MenuEventHandler meh){
169 return manager.addHandler(type, meh);
170 }
171
172 public void addSpecialStyle(String style){
173 styleName = style;
174 }
175
176 public String getSpecialStyle(){
177 return styleName;
178 }
179
180 }