diff --git a/uberfire-workbench/uberfire-workbench-client/src/main/java/org/uberfire/client/workbench/widgets/menu/WorkbenchMenuBarPresenter.java b/uberfire-workbench/uberfire-workbench-client/src/main/java/org/uberfire/client/workbench/widgets/menu/WorkbenchMenuBarPresenter.java index 6b86cd75de..6efca28fd0 100644 --- a/uberfire-workbench/uberfire-workbench-client/src/main/java/org/uberfire/client/workbench/widgets/menu/WorkbenchMenuBarPresenter.java +++ b/uberfire-workbench/uberfire-workbench-client/src/main/java/org/uberfire/client/workbench/widgets/menu/WorkbenchMenuBarPresenter.java @@ -19,12 +19,13 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; - import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.event.Observes; import javax.inject.Inject; +import com.google.gwt.user.client.ui.IsWidget; +import com.google.gwt.user.client.ui.Widget; import org.jboss.errai.ioc.client.container.IOC; import org.jboss.errai.security.shared.api.identity.User; import org.uberfire.client.menu.AuthFilterMenuVisitor; @@ -51,9 +52,6 @@ import org.uberfire.workbench.model.menu.Menus; import org.uberfire.workbench.model.menu.impl.BaseMenuVisitor; -import com.google.gwt.user.client.ui.IsWidget; -import com.google.gwt.user.client.ui.Widget; - /** * Presenter for WorkbenchMenuBar that mediates changes to the Workbench MenuBar * in response to changes to the selected WorkbenchPart. The menu structure is @@ -261,9 +259,9 @@ public void enabledStateChanged( final boolean enabled ) { } ); } - } + } ) ); - ) ); + synchronizeUIWithMenus( addedMenu.getItems() ); } private String getMenuItemId( final MenuItem menuItem ) { @@ -274,70 +272,72 @@ private void addPerspectiveMenus( final PerspectiveActivity perspective ) { final String perspectiveId = perspective.getIdentifier(); final Menus menus = perspective.getMenus(); view.clearContextMenu(); - if( menus != null ) { - menus.accept(new AuthFilterMenuVisitor(authzManager, identity, new BaseMenuVisitor() { + if ( menus != null ) { + menus.accept( new AuthFilterMenuVisitor( authzManager, identity, new BaseMenuVisitor() { private String parentId = null; @Override - public boolean visitEnter(final MenuGroup menuGroup) { - parentId = getMenuItemId(menuGroup); - view.addContextGroupMenuItem(perspectiveId, parentId, menuGroup.getCaption(), menuGroup.getPosition()); + public boolean visitEnter( final MenuGroup menuGroup ) { + parentId = getMenuItemId( menuGroup ); + view.addContextGroupMenuItem( perspectiveId, parentId, menuGroup.getCaption(), menuGroup.getPosition() ); return true; } @Override - public void visitLeave(MenuGroup menuGroup) { + public void visitLeave( MenuGroup menuGroup ) { parentId = null; } @Override - public void visit(final MenuItemPlain menuItemPlain) { - view.addContextMenuItem(perspectiveId, getMenuItemId(menuItemPlain), menuItemPlain.getCaption(), parentId, null, menuItemPlain.getPosition()); - setupEnableDisableContextMenuItem(menuItemPlain); + public void visit( final MenuItemPlain menuItemPlain ) { + view.addContextMenuItem( perspectiveId, getMenuItemId( menuItemPlain ), menuItemPlain.getCaption(), parentId, null, menuItemPlain.getPosition() ); + setupEnableDisableContextMenuItem( menuItemPlain ); } @Override - public void visit(final MenuCustom menuCustom) { - view.addContextMenuItem(perspectiveId, getMenuItemId(menuCustom), menuCustom.getCaption(), parentId, null, menuCustom.getPosition()); - setupEnableDisableContextMenuItem(menuCustom); + public void visit( final MenuCustom menuCustom ) { + view.addContextMenuItem( perspectiveId, getMenuItemId( menuCustom ), menuCustom.getCaption(), parentId, null, menuCustom.getPosition() ); + setupEnableDisableContextMenuItem( menuCustom ); } @Override - public void visit(final MenuItemCommand menuItemCommand) { - view.addContextMenuItem(perspectiveId, getMenuItemId(menuItemCommand), menuItemCommand.getCaption(), parentId, menuItemCommand.getCommand(), menuItemCommand.getPosition()); - setupEnableDisableContextMenuItem(menuItemCommand); + public void visit( final MenuItemCommand menuItemCommand ) { + view.addContextMenuItem( perspectiveId, getMenuItemId( menuItemCommand ), menuItemCommand.getCaption(), parentId, menuItemCommand.getCommand(), menuItemCommand.getPosition() ); + setupEnableDisableContextMenuItem( menuItemCommand ); } @Override - public void visit(final MenuItemPerspective menuItemPerspective) { - view.addContextMenuItem(perspectiveId, menuItemPerspective.getPlaceRequest().getIdentifier(), menuItemPerspective.getCaption(), parentId, new Command() { + public void visit( final MenuItemPerspective menuItemPerspective ) { + view.addContextMenuItem( perspectiveId, menuItemPerspective.getPlaceRequest().getIdentifier(), menuItemPerspective.getCaption(), parentId, new Command() { @Override public void execute() { - IOC.getBeanManager().lookupBean(PlaceManager.class).getInstance().goTo(menuItemPerspective.getPlaceRequest()); + IOC.getBeanManager().lookupBean( PlaceManager.class ).getInstance().goTo( menuItemPerspective.getPlaceRequest() ); } - }, menuItemPerspective.getPosition()); - setupEnableDisableContextMenuItem(menuItemPerspective); + }, menuItemPerspective.getPosition() ); + setupEnableDisableContextMenuItem( menuItemPerspective ); } - private void setupEnableDisableContextMenuItem(final MenuItem menuItem) { - menuItem.addEnabledStateChangeListener(new EnabledStateChangeListener() { + private void setupEnableDisableContextMenuItem( final MenuItem menuItem ) { + menuItem.addEnabledStateChangeListener( new EnabledStateChangeListener() { @Override - public void enabledStateChanged(final boolean enabled) { - view.enableContextMenuItem(getMenuItemId(menuItem), - enabled); + public void enabledStateChanged( final boolean enabled ) { + view.enableContextMenuItem( getMenuItemId( menuItem ), + enabled ); } - }); + } ); } - })); + } ) ); + + synchronizeUIWithMenus( menus.getItems() ); } } protected void onPerspectiveChange( @Observes final PerspectiveChange perspectiveChange ) { - final Activity activity = activityManager.getActivity(perspectiveChange.getPlaceRequest()); - if( activity!= null && activity.isType( ActivityResourceType.PERSPECTIVE.name() ) ) { - addPerspectiveMenus( (PerspectiveActivity) activity); + final Activity activity = activityManager.getActivity( perspectiveChange.getPlaceRequest() ); + if ( activity != null && activity.isType( ActivityResourceType.PERSPECTIVE.name() ) ) { + addPerspectiveMenus( (PerspectiveActivity) activity ); } view.selectMenuItem( perspectiveChange.getPlaceRequest().getIdentifier() ); } @@ -392,4 +392,17 @@ public void addExpandHandler( final Command command ) { List getAddedMenus() { return this.addedMenus; } + + //Force UI to update to state of MenuItems. Should be called after MenuItems are configured with EnabledStateChangeListener's. + void synchronizeUIWithMenus( final List menuItems ) { + for ( MenuItem menuItem : menuItems ) { + if ( menuItem instanceof MenuGroup ) { + synchronizeUIWithMenus( ( (MenuGroup) menuItem ).getItems() ); + + } else { + menuItem.setEnabled( menuItem.isEnabled() ); + } + } + + } } diff --git a/uberfire-workbench/uberfire-workbench-client/src/test/java/org/uberfire/client/workbench/widgets/menu/WorkbenchMenuBarPresenterTest.java b/uberfire-workbench/uberfire-workbench-client/src/test/java/org/uberfire/client/workbench/widgets/menu/WorkbenchMenuBarPresenterTest.java index 266f6eb648..b83d38e64b 100644 --- a/uberfire-workbench/uberfire-workbench-client/src/test/java/org/uberfire/client/workbench/widgets/menu/WorkbenchMenuBarPresenterTest.java +++ b/uberfire-workbench/uberfire-workbench-client/src/test/java/org/uberfire/client/workbench/widgets/menu/WorkbenchMenuBarPresenterTest.java @@ -16,20 +16,6 @@ package org.uberfire.client.workbench.widgets.menu; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.isNull; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import org.jboss.errai.security.shared.api.identity.User; import org.junit.Test; import org.junit.runner.RunWith; @@ -54,6 +40,13 @@ import org.uberfire.workbench.model.menu.MenuPosition; import org.uberfire.workbench.model.menu.Menus; +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.*; + @RunWith(MockitoJUnitRunner.class) public class WorkbenchMenuBarPresenterTest { @@ -115,7 +108,7 @@ public void testPerspectiveChangeEvent() { final PerspectiveChange perspectiveChange = new PerspectiveChange( placeRequest, null, null, perspectiveId ); when( perspectiveActivity.getPlace() ).thenReturn( placeRequest ); - when (perspectiveActivity.isType( ActivityResourceType.PERSPECTIVE.name() )).thenReturn( true ); + when( perspectiveActivity.isType( ActivityResourceType.PERSPECTIVE.name() ) ).thenReturn( true ); when( authzManager.authorize( any( Resource.class ), eq( identity ) ) ).thenReturn( true ); presenter.addMenus( menus ); @@ -161,12 +154,11 @@ public void testAddContextMenuWithPermission() { when( activity.getIdentifier() ).thenReturn( perspectiveId ); when( activity.getMenus() ).thenReturn( contextMenus ); - when (activity.isType( ActivityResourceType.PERSPECTIVE.name() )).thenReturn( true ); + when( activity.isType( ActivityResourceType.PERSPECTIVE.name() ) ).thenReturn( true ); when( authzManager.authorize( contextMenus.getItems().get( 0 ), identity ) ).thenReturn( true ); when( activityManager.getActivity( placeRequest ) ).thenReturn( activity ); - - presenter.onPerspectiveChange( new PerspectiveChange(placeRequest, null, contextMenus, perspectiveId) ); + presenter.onPerspectiveChange( new PerspectiveChange( placeRequest, null, contextMenus, perspectiveId ) ); verify( authzManager ).authorize( contextMenus.getItems().get( 0 ), identity ); verify( view ).clearContextMenu(); @@ -183,11 +175,11 @@ public void testAddContextMenuWithoutPermission() { when( activity.getIdentifier() ).thenReturn( perspectiveId ); when( activity.getMenus() ).thenReturn( contextMenus ); - when (activity.isType( ActivityResourceType.PERSPECTIVE.name() )).thenReturn( true ); + when( activity.isType( ActivityResourceType.PERSPECTIVE.name() ) ).thenReturn( true ); when( authzManager.authorize( contextMenus.getItems().get( 0 ), identity ) ).thenReturn( false ); when( activityManager.getActivity( placeRequest ) ).thenReturn( activity ); - presenter.onPerspectiveChange( new PerspectiveChange(placeRequest, null, contextMenus, perspectiveId) ); + presenter.onPerspectiveChange( new PerspectiveChange( placeRequest, null, contextMenus, perspectiveId ) ); verify( authzManager ).authorize( contextMenus.getItems().get( 0 ), identity ); verify( view ).clearContextMenu(); @@ -203,12 +195,16 @@ public void testSetupEnableDisableMenuItemCommand() { when( authzManager.authorize( menus.getItems().get( 0 ), identity ) ).thenReturn( true ); presenter.addMenus( menus ); + verify( view, + times( 1 ) ).enableMenuItem( anyString(), eq( true ) ); menus.getItems().get( 0 ).setEnabled( true ); - verify( view ).enableMenuItem( anyString(), eq( true ) ); + verify( view, + times( 2 ) ).enableMenuItem( anyString(), eq( true ) ); menus.getItems().get( 0 ).setEnabled( false ); - verify( view ).enableMenuItem( anyString(), eq( false ) ); + verify( view, + times( 1 ) ).enableMenuItem( anyString(), eq( false ) ); } @Test @@ -220,12 +216,16 @@ public void testSetupEnableDisableMenuItemPlace() { when( authzManager.authorize( menus.getItems().get( 0 ), identity ) ).thenReturn( true ); presenter.addMenus( menus ); + verify( view, + times( 1 ) ).enableMenuItem( anyString(), eq( true ) ); menus.getItems().get( 0 ).setEnabled( true ); - verify( view ).enableMenuItem( anyString(), eq( true ) ); + verify( view, + times( 2 ) ).enableMenuItem( anyString(), eq( true ) ); menus.getItems().get( 0 ).setEnabled( false ); - verify( view ).enableMenuItem( anyString(), eq( false ) ); + verify( view, + times( 1 ) ).enableMenuItem( anyString(), eq( false ) ); } @Test @@ -237,12 +237,16 @@ public void testSetupEnableDisableMenuItemPerspective() { when( authzManager.authorize( menus.getItems().get( 0 ), identity ) ).thenReturn( true ); presenter.addMenus( menus ); + verify( view, + times( 1 ) ).enableMenuItem( anyString(), eq( true ) ); menus.getItems().get( 0 ).setEnabled( true ); - verify( view ).enableMenuItem( anyString(), eq( true ) ); + verify( view, + times( 2 ) ).enableMenuItem( anyString(), eq( true ) ); menus.getItems().get( 0 ).setEnabled( false ); - verify( view ).enableMenuItem( anyString(), eq( false ) ); + verify( view, + times( 1 ) ).enableMenuItem( anyString(), eq( false ) ); } @Test @@ -255,14 +259,17 @@ public void testSetupEnableDisableContextMenuItem() { when( activity.getIdentifier() ).thenReturn( perspectiveId ); when( activity.getMenus() ).thenReturn( contextMenus ); - when (activity.isType( ActivityResourceType.PERSPECTIVE.name() )).thenReturn( true ); + when( activity.isType( ActivityResourceType.PERSPECTIVE.name() ) ).thenReturn( true ); when( authzManager.authorize( contextMenus.getItems().get( 0 ), identity ) ).thenReturn( true ); when( activityManager.getActivity( placeRequest ) ).thenReturn( activity ); presenter.onPerspectiveChange( new PerspectiveChange( placeRequest, null, contextMenus, perspectiveId ) ); + verify( view, + times( 1 ) ).enableContextMenuItem( anyString(), eq( true ) ); contextMenus.getItems().get( 0 ).setEnabled( true ); - verify( view ).enableContextMenuItem( anyString(), eq( true ) ); + verify( view, + times( 2 ) ).enableContextMenuItem( anyString(), eq( true ) ); contextMenus.getItems().get( 0 ).setEnabled( false ); verify( view ).enableContextMenuItem( anyString(), eq( false ) ); @@ -292,100 +299,100 @@ public void testMenuInsertionOrder() { @Test public void testView() { - assertEquals(view, presenter.getView()); + assertEquals( view, presenter.getView() ); } @Test public void testCollapse() { presenter.collapse(); - assertFalse(presenter.isUseExpandedMode()); - verify(view).collapse(); + assertFalse( presenter.isUseExpandedMode() ); + verify( view ).collapse(); } @Test public void testExpand() { presenter.expand(); - assertTrue(presenter.isUseExpandedMode()); - verify(view).expand(); + assertTrue( presenter.isUseExpandedMode() ); + verify( view ).expand(); } @Test - public void testAddCollapseHandler(){ - final Command command = mock(Command.class); + public void testAddCollapseHandler() { + final Command command = mock( Command.class ); - presenter.addCollapseHandler(command); + presenter.addCollapseHandler( command ); - verify(view).addCollapseHandler(command); + verify( view ).addCollapseHandler( command ); } @Test - public void testExpandHandler(){ - doAnswer(new Answer() { + public void testExpandHandler() { + doAnswer( new Answer() { @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - ((Command) invocation.getArguments()[0]).execute(); + public Object answer( InvocationOnMock invocation ) throws Throwable { + ( (Command) invocation.getArguments()[ 0 ] ).execute(); return null; } - }).when(view).addExpandHandler(any(Command.class)); + } ).when( view ).addExpandHandler( any( Command.class ) ); presenter.setup(); - assertTrue(presenter.isExpanded()); + assertTrue( presenter.isExpanded() ); } @Test - public void testCollapseHandler(){ - doAnswer(new Answer() { + public void testCollapseHandler() { + doAnswer( new Answer() { @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - ((Command) invocation.getArguments()[0]).execute(); + public Object answer( InvocationOnMock invocation ) throws Throwable { + ( (Command) invocation.getArguments()[ 0 ] ).execute(); return null; } - }).when(view).addCollapseHandler(any(Command.class)); + } ).when( view ).addCollapseHandler( any( Command.class ) ); presenter.setup(); - assertFalse(presenter.isExpanded()); + assertFalse( presenter.isExpanded() ); } @Test - public void testAddExpandHandler(){ - final Command command = mock(Command.class); + public void testAddExpandHandler() { + final Command command = mock( Command.class ); - presenter.addExpandHandler(command); + presenter.addExpandHandler( command ); - verify(view).addExpandHandler(command); + verify( view ).addExpandHandler( command ); } @Test public void testClear() { presenter.clear(); - verify(view).clear(); + verify( view ).clear(); } @Test public void testOnPlaceMaximized() { - presenter.onPlaceMaximized(mock(PlaceMaximizedEvent.class)); + presenter.onPlaceMaximized( mock( PlaceMaximizedEvent.class ) ); - verify(view).collapse(); + verify( view ).collapse(); } @Test public void testOnPlaceMinimized() { - presenter.onPlaceMinimized(mock(PlaceMinimizedEvent.class)); + presenter.onPlaceMinimized( mock( PlaceMinimizedEvent.class ) ); - verify(view).expand(); + verify( view ).expand(); } @Test public void testOnPlaceMinimizedExpandMode() { presenter.collapse(); - presenter.onPlaceMinimized(mock(PlaceMinimizedEvent.class)); + presenter.onPlaceMinimized( mock( PlaceMinimizedEvent.class ) ); - verify(view, never()).expand(); + verify( view, never() ).expand(); } } \ No newline at end of file