View Javadoc

1   package org.kuali.student.common.ui.client.configurable.mvc.layouts;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import org.kuali.student.common.ui.client.configurable.mvc.LayoutController;
9   import org.kuali.student.common.ui.client.mvc.Callback;
10  import org.kuali.student.common.ui.client.mvc.View;
11  import org.kuali.student.common.ui.client.util.ExportElement;
12  import org.kuali.student.common.ui.client.widgets.KSButton;
13  import org.kuali.student.common.ui.client.widgets.headers.KSDocumentHeader;
14  import org.kuali.student.common.ui.client.widgets.menus.KSMenuItemData;
15  import org.kuali.student.common.ui.client.widgets.menus.impl.KSBlockMenuImpl;
16  import org.kuali.student.common.ui.client.widgets.panels.collapsable.VerticalCollapsableDrawer;
17  
18  import com.google.gwt.event.dom.client.ClickEvent;
19  import com.google.gwt.event.dom.client.ClickHandler;
20  import com.google.gwt.user.client.Command;
21  import com.google.gwt.user.client.DeferredCommand;
22  import com.google.gwt.user.client.ui.FlowPanel;
23  import com.google.gwt.user.client.ui.SimplePanel;
24  import com.google.gwt.user.client.ui.VerticalPanel;
25  import com.google.gwt.user.client.ui.Widget;
26  
27  /**
28   * A layout controller which generates a menu for views that added to it through addMenuItem calls.
29   * The user can click on items in the menu to show the view.
30   * 
31   * @author Kuali Student Team
32   *
33   */
34  public class MenuSectionController extends LayoutController implements ContentNavLayoutController {
35  
36      private KSBlockMenuImpl menu = new KSBlockMenuImpl();
37      private List<KSMenuItemData> topLevelMenuItems = new ArrayList<KSMenuItemData>();
38      protected Map<String, List<View>> menuViewMap = new HashMap<String, List<View>>();
39      private Map<Enum<?>, List<KSButton>> viewButtonsMap = new HashMap<Enum<?>, List<KSButton>>();
40      protected Map<Enum<?>, KSMenuItemData> viewMenuItemMap = new HashMap<Enum<?>, KSMenuItemData>();
41      private List<View> menuOrder = new ArrayList<View>();
42      private FlowPanel layout = new FlowPanel();
43      private KSDocumentHeader header = new KSDocumentHeader();
44      private FlowPanel rightPanel = new FlowPanel();
45      private FlowPanel contentPanel = new FlowPanel();
46      private FlowPanel buttonPanel = new FlowPanel();
47      private VerticalPanel leftPanel = new VerticalPanel();
48      private SimplePanel sideBar = new SimplePanel();
49      private boolean refreshMenuOnAdd = true;
50      private VerticalCollapsableDrawer collapsablePanel = new VerticalCollapsableDrawer();
51  
52      private Callback<Boolean> showViewCallback = new Callback<Boolean>() {
53  
54          @Override
55          public void exec(Boolean result) {
56              if (result == false) {
57                  KSMenuItemData item = viewMenuItemMap.get(MenuSectionController.this.getCurrentView().getViewEnum());
58                  if (item != null) {
59                      item.setSelected(true, false);
60                  }
61              }
62          }
63      };
64  
65      public MenuSectionController() {
66          super();
67          List<View> list = new ArrayList<View>();
68          menuViewMap.put("", list);
69          menu.setStyleName("ks-menu-layout-menu");
70          rightPanel.setStyleName("ks-menu-layout-rightColumn");
71          collapsablePanel.addStyleName("ks-menu-layout-leftColumn");
72          layout.addStyleName("ks-menu-layout");
73          menu.setTopLevelItems(topLevelMenuItems);
74          collapsablePanel.setContent(leftPanel);
75          leftPanel.add(menu);
76          leftPanel.add(sideBar);
77          rightPanel.add(header);
78          rightPanel.add(contentPanel);
79          rightPanel.add(buttonPanel);
80          layout.add(collapsablePanel);
81          layout.add(rightPanel);
82          header.setVisible(false);
83          this.showPrint(true);
84          this.initWidget(layout);
85      }
86  
87      public void removeMenuNavigation() {
88          collapsablePanel.removeFromParent();
89      }
90  
91      public void setContentTitle(String title) {
92          header.setTitle(title);
93          header.setVisible(true);
94      }
95  
96      public void addContentWidget(Widget w) {
97          header.addWidget(w);
98          header.setVisible(true);
99      }
100 
101     public void setSideBarWidget(Widget w) {
102         sideBar.setWidget(w);
103     }
104 
105     public void setContentInfo(String info) {
106         header.getInfoLabel().setHTML(info);
107         header.getInfoLabel().removeStyleName("content-warning");
108         header.getInfoLabel().addStyleName("content-info");
109     }
110 
111     public void setContentWarning(String info) {
112         header.getInfoLabel().setHTML(info);
113         header.getInfoLabel().removeStyleName("content-info");
114         header.getInfoLabel().addStyleName("content-warning");
115 
116     }
117 
118     public void showPrint(boolean show) {
119         header.showPrint(show);
120     }
121     
122     public void showExport(boolean show) {
123         header.showExport(show);
124     }
125 
126     /**
127      * @see org.kuali.student.common.ui.client.configurable.mvc.layouts.ContentNavLayoutController#addCommonButton(java.lang.String, org.kuali.student.common.ui.client.widgets.KSButton)
128      */
129     public void addCommonButton(String parentMenu, KSButton button) {
130         if (parentMenu != null) {
131             List<View> views = menuViewMap.get(parentMenu);
132             if (views != null) {
133                 for (int i = 0; i < views.size(); i++) {
134                     addButtonForView(views.get(i).getViewEnum(), button);
135                 }
136             }
137         }
138     }
139 
140     /**
141      * @see org.kuali.student.common.ui.client.configurable.mvc.layouts.ContentNavLayoutController#addCommonButton(java.lang.String, org.kuali.student.common.ui.client.widgets.KSButton, java.util.List)
142      */
143     public void addCommonButton(String parentMenu, KSButton button, List<Enum<?>> excludedViews) {
144         if (parentMenu != null) {
145             List<View> views = menuViewMap.get(parentMenu);
146             if (views != null) {
147                 for (int i = 0; i < views.size(); i++) {
148                     if (!excludedViews.contains(views.get(i).getViewEnum())) {
149                         addButtonForView(views.get(i).getViewEnum(), button);
150                     }
151                 }
152             }
153         }
154     }
155 
156     public void showNextViewOnMenu() {
157         int i = menuOrder.indexOf(this.getCurrentView());
158         if (i != -1 && i + 1 < menuOrder.size()) {
159             this.showView(menuOrder.get(i + 1).getViewEnum());
160         }
161     }
162 
163     public void addButtonForView(Enum<?> viewType, KSButton button) {
164         List<KSButton> buttons = viewButtonsMap.get(viewType);
165         if (buttons == null) {
166             buttons = new ArrayList<KSButton>();
167             button.addStyleName("ks-button-spacing");
168             buttons.add(button);
169             viewButtonsMap.put(viewType, buttons);
170         } else {
171             buttons.add(button);
172         }
173     }
174 
175     @Override
176     protected void hideView(View view) {
177         contentPanel.clear();
178         buttonPanel.clear();
179     }
180 
181     @Override
182     protected void renderView(View view) {
183         contentPanel.add(view.asWidget());
184         List<KSButton> buttons = viewButtonsMap.get(view.getViewEnum());
185         if (buttons != null) {
186             for (KSButton button : buttons) {
187                 buttonPanel.add(button);
188             }
189         }
190         KSMenuItemData item = viewMenuItemMap.get(view.getViewEnum());
191         if (item != null) {
192             item.setSelected(true, false);
193         }
194     }
195 
196     /* Adds 'name' of the menu above menu items. This menu name has no view */
197 
198     public void addMenu(String title) {
199         if (title != null && !title.equals("")) {
200             KSMenuItemData item = new KSMenuItemData(title);
201             topLevelMenuItems.add(item);
202             List<View> list = new ArrayList<View>();
203             menuViewMap.put(title, list);
204             if (refreshMenuOnAdd) {
205                 menu.refresh();
206             }
207         }
208     }
209 
210     /**
211      * Adds a view whose view the menu at first.  addMenuItem() should not be called before this method if it is
212      * intended for this to be the first view the user sees.  revealMenuItems()
213      * must be called to show any items that were added after this call.
214      * IMPORTANT: the order in which you call this method, addMenuItem and addSpecialMenuItem affects the order in which they appear
215      * on the menu, but also the order in which they are shown when the showNextViewOnMenu is called.  Care must be
216      * taken when calling these methods to insure a consistent/logical UI.
217      */
218     public void addStartMenuItem(String parentMenu, final View view) {
219         addMenuItem(parentMenu, view);
220         this.setDefaultView(view.getViewEnum());
221         refreshMenuOnAdd = false;
222         menu.refresh();
223     }
224 
225     public void revealMenuItems() {
226         menu.refresh();
227         refreshMenuOnAdd = true;
228     }
229 
230     /**
231      * Adds a view whose view name will appear as a link on the menu, the view will be shown when this item is clicked.
232      * IMPORTANT: the order in which you call addMenuItem and addSpecialMenuItem affects the order in which they appear
233      * on the menu, but also the order in which they are shown when the showNextViewOnMenu is called.  Care must be
234      * taken when calling these methods to insure a consistent/logical UI.
235      */
236     @Override
237     public void addMenuItem(String parentMenu, final View view) {
238         super.addView(view);
239         KSMenuItemData parentItem = null;
240         for (int i = 0; i < topLevelMenuItems.size(); i++) {
241             if (topLevelMenuItems.get(i).getLabel().equals(parentMenu)) {
242                 parentItem = topLevelMenuItems.get(i);
243                 break;
244             }
245         }
246 
247         KSMenuItemData item = new KSMenuItemData(view.getName());
248         viewMenuItemMap.put(view.getViewEnum(), item);
249         item.setClickHandler(new ClickHandler() {
250 
251             @Override
252             public void onClick(ClickEvent event) {
253                 DeferredCommand.addCommand(new Command() {
254                     @Override
255                     public void execute() {
256                         MenuSectionController.this.showView(view.getViewEnum(), showViewCallback);
257                     }
258                 });
259             }
260 
261         });
262 
263         if (parentItem != null) {
264             menuOrder.add(view);
265             parentItem.addSubItem(item);
266             menuViewMap.get(parentMenu).add(view);
267         } else {
268             menuOrder.add(view);
269             topLevelMenuItems.add(item);
270             menuViewMap.get("").add(view);
271         }
272 
273         if (refreshMenuOnAdd) {
274             menu.refresh();
275         }
276     }
277 
278 
279     /**
280      * This adds a special item that is a top level menu item to the menu, this will appear directly below other
281      * menus that are currently added to the menu.  This menu item has special styling to make it more apparent on
282      * the menu visually.
283      * IMPORTANT: the order in which you call addMenuItem and addSpecialMenuItem affects the order in which they appear
284      * on the menu, but also the order in which they are shown when the showNextViewOnMenu is called.  Care must be
285      * taken when calling these methods to insure a consistent/logical UI.
286      */
287     @Override
288     public void addSpecialMenuItem(final View view, String description) {
289         //TODO add description to the menu item
290         super.addView(view);
291         menuViewMap.get("").add(view);
292         menuOrder.add(view);
293         KSMenuItemData item = new KSMenuItemData(view.getName());
294         item.addSpecialStyle("ks-menu-layout-special-menu-item-panel");
295         viewMenuItemMap.put(view.getViewEnum(), item);
296         item.setClickHandler(new ClickHandler() {
297 
298             @Override
299             public void onClick(ClickEvent event) {
300                 DeferredCommand.addCommand(new Command() {
301                     @Override
302                     public void execute() {
303                         MenuSectionController.this.showView(view.getViewEnum(), showViewCallback);
304                     }
305                 });
306             }
307 
308         });
309         topLevelMenuItems.add(item);
310         if (refreshMenuOnAdd) {
311             menu.refresh();
312         }
313     }
314 
315     /**
316      * This version of updateModel only traverses views that can be accessed through its menu.
317      * Views that are part of this controller, but not part of the menu, can update their views through
318      * updateModelFromCurrentView and updateModelFromView methods.
319      */
320     @Override
321     public void updateModel() {
322         for (View v : menuOrder) {
323             v.updateModel();
324         }
325 
326     }
327 
328     @Override
329     public ArrayList<ExportElement> getExportElementsFromView() {
330         return super.getExportElementsFromView();
331         
332     }
333 }