package org.jbpm.designer.client.handlers;

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

import com.google.gwt.user.client.ui.IsWidget;
import org.guvnor.common.services.project.model.Package;
import org.jboss.errai.common.client.api.Caller;
import org.jboss.errai.common.client.api.RemoteCallback;
import org.jbpm.designer.client.resources.i18n.DesignerEditorConstants;
import org.jbpm.designer.client.type.Bpmn2Type;
import org.jbpm.designer.service.DesignerAssetService;
import org.kie.uberfire.client.callbacks.DefaultErrorCallback;
import org.kie.workbench.common.services.shared.validation.ValidatorWithReasonCallback;
import org.kie.workbench.common.widgets.client.handlers.DefaultNewResourceHandler;
import org.kie.workbench.common.widgets.client.handlers.NewResourcePresenter;
import org.kie.workbench.common.widgets.client.resources.i18n.CommonConstants;
import org.uberfire.backend.vfs.Path;
import org.uberfire.client.mvp.PlaceManager;
import org.uberfire.client.workbench.widgets.common.ErrorPopup;
import org.uberfire.mvp.PlaceRequest;
import org.uberfire.mvp.impl.PathPlaceRequest;
import org.uberfire.workbench.type.ResourceTypeDefinition;

@ApplicationScoped
public class NewProcessHandler extends DefaultNewResourceHandler {

    @Inject
    private Caller<DesignerAssetService> designerAssetService;

    @Inject
    private PlaceManager placeManager;

    @Inject
    private Bpmn2Type resourceType;

    @Override
    public String getDescription() {
        return DesignerEditorConstants.INSTANCE.businessProcess();
    }

    @Override
    public IsWidget getIcon() {
        return null;
    }

    @Override
    public ResourceTypeDefinition getResourceType() {
        return resourceType;
    }

    @Override
    public void create( final Package pkg,
                        final String baseFileName,
                        final NewResourcePresenter presenter ) {
        designerAssetService.call( new RemoteCallback<Path>() {
            @Override
            public void callback( final Path path ) {
                presenter.complete();
                notifySuccess();
                final PlaceRequest place = new PathPlaceRequest( path );
                placeManager.goTo( place );
            }
        }, new DefaultErrorCallback() ).createProcess( pkg.getPackageMainResourcesPath(), buildFileName( baseFileName,
                                                                                                         resourceType ) );
    }

    @Override
    public void validate( final String baseFileName,
                          final ValidatorWithReasonCallback callback ) {
        if ( pathLabel.getPath() == null ) {
            ErrorPopup.showMessage( CommonConstants.INSTANCE.MissingPath() );
            callback.onFailure();
            return;
        }

        final String fileName = buildFileName( baseFileName,
                                               getResourceType() );

        validationService.call( new RemoteCallback<Boolean>() {
            @Override
            public void callback( final Boolean response ) {
                if ( Boolean.TRUE.equals( response ) ) {
                    callback.onSuccess();
                } else {
                    callback.onFailure( CommonConstants.INSTANCE.InvalidFileName0( baseFileName ) );
                }
            }
        } ).isFileNameValid( fileName );
    }

}

@@ -569,96 +569,8 @@
 ck;%0A
-import org.kie.workbench.common.services.shared.validation.ValidatorWithReasonCallback;%0A
 impo
@@ -730,87 +730,8 @@
 er;%0A
-import org.kie.workbench.common.widgets.client.resources.i18n.CommonConstants;%0A
 impo
@@ -813,72 +813,8 @@
 er;%0A
-import org.uberfire.client.workbench.widgets.common.ErrorPopup;%0A
 impo
@@ -2289,916 +2289,6 @@
  %7D%0A%0A
-    @Override%0A    public void validate( final String baseFileName,%0A                          final ValidatorWithReasonCallback callback ) %7B%0A        if ( pathLabel.getPath() == null ) %7B%0A            ErrorPopup.showMessage( CommonConstants.INSTANCE.MissingPath() );%0A            callback.onFailure();%0A            return;%0A        %7D%0A%0A        final String fileName = buildFileName( baseFileName,%0A                                               getResourceType() );%0A%0A        validationService.call( new RemoteCallback%3CBoolean%3E() %7B%0A            @Override%0A            public void callback( final Boolean response ) %7B%0A                if ( Boolean.TRUE.equals( response ) ) %7B%0A                    callback.onSuccess();%0A                %7D else %7B%0A                    callback.onFailure( CommonConstants.INSTANCE.InvalidFileName0( baseFileName ) );%0A                %7D%0A            %7D%0A        %7D ).isFileNameValid( fileName );%0A    %7D%0A%0A
 %7D%0A

Add 'Package Selector' to 'New Resource' popup