<commit_before>/*******************************************************************************
 * Copyright (c) 2012-2015 <NAME>.
 * 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:
 *   <NAME>. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.extension.machine.client.perspective.widgets.machine.panel;

import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.api.machine.gwt.client.MachineServiceClient;
import org.eclipse.che.api.machine.shared.dto.MachineDto;
import org.eclipse.che.api.machine.shared.dto.MachineStateDto;
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.promises.client.PromiseError;
import org.eclipse.che.api.workspace.shared.dto.UsersWorkspaceDto;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.parts.base.BasePresenter;
import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant;
import org.eclipse.che.ide.extension.machine.client.MachineResources;
import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory;
import org.eclipse.che.ide.extension.machine.client.machine.Machine;
import org.eclipse.che.api.machine.gwt.client.events.MachineStartingEvent;
import org.eclipse.che.api.machine.gwt.client.events.MachineStartingHandler;
import org.eclipse.che.ide.extension.machine.client.machine.events.MachineStateEvent;
import org.eclipse.che.ide.extension.machine.client.machine.events.MachineStateHandler;
import org.eclipse.che.ide.extension.machine.client.perspective.widgets.machine.appliance.MachineAppliancePresenter;
import org.eclipse.che.ide.workspace.start.StartWorkspaceEvent;
import org.eclipse.che.ide.workspace.start.StartWorkspaceHandler;
import org.eclipse.che.ide.workspace.start.StopWorkspaceEvent;
import org.eclipse.che.ide.workspace.start.StopWorkspaceHandler;
import org.vectomatic.dom.svg.ui.SVGResource;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The class contains business logic to control displaying of machines on special view.
 *
 * @author <NAME>
 * @author <NAME>
 */
@Singleton
public class MachinePanelPresenter extends BasePresenter implements MachinePanelView.ActionDelegate,
                                                                    MachineStateHandler,
                                                                    StartWorkspaceHandler,
                                                                    StopWorkspaceHandler,
                                                                    MachineStartingHandler {
    private final MachinePanelView                      view;
    private final MachineServiceClient                  service;
    private final EntityFactory                         entityFactory;
    private final MachineLocalizationConstant           locale;
    private final MachineAppliancePresenter             appliance;
    private final MachineResources                      resources;
    private final Map<MachineStateDto, MachineTreeNode> existingMachineNodes;
    private final Map<MachineStateDto, Machine>         cachedMachines;
    private final MachineTreeNode                       rootNode;
    private final List<MachineTreeNode>                 machineNodes;
    private final AppContext                            appContext;
    private MachineStateDto                             selectedMachineState;
    private boolean                                     isMachineRunning;

    @Inject
    public MachinePanelPresenter(MachinePanelView view,
                                 MachineServiceClient service,
                                 EntityFactory entityFactory,
                                 MachineLocalizationConstant locale,
                                 MachineAppliancePresenter appliance,
                                 EventBus eventBus,
                                 MachineResources resources,
                                 AppContext appContext) {
        this.view = view;
        this.view.setDelegate(this);

        this.service = service;
        this.entityFactory = entityFactory;
        this.locale = locale;
        this.appliance = appliance;
        this.resources = resources;
        this.appContext = appContext;

        this.machineNodes = new ArrayList<>();
        this.rootNode = entityFactory.createMachineNode(null, "root", machineNodes);

        this.existingMachineNodes = new HashMap<>();
        this.cachedMachines = new HashMap<>();

        eventBus.addHandler(MachineStateEvent.TYPE, this);
        eventBus.addHandler(StartWorkspaceEvent.TYPE, this);
        eventBus.addHandler(StopWorkspaceEvent.TYPE, this);
        eventBus.addHandler(MachineStartingEvent.TYPE, this);
    }

    /** Gets all machines and adds them to special place on view. */
    public Promise<List<MachineStateDto>> showMachines() {
        return showMachines(appContext.getWorkspace().getId());
    }

    private Promise<List<MachineStateDto>> showMachines(String workspaceId) {
        Promise<List<MachineStateDto>> machinesPromise = service.getMachinesStates(workspaceId);

        return machinesPromise.then(new Operation<List<MachineStateDto>>() {
            @Override
            public void apply(List<MachineStateDto> machineStates) throws OperationException {
                machineNodes.clear();
                if (machineStates.isEmpty()) {
                    appliance.showStub(locale.unavailableMachineInfo());

                    return;
                }

                for (MachineStateDto machineState : machineStates) {
                    addNodeToTree(machineState);
                }

                view.setData(rootNode);

                selectFirstNode();
            }
        });
    }

    private void addNodeToTree(MachineStateDto machineState) {
        MachineTreeNode machineNode = entityFactory.createMachineNode(rootNode, machineState, null);

        existingMachineNodes.put(machineState, machineNode);

        if (!machineNodes.contains(machineNode)) {
            machineNodes.add(machineNode);
        }
    }

    private void selectFirstNode() {
        if (!machineNodes.isEmpty()) {
            MachineTreeNode firstNode = machineNodes.get(0);

            view.selectNode(firstNode);
        }
    }

    /**
     * Returns selected machine state.
     */
    public MachineStateDto getSelectedMachineState() {
        return selectedMachineState;
    }

    /** {@inheritDoc} */
    @Override
    public void onMachineSelected(final MachineStateDto selectedMachineState) {
        this.selectedMachineState = selectedMachineState;

        isMachineRunning = true;

        if (cachedMachines.containsKey(selectedMachineState)) {
            appliance.showAppliance(cachedMachines.get(selectedMachineState));

            return;
        }

        service.getMachine(selectedMachineState.getId()).then(new Operation<MachineDto>() {
            @Override
            public void apply(MachineDto machineDto) throws OperationException {
                Machine machine = entityFactory.createMachine(machineDto);

                cachedMachines.put(selectedMachineState, machine);

                appliance.showAppliance(machine);
            }
        }).catchError(new Operation<PromiseError>() {
            @Override
            public void apply(PromiseError error) throws OperationException {
                isMachineRunning = false;

                appliance.showStub(locale.unavailableMachineStarting(selectedMachineState.getName()));
            }
        });
    }

    /** {@inheritDoc} */
    @NotNull
    @Override
    public String getTitle() {
        return locale.machinePanelTitle();
    }
<commit_msg>IDEX-3632. Don't duplicate message about starting machine for dev macnine<commit_after>/*******************************************************************************
 * Copyright (c) 2012-2015 <NAME>.
 * 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:
 *   <NAME>. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.extension.machine.client.perspective.widgets.machine.panel;

import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.api.machine.gwt.client.MachineServiceClient;
import org.eclipse.che.api.machine.shared.dto.MachineDto;
import org.eclipse.che.api.machine.shared.dto.MachineStateDto;
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.promises.client.PromiseError;
import org.eclipse.che.api.workspace.shared.dto.UsersWorkspaceDto;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.parts.base.BasePresenter;
import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant;
import org.eclipse.che.ide.extension.machine.client.MachineResources;
import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory;
import org.eclipse.che.ide.extension.machine.client.machine.Machine;
import org.eclipse.che.api.machine.gwt.client.events.MachineStartingEvent;
import org.eclipse.che.api.machine.gwt.client.events.MachineStartingHandler;
import org.eclipse.che.ide.extension.machine.client.machine.events.MachineStateEvent;
import org.eclipse.che.ide.extension.machine.client.machine.events.MachineStateHandler;
import org.eclipse.che.ide.extension.machine.client.perspective.widgets.machine.appliance.MachineAppliancePresenter;
import org.eclipse.che.ide.workspace.start.StartWorkspaceEvent;
import org.eclipse.che.ide.workspace.start.StartWorkspaceHandler;
import org.eclipse.che.ide.workspace.start.StopWorkspaceEvent;
import org.eclipse.che.ide.workspace.start.StopWorkspaceHandler;
import org.vectomatic.dom.svg.ui.SVGResource;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The class contains business logic to control displaying of machines on special view.
 *
 * @author <NAME>
 * @author <NAME>
 */
@Singleton
public class MachinePanelPresenter extends BasePresenter implements MachinePanelView.ActionDelegate,
                                                                    MachineStateHandler,
                                                                    StartWorkspaceHandler,
                                                                    StopWorkspaceHandler,
                                                                    MachineStartingHandler {
    private final MachinePanelView                      view;
    private final MachineServiceClient                  service;
    private final EntityFactory                         entityFactory;
    private final MachineLocalizationConstant           locale;
    private final MachineAppliancePresenter             appliance;
    private final MachineResources                      resources;
    private final Map<MachineStateDto, MachineTreeNode> existingMachineNodes;
    private final Map<MachineStateDto, Machine>         cachedMachines;
    private final MachineTreeNode                       rootNode;
    private final List<MachineTreeNode>                 machineNodes;
    private final AppContext                            appContext;
    private       MachineStateDto                       selectedMachineState;
    private       boolean                               isMachineRunning;

    @Inject
    public MachinePanelPresenter(MachinePanelView view,
                                 MachineServiceClient service,
                                 EntityFactory entityFactory,
                                 MachineLocalizationConstant locale,
                                 MachineAppliancePresenter appliance,
                                 EventBus eventBus,
                                 MachineResources resources,
                                 AppContext appContext) {
        this.view = view;
        this.view.setDelegate(this);

        this.service = service;
        this.entityFactory = entityFactory;
        this.locale = locale;
        this.appliance = appliance;
        this.resources = resources;
        this.appContext = appContext;

        this.machineNodes = new ArrayList<>();
        this.rootNode = entityFactory.createMachineNode(null, "root", machineNodes);

        this.existingMachineNodes = new HashMap<>();
        this.cachedMachines = new HashMap<>();

        eventBus.addHandler(MachineStateEvent.TYPE, this);
        eventBus.addHandler(StartWorkspaceEvent.TYPE, this);
        eventBus.addHandler(StopWorkspaceEvent.TYPE, this);
        eventBus.addHandler(MachineStartingEvent.TYPE, this);
    }

    /** Gets all machines and adds them to special place on view. */
    public Promise<List<MachineStateDto>> showMachines() {
        return showMachines(appContext.getWorkspace().getId());
    }

    private Promise<List<MachineStateDto>> showMachines(String workspaceId) {
        Promise<List<MachineStateDto>> machinesPromise = service.getMachinesStates(workspaceId);

        return machinesPromise.then(new Operation<List<MachineStateDto>>() {
            @Override
            public void apply(List<MachineStateDto> machineStates) throws OperationException {
                machineNodes.clear();
                if (machineStates.isEmpty()) {
                    appliance.showStub(locale.unavailableMachineInfo());

                    return;
                }

                for (MachineStateDto machineState : machineStates) {
                    addNodeToTree(machineState);
                }

                view.setData(rootNode);

                selectFirstNode();
            }
        });
    }

    private void addNodeToTree(MachineStateDto machineState) {
        MachineTreeNode machineNode = entityFactory.createMachineNode(rootNode, machineState, null);

        existingMachineNodes.put(machineState, machineNode);

        if (!machineNodes.contains(machineNode)) {
            machineNodes.add(machineNode);
        }
    }

    private void selectFirstNode() {
        if (!machineNodes.isEmpty()) {
            MachineTreeNode firstNode = machineNodes.get(0);

            view.selectNode(firstNode);
        }
    }

    /**
     * Returns selected machine state.
     */
    public MachineStateDto getSelectedMachineState() {
        return selectedMachineState;
    }

    /** {@inheritDoc} */
    @Override
    public void onMachineSelected(final MachineStateDto selectedMachineState) {
        this.selectedMachineState = selectedMachineState;

        isMachineRunning = true;

        if (cachedMachines.containsKey(selectedMachineState)) {
            appliance.showAppliance(cachedMachines.get(selectedMachineState));

            return;
        }

        service.getMachine(selectedMachineState.getId()).then(new Operation<MachineDto>() {
            @Override
            public void apply(MachineDto machineDto) throws OperationException {
                Machine machine = entityFactory.createMachine(machineDto);

                cachedMachines.put(selectedMachineState, machine);

                appliance.showAppliance(machine);
            }
        }).catchError(new Operation<PromiseError>() {
            @Override
            public void apply(PromiseError error) throws OperationException {
                isMachineRunning = false;

                // we show the loader for dev machine so this message isn't necessary for dev machine
                if (!selectedMachineState.isDev()) {
                    appliance.showStub(locale.unavailableMachineStarting(selectedMachineState.getName()));
                }
            }
        });
    }

    /** {@inheritDoc} */
    @NotNull
    @Override
    public String getTitle() {
        return locale.machinePanelTitle();
    }

{'fraction_non_alphanumeric': 0.10068011393361623, 'fraction_numerical': 0.0017438818810672557, 'mean_word_length': 2.496037390774233, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 31, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4676217', 'n_tokens_mistral': 4158, 'n_tokens_neox': 4039, 'n_words': 780}