<commit_before>package org.jetbrains.plugins.scala.lang.refactoring.introduceVariable;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.ui.EditorComboBoxEditor;
import com.intellij.ui.EditorComboBoxRenderer;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.StringComboboxEditor;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.scala.ScalaBundle;
import org.jetbrains.plugins.scala.ScalaFileType;
import org.jetbrains.plugins.scala.lang.psi.api.base.types.ScTypeElement;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScClass;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTrait;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTypeDefinition;
import org.jetbrains.plugins.scala.lang.refactoring.introduceVariable.ScopeItem;
import org.jetbrains.plugins.scala.lang.refactoring.util.*;
import scala.Tuple2;

import javax.swing.*;
import javax.swing.event.EventListenerList;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import java.util.HashMap;
import java.util.Map;


public class ScalaIntroduceTypeAliasDialog extends DialogWrapper implements NamedDialog {
    private JPanel contentPane;
    private JLabel myTypeLabel;
    private ComboBox myNameComboBox;
    private JLabel myScopeLabel;
    private JComboBox myScopeCombobox;
    private JLabel myNameLabel;
    private JCheckBox myCbReplaceAllOccurences;
    private JTextField myTypeTextField;
    private JCheckBox myReplaceCompanionObjectOcc;
    private JCheckBox myReplaceInInheritors;
    private JButton buttonOK;
    private JButton buttonCancel;

    private Project project;
    private ScTypeElement myTypeElement;
    private int occurrencesCount;
    private int companionObjOccCount;
    private ScalaValidator validator;
    private String[] possibleNames;
    private EventListenerList myListenerList = new EventListenerList();
    private ConflictsReporter conflictsReporter;
    private Editor editor;
    private ScopeItem currentScope;
    private Map<ScopeItem, Tuple2<ScTypeElement[], ScalaTypeValidator[]>> inheritanceDataMap;

    private static final String REFACTORING_NAME = ScalaBundle.message("introduce.type.alias.title");

    public ScalaIntroduceTypeAliasDialog(Project project,
                                         ScTypeElement myTypeElement,
                                         ScopeItem[] possibleScopes,
                                         ScopeItem mainScope,
                                         ConflictsReporter conflictReporter,
                                         Editor editor) {
        super(project, true);
        this.project = project;
        this.myTypeElement = myTypeElement;
        this.currentScope = possibleScopes[0];
        this.occurrencesCount = currentScope.usualOccurrences().length;
        this.companionObjOccCount = currentScope.occurrencesInCompanion().length;
        this.validator = currentScope.typeValidator();
        this.possibleNames = currentScope.availableNames();
        this.conflictsReporter = conflictReporter;
        this.editor = editor;
        this.inheritanceDataMap = new HashMap<ScopeItem, Tuple2<ScTypeElement[], ScalaTypeValidator[]>>();


        setUpNameComboBox(possibleNames);
        setUpScopeComboBox(possibleScopes, mainScope);

        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        setTitle(REFACTORING_NAME);
        init();
        setUpDialog();
        updateOkStatus();
    }

    protected void doOKAction() {
        ScTypeDefinition classOrTrait = PsiTreeUtil.getParentOfType(currentScope.fileEncloser(), ScClass.class, ScTrait.class);
        if (classOrTrait != null) {
            if (!handleInheritedClasses(classOrTrait)) {
                return;
            }
        }

        if (!validator.isOK(this)) {
            return;
        }

        super.doOKAction();
    }

    private void setUpNameComboBox(String[] possibleNames) {

        final EditorComboBoxEditor comboEditor = new StringComboboxEditor(project, ScalaFileType.SCALA_FILE_TYPE, myNameComboBox);

        myNameComboBox.setEditor(comboEditor);
        myNameComboBox.setRenderer(new EditorComboBoxRenderer(comboEditor));

        myNameComboBox.setEditable(true);
        myNameComboBox.setMaximumRowCount(8);
        myListenerList.add(DataChangedListener.class, new DataChangedListener());

        myNameComboBox.addItemListener(
                new ItemListener() {
                    public void itemStateChanged(ItemEvent e) {
                        fireNameDataChanged();
                    }
                }
        );

        ((EditorTextField) myNameComboBox.getEditor().getEditorComponent()).addDocumentListener(new DocumentListener() {
                                                                                                    public void beforeDocumentChange(DocumentEvent event) {
                                                                                                    }

                                                                                                    public void documentChanged(DocumentEvent event) {
                                                                                                        fireNameDataChanged();
                                                                                                    }
                                                                                                }
        );

        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myNameComboBox.requestFocus();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.ALT_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);

        for (String possibleName : possibleNames) {
            myNameComboBox.addItem(possibleName);<commit_msg>focus at name in introduce type alias dialog<commit_after>package org.jetbrains.plugins.scala.lang.refactoring.introduceVariable;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.ui.EditorComboBoxEditor;
import com.intellij.ui.EditorComboBoxRenderer;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.StringComboboxEditor;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.scala.ScalaBundle;
import org.jetbrains.plugins.scala.ScalaFileType;
import org.jetbrains.plugins.scala.lang.psi.api.base.types.ScTypeElement;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScClass;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTrait;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTypeDefinition;
import org.jetbrains.plugins.scala.lang.refactoring.util.*;
import scala.Tuple2;

import javax.swing.*;
import javax.swing.event.EventListenerList;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import java.util.HashMap;
import java.util.Map;


public class ScalaIntroduceTypeAliasDialog extends DialogWrapper implements NamedDialog {
    private JPanel contentPane;
    private JLabel myTypeLabel;
    private ComboBox myNameComboBox;
    private JLabel myScopeLabel;
    private JComboBox myScopeCombobox;
    private JLabel myNameLabel;
    private JCheckBox myCbReplaceAllOccurences;
    private JTextField myTypeTextField;
    private JCheckBox myReplaceCompanionObjectOcc;
    private JCheckBox myReplaceInInheritors;
    private JButton buttonOK;
    private JButton buttonCancel;

    private Project project;
    private ScTypeElement myTypeElement;
    private int occurrencesCount;
    private int companionObjOccCount;
    private ScalaValidator validator;
    private String[] possibleNames;
    private EventListenerList myListenerList = new EventListenerList();
    private ConflictsReporter conflictsReporter;
    private Editor editor;
    private ScopeItem currentScope;
    private Map<ScopeItem, Tuple2<ScTypeElement[], ScalaTypeValidator[]>> inheritanceDataMap;

    private static final String REFACTORING_NAME = ScalaBundle.message("introduce.type.alias.title");

    public ScalaIntroduceTypeAliasDialog(Project project,
                                         ScTypeElement myTypeElement,
                                         ScopeItem[] possibleScopes,
                                         ScopeItem mainScope,
                                         ConflictsReporter conflictReporter,
                                         Editor editor) {
        super(project, true);
        this.project = project;
        this.myTypeElement = myTypeElement;
        this.currentScope = possibleScopes[0];
        this.occurrencesCount = currentScope.usualOccurrences().length;
        this.companionObjOccCount = currentScope.occurrencesInCompanion().length;
        this.validator = currentScope.typeValidator();
        this.possibleNames = currentScope.availableNames();
        this.conflictsReporter = conflictReporter;
        this.editor = editor;
        this.inheritanceDataMap = new HashMap<ScopeItem, Tuple2<ScTypeElement[], ScalaTypeValidator[]>>();


        setUpNameComboBox(possibleNames);
        setUpScopeComboBox(possibleScopes, mainScope);

        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        setTitle(REFACTORING_NAME);
        init();
        setUpDialog();
        updateOkStatus();
    }

    protected void doOKAction() {
        ScTypeDefinition classOrTrait = PsiTreeUtil.getParentOfType(currentScope.fileEncloser(), ScClass.class, ScTrait.class);
        if (classOrTrait != null) {
            if (!handleInheritedClasses(classOrTrait)) {
                return;
            }
        }

        if (!validator.isOK(this)) {
            return;
        }

        super.doOKAction();
    }

    public JComponent getPreferredFocusedComponent() {
        return myNameComboBox;
    }

    private void setUpNameComboBox(String[] possibleNames) {

        final EditorComboBoxEditor comboEditor = new StringComboboxEditor(project, ScalaFileType.SCALA_FILE_TYPE, myNameComboBox);

        myNameComboBox.setEditor(comboEditor);
        myNameComboBox.setRenderer(new EditorComboBoxRenderer(comboEditor));

        myNameComboBox.setEditable(true);
        myNameComboBox.setMaximumRowCount(8);
        myListenerList.add(DataChangedListener.class, new DataChangedListener());

        myNameComboBox.addItemListener(
                new ItemListener() {
                    public void itemStateChanged(ItemEvent e) {
                        fireNameDataChanged();
                    }
                }
        );

        ((EditorTextField) myNameComboBox.getEditor().getEditorComponent()).addDocumentListener(new DocumentListener() {
            public void beforeDocumentChange(DocumentEvent event) {
            }

            public void documentChanged(DocumentEvent event) {
                fireNameDataChanged();
            }
        }
        );

        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myNameComboBox.requestFocus();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.ALT_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);

        for (String possibleName : possibleNames) {
            myNameComboBox.addItem(possibleName);
{'fraction_non_alphanumeric': 0.07861507128309572, 'fraction_numerical': 0.000814663951120163, 'mean_word_length': 2.63302752293578, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3146543', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3105, 'n_words': 547}