@Nullable
 private ParameterInfo findParamByOldName(String name) {
   for (ParameterInfo info : myInfo.getParameterInfos()) {
     if (name.equals(info.getOldName())) return info;
   }
   return null;
 }
  @NotNull
  public LinkedHashSet<String> suggestNames() {
    GrVariable var = GroovyIntroduceParameterUtil.findVar(myInfo);
    GrExpression expr = GroovyIntroduceParameterUtil.findExpr(myInfo);
    StringPartInfo stringPart = findStringPart();

    return GroovyIntroduceParameterUtil.suggestNames(
        var, expr, stringPart, myInfo.getToReplaceIn(), myProject);
  }
 @Nullable
 private PsiClass getContainingClass() {
   final GrParametersOwner toReplaceIn = myInfo.getToReplaceIn();
   if (toReplaceIn instanceof GrMethod) {
     return ((GrMethod) toReplaceIn).getContainingClass();
   } else {
     return PsiTreeUtil.getContextOfType(toReplaceIn, PsiClass.class);
   }
 }
  @Override
  public void doOKAction() {
    saveSettings();

    super.doOKAction();

    final GrParametersOwner toReplaceIn = myInfo.getToReplaceIn();

    final GrExpression expr = GroovyIntroduceParameterUtil.findExpr(myInfo);
    final GrVariable var = GroovyIntroduceParameterUtil.findVar(myInfo);
    final StringPartInfo stringPart = findStringPart();

    if (myTypeComboBox.isClosureSelected() || expr == null && var == null && stringPart == null) {
      GrIntroduceParameterSettings settings =
          new ExtractClosureHelperImpl(
              myInfo,
              getEnteredName(),
              myDeclareFinalCheckBox.isSelected(),
              getParametersToRemove(),
              myDelegateViaOverloadingMethodCheckBox.isSelected(),
              getReplaceFieldsWithGetter(),
              myForceReturnCheckBox.isSelected(),
              false,
              myTypeComboBox.getSelectedType() == null);
      if (toReplaceIn instanceof GrMethod) {
        invokeRefactoring(new ExtractClosureFromMethodProcessor(settings));
      } else {
        invokeRefactoring(new ExtractClosureFromClosureProcessor(settings));
      }
    } else {

      GrIntroduceParameterSettings settings =
          new GrIntroduceExpressionSettingsImpl(
              myInfo,
              getEnteredName(),
              myDeclareFinalCheckBox.isSelected(),
              getParametersToRemove(),
              myDelegateViaOverloadingMethodCheckBox.isSelected(),
              getReplaceFieldsWithGetter(),
              expr,
              var,
              myTypeComboBox.getSelectedType(),
              var != null,
              true,
              myForceReturnCheckBox.isSelected());
      if (toReplaceIn instanceof GrMethod) {
        invokeRefactoring(new GrIntroduceParameterProcessor(settings));
      } else {
        invokeRefactoring(new GrIntroduceClosureParameterProcessor(settings));
      }
    }
  }
  public GrIntroduceParameterDialog(IntroduceParameterInfo info) {
    super(info.getProject(), true);
    myInfo = info;
    myProject = info.getProject();
    myCanIntroduceSimpleParameter =
        GroovyIntroduceParameterUtil.findExpr(myInfo) != null
            || GroovyIntroduceParameterUtil.findVar(myInfo) != null
            || findStringPart() != null;

    TObjectIntHashMap<GrParameter> parametersToRemove =
        GroovyIntroduceParameterUtil.findParametersToRemove(info);
    toRemoveCBs = new TObjectIntHashMap<JCheckBox>(parametersToRemove.size());
    for (Object p : parametersToRemove.keys()) {
      JCheckBox cb =
          new JCheckBox(
              GroovyRefactoringBundle.message(
                  "remove.parameter.0.no.longer.used", ((GrParameter) p).getName()));
      toRemoveCBs.put(cb, parametersToRemove.get((GrParameter) p));
      cb.setSelected(true);
    }

    init();
  }
 private void initReplaceFieldsWithGetters(JavaRefactoringSettings settings) {
   final PsiField[] usedFields =
       GroovyIntroduceParameterUtil.findUsedFieldsWithGetters(
           myInfo.getStatements(), getContainingClass());
   myGetterPanel.setVisible(usedFields.length > 0);
   switch (settings.INTRODUCE_PARAMETER_REPLACE_FIELDS_WITH_GETTERS) {
     case IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL:
       myReplaceAllFieldsRadioButton.setSelected(true);
       break;
     case IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE:
       myReplaceFieldsInaccessibleInRadioButton.setSelected(true);
       break;
     case IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE:
       myDoNotReplaceRadioButton.setSelected(true);
       break;
   }
 }
  private GrTypeComboBox createTypeComboBox(
      GrVariable var, GrExpression expr, StringPartInfo stringPartInfo) {
    GrTypeComboBox box;
    if (var != null) {
      box = GrTypeComboBox.createTypeComboBoxWithDefType(var.getDeclaredType(), var);
    } else if (expr != null) {
      box = GrTypeComboBox.createTypeComboBoxFromExpression(expr);
    } else if (stringPartInfo != null) {
      box = GrTypeComboBox.createTypeComboBoxFromExpression(stringPartInfo.getLiteral());
    } else {
      box = GrTypeComboBox.createEmptyTypeComboBox();
    }

    box.addClosureTypesFrom(inferClosureReturnType(), myInfo.getContext());
    if (expr == null && var == null && stringPartInfo == null) {
      box.setSelectedIndex(box.getItemCount() - 1);
    }
    return box;
  }
 private StringPartInfo findStringPart() {
   return myInfo.getStringPartInfo();
 }
  @Override
  protected void init() {
    super.init();

    JavaRefactoringSettings settings = JavaRefactoringSettings.getInstance();

    initReplaceFieldsWithGetters(settings);

    myDeclareFinalCheckBox.setSelected(hasFinalModifier());
    myDelegateViaOverloadingMethodCheckBox.setVisible(myInfo.getToSearchFor() != null);

    setTitle(RefactoringBundle.message("introduce.parameter.title"));

    myTable.init(myInfo);

    final GrParameter[] parameters = myInfo.getToReplaceIn().getParameters();
    toRemoveCBs.forEachEntry(
        new TObjectIntProcedure<JCheckBox>() {
          @Override
          public boolean execute(JCheckBox checkbox, int index) {
            checkbox.setSelected(true);

            final GrParameter param = parameters[index];
            final ParameterInfo pinfo = findParamByOldName(param.getName());
            if (pinfo != null) {
              pinfo.setPassAsParameter(false);
            }
            return true;
          }
        });

    updateSignature();

    if (myCanIntroduceSimpleParameter) {
      mySignaturePanel.setVisible(false);

      // action to hide signature panel if we have variants to introduce simple parameter
      myTypeComboBox.addItemListener(
          new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
              mySignaturePanel.setVisible(myTypeComboBox.isClosureSelected());
              pack();
            }
          });
    }

    final PsiType closureReturnType = inferClosureReturnType();
    if (PsiType.VOID.equals(closureReturnType)) {
      myForceReturnCheckBox.setEnabled(false);
      myForceReturnCheckBox.setSelected(false);
    } else {
      myForceReturnCheckBox.setSelected(isForceReturn());
    }

    if (myInfo.getToReplaceIn() instanceof GrClosableBlock) {
      myDelegateViaOverloadingMethodCheckBox.setEnabled(false);
      myDelegateViaOverloadingMethodCheckBox.setToolTipText(
          "Delegating is not allowed in closure context");
    }

    pack();
  }