package com.intellij.execution;

import com.intellij.execution.configurations.RunConfiguration;
import com.intellij.execution.configurations.RuntimeConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.DeferredIconImpl;
import com.intellij.ui.LayeredIcon;
import com.intellij.util.Function;

import javax.swing.*;

/**
 * @author spleaner
 */
public class ExecutionUtil {

  private static final Icon INVALID_CONFIGURATION = IconLoader.getIcon("/runConfigurations/invalidConfigurationLayer.png");

  private ExecutionUtil() {
  }

  public static Icon getConfigurationIcon(final Project project, final RunnerAndConfigurationSettings settings, final boolean invalid) {
    final RunManager runManager = RunManager.getInstance(project);
    RunConfiguration configuration = settings.getConfiguration();
    final Icon icon = settings.getFactory().getIcon(configuration);
    final Icon configurationIcon = runManager.isTemporary(configuration) ? IconLoader.getTransparentIcon(icon, 0.3f) : icon;
    if (invalid) {
      return LayeredIcon.create(configurationIcon, INVALID_CONFIGURATION);
    }

    return configurationIcon;
  }

  public static Icon getConfigurationIcon(final Project project, final RunnerAndConfigurationSettings settings) {
    final Icon baseIcon = getConfigurationIcon(project, settings, false);
    return new DeferredIconImpl<RunnerAndConfigurationSettings>(baseIcon, settings, new Function<RunnerAndConfigurationSettings, Icon>() {
      public Icon fun(RunnerAndConfigurationSettings runnerAndConfigurationSettings) {
        try {
          settings.checkSettings();
          return baseIcon;
        }
        catch (RuntimeConfigurationException ex) {
          return getConfigurationIcon(project, settings, true);
        }
      }
    });
  }

  public static String shortenName(final String name, final int toBeAdded) {
    if (name == null) return "";
    final int symbols = Math.max(10, 20 - toBeAdded);
    if (name.length() < symbols) return name;
    else return name.substring(0, symbols) + "...";
  }

}

@@ -282,93 +282,17 @@
 .ui.
-DeferredIconImpl;%0Aimport com.intellij.ui.LayeredIcon;%0Aimport com.intellij.util.Functi
+LayeredIc
 on;%0A
@@ -1252,395 +1252,113 @@
     
-final Icon baseIcon = getConfigurationIcon(project, settings, false);%0A    return new DeferredIconImpl%3CRunnerAndConfigurationSettings%3E(baseIcon, settings, new Function%3CRunnerAndConfigurationSettings, Icon%3E() %7B%0A      public Icon fun(RunnerAndConfigurationSettings runnerAndConfigurationSettings) %7B%0A        try %7B%0A          settings.checkSettings();%0A          return baseIcon;%0A   
+try %7B%0A      settings.checkSettings();%0A      return getConfigurationIcon(project, settings, false);%0A
     
- 
 %7D%0A
-    
     
@@ -1406,20 +1406,16 @@
 %7B%0A      
-  
-  
 return g
@@ -1468,29 +1468,9 @@
     
-    %7D%0A      %7D%0A    %7D);
+%7D
 %0A  %7D

revert accidental checkin