package org.kie.workbench.common.widgets.client.menu;

import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import org.guvnor.common.services.project.events.ProjectChangeEvent;
import org.guvnor.common.services.project.model.Project;
import org.guvnor.common.services.project.service.ProjectService;
import org.jboss.errai.bus.client.api.RemoteCallback;
import org.jboss.errai.ioc.client.api.Caller;
import org.kie.workbench.common.widgets.client.resources.i18n.ToolsMenuConstants;
import org.uberfire.client.mvp.PlaceManager;
import org.uberfire.mvp.Command;
import org.uberfire.workbench.events.PathChangeEvent;
import org.uberfire.workbench.events.RepositoryChangeEvent;
import org.uberfire.workbench.model.menu.MenuFactory;
import org.uberfire.workbench.model.menu.MenuItem;

@ApplicationScoped
public class ToolsMenu {

    @Inject
    private PlaceManager placeManager;

    @Inject
    protected Caller<ProjectService> projectService;

    private MenuItem projectScreen = MenuFactory.newSimpleItem( ToolsMenuConstants.INSTANCE.ProjectEditor() ).respondsWith(
            new Command() {
                @Override
                public void execute() {
                    placeManager.goTo( "projectScreen" );
                }
            } ).endMenu().build().getItems().get( 0 );

    private MenuItem dataModelerScreen = MenuFactory.newSimpleItem( ToolsMenuConstants.INSTANCE.DataModeller() ).respondsWith(
            new Command() {
                @Override
                public void execute() {
                    placeManager.goTo( "dataModelerScreen" );
                }
            } ).endMenu().build().getItems().get( 0 );

    public List<MenuItem> getMenuItems() {
        ArrayList<MenuItem> menuItems = new ArrayList<MenuItem>();

        menuItems.add( projectScreen );
        menuItems.add( dataModelerScreen );

        return menuItems;
    }

    public void selectedPathChanged( @Observes final PathChangeEvent event ) {
        projectService.call( new RemoteCallback<Project>() {
            @Override
            public void callback( final Project project ) {
                projectScreen.setEnabled( project != null );
                dataModelerScreen.setEnabled( project != null );
            }
        } ).resolveProject( event.getPath() );
    }

    public void selectedPathChanged( @Observes final ProjectChangeEvent event ) {
        projectScreen.setEnabled( event.getProject() != null );
        dataModelerScreen.setEnabled( event.getProject() != null );
    }

    public void selectedPathChanged( @Observes final RepositoryChangeEvent event ) {
        projectScreen.setEnabled( false );
        dataModelerScreen.setEnabled( false );
    }
}

@@ -219,16 +219,151 @@
 nject;%0A%0A
+import org.guvnor.common.services.project.context.ProjectContext;%0Aimport org.guvnor.common.services.project.events.PackageChangeEvent;%0A
 import o
@@ -550,62 +550,8 @@
 ce;%0A
-import org.jboss.errai.bus.client.api.RemoteCallback;%0A
 impo
@@ -789,20 +789,21 @@
 .events.
-Path
+Group
 ChangeEv
@@ -1136,16 +1136,67 @@
 rvice;%0A%0A
+    @Inject%0A    protected ProjectContext context;%0A%0A
     priv
@@ -2139,36 +2139,37 @@
 ic void selected
-Path
+Group
 Changed( @Observ
@@ -2177,20 +2177,21 @@
 s final 
-Path
+Group
 ChangeEv
@@ -2216,137 +2216,152 @@
     
-projectService.call( new RemoteCallback%3C
+enableToolsMenuItems( context.getActive
 Project
-%3E
 () 
-%7B
+);
 %0A    
-        @Override%0A            public void callback( final Project projec
+%7D%0A%0A    public void selectedRepositoryChanged( @Observes final RepositoryChangeEvent even
 t ) 
@@ -2374,181 +2374,206 @@
     
-   
-     projectScreen.setEnabled( project != null );%0A                dataModelerScreen.setEnabled( project != null );%0A            %7D%0A        %7D ).resolveProject( event.getPath
+enableToolsMenuItems( context.getActiveProject() );%0A    %7D%0A%0A    public void selectedProjectChanged( @Observes final ProjectChangeEvent event ) %7B%0A        enableToolsMenuItems( context.getActiveProject
 () )
@@ -2599,34 +2599,37 @@
  void selectedPa
-th
+ckage
 Changed( @Observ
@@ -2630,38 +2630,38 @@
 Observes final P
-roject
+ackage
 ChangeEvent even
@@ -2678,43 +2678,47 @@
     
-projectScreen.setEnabled( event.get
+enableToolsMenuItems( context.getActive
 Proj
@@ -2722,32 +2722,24 @@
 roject()
- != null
  );%0A    
     data
@@ -2734,163 +2734,128 @@
     
+%7D%0A%0A
     
-dataModelerScreen.setEnabled( event.getProject() != null );
+private void enableToolsMenuItems( final Project project ) %7B
 %0A    
-%7D%0A%0A
     
-public void selectedPathChanged( @Observes final RepositoryChangeEvent event ) %7B
+final boolean enabled = ( project != null );
 %0A   
@@ -2877,37 +2877,39 @@
 een.setEnabled( 
-false
+enabled
  );%0A        data
@@ -2938,13 +2938,15 @@
 ed( 
-false
+enabled
  );%0A
@@ -2951,10 +2951,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Make ToolsMenu only respond to Group, Repository, Project and Package change events to be consistent with NewResourcePresenter