com.intellij.plugins.haxe.ide.refactoring.introduce.HaxeIntroduceDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.intellij.plugins.haxe.ide.refactoring.introduce.HaxeIntroduceDialog.java

Source

/*
 * Copyright 2000-2013 JetBrains s.r.o.
 * Copyright 2014-2014 AS3Boyan
 * Copyright 2014-2014 Elias Ku
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.plugins.haxe.ide.refactoring.introduce;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.plugins.haxe.HaxeFileType;
import com.intellij.plugins.haxe.lang.psi.HaxeExpression;
import com.intellij.ui.EditorComboBoxEditor;
import com.intellij.ui.EditorComboBoxRenderer;
import com.intellij.ui.StringComboboxEditor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Collection;

/**
 * User: Fedor.Korotkov
 */
public class HaxeIntroduceDialog extends DialogWrapper {
    private JPanel myContentPane;
    private JLabel myNameLabel;
    private ComboBox myNameComboBox;
    private JCheckBox myReplaceAll;

    private final Project myProject;
    private final int myOccurrencesCount;
    private final HaxeExpression myExpression;

    public HaxeIntroduceDialog(@NotNull final Project project, @NotNull final String caption,
            final HaxeIntroduceOperation operation) {
        super(project, true);
        myOccurrencesCount = operation.getOccurrences().size();
        myProject = project;
        myExpression = operation.getInitializer();
        setUpNameComboBox(operation.getSuggestedNames());

        setTitle(caption);
        init();
        setupDialog();
    }

    private void setUpNameComboBox(Collection<String> possibleNames) {
        final EditorComboBoxEditor comboEditor = new StringComboboxEditor(myProject, HaxeFileType.HAXE_FILE_TYPE,
                myNameComboBox);

        myNameComboBox.setEditor(comboEditor);
        myNameComboBox.setRenderer(new EditorComboBoxRenderer(comboEditor));
        myNameComboBox.setEditable(true);
        myNameComboBox.setMaximumRowCount(8);

        myContentPane.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);
        }
    }

    private void setupDialog() {
        myReplaceAll.setMnemonic(KeyEvent.VK_A);
        myNameLabel.setLabelFor(myNameComboBox);

        // Replace occurrences check box setup
        if (myOccurrencesCount > 1) {
            myReplaceAll.setSelected(false);
            myReplaceAll.setEnabled(true);
            myReplaceAll.setText(myReplaceAll.getText() + " (" + myOccurrencesCount + " occurrences)");
        } else {
            myReplaceAll.setSelected(false);
            myReplaceAll.setEnabled(false);
        }
    }

    public JComponent getPreferredFocusedComponent() {
        return myNameComboBox;
    }

    protected JComponent createCenterPanel() {
        return myContentPane;
    }

    @Nullable
    public String getName() {
        final Object item = myNameComboBox.getEditor().getItem();
        if ((item instanceof String) && ((String) item).length() > 0) {
            return ((String) item).trim();
        }
        return null;
    }

    public Project getProject() {
        return myProject;
    }

    public HaxeExpression getExpression() {
        return myExpression;
    }

    public boolean doReplaceAllOccurrences() {
        return myReplaceAll.isSelected();
    }
}