Example usage for com.intellij.openapi.ui ComboBox setPopupVisible

List of usage examples for com.intellij.openapi.ui ComboBox setPopupVisible

Introduction

In this page you can find the example usage for com.intellij.openapi.ui ComboBox setPopupVisible.

Prototype

@Override
    public void setPopupVisible(boolean visible) 

Source Link

Usage

From source file:com.intellij.codeInsight.intention.impl.CreateFieldFromParameterDialog.java

License:Apache License

@Override
protected JComponent createNorthPanel() {
    if (myNames.length > 1) {
        final ComboBox combobox = new ComboBox(myNames, 200);
        myNameField = combobox;/* w ww.  j  av a2s  .c o m*/
        combobox.setEditable(true);
        combobox.setSelectedIndex(0);
        combobox.setMaximumRowCount(8);

        combobox.registerKeyboardAction(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (combobox.isPopupVisible()) {
                    combobox.setPopupVisible(false);
                } else {
                    doCancelAction();
                }
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        combobox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                updateOkStatus();
            }
        });
        combobox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                updateOkStatus();
            }

            @Override
            public void keyReleased(KeyEvent e) {
                updateOkStatus();
            }

            @Override
            public void keyTyped(KeyEvent e) {
                updateOkStatus();
            }
        });
    } else {
        JTextField field = new JTextField() {
            @Override
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                return new Dimension(200, size.height);
            }
        };
        myNameField = field;
        field.setText(myNames[0]);

        field.getDocument().addDocumentListener(new DocumentAdapter() {
            @Override
            protected void textChanged(DocumentEvent e) {
                updateOkStatus();
            }
        });
    }

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints gbConstraints = new GridBagConstraints();

    gbConstraints.insets = new Insets(4, 4, 4, 4);
    gbConstraints.anchor = GridBagConstraints.EAST;
    gbConstraints.fill = GridBagConstraints.BOTH;

    gbConstraints.gridwidth = 1;
    gbConstraints.weightx = 1;
    gbConstraints.weighty = 1;
    gbConstraints.gridx = 0;
    gbConstraints.gridy = 0;
    final JLabel typeLabel = new JLabel(
            CodeInsightBundle.message("dialog.create.field.from.parameter.field.type.label"));
    panel.add(typeLabel, gbConstraints);
    gbConstraints.gridx = 1;
    if (myTypes.length > 1) {
        myTypeSelector = new TypeSelector(myProject);
        myTypeSelector.setTypes(myTypes);
    } else {
        myTypeSelector = new TypeSelector(myTypes[0], myProject);
    }
    panel.add(myTypeSelector.getComponent(), gbConstraints);

    gbConstraints.gridwidth = 1;
    gbConstraints.weightx = 0;
    gbConstraints.weighty = 1;
    gbConstraints.gridx = 0;
    gbConstraints.gridy = 1;
    JLabel namePrompt = new JLabel(
            CodeInsightBundle.message("dialog.create.field.from.parameter.field.name.label"));
    panel.add(namePrompt, gbConstraints);

    gbConstraints.gridwidth = 1;
    gbConstraints.weightx = 1;
    gbConstraints.gridx = 1;
    gbConstraints.gridy = 1;
    panel.add(myNameField, gbConstraints);

    return panel;
}