/** {@inheritDoc} */
  @Override
  public Runner launchRunner() {
    CurrentProject currentProject = appContext.getCurrentProject();
    if (currentProject == null) {
      throw new IllegalStateException(
          "Can't launch runner for current project. Current project is absent...");
    }

    int ram = DEFAULT.getValue();

    RunnersDescriptor runnersDescriptor = currentProject.getProjectDescription().getRunners();
    String defaultRunner = runnersDescriptor.getDefault();

    if (!EnvironmentIdValidator.isValid(defaultRunner)) {
      defaultRunner = URL.encode(defaultRunner);
    }

    RunnerConfiguration defaultConfigs = runnersDescriptor.getConfigs().get(defaultRunner);

    if (defaultRunner != null && defaultConfigs != null) {
      ram = defaultConfigs.getRam();
    }

    RunOptions runOptions =
        dtoFactory
            .createDto(RunOptions.class)
            .withSkipBuild(Boolean.valueOf(currentProject.getAttributeValue("runner:skipBuild")))
            .withEnvironmentId(defaultRunner)
            .withMemorySize(ram);

    Environment environment = chooseRunnerAction.selectEnvironment();
    if (environment != null) {
      if (defaultRunner != null && defaultRunner.equals(environment.getId())) {
        Runner runner = modelsFactory.createRunner(runOptions);
        if (defaultRunner.startsWith(PROJECT_PREFIX)) {
          runner.setScope(PROJECT);
        }
        return launchRunner(runner);
      }
      runOptions =
          runOptions
              .withOptions(environment.getOptions())
              .withMemorySize(environment.getRam())
              .withEnvironmentId(environment.getId());
      Runner runner =
          modelsFactory.createRunner(runOptions, environment.getScope(), environment.getName());
      if (environment.getId().startsWith(PROJECT_PREFIX)) {
        runner.setScope(PROJECT);
      }
      return launchRunner(runner);
    }

    Runner runner = modelsFactory.createRunner(runOptions);
    if (defaultRunner != null && defaultRunner.startsWith(PROJECT_PREFIX)) {
      runner.setScope(PROJECT);
    }
    return launchRunner(modelsFactory.createRunner(runOptions));
  }
  /**
   * Adds already running runner.
   *
   * @param processDescriptor The descriptor of new runner
   * @return instance of new runner
   */
  @NotNull
  public Runner addRunner(@NotNull ApplicationProcessDescriptor processDescriptor) {
    RunOptions runOptions = dtoFactory.createDto(RunOptions.class);
    Runner runner = modelsFactory.createRunner(runOptions);

    String environmentId = processDescriptor.getEnvironmentId();

    if (environmentId != null && environmentId.startsWith(PROJECT_PREFIX)) {
      runner.setScope(PROJECT);
    }

    runnersId.add(processDescriptor.getProcessId());

    runner.setProcessDescriptor(processDescriptor);
    runner.setRAM(processDescriptor.getMemorySize());
    runner.setStatus(Runner.Status.DONE);
    runner.resetCreationTime();

    history.addRunner(runner);

    onSelectionChanged(RUNNER);

    runnerTimer.schedule(ONE_SEC.getValue());

    LaunchAction launchAction = actionFactory.createLaunch();
    runnerActions.put(runner, launchAction);

    launchAction.perform(runner);

    selectHistoryTab();

    return runner;
  }
  /** {@inheritDoc} */
  @Override
  public void onRunButtonClicked() {
    if (State.TEMPLATE.equals(panelState.getState())) {
      RunOptions runOptions =
          dtoFactory
              .createDto(RunOptions.class)
              .withOptions(selectedEnvironment.getOptions())
              .withEnvironmentId(selectedEnvironment.getId())
              .withMemorySize(selectedEnvironment.getRam());

      Runner runner =
          modelsFactory.createRunner(
              runOptions, selectedEnvironment.getScope(), selectedEnvironment.getName());

      if (PROJECT.equals(selectedEnvironment.getScope())) {
        runner.setScope(PROJECT);
      }

      launchRunner(runner);
    } else {
      launchRunner();
    }
  }