/**
   * Adds new terminal to the processes panel
   *
   * @param machineId id of machine in which the terminal will be added
   */
  @Override
  public void onAddTerminal(@NotNull final String machineId) {
    machineService
        .getMachine(machineId)
        .then(
            new Operation<MachineDto>() {
              @Override
              public void apply(MachineDto arg) throws OperationException {
                Machine machine = entityFactory.createMachine(arg);
                final ProcessTreeNode machineTreeNode = findProcessTreeNodeById(machineId);

                if (machineTreeNode == null) {
                  notificationManager.notify(
                      localizationConstant.failedToConnectTheTerminal(),
                      localizationConstant.machineNotFound(machineId),
                      FAIL,
                      FLOAT_MODE);
                  Log.error(getClass(), localizationConstant.machineNotFound(machineId));
                  return;
                }

                final TerminalPresenter newTerminal = terminalFactory.create(machine);
                final IsWidget terminalWidget = newTerminal.getView();
                final String terminalName = getUniqueTerminalName(machineTreeNode);
                final ProcessTreeNode terminalNode =
                    new ProcessTreeNode(
                        TERMINAL_NODE,
                        machineTreeNode,
                        terminalName,
                        resources.terminalTreeIcon(),
                        null);
                addChildToMachineNode(terminalNode, machineTreeNode);

                final String terminalId = terminalNode.getId();
                terminals.put(terminalId, newTerminal);
                view.addProcessNode(terminalNode);
                view.addProcessWidget(terminalId, terminalWidget);
                resfreshStopButtonState(terminalId);

                newTerminal.setVisible(true);
                newTerminal.connect();
                newTerminal.setListener(
                    new TerminalStateListener() {
                      @Override
                      public void onExit() {
                        onStopProcess(terminalNode);
                        terminals.remove(terminalId);
                      }
                    });
              }
            })
        .catchError(
            new Operation<PromiseError>() {
              @Override
              public void apply(PromiseError arg) throws OperationException {
                notificationManager.notify(localizationConstant.failedToFindMachine(machineId));
              }
            });
  }
  @Override
  public void onMachineRunning(MachineStateEvent event) {
    workspaceAgent.setActivePart(this);

    machineService
        .getMachine(event.getMachineId())
        .then(
            new Operation<MachineDto>() {
              @Override
              public void apply(MachineDto machine) throws OperationException {
                addMachineToConsoles(machine);
              }
            });
  }
  /** Get the list of all available machines. */
  public void fetchMachines() {
    String workspaceId = appContext.getWorkspaceId();

    machineService
        .getMachines(workspaceId)
        .then(
            new Operation<List<MachineDto>>() {
              @Override
              public void apply(List<MachineDto> machines) throws OperationException {
                rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, rootChildren);

                MachineDto devMachine = getDevMachine(machines);
                addMachineToConsoles(devMachine);

                machines.remove(devMachine);

                for (MachineDto machine : machines) {
                  addMachineToConsoles(machine);
                }
              }
            });
  }
  private void restoreState(final String machineId) {
    machineService
        .getProcesses(machineId)
        .then(
            new Operation<List<MachineProcessDto>>() {
              @Override
              public void apply(List<MachineProcessDto> arg) throws OperationException {
                for (MachineProcessDto machineProcessDto : arg) {
                  final CommandDto commandDto =
                      dtoFactory
                          .createDto(CommandDto.class)
                          .withName(machineProcessDto.getName())
                          .withCommandLine(machineProcessDto.getCommandLine())
                          .withType(machineProcessDto.getType());

                  final CommandType type =
                      commandTypeRegistry.getCommandTypeById(commandDto.getType());
                  if (type != null) {
                    final CommandConfiguration configuration =
                        type.getConfigurationFactory().createFromDto(commandDto);
                    final CommandOutputConsole console =
                        commandConsoleFactory.create(configuration, machineId);
                    console.listenToOutput(machineProcessDto.getOutputChannel());
                    console.attachToProcess(machineProcessDto);
                    addCommandOutput(machineId, console);
                  }
                }
              }
            })
        .catchError(
            new Operation<PromiseError>() {
              @Override
              public void apply(PromiseError arg) throws OperationException {
                notificationManager.notify(localizationConstant.failedToGetProcesses(machineId));
              }
            });
  }