View Javadoc

1   /**
2    * 
3    */
4   package org.appfuse.webapp.client.ui.navigation;
5   
6   import org.appfuse.webapp.client.application.utils.menu.MenuItem;
7   
8   import com.github.gwtbootstrap.client.ui.constants.Constants;
9   import com.google.gwt.core.client.GWT;
10  import com.google.gwt.dom.client.AnchorElement;
11  import com.google.gwt.dom.client.Document;
12  import com.google.gwt.dom.client.Element;
13  import com.google.gwt.dom.client.LIElement;
14  import com.google.gwt.dom.client.UListElement;
15  import com.google.gwt.place.shared.Place;
16  import com.google.gwt.place.shared.PlaceChangeEvent;
17  import com.google.gwt.uibinder.client.UiBinder;
18  import com.google.gwt.uibinder.client.UiField;
19  import com.google.gwt.user.client.ui.Widget;
20  
21  /**
22   * @author ivangsa
23   *
24   */
25  public class NavigationBar extends BaseNavigationBar implements PlaceChangeEvent.Handler {
26      private static NavigationBarUiBinder uiBinder = GWT.create(NavigationBarUiBinder.class);
27  
28      interface NavigationBarUiBinder extends UiBinder<Widget, NavigationBar> {
29      }
30  
31      @UiField
32      AnchorElement brand;
33      @UiField
34      UListElement navBar;
35  
36      public NavigationBar() {
37          initWidget(uiBinder.createAndBindUi(this));
38          this.setStyleName(DEBUG_ID_PREFIX);
39      }
40  
41      public void load() {
42          brand.setHref(application.getContextPath());
43          navBar.setInnerHTML("");
44          for (final MenuItem menuItem : menu.getRootMenu()) {
45              drawMenu(navBar, menuItem);
46          }
47          activateCurrentMenuItem(application.getPlaceController().getWhere());
48      }
49  
50      private void drawMenu(final Element panel, final MenuItem menuItem) {
51          if (!isMenuItemAllowed(menuItem)) {
52              return;
53          }
54          if (menuItem.isLeafMenuItem()) {
55              final LIElement li = Document.get().createLIElement();
56              final AnchorElement a = Document.get().createAnchorElement();
57              a.setTitle(menuItem.getTitle());
58              a.setInnerText(menuItem.getTitle());
59              a.setHref("#" + application.getPlaceHistoryMapper().getToken(menuItem.getPlace()));
60              li.appendChild(a);
61              panel.appendChild(li);
62              menuItem.setElement(li);
63          } else {
64              final LIElement dropdown = Document.get().createLIElement();
65              dropdown.addClassName("dropdown");
66              final AnchorElement toogle = Document.get().createAnchorElement();
67              toogle.setTitle(menuItem.getTitle());
68              toogle.setInnerText(menuItem.getTitle());
69              toogle.addClassName("dropdown-toggle");
70              toogle.setAttribute("data-toggle", "dropdown");// XXX
71              menuItem.setElement(dropdown);
72              final UListElement dropdownMenu = Document.get().createULElement();
73              dropdownMenu.addClassName("dropdown-menu");
74              dropdown.appendChild(toogle);
75              dropdown.appendChild(dropdownMenu);
76              panel.appendChild(dropdown);
77              for (final MenuItem childItem : menuItem) {
78                  drawMenu(dropdownMenu, childItem);
79              }
80          }
81      }
82  
83      @Override
84      public void onPlaceChange(final PlaceChangeEvent event) {
85          activateCurrentMenuItem(event.getNewPlace());
86      }
87  
88      private void activateCurrentMenuItem(final Place currentPlace) {
89          for (final MenuItem menuItem : menu.asList()) {
90              final Element element = menuItem.getElement();
91              if (element != null) {
92                  element.removeClassName(Constants.ACTIVE);
93              }
94          }
95          final MenuItem currentMenuItem = findCurrentMenuItem(currentPlace);
96          if (currentMenuItem != null) {
97              setActive(currentMenuItem);
98          }
99      }
100 
101     /**
102      * Marks as 'active' menu item for current place and its parent menu places.
103      * 
104      * @param menuItem
105      * @return
106      */
107     private void setActive(final MenuItem menuItem) {
108         if (menuItem != null) {
109             final Element element = menuItem.getElement();
110             if (element != null) {
111                 element.addClassName(Constants.ACTIVE);
112             }
113             setActive(menuItem.getParent());
114         }
115     }
116 }