/*******************************************************************************
 * Copyright (c) 2012-2017 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.projectimport.wizard;

import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.api.core.jsonrpc.commons.RequestHandlerConfigurator;
import org.eclipse.che.api.core.jsonrpc.commons.RequestHandlerManager;
import org.eclipse.che.api.project.shared.dto.ImportProgressRecordDto;
import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.notification.StatusNotification;
import org.eclipse.che.ide.api.project.wizard.ProjectNotificationSubscriber;

import static com.google.common.base.Strings.nullToEmpty;
import static org.eclipse.che.api.project.shared.Constants.EVENT_IMPORT_OUTPUT_PROGRESS;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.PROGRESS;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.SUCCESS;

/**
 * Json RPC based implementation of the {@link ProjectNotificationSubscriber} which notifies user
 * about output events via popup notification.
 *
 * @author Vlad Zhukovskyi
 * @since 5.9.0
 */
public class ProjectImportOutputJsonRpcNotifier implements ProjectNotificationSubscriber {

    private final NotificationManager        notificationManager;
    private final CoreLocalizationConstant   locale;
    private final RequestHandlerConfigurator configurator;
    private final RequestHandlerManager      requestHandlerManager;

    private StatusNotification singletonNotification;
    private String             projectName;

    @Inject
    public ProjectImportOutputJsonRpcNotifier(NotificationManager notificationManager,
                                              CoreLocalizationConstant locale,
                                              EventBus eventBus,
                                              RequestHandlerConfigurator configurator,
                                              RequestHandlerManager requestHandlerManager) {
        this.notificationManager = notificationManager;
        this.locale = locale;
        this.configurator = configurator;
        this.requestHandlerManager = requestHandlerManager;

        eventBus.addHandler(WsAgentStateEvent.TYPE, new WsAgentStateHandler() {
            @Override
            public void onWsAgentStarted(WsAgentStateEvent event) {
            }

            @Override
            public void onWsAgentStopped(WsAgentStateEvent event) {
                requestHandlerManager.deregister(EVENT_IMPORT_OUTPUT_PROGRESS + "/" + projectName);

                singletonNotification.setStatus(FAIL);
                singletonNotification.setContent("");
            }
        });
    }

    @Override
    public void subscribe(String projectName, StatusNotification notification) {
        this.projectName = projectName;
        this.singletonNotification = notification;

        configurator.newConfiguration()
                    .methodName(EVENT_IMPORT_OUTPUT_PROGRESS + "/" + projectName)
                    .paramsAsDto(ImportProgressRecordDto.class)
                    .noResult()
                    .withConsumer(progressRecord -> {
                        singletonNotification.setTitle(locale.importingProject(ProjectImportOutputJsonRpcNotifier.this.projectName));
                        singletonNotification.setContent(nullToEmpty(progressRecord.getLine()));
                    });
    }

    @Override
    public void subscribe(String projectName) {
        singletonNotification = notificationManager.notify(locale.importingProject(projectName), PROGRESS, FLOAT_MODE);
        subscribe(projectName, singletonNotification);
    }

    @Override
    public void onSuccess() {
        requestHandlerManager.deregister(EVENT_IMPORT_OUTPUT_PROGRESS + "/" + projectName);

        singletonNotification.setStatus(SUCCESS);
        singletonNotification.setTitle(locale.importProjectMessageSuccess(projectName));
        singletonNotification.setContent("");
    }

    @Override
    public void onFailure(String errorMessage) {
        requestHandlerManager.deregister(EVENT_IMPORT_OUTPUT_PROGRESS + "/" + projectName);

        singletonNotification.setStatus(FAIL);
        singletonNotification.setContent(errorMessage);
    }
}

@@ -3377,33 +3377,89 @@
 + projectName);%0A
-%0A
+                if (singletonNotification != null) %7B%0A    
                 
@@ -3505,32 +3505,36 @@
                 
+   
+ 
 singletonNotific
@@ -3551,24 +3551,42 @@
 ontent(%22%22);%0A
+                %7D%0A
             

Fix NPE (#5582)