View Javadoc

1   /**
2    * 
3    */
4   package org.appfuse.webapp.client.ui.navigation;
5   
6   import org.appfuse.webapp.client.application.Application;
7   import org.appfuse.webapp.client.application.ApplicationMenu;
8   import org.appfuse.webapp.client.application.ApplicationResources;
9   import org.appfuse.webapp.client.application.base.place.EntityProxyPlace;
10  import org.appfuse.webapp.client.application.base.place.EntitySearchPlace;
11  import org.appfuse.webapp.client.application.utils.menu.MenuItem;
12  
13  import com.google.gwt.core.client.GWT;
14  import com.google.gwt.place.shared.Place;
15  import com.google.gwt.place.shared.PlaceChangeEvent;
16  import com.google.gwt.user.client.ui.Composite;
17  
18  /**
19   * @author ivangsa
20   *
21   */
22  public abstract class BaseNavigationBar extends Composite implements PlaceChangeEvent.Handler {
23  
24      protected static final ApplicationResources i18n = GWT.create(ApplicationResources.class);
25  
26      protected Application application;
27      protected ApplicationMenu menu;
28  
29      /**
30       * 
31       */
32      public BaseNavigationBar() {
33          super();
34      }
35  
36      public void setApplication(final Application application) {
37          this.application = application;
38          this.menu = application.getMenu();
39          this.application.getEventBus().addHandler(PlaceChangeEvent.TYPE, this);
40      }
41  
42      protected boolean isSamePlace(final Place newPlace, final Place menuPlace) {
43          if (newPlace == null || menuPlace == null) {
44              return false;
45          }
46  
47          if (newPlace instanceof EntityProxyPlace && menuPlace instanceof EntityProxyPlace) {
48              final EntityProxyPlace newEntityPlace = (EntityProxyPlace) newPlace;
49              final EntityProxyPlace menuEntityPlace = (EntityProxyPlace) menuPlace;
50              return newEntityPlace.getProxyClass().equals(menuEntityPlace.getProxyClass());
51          }
52          else if (newPlace instanceof EntitySearchPlace && menuPlace instanceof EntitySearchPlace) {
53              final EntitySearchPlace newEntityPlace = (EntitySearchPlace) newPlace;
54              final EntitySearchPlace menuEntityPlace = (EntitySearchPlace) menuPlace;
55              return newEntityPlace.getProxyClass().equals(menuEntityPlace.getProxyClass());
56          }
57          return newPlace.getClass().equals(menuPlace.getClass());
58      }
59  
60      protected boolean isMenuItemAllowed(final MenuItem menuItem) {
61          return application.hasAnyRole(menuItem.getRoles().toArray(new String[menuItem.getRoles().size()]));
62      }
63  
64      protected MenuItem findCurrentMenuItem(final Place currentPlace) {
65          for (final MenuItem menuItem : menu.asList()) {
66              if (isSamePlace(currentPlace, menuItem.getPlace())) {
67                  return menuItem;
68              }
69          }
70          return null;
71      }
72  
73  }