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.impl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.student.common.ui.client.widgets.KSLabel;
22  import org.kuali.student.common.ui.client.widgets.menus.KSBasicMenuAbstract;
23  import org.kuali.student.common.ui.client.widgets.menus.KSMenuItemData;
24  import org.kuali.student.common.ui.client.widgets.menus.MenuChangeEvent;
25  import org.kuali.student.common.ui.client.widgets.menus.MenuEventHandler;
26  import org.kuali.student.common.ui.client.widgets.menus.MenuSelectEvent;
27  
28  import com.google.gwt.event.dom.client.BlurEvent;
29  import com.google.gwt.event.dom.client.BlurHandler;
30  import com.google.gwt.event.dom.client.ClickEvent;
31  import com.google.gwt.event.dom.client.ClickHandler;
32  import com.google.gwt.event.dom.client.FocusEvent;
33  import com.google.gwt.event.dom.client.FocusHandler;
34  import com.google.gwt.event.dom.client.MouseOutEvent;
35  import com.google.gwt.event.dom.client.MouseOutHandler;
36  import com.google.gwt.event.dom.client.MouseOverEvent;
37  import com.google.gwt.event.dom.client.MouseOverHandler;
38  import com.google.gwt.user.client.ui.FocusPanel;
39  import com.google.gwt.user.client.ui.HorizontalPanel;
40  import com.google.gwt.user.client.ui.Image;
41  import com.google.gwt.user.client.ui.VerticalPanel;
42  import com.google.gwt.user.client.ui.Widget;
43  
44  public class KSBasicMenuImpl extends KSBasicMenuAbstract{
45      private VerticalPanel menuPanel = new VerticalPanel();
46      private VerticalPanel menuTitlePanel = new VerticalPanel();
47      private KSLabel menuTitle = new KSLabel();
48      private KSLabel menuDescription = new KSLabel();
49      private VerticalPanel menuContainer = new VerticalPanel();
50      private List<MenuItemPanel> menuItems = new ArrayList<MenuItemPanel>();
51      private boolean numberAllItems = false;
52  
53      private EventHandler handler = new EventHandler();
54  
55      private MenuEventHandler menuHandler = new MenuEventHandler(){
56  
57          @Override
58          public void onChange(MenuChangeEvent e) {
59              KSMenuItemData i = (KSMenuItemData) e.getSource();
60              MenuItemPanel itemToChange = null;
61              for(MenuItemPanel p: menuItems){
62                  if(i.equals(p.getItem())){
63                      itemToChange = p;
64                  }
65              }
66              if(itemToChange != null){
67                  if(!(i.getLabel().equals(itemToChange.getItemLabel().getText()))){
68                      itemToChange.getItemLabel().setText(i.getLabel());
69                  }
70                  else if(i.getShownIcon() != null){
71                      itemToChange.addImage(i.getShownIcon());
72                  }
73              }
74  
75          }
76  
77          @Override
78          public void onSelect(MenuSelectEvent e) {
79  
80              KSMenuItemData i = (KSMenuItemData) e.getSource();
81              MenuItemPanel itemToSelect = null;
82              for(MenuItemPanel p: menuItems){
83                  if(i.equals(p.getItem())){
84                      itemToSelect = p;
85                  }
86              }
87              if(itemToSelect != null){
88                  itemToSelect.fireEvent(new ClickEvent(){});
89              }
90          }
91      };
92  
93      public KSBasicMenuImpl(){
94          menuTitlePanel.add(menuTitle);
95          menuTitlePanel.add(menuDescription);
96          menuContainer.add(menuTitlePanel);
97          menuContainer.add(menuPanel);
98  
99          menuContainer.addStyleName("KS-Basic-Menu-Parent-Container");
100         menuTitlePanel.addStyleName("KS-Basic-Menu-Title-Panel");
101         menuPanel.addStyleName("KS-Basic-Menu-Panel");
102 
103         menuTitle.addStyleName("KS-Basic-Menu-Title-Label");
104         menuTitle.addStyleName("KS-Indent" + "-1");
105 
106         menuDescription.addStyleName("KS-Basic-Menu-Desc-Label");
107         menuDescription.addStyleName("KS-Indent" + "-1");
108 
109         this.initWidget(menuContainer);
110     }
111 
112     public void setTitle(String title){
113         menuTitle.setText(title);
114         menuTitle.setWordWrap(true);
115         //add style
116     }
117 
118     public void setDescription(String description){
119         menuDescription.setText(description);
120         menuDescription.setWordWrap(true);
121         //add style
122     }
123 
124     private class EventHandler implements ClickHandler, MouseOverHandler, MouseOutHandler, FocusHandler, BlurHandler{
125 
126         @Override
127         public void onClick(ClickEvent event) {
128             Widget sender = (Widget) event.getSource();
129             if(sender instanceof MenuItemPanel){
130                 selectMenuItemPanel((MenuItemPanel)sender);
131                 sender.removeStyleName("KS-Basic-Menu-Item-Panel-Hover");
132                 ((MenuItemPanel) sender).getItemLabel().removeStyleName("KS-Basic-Menu-Item-Label-Hover");
133             }
134         }
135 
136 
137 
138         @Override
139         public void onMouseOver(MouseOverEvent event) {
140             Widget sender = (Widget) event.getSource();
141             if(sender instanceof MenuItemPanel){
142                 if(((MenuItemPanel) sender).isSelectable() && !((MenuItemPanel) sender).isSelected()){
143                     sender.addStyleName("KS-Basic-Menu-Item-Panel-Hover");
144                     ((MenuItemPanel) sender).getItemLabel().addStyleName("KS-Basic-Menu-Item-Label-Hover");
145                 }
146             }
147         }
148 
149         @Override
150         public void onMouseOut(MouseOutEvent event) {
151             Widget sender = (Widget) event.getSource();
152             if(sender instanceof MenuItemPanel){
153                 if(((MenuItemPanel) sender).isSelectable()){
154                     sender.removeStyleName("KS-Basic-Menu-Item-Panel-Hover");
155                     ((MenuItemPanel) sender).getItemLabel().removeStyleName("KS-Basic-Menu-Item-Label-Hover");
156                 }
157             }
158         }
159 
160         @Override
161         public void onFocus(FocusEvent event) {
162             // no function yet
163 
164         }
165 
166         @Override
167         public void onBlur(BlurEvent event) {
168             // no function yet
169 
170         }
171 
172     }
173 
174     private void selectMenuItemPanel(MenuItemPanel toBeSelected) {
175         if(toBeSelected.isSelectable()){
176 
177             clearSelected();
178 
179             toBeSelected.select();
180             toBeSelected.getItem().unhandledSetSelected(true);
181         }
182 
183     }
184 
185     private class MenuItemPanel extends FocusPanel{
186         KSLabel itemLabel = new KSLabel();
187         HorizontalPanel contentPanel = new HorizontalPanel();
188         boolean selectable = false;
189         boolean selected = false;
190         KSMenuItemData item;
191         int indent;
192         int itemNum;
193 
194         public MenuItemPanel(KSMenuItemData item, int indent, int itemNum){
195             this.item = item;
196             this.indent = indent;
197             this.itemNum = itemNum;
198 
199             if(numberAllItems){
200                 // logic for indent maybe
201                 itemLabel.setText(itemNum + ". " + item.getLabel());
202             }
203             else{
204                 if(indent == 1 && !(item.getSubItems().isEmpty())){
205                     itemLabel.setText(itemNum + ". " + item.getLabel());
206                     itemLabel.addStyleName("KS-Basic-Menu-Toplevel-Item-Label");
207                     this.addStyleName("KS-Basic-Menu-Toplevel-Item-Panel");
208                 }
209                 else{
210                     itemLabel.setText(item.getLabel());
211                 }
212             }
213 
214             if(indent > 0 && indent <= 7){
215                 itemLabel.addStyleName("KS-Indent" + "-" + indent);
216             }
217 
218             itemLabel.setWordWrap(true);
219             this.addStyleName("KS-Basic-Menu-Item-Label");
220             itemLabel.addStyleName("KS-Basic-Menu-Item-Label");
221             if(item.getClickHandler() != null)
222             {
223                 this.addClickHandler(item.getClickHandler());
224                 itemLabel.addStyleName("KS-Basic-Menu-Clickable-Item-Label");
225                 selectable = true;
226             }
227             this.addClickHandler(handler);
228             this.addMouseOverHandler(handler);
229             this.addMouseOutHandler(handler);
230             this.addFocusHandler(handler);
231             this.addBlurHandler(handler);
232             contentPanel.add(itemLabel);
233             this.add(contentPanel);
234         }
235 
236         public void addImage(Image shownIcon) {
237             shownIcon.addStyleName("KS-Basic-Menu-Item-Image");
238             contentPanel.add(shownIcon);
239 
240         }
241 
242         public void deSelect(){
243             this.removeStyleName("KS-Basic-Menu-Item-Panel-Selected");
244             itemLabel.removeStyleName("KS-Basic-Menu-Item-Label-Selected");
245             selected = false;
246         }
247 
248         public void select(){
249             this.addStyleName("KS-Basic-Menu-Item-Panel-Selected");
250             itemLabel.addStyleName("KS-Basic-Menu-Item-Label-Selected");
251             selected = true;
252         }
253 
254         public KSLabel getItemLabel() {
255             return itemLabel;
256         }
257 
258         public boolean isSelectable() {
259             return selectable;
260         }
261 
262         public void setSelectable(boolean selectable) {
263             this.selectable = selectable;
264         }
265 
266         public KSMenuItemData getItem() {
267             return item;
268         }
269 
270 		public boolean isSelected() {
271 			return selected;
272 		}
273 
274 		public void setSelected(boolean selected) {
275 			this.selected = selected;
276 		}
277 
278 
279     }
280 
281     @Override
282     protected void populateMenu() {
283         createMenuItems(items, 1);
284     }
285 
286     private void createMenuItems(List<KSMenuItemData> theItems, int currentDepth){
287         int itemNum = 0;
288         for(KSMenuItemData i: theItems){
289             itemNum++;
290             addMenuItem(new MenuItemPanel(i, currentDepth, itemNum));
291             if(!(i.getSubItems().isEmpty())){
292                 createMenuItems(i.getSubItems(), currentDepth + 1);
293             }
294             i.addMenuEventHandler(MenuSelectEvent.TYPE, menuHandler);
295             i.addMenuEventHandler(MenuChangeEvent.TYPE, menuHandler);
296         }
297     }
298 
299     private void addMenuItem(MenuItemPanel panel){
300         menuPanel.add(panel);
301         menuItems.add(panel);
302     }
303 
304     public boolean isNumberAllItems() {
305         return numberAllItems;
306     }
307 
308     public void setNumberAllItems(boolean numberAllItems) {
309         this.numberAllItems = numberAllItems;
310     }
311 
312     @Override
313     public boolean selectMenuItem(String[] hierarchy) {
314         List<KSMenuItemData> currentItems = items;
315         KSMenuItemData itemToSelect = null;
316         for(String s: hierarchy){
317             s = s.trim();
318             for(KSMenuItemData i: currentItems){
319                 if(s.equalsIgnoreCase(i.getLabel().trim())){
320                     itemToSelect = i;
321                     currentItems = i.getSubItems();
322                     break;
323                 }
324             }
325         }
326 
327         if(itemToSelect != null){
328 
329             for(MenuItemPanel p: menuItems){
330                 if(itemToSelect.equals(p.getItem())){
331                     p.getItem().setSelected(true);
332                     return true;
333                 }
334             }
335 
336         }
337 
338         return false;
339     }
340 
341     /**
342      * @see org.kuali.student.common.ui.client.widgets.menus.KSMenu#clearSelected()
343      */
344     @Override
345     public void clearSelected() {
346         for(MenuItemPanel m : menuItems){
347             m.deSelect();
348             m.getItem().unhandledSetSelected(false);
349         }
350     }
351 
352 }