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

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

Introduction

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

Prototype

public ComboBox(@NotNull ComboBoxModel<E> model, int width) 

Source Link

Usage

From source file:com.intellij.codeInsight.hint.ImplementationViewComponent.java

License:Apache License

public ImplementationViewComponent(PsiElement[] elements, final int index) {
    super(new BorderLayout());

    final Project project = elements.length > 0 ? elements[0].getProject() : null;
    EditorFactory factory = EditorFactory.getInstance();
    Document doc = factory.createDocument("");
    doc.setReadOnly(true);//from  w  w  w  .j av  a 2s.c om
    myEditor = factory.createEditor(doc, project);
    ((EditorEx) myEditor).setBackgroundColor(EditorFragmentComponent.getBackgroundColor(myEditor));

    final EditorSettings settings = myEditor.getSettings();
    settings.setAdditionalLinesCount(1);
    settings.setAdditionalColumnsCount(1);
    settings.setLineMarkerAreaShown(false);
    settings.setIndentGuidesShown(false);
    settings.setLineNumbersShown(false);
    settings.setFoldingOutlineShown(false);

    myBinarySwitch = new CardLayout();
    myViewingPanel = new JPanel(myBinarySwitch);
    myEditor.setBorder(null);
    ((EditorEx) myEditor).getScrollPane().setViewportBorder(JBScrollPane.createIndentBorder());
    myViewingPanel.add(myEditor.getComponent(), TEXT_PAGE_KEY);

    myBinaryPanel = new JPanel(new BorderLayout());
    myViewingPanel.add(myBinaryPanel, BINARY_PAGE_KEY);

    add(myViewingPanel, BorderLayout.CENTER);

    myToolbar = createToolbar();
    myLocationLabel = new JLabel();
    myCountLabel = new JLabel();

    final JPanel header = new JPanel(new BorderLayout(2, 0));
    header.setBorder(BorderFactory.createCompoundBorder(IdeBorderFactory.createBorder(SideBorder.BOTTOM),
            IdeBorderFactory.createEmptyBorder(0, 0, 0, 5)));
    final JPanel toolbarPanel = new JPanel(new GridBagLayout());
    final GridBagConstraints gc = new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0, 0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0);
    toolbarPanel.add(myToolbar.getComponent(), gc);

    setPreferredSize(new Dimension(600, 400));

    update(elements, new PairFunction<PsiElement[], List<FileDescriptor>, Boolean>() {
        @Override
        public Boolean fun(final PsiElement[] psiElements, final List<FileDescriptor> fileDescriptors) {
            if (psiElements.length == 0)
                return false;
            myElements = psiElements;

            myIndex = index < myElements.length ? index : 0;
            PsiFile psiFile = getContainingFile(myElements[myIndex]);

            VirtualFile virtualFile = psiFile.getVirtualFile();
            EditorHighlighter highlighter;
            if (virtualFile != null)
                highlighter = HighlighterFactory.createHighlighter(project, virtualFile);
            else {
                String fileName = psiFile.getName(); // some artificial psi file, lets do best we can
                highlighter = HighlighterFactory.createHighlighter(project, fileName);
            }

            ((EditorEx) myEditor).setHighlighter(highlighter);

            gc.fill = GridBagConstraints.HORIZONTAL;
            gc.weightx = 1;
            myLabel = new JLabel();
            myFileChooser = new ComboBox(fileDescriptors.toArray(new FileDescriptor[fileDescriptors.size()]),
                    250);
            myFileChooser.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int index = myFileChooser.getSelectedIndex();
                    if (myIndex != index) {
                        myIndex = index;
                        updateControls();
                    }
                }
            });
            toolbarPanel.add(myFileChooser, gc);

            if (myElements.length > 1) {
                updateRenderer(project);
                myLabel.setVisible(false);
            } else {
                myFileChooser.setVisible(false);
                myCountLabel.setVisible(false);

                VirtualFile file = psiFile.getVirtualFile();
                if (file != null) {
                    myLabel.setIcon(getIconForFile(psiFile));
                    myLabel.setForeground(FileStatusManager.getInstance(project).getStatus(file).getColor());
                    myLabel.setText(file.getPresentableName());
                    myLabel.setBorder(new CompoundBorder(IdeBorderFactory.createRoundedBorder(),
                            IdeBorderFactory.createEmptyBorder(0, 0, 0, 5)));
                }
                toolbarPanel.add(myLabel, gc);
            }

            gc.fill = GridBagConstraints.NONE;
            gc.weightx = 0;
            toolbarPanel.add(myCountLabel, gc);

            header.add(toolbarPanel, BorderLayout.CENTER);
            header.add(myLocationLabel, BorderLayout.EAST);

            add(header, BorderLayout.NORTH);

            updateControls();
            return true;
        }
    });
}

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 w w.j  a v  a 2s  .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;
}

From source file:com.intellij.debugger.ui.DebuggerExpressionComboBox.java

License:Apache License

public DebuggerExpressionComboBox(Project project, PsiElement context, @NonNls String recentsId,
        final CodeFragmentFactory factory) {
    super(project, context, recentsId, factory);
    setLayout(new BorderLayout(0, 0));

    myComboBox = new ComboBox(new MyComboboxModel(getRecents()), 100);
    myComboBox.setSwingPopup(false);//from  w w  w.ja v  a  2  s . co m

    // Have to turn this off because when used in DebuggerTreeInplaceEditor, the combobox popup is hidden on every change of selection
    // See comment to SynthComboBoxUI.FocusHandler.focusLost()
    myComboBox.setLightWeightPopupEnabled(false);

    myEditor = new MyEditorComboBoxEditor(getProject(), getCurrentFactory().getFileType());
    //noinspection GtkPreferredJComboBoxRenderer
    myComboBox.setRenderer(new EditorComboBoxRenderer(myEditor));

    myComboBox.setEditable(true);
    myComboBox.setEditor(myEditor);
    add(addChooseFactoryLabel(myComboBox, false));
}

From source file:com.intellij.execution.ui.JrePathEditor.java

License:Apache License

/**
 * This constructor can be used in UI forms. <strong>Don't forget to call {@link #setDefaultJreSelector(DefaultJreSelector)}!</strong>
 *//*w w w  .j  a va2  s  .c  o  m*/
public JrePathEditor() {
    myLabel = new JBLabel(ExecutionBundle.message("run.configuration.jre.label"));

    myComboBoxModel = new SortedComboBoxModel<JreComboBoxItem>((o1, o2) -> {
        int result = Comparing.compare(o1.getOrder(), o2.getOrder());
        if (result != 0) {
            return result;
        }
        return o1.getPresentableText().compareToIgnoreCase(o2.getPresentableText());
    });
    myDefaultJreItem = new DefaultJreItem();
    myComboBoxModel.add(myDefaultJreItem);
    final Sdk[] allJDKs = ProjectJdkTable.getInstance().getAllJdks();
    for (Sdk sdk : allJDKs) {
        myComboBoxModel.add(new SdkAsJreItem(sdk));
    }

    final Set<String> jrePaths = new HashSet<String>();
    for (JreProvider provider : JreProvider.EP_NAME.getExtensions()) {
        String path = provider.getJrePath();
        if (!StringUtil.isEmpty(path)) {
            jrePaths.add(path);
            myComboBoxModel.add(new CustomJreItem(path));
        }
    }

    for (Sdk jdk : allJDKs) {
        String homePath = jdk.getHomePath();

        if (!SystemInfo.isMac) {
            final File jre = new File(jdk.getHomePath(), "jre");
            if (jre.isDirectory()) {
                homePath = jre.getPath();
            }
        }
        if (jrePaths.add(homePath)) {
            myComboBoxModel.add(new CustomJreItem(homePath));
        }
    }
    ComboBox comboBox = new ComboBox(myComboBoxModel, 100);
    comboBox.setEditable(true);
    comboBox.setRenderer(new ColoredListCellRendererWrapper<JreComboBoxItem>() {
        @Override
        protected void doCustomize(JList list, JreComboBoxItem value, int index, boolean selected,
                boolean hasFocus) {
            if (value != null) {
                value.render(this, selected);
            }
        }
    });
    myComboboxEditor = new JreComboboxEditor(myComboBoxModel);
    myComboboxEditor.getEditorComponent().setTextToTriggerEmptyTextStatus(DEFAULT_JRE_TEXT);
    comboBox.setEditor(myComboboxEditor);
    myPathField = new ComboboxWithBrowseButton(comboBox);
    myPathField.addBrowseFolderListener(ExecutionBundle.message("run.configuration.select.alternate.jre.label"),
            ExecutionBundle.message("run.configuration.select.jre.dir.label"), null,
            BrowseFilesListener.SINGLE_DIRECTORY_DESCRIPTOR, JreComboboxEditor.TEXT_COMPONENT_ACCESSOR);

    setLayout(new MigLayout("ins 0, gap 10, fill, flowx"));
    add(myLabel, "shrinkx");
    add(myPathField, "growx, pushx");

    InsertPathAction.addTo(myComboboxEditor.getEditorComponent());

    setAnchor(myLabel);

    updateUI();
}

From source file:com.intellij.refactoring.makeStatic.MakeParameterizedStaticDialog.java

License:Apache License

private JComboBox createComboBoxForName() {
    final ComboBox combobox = new ComboBox(myNameSuggestions, -1);

    combobox.setEditable(true);/*from   w w  w.  j a  va2 s .  c  o m*/
    combobox.setSelectedIndex(0);
    combobox.setMaximumRowCount(8);

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

        public void keyReleased(KeyEvent e) {
            updateControls();
        }

        public void keyTyped(KeyEvent e) {
            updateControls();
        }
    });

    return combobox;
}

From source file:com.intellij.refactoring.ui.NameSuggestionsField.java

License:Apache License

public NameSuggestionsField(Project project) {
    super(new BorderLayout());
    myProject = project;//  ww  w  .j av  a 2s  . com
    myComboBoxModel = new MyComboBoxModel();
    final ComboBox comboBox = new ComboBox(myComboBoxModel, -1);
    myComponent = comboBox;
    add(myComponent, BorderLayout.CENTER);
    setupComboBox(comboBox, InternalStdFileTypes.JAVA);
}

From source file:org.jetbrains.plugins.groovy.mvc.MvcRunTargetDialog.java

License:Apache License

private void createUIComponents() {
    myTargetField = new ComboBox(MvcRunTargetHistoryService.getInstance().getHistory(), -1);
    myTargetField.setLightWeightPopupEnabled(false);

    EditorComboBoxEditor editor = new StringComboboxEditor(myModule.getProject(), PlainTextFileType.INSTANCE,
            myTargetField);//from   www . j  a v  a2  s  . c om
    myTargetField.setRenderer(new EditorComboBoxRenderer(editor));

    myTargetField.setEditable(true);
    myTargetField.setEditor(editor);

    EditorTextField editorTextField = (EditorTextField) editor.getEditorComponent();

    myFakePanel = new JPanel(new BorderLayout());
    myFakePanel.add(myTargetField, BorderLayout.CENTER);

    TextFieldCompletionProvider vmOptionCompletionProvider = new TextFieldCompletionProviderDumbAware() {
        @NotNull
        @Override
        protected String getPrefix(@NotNull String currentTextPrefix) {
            return MvcRunTargetDialog.getPrefix(currentTextPrefix);
        }

        @Override
        protected void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix,
                @NotNull CompletionResultSet result) {
            if (prefix.endsWith("-D")) {
                result.addAllElements(MvcTargetDialogCompletionUtils.getSystemPropertiesVariants());
            }
        }
    };
    myVmOptionsField = vmOptionCompletionProvider.createEditor(myModule.getProject());

    new TextFieldCompletionProviderDumbAware() {

        @NotNull
        @Override
        protected String getPrefix(@NotNull String currentTextPrefix) {
            return MvcRunTargetDialog.getPrefix(currentTextPrefix);
        }

        @Override
        protected void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix,
                @NotNull CompletionResultSet result) {
            for (LookupElement variant : MvcTargetDialogCompletionUtils.collectVariants(myModule, text, offset,
                    prefix)) {
                result.addElement(variant);
            }
        }
    }.apply(editorTextField);

    editorTextField.getDocument().addDocumentListener(new DocumentAdapter() {
        @Override
        public void documentChanged(DocumentEvent e) {
            setOKActionEnabled(!StringUtil.isEmptyOrSpaces(e.getDocument().getText()));
        }
    });
    setOKActionEnabled(false);
}