@Before
  public void setup() {
    when(authzManager.authorize(any(Resource.class), any(User.class))).thenReturn(true);

    activatedActivity = mock(Activity.class);
    when(activatedActivity.getIdentifier()).thenReturn("activated activity");

    when(activatedActivityBean.getInstance()).thenReturn(activatedActivity);
    when(activatedActivityBean.isActivated()).thenReturn(true);

    when(nonActivatedActivityBean.isActivated()).thenReturn(false);

    Collection<SyncBeanDef<Activity>> activityList = new ArrayList<SyncBeanDef<Activity>>();
    activityList.add(activatedActivityBean);
    activityList.add(nonActivatedActivityBean);

    // This covers the case where the activity manager goes directly to the Errai bean manager.
    // The list includes all beans, active or otherwise, and the activity manager has to filter
    // them.
    when(iocManager.lookupBeans(Activity.class)).thenReturn(activityList);

    // And this covers the case where the activity manager does the lookup via the
    // ActivityBeansCache.
    // We set this up assuming ActivityBeansCache is well-behaved, and hides the existence of
    // inactive beans.
    // (of course this assumption is verified in a separate test)
    ActivityAndMetaInfo activatedActivityAndMetaInfo =
        activityBeansCache
        .new ActivityAndMetaInfo(activatedActivityBean, 0, Collections.<String>emptyList());
    when(activityBeansCache.getResourceActivities())
        .thenReturn(singletonList(activatedActivityAndMetaInfo));
    when(activityBeansCache.getActivity("activated activity")).thenReturn(activatedActivityBean);
  }
Пример #2
0
  public Widget makeItem(final MenuItem item, boolean isRoot) {
    if (!authzManager.authorize(item, identity)) {
      return null;
    }

    if (item instanceof MenuItemCommand) {
      final MenuItemCommand cmdItem = (MenuItemCommand) item;
      if (isRoot) {
        final Button button = new Button(cmdItem.getCaption());
        button.setSize(ButtonSize.SMALL);
        button.setEnabled(item.isEnabled());
        button.addClickHandler(
            new ClickHandler() {
              @Override
              public void onClick(final ClickEvent event) {
                cmdItem.getCommand().execute();
              }
            });
        item.addEnabledStateChangeListener(
            new EnabledStateChangeListener() {
              @Override
              public void enabledStateChanged(final boolean enabled) {
                button.setEnabled(enabled);
              }
            });
        return button;
      } else {
        final NavbarLink navbarLink = new NavbarLink();
        navbarLink.setText(cmdItem.getCaption());
        if (!item.isEnabled()) {
          navbarLink.addStyleName("disabled");
        }
        navbarLink.addClickHandler(
            new ClickHandler() {
              @Override
              public void onClick(final ClickEvent event) {
                cmdItem.getCommand().execute();
              }
            });
        item.addEnabledStateChangeListener(
            new EnabledStateChangeListener() {
              @Override
              public void enabledStateChanged(final boolean enabled) {
                if (enabled) {
                  navbarLink.removeStyleName("disabled");
                } else {
                  navbarLink.addStyleName("disabled");
                }
              }
            });
        return navbarLink;
      }

    } else if (item instanceof MenuGroup) {
      final MenuGroup groups = (MenuGroup) item;
      if (isRoot) {
        final List<Widget> widgetList = new ArrayList<Widget>();
        for (final MenuItem _item : groups.getItems()) {
          final Widget widget = makeItem(_item, false);
          if (widget != null) {
            widgetList.add(widget);
          }
        }

        if (widgetList.isEmpty()) {
          return null;
        }

        return makeDropDownMenuButton(groups.getCaption(), widgetList);

      } else {
        final List<Widget> widgetList = new ArrayList<Widget>();
        for (final MenuItem _item : groups.getItems()) {
          final Widget result = makeItem(_item, false);
          if (result != null) {
            widgetList.add(result);
          }
        }

        if (widgetList.isEmpty()) {
          return null;
        }

        return makeDropDownMenuButton(groups.getCaption(), widgetList);
      }

    } else if (item instanceof MenuCustom) {
      final Object result = ((MenuCustom) item).build();
      if (result instanceof Widget) {
        return (Widget) result;
      }
    }

    return null;
  }