View Javadoc

1   package org.appfuse.webapp.client.ui.navigation;
2   
3   import org.appfuse.webapp.client.application.utils.menu.MenuItem;
4   
5   import com.github.gwtbootstrap.client.ui.constants.Constants;
6   import com.google.gwt.core.client.GWT;
7   import com.google.gwt.dom.client.AnchorElement;
8   import com.google.gwt.dom.client.Document;
9   import com.google.gwt.dom.client.LIElement;
10  import com.google.gwt.dom.client.UListElement;
11  import com.google.gwt.place.shared.PlaceChangeEvent;
12  import com.google.gwt.uibinder.client.UiBinder;
13  import com.google.gwt.uibinder.client.UiField;
14  import com.google.gwt.user.client.ui.Widget;
15  
16  public class SideNavigationBar extends BaseNavigationBar {
17  
18      private static SideNavigationBarUiBinder uiBinder = GWT.create(SideNavigationBarUiBinder.class);
19  
20      interface SideNavigationBarUiBinder extends UiBinder<Widget, SideNavigationBar> {
21      }
22  
23      @UiField
24      UListElement sideBar;
25  
26      public SideNavigationBar() {
27          initWidget(uiBinder.createAndBindUi(this));
28      }
29  
30      @Override
31      public void onPlaceChange(final PlaceChangeEvent event) {
32          sideBar.setInnerHTML("");
33          final MenuItem currentMenuItem = findCurrentMenuItem(event.getNewPlace());
34          if (currentMenuItem != null && currentMenuItem.getParent() != null && currentMenuItem.getParent().getTitle() != null) {
35              drawMenu(currentMenuItem);
36              sideBar.removeClassName("hide");
37          } else {
38              // do not draw root menu on the side
39              sideBar.addClassName("hide");
40          }
41      }
42  
43      private void drawMenu(final MenuItem currentMenuItem) {
44          final LIElement navHeader = Document.get().createLIElement();
45          navHeader.setInnerText(currentMenuItem.getParent().getTitle());
46          navHeader.addClassName("nav-header");
47          sideBar.appendChild(navHeader);
48  
49          for (final MenuItem menuItem : currentMenuItem.getParent()) {
50              final LIElement li = Document.get().createLIElement();
51              final AnchorElement anchor = Document.get().createAnchorElement();
52              anchor.setTitle(menuItem.getTitle());
53              anchor.setInnerText(menuItem.getTitle());
54              anchor.setHref("#" + application.getPlaceHistoryMapper().getToken(menuItem.getPlace()));
55              li.appendChild(anchor);
56              menuItem.setElement(li);
57              sideBar.appendChild(li);
58  
59              if (menuItem == currentMenuItem) {
60                  li.addClassName(Constants.ACTIVE);
61              }
62          }
63      }
64  
65  }