View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.widgets.menus;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import com.google.gwt.event.dom.client.ClickHandler;
23  import com.google.gwt.event.shared.HandlerManager;
24  import com.google.gwt.event.shared.HandlerRegistration;
25  import com.google.gwt.event.shared.GwtEvent.Type;
26  import com.google.gwt.user.client.ui.Image;
27  
28  /**
29   * The data object used to populate interactive ui menus.
30   * 
31   * @author Kuali Student Team
32   *
33   */
34  public class KSMenuItemData {
35  	private String label;
36  	private String styleName;
37  	private ClickHandler clickHandler;
38  	private List<KSMenuItemData> subItems = new ArrayList<KSMenuItemData>();
39  	private KSMenuItemData parent = null;
40  	private boolean selected = false;
41  	private Image shownIcon = null;
42  	
43  	private HandlerManager manager = new HandlerManager(this);
44  
45  	public KSMenuItemData(String label) {
46  		super();
47  		this.label = label;
48  	}
49  		
50  	public KSMenuItemData(String label, ClickHandler clickHandler) {
51  		super();
52  		this.label = label;
53  		this.clickHandler = clickHandler;
54  	}
55  	
56  	public KSMenuItemData(String label, Image icon, ClickHandler clickHandler) {
57  		super();
58  		this.label = label;
59  		this.shownIcon = icon;
60  		this.clickHandler = clickHandler;
61  	}
62  
63  	/**
64  	 * Get the text used for this menu item
65  	 * 
66  	 * @return the "label" for this menu item
67  	 */
68  	public String getLabel() {
69  		return label;
70  	}
71  	/**
72  	 * Set the label to be used in the menu for this menu item
73  	 * 
74  	 * @param label the "label" of this menu item
75  	 */
76  	public void setLabel(String label) {
77  		this.label = label;
78  		manager.fireEvent(new MenuChangeEvent());
79  	}
80  	/**
81  	 * Get the ClickHandler for this menu item.
82  	 * 
83  	 * @return ClickHandler which controls what this menu item does when selected
84  	 */
85  	public ClickHandler getClickHandler() {
86  		return clickHandler;
87  	}
88  	/**
89  	 * Set the click handler for this menu item (what the menu item does when selected).
90  	 * 
91  	 * @param clickHandler a ClickHandler for this menu item.
92  	 */
93  	public void setClickHandler(ClickHandler clickHandler) {
94  		this.clickHandler = clickHandler;
95  	}
96  	
97  	/**
98  	 * Adds a KSMenuItemData to the list of children for this menu "category".
99  	 * 
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 }