<commit_before>/*******************************************************************************
 * Copyright (c) 2012-2016 <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;

import com.google.gwt.core.client.Scheduler;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler;
import org.eclipse.che.ide.actions.StopWorkspaceAction;
import org.eclipse.che.ide.api.action.ActionManager;
import org.eclipse.che.ide.api.action.DefaultActionGroup;
import org.eclipse.che.ide.api.action.IdeActions;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.constraints.Constraints;
import org.eclipse.che.ide.api.extension.Extension;
import org.eclipse.che.ide.api.icon.Icon;
import org.eclipse.che.ide.api.icon.IconRegistry;
import org.eclipse.che.ide.api.keybinding.KeyBindingAgent;
import org.eclipse.che.ide.api.keybinding.KeyBuilder;
import org.eclipse.che.ide.api.parts.PartStackType;
import org.eclipse.che.ide.api.parts.PerspectiveManager;
import org.eclipse.che.ide.api.parts.WorkspaceAgent;
import org.eclipse.che.ide.extension.machine.client.actions.CreateMachineAction;
import org.eclipse.che.ide.extension.machine.client.actions.CreateSnapshotAction;
import org.eclipse.che.ide.extension.machine.client.actions.DestroyMachineAction;
import org.eclipse.che.ide.extension.machine.client.actions.EditCommandsAction;
import org.eclipse.che.ide.extension.machine.client.actions.ExecuteSelectedCommandAction;
import org.eclipse.che.ide.extension.machine.client.actions.RestartMachineAction;
import org.eclipse.che.ide.extension.machine.client.actions.RunCommandAction;
import org.eclipse.che.ide.extension.machine.client.actions.SelectCommandComboBox;
import org.eclipse.che.ide.extension.machine.client.actions.SwitchPerspectiveAction;
import org.eclipse.che.ide.extension.machine.client.command.custom.CustomCommandType;
import org.eclipse.che.ide.extension.machine.client.command.valueproviders.ServerPortProvider;
import org.eclipse.che.ide.extension.machine.client.perspective.OperationsPerspective;
import org.eclipse.che.ide.extension.machine.client.processes.ConsolesPanelPresenter;
import org.eclipse.che.ide.extension.machine.client.actions.NewTerminalAction;
import org.eclipse.che.ide.extension.machine.client.processes.actions.CloseConsoleAction;
import org.eclipse.che.ide.extension.machine.client.processes.actions.ReRunProcessAction;
import org.eclipse.che.ide.extension.machine.client.processes.actions.StopProcessAction;
import org.eclipse.che.ide.extension.machine.client.targets.EditTargetsAction;
import org.eclipse.che.ide.util.input.KeyCodeMap;

import static org.eclipse.che.ide.api.action.IdeActions.GROUP_CENTER_TOOLBAR;
import static org.eclipse.che.ide.api.action.IdeActions.GROUP_MAIN_MENU;
import static org.eclipse.che.ide.api.action.IdeActions.GROUP_RIGHT_TOOLBAR;
import static org.eclipse.che.ide.api.action.IdeActions.GROUP_RUN;
import static org.eclipse.che.ide.api.action.IdeActions.GROUP_WORKSPACE;
import static org.eclipse.che.ide.api.constraints.Anchor.AFTER;
import static org.eclipse.che.ide.api.constraints.Constraints.FIRST;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;

/**
 * Machine extension entry point.
 *
 * @author <NAME>
 * @author <NAME>
 */
@Singleton
@Extension(title = "Machine", version = "1.0.0")
public class MachineExtension {

    public static final String GROUP_MACHINE_CONSOLE_TOOLBAR = "MachineConsoleToolbar";
    public static final String GROUP_MACHINE_TOOLBAR         = "MachineGroupToolbar";
    public static final String GROUP_COMMANDS_DROPDOWN       = "CommandsSelector";
    public static final String GROUP_COMMANDS_LIST           = "CommandsListGroup";
    public static final String GROUP_MACHINES_DROPDOWN       = "MachinesSelector";
    public static final String GROUP_MACHINES_LIST           = "MachinesListGroup";

    @Inject
    public MachineExtension(MachineResources machineResources,
                            final EventBus eventBus,
                            final WorkspaceAgent workspaceAgent,
                            final AppContext   appContext,
                            final ConsolesPanelPresenter consolesPanelPresenter,
                            final Provider<ServerPortProvider> machinePortProvider,
                            final PerspectiveManager perspectiveManager,
                            IconRegistry iconRegistry,
                            CustomCommandType arbitraryCommandType) {
        machineResources.getCss().ensureInjected();

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

                /**
                 * There is a bug in perspective management and it's unable to add Consoles part in
                 * OperationsPerspective and ProjectPerspective directly. Following code resolves the issue.
                 */

                if (appContext.getFactory() == null) {
                    consolesPanelPresenter.newTerminal();
                }
            }

            @Override
            public void onWsAgentStopped(WsAgentStateEvent event) {
            }
        });

        Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
            @Override
            public void execute() {
                /* Add Consoles to Operation perspective */
                perspectiveManager.setPerspectiveId(OperationsPerspective.OPERATIONS_PERSPECTIVE_ID);
                workspaceAgent.openPart(consolesPanelPresenter, PartStackType.INFORMATION);

                /* Add Consoles to Project perspective */
                perspectiveManager.setPerspectiveId(PROJECT_PERSPECTIVE_ID);
                workspaceAgent.openPart(consolesPanelPresenter, PartStackType.INFORMATION);

                workspaceAgent.setActivePart(consolesPanelPresenter);
            }
        });

        iconRegistry.registerIcon(new Icon(arbitraryCommandType.getId() + ".commands.category.icon", machineResources.customCommandType()));
    }

    @Inject
    private void prepareActions(MachineLocalizationConstant localizationConstant,
                                ActionManager actionManager,
                                KeyBindingAgent keyBinding,
                                ExecuteSelectedCommandAction executeSelectedCommandAction,
                                SelectCommandComboBox selectCommandAction,
                                EditCommandsAction editCommandsAction,
                                CreateMachineAction createMachine,
                                RestartMachineAction restartMachine,
                                DestroyMachineAction destroyMachineAction,
                                StopWorkspaceAction stopWorkspaceAction,
                                SwitchPerspectiveAction switchPerspectiveAction,
                                CreateSnapshotAction createSnapshotAction,
                                RunCommandAction runCommandAction,
                                NewTerminalAction newTerminalAction,
                                EditTargetsAction editTargetsAction,
                                IconRegistry iconRegistry,
                                MachineResources machineResources,
                                ReRunProcessAction reRunProcessAction,
                                StopProcessAction stopProcessAction,<commit_msg>CODENVY-703; do not show terminal by default in factories (#1586)<commit_after>/*******************************************************************************
 * Copyright (c) 2012-2016 <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;

import com.google.gwt.core.client.Scheduler;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler;
import org.eclipse.che.ide.actions.StopWorkspaceAction;
import org.eclipse.che.ide.api.action.ActionManager;
import org.eclipse.che.ide.api.action.DefaultActionGroup;
import org.eclipse.che.ide.api.action.IdeActions;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.constraints.Constraints;
import org.eclipse.che.ide.api.extension.Extension;
import org.eclipse.che.ide.api.icon.Icon;
import org.eclipse.che.ide.api.icon.IconRegistry;
import org.eclipse.che.ide.api.keybinding.KeyBindingAgent;
import org.eclipse.che.ide.api.keybinding.KeyBuilder;
import org.eclipse.che.ide.api.parts.PartStackType;
import org.eclipse.che.ide.api.parts.PerspectiveManager;
import org.eclipse.che.ide.api.parts.WorkspaceAgent;
import org.eclipse.che.ide.extension.machine.client.actions.CreateMachineAction;
import org.eclipse.che.ide.extension.machine.client.actions.CreateSnapshotAction;
import org.eclipse.che.ide.extension.machine.client.actions.DestroyMachineAction;
import org.eclipse.che.ide.extension.machine.client.actions.EditCommandsAction;
import org.eclipse.che.ide.extension.machine.client.actions.ExecuteSelectedCommandAction;
import org.eclipse.che.ide.extension.machine.client.actions.RestartMachineAction;
import org.eclipse.che.ide.extension.machine.client.actions.RunCommandAction;
import org.eclipse.che.ide.extension.machine.client.actions.SelectCommandComboBox;
import org.eclipse.che.ide.extension.machine.client.actions.SwitchPerspectiveAction;
import org.eclipse.che.ide.extension.machine.client.command.custom.CustomCommandType;
import org.eclipse.che.ide.extension.machine.client.command.valueproviders.ServerPortProvider;
import org.eclipse.che.ide.extension.machine.client.perspective.OperationsPerspective;
import org.eclipse.che.ide.extension.machine.client.processes.ConsolesPanelPresenter;
import org.eclipse.che.ide.extension.machine.client.actions.NewTerminalAction;
import org.eclipse.che.ide.extension.machine.client.processes.actions.CloseConsoleAction;
import org.eclipse.che.ide.extension.machine.client.processes.actions.ReRunProcessAction;
import org.eclipse.che.ide.extension.machine.client.processes.actions.StopProcessAction;
import org.eclipse.che.ide.extension.machine.client.targets.EditTargetsAction;
import org.eclipse.che.ide.util.input.KeyCodeMap;

import static org.eclipse.che.ide.api.action.IdeActions.GROUP_CENTER_TOOLBAR;
import static org.eclipse.che.ide.api.action.IdeActions.GROUP_MAIN_MENU;
import static org.eclipse.che.ide.api.action.IdeActions.GROUP_RIGHT_TOOLBAR;
import static org.eclipse.che.ide.api.action.IdeActions.GROUP_RUN;
import static org.eclipse.che.ide.api.action.IdeActions.GROUP_WORKSPACE;
import static org.eclipse.che.ide.api.constraints.Anchor.AFTER;
import static org.eclipse.che.ide.api.constraints.Constraints.FIRST;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;

/**
 * Machine extension entry point.
 *
 * @author <NAME>
 * @author <NAME>
 */
@Singleton
@Extension(title = "Machine", version = "1.0.0")
public class MachineExtension {

    public static final String GROUP_MACHINE_CONSOLE_TOOLBAR = "MachineConsoleToolbar";
    public static final String GROUP_MACHINE_TOOLBAR         = "MachineGroupToolbar";
    public static final String GROUP_COMMANDS_DROPDOWN       = "CommandsSelector";
    public static final String GROUP_COMMANDS_LIST           = "CommandsListGroup";
    public static final String GROUP_MACHINES_DROPDOWN       = "MachinesSelector";
    public static final String GROUP_MACHINES_LIST           = "MachinesListGroup";

    @Inject
    public MachineExtension(MachineResources machineResources,
                            final EventBus eventBus,
                            final WorkspaceAgent workspaceAgent,
                            final AppContext   appContext,
                            final ConsolesPanelPresenter consolesPanelPresenter,
                            final Provider<ServerPortProvider> machinePortProvider,
                            final PerspectiveManager perspectiveManager,
                            IconRegistry iconRegistry,
                            CustomCommandType arbitraryCommandType) {
        machineResources.getCss().ensureInjected();

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

            @Override
            public void onWsAgentStopped(WsAgentStateEvent event) {
            }
        });

        Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
            @Override
            public void execute() {
                /* There is a bug in perspective management and it's unable to add Consoles part in
                 * OperationsPerspective and ProjectPerspective directly. Following code resolves the issue.
                 */

                /* Add Consoles to Operation perspective */
                perspectiveManager.setPerspectiveId(OperationsPerspective.OPERATIONS_PERSPECTIVE_ID);
                workspaceAgent.openPart(consolesPanelPresenter, PartStackType.INFORMATION);

                /* Add Consoles to Project perspective */
                perspectiveManager.setPerspectiveId(PROJECT_PERSPECTIVE_ID);
                workspaceAgent.openPart(consolesPanelPresenter, PartStackType.INFORMATION);

                /* Do not show terminal on factories by default */
                if (appContext.getFactory() == null) {
                    consolesPanelPresenter.newTerminal();
                    workspaceAgent.setActivePart(consolesPanelPresenter);
                }
            }
        });

        iconRegistry.registerIcon(new Icon(arbitraryCommandType.getId() + ".commands.category.icon", machineResources.customCommandType()));
    }

    @Inject
    private void prepareActions(MachineLocalizationConstant localizationConstant,
                                ActionManager actionManager,
                                KeyBindingAgent keyBinding,
                                ExecuteSelectedCommandAction executeSelectedCommandAction,
                                SelectCommandComboBox selectCommandAction,
                                EditCommandsAction editCommandsAction,
                                CreateMachineAction createMachine,
                                RestartMachineAction restartMachine,
                                DestroyMachineAction destroyMachineAction,
                                StopWorkspaceAction stopWorkspaceAction,
                                SwitchPerspectiveAction switchPerspectiveAction,
                                CreateSnapshotAction createSnapshotAction,
                                RunCommandAction runCommandAction,
                                NewTerminalAction newTerminalAction,
                                EditTargetsAction editTargetsAction,
                                IconRegistry iconRegistry,
                                MachineResources machineResources,
                                ReRunProcessAction reRunProcessAction,
                                StopProcessAction stopProcessAction,
{'fraction_non_alphanumeric': 0.09684300341296928, 'fraction_numerical': 0.0022549975621647977, 'mean_word_length': 2.899477186311787, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '146985', 'n_tokens_mistral': 4157, 'n_tokens_neox': 3990, 'n_words': 688}