Search in sources :

Example 1 with JBComboBoxLabel

use of com.intellij.ui.components.JBComboBoxLabel in project intellij-community by JetBrains.

the class ParameterTablePanel method init.

public void init(VariableData[] variableData, Project project, PsiElement... scopeElements) {
    super.init(variableData);
    myParameterTypeSelectors = new TypeSelector[getVariableData().length];
    for (int i = 0; i < myParameterTypeSelectors.length; i++) {
        final VariableData data = variableData[i];
        final TypeSelector selector = createSelector(project, data, scopeElements);
        myParameterTypeSelectors[i] = selector;
        //reverse order
        getVariableData()[i].type = myParameterTypeSelectors[i].getSelectedType();
    }
    myTypeRendererCombo = new ComboBox<>(getVariableData());
    myTypeRendererCombo.setOpaque(true);
    myTypeRendererCombo.setBorder(null);
    myTypeRendererCombo.setRenderer(new ListCellRendererWrapper<VariableData>() {

        @Override
        public void customize(JList list, VariableData value, int index, boolean selected, boolean hasFocus) {
            if (value != null) {
                setText(value.type.getPresentableText());
            }
        }
    });
    final TableColumn typeColumn = myTable.getColumnModel().getColumn(1);
    typeColumn.setCellEditor(new AbstractTableCellEditor() {

        TypeSelector myCurrentSelector;

        final JBComboBoxTableCellEditorComponent myEditorComponent = new JBComboBoxTableCellEditorComponent();

        final JLabel myTypeLabel = new JLabel();

        @Nullable
        public Object getCellEditorValue() {
            if (myCurrentSelector.getComponent() instanceof JLabel) {
                return myCurrentSelector.getSelectedType();
            }
            return myEditorComponent.getEditorValue();
        }

        public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected, final int row, final int column) {
            myCurrentSelector = myParameterTypeSelectors[row];
            if (myParameterTypeSelectors[row].getComponent() instanceof JLabel) {
                PsiType selectedType = myCurrentSelector.getSelectedType();
                if (selectedType != null) {
                    myTypeLabel.setText(selectedType.getPresentableText());
                }
                return myTypeLabel;
            }
            myEditorComponent.setCell(table, row, column);
            myEditorComponent.setOptions(myCurrentSelector.getTypes());
            myEditorComponent.setDefaultValue(getVariableData()[row].type);
            myEditorComponent.setToString(o -> ((PsiType) o).getPresentableText());
            return myEditorComponent;
        }
    });
    myTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer() {

        private final JBComboBoxLabel myLabel = new JBComboBoxLabel();

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value != null) {
                myLabel.setText(((PsiType) value).getPresentableText());
                myLabel.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
                myLabel.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
                if (isSelected) {
                    myLabel.setSelectionIcon();
                } else {
                    myLabel.setRegularIcon();
                }
            }
            return myLabel;
        }
    });
}
Also used : ComboBox(com.intellij.openapi.ui.ComboBox) AbstractTableCellEditor(com.intellij.util.ui.AbstractTableCellEditor) TypeSelectorManagerImpl(com.intellij.refactoring.ui.TypeSelectorManagerImpl) TableColumn(javax.swing.table.TableColumn) Predicate(java.util.function.Predicate) TypeSelector(com.intellij.refactoring.ui.TypeSelector) ColumnInfo(com.intellij.util.ui.ColumnInfo) TypeSelectorManager(com.intellij.refactoring.ui.TypeSelectorManager) ListCellRendererWrapper(com.intellij.ui.ListCellRendererWrapper) LanguageNamesValidation(com.intellij.lang.LanguageNamesValidation) ArrayList(java.util.ArrayList) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JBComboBoxTableCellEditorComponent(com.intellij.ui.components.editors.JBComboBoxTableCellEditorComponent) JBComboBoxLabel(com.intellij.ui.components.JBComboBoxLabel) Project(com.intellij.openapi.project.Project) com.intellij.psi(com.intellij.psi) JavaLanguage(com.intellij.lang.java.JavaLanguage) javax.swing(javax.swing) JBComboBoxLabel(com.intellij.ui.components.JBComboBoxLabel) TableColumn(javax.swing.table.TableColumn) JBComboBoxTableCellEditorComponent(com.intellij.ui.components.editors.JBComboBoxTableCellEditorComponent) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) AbstractTableCellEditor(com.intellij.util.ui.AbstractTableCellEditor) JBComboBoxTableCellEditorComponent(com.intellij.ui.components.editors.JBComboBoxTableCellEditorComponent) TypeSelector(com.intellij.refactoring.ui.TypeSelector) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LanguageNamesValidation (com.intellij.lang.LanguageNamesValidation)1 JavaLanguage (com.intellij.lang.java.JavaLanguage)1 Project (com.intellij.openapi.project.Project)1 ComboBox (com.intellij.openapi.ui.ComboBox)1 com.intellij.psi (com.intellij.psi)1 TypeSelector (com.intellij.refactoring.ui.TypeSelector)1 TypeSelectorManager (com.intellij.refactoring.ui.TypeSelectorManager)1 TypeSelectorManagerImpl (com.intellij.refactoring.ui.TypeSelectorManagerImpl)1 ListCellRendererWrapper (com.intellij.ui.ListCellRendererWrapper)1 JBComboBoxLabel (com.intellij.ui.components.JBComboBoxLabel)1 JBComboBoxTableCellEditorComponent (com.intellij.ui.components.editors.JBComboBoxTableCellEditorComponent)1 AbstractTableCellEditor (com.intellij.util.ui.AbstractTableCellEditor)1 ColumnInfo (com.intellij.util.ui.ColumnInfo)1 java.awt (java.awt)1 ArrayList (java.util.ArrayList)1 Predicate (java.util.function.Predicate)1 javax.swing (javax.swing)1 DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)1 TableColumn (javax.swing.table.TableColumn)1 Nullable (org.jetbrains.annotations.Nullable)1