package org.jbpm.designer.client.perspectives;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.jboss.errai.ioc.client.container.SyncBeanManager;
import org.kie.workbench.common.screens.projecteditor.client.menu.ProjectMenu;
import org.kie.workbench.common.widgets.client.handlers.NewResourcePresenter;
import org.kie.workbench.common.widgets.client.handlers.NewResourcesMenu;
import org.uberfire.client.annotations.Perspective;
import org.uberfire.client.annotations.WorkbenchMenu;
import org.uberfire.client.annotations.WorkbenchPerspective;
import org.uberfire.client.mvp.PlaceManager;
import org.uberfire.client.workbench.panels.impl.MultiListWorkbenchPanelPresenter;
import org.uberfire.client.workbench.panels.impl.SimpleWorkbenchPanelPresenter;
import org.uberfire.mvp.Command;
import org.uberfire.mvp.impl.DefaultPlaceRequest;
import org.uberfire.workbench.model.CompassPosition;
import org.uberfire.workbench.model.PanelDefinition;
import org.uberfire.workbench.model.PerspectiveDefinition;
import org.uberfire.workbench.model.impl.PanelDefinitionImpl;
import org.uberfire.workbench.model.impl.PartDefinitionImpl;
import org.uberfire.workbench.model.impl.PerspectiveDefinitionImpl;
import org.uberfire.workbench.model.menu.MenuFactory;
import org.uberfire.workbench.model.menu.Menus;

@ApplicationScoped
@WorkbenchPerspective(identifier = "home", isDefault = true)
public class HomePerspective {

    @Inject
    private NewResourcePresenter newResourcePresenter;

    @Inject
    private NewResourcesMenu newResourcesMenu;

    @Inject
    private PlaceManager placeManager;

    @Inject
    private SyncBeanManager iocManager;

    @Inject
    private ProjectMenu projectMenu;

    @Perspective
    public PerspectiveDefinition buildPerspective() {

        final PerspectiveDefinitionImpl perspective = new PerspectiveDefinitionImpl( MultiListWorkbenchPanelPresenter.class.getName() );
        perspective.setName( "Author" );

        final PanelDefinition west = new PanelDefinitionImpl( SimpleWorkbenchPanelPresenter.class.getName() );
        west.setWidth( 400 );
        west.setMinWidth( 350 );
        west.addPart( new PartDefinitionImpl( new DefaultPlaceRequest( "org.kie.guvnor.explorer" ) ) );

        perspective.getRoot().insertChild( CompassPosition.WEST,
                                           west );

        return perspective;
    }

    @WorkbenchMenu
    private Menus buildMenuBar() {
        return MenuFactory
                .newTopLevelMenu( "Projects" )
                .respondsWith( new Command() {
                    @Override
                    public void execute() {
                        placeManager.goTo( "org.kie.guvnor.explorer" );
                    }
                } )
                .endMenu()

                .newTopLevelMenu( "New" )
                .withItems( newResourcesMenu.getMenuItems() )
                .endMenu()

                .newTopLevelMenu( "Tools" )
                .withItems( projectMenu.getMenuItems() )
                .endMenu()

                .build();
    }
}

@@ -41,16 +41,55 @@
 tives;%0A%0A
+import javax.annotation.PostConstruct;%0A
 import j
@@ -296,24 +296,91 @@
 rojectMenu;%0A
+import org.kie.workbench.common.services.security.KieWorkbenchACL;%0A
 import org.k
@@ -1840,52 +1840,176 @@
    @
-Perspective%0A    public PerspectiveDefinition
+Inject%0A    private KieWorkbenchACL kieACL;%0A%0A    private PerspectiveDefinition perspective;%0A    private Menus menus;%0A%0A    @PostConstruct%0A    public void init() %7B%0A       
  bui
@@ -2023,19 +2023,17 @@
 ective()
- %7B%0A
+;
 %0A       
@@ -2037,40 +2037,274 @@
     
-final PerspectiveDefinitionImpl 
+buildMenuBar();%0A    %7D%0A%0A    @Perspective%0A    public PerspectiveDefinition getPerspective() %7B%0A        return this.perspective;%0A    %7D%0A%0A    @WorkbenchMenu%0A    public Menus getMenus() %7B%0A        return this.menus;%0A    %7D%0A%0A    public void buildPerspective() %7B%0A%0A        this.
 pers
@@ -2396,32 +2396,37 @@
 me() );%0A        
+this.
 perspective.setN
@@ -2436,13 +2436,21 @@
 ( %22A
-uthor
+dministration
 %22 );
@@ -2738,16 +2738,21 @@
         
+this.
 perspect
@@ -2855,62 +2855,14 @@
  );%0A
-%0A        return perspective;%0A    %7D%0A%0A    @WorkbenchMenu
+    %7D%0A
 %0A   
@@ -2870,21 +2870,20 @@
 private 
-Menus
+void
  buildMe
@@ -2900,22 +2900,28 @@
         
-return
+this.menus =
  MenuFac
@@ -2968,24 +2968,96 @@
 Projects%22 )%0A
+                .withRoles(kieACL.getGrantedRoles(%22wb_administration%22))%0A
             
@@ -3074,17 +3074,16 @@
 ndsWith(
- 
 new Comm
@@ -3206,17 +3206,16 @@
 er.goTo(
- 
 %22org.kie
@@ -3231,17 +3231,16 @@
 xplorer%22
- 
 );%0A     
@@ -3273,17 +3273,16 @@
        %7D
- 
 )%0A      
@@ -3340,15 +3340,85 @@
 enu(
- 
 %22New%22
- 
+)%0A                .withRoles(kieACL.getGrantedRoles(%22wb_administration%22)
 )%0A  
@@ -3442,17 +3442,16 @@
 thItems(
- 
 newResou
@@ -3465,33 +3465,32 @@
 u.getMenuItems()
- 
 )%0A              
@@ -3540,17 +3540,87 @@
 enu(
- 
 %22Tools%22
- 
+)%0A                .withRoles(kieACL.getGrantedRoles(%22wb_administration%22)
 )%0A  
@@ -3644,17 +3644,16 @@
 thItems(
- 
 projectM
@@ -3670,17 +3670,16 @@
 uItems()
- 
 )%0A      

fix for standalone menubar setup