Search in sources :

Example 1 with JBTableRow

use of com.intellij.util.ui.table.JBTableRow in project intellij-community by JetBrains.

the class JavaChangeSignatureDialog method createParametersListTable.

@Override
protected ParametersListTable createParametersListTable() {
    return new ParametersListTable() {

        private final EditorTextFieldJBTableRowRenderer myRowRenderer = new EditorTextFieldJBTableRowRenderer(getProject(), JavaChangeSignatureDialog.this.getFileType(), myDisposable) {

            @Override
            protected String getText(JTable table, int row) {
                ParameterTableModelItemBase<ParameterInfoImpl> item = getRowItem(row);
                final String typeText = item.typeCodeFragment.getText();
                final String separator = StringUtil.repeatSymbol(' ', getTypesMaxLength() - typeText.length() + 1);
                String text = typeText + separator + item.parameter.getName();
                final String defaultValue = item.defaultValueCodeFragment.getText();
                String tail = "";
                if (StringUtil.isNotEmpty(defaultValue)) {
                    tail += " default value = " + defaultValue;
                }
                if (item.parameter.isUseAnySingleVariable()) {
                    if (StringUtil.isNotEmpty(defaultValue)) {
                        tail += ";";
                    }
                    tail += " Use any var.";
                }
                if (!StringUtil.isEmpty(tail)) {
                    text += " //" + tail;
                }
                return " " + text;
            }
        };

        @Override
        protected JBTableRowRenderer getRowRenderer(int row) {
            return myRowRenderer;
        }

        @NotNull
        @Override
        protected JBTableRowEditor getRowEditor(final ParameterTableModelItemBase<ParameterInfoImpl> item) {
            return new JBTableRowEditor() {

                private EditorTextField myTypeEditor;

                private EditorTextField myNameEditor;

                private EditorTextField myDefaultValueEditor;

                private JCheckBox myAnyVar;

                @Override
                public void prepareEditor(JTable table, int row) {
                    setLayout(new BorderLayout());
                    final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(item.typeCodeFragment);
                    myTypeEditor = new EditorTextField(document, getProject(), getFileType());
                    myTypeEditor.addDocumentListener(mySignatureUpdater);
                    myTypeEditor.setPreferredWidth(getTable().getWidth() / 2);
                    myTypeEditor.addDocumentListener(new RowEditorChangeListener(0));
                    add(createLabeledPanel("Type:", myTypeEditor), BorderLayout.WEST);
                    myNameEditor = new EditorTextField(item.parameter.getName(), getProject(), getFileType());
                    myNameEditor.addDocumentListener(mySignatureUpdater);
                    myNameEditor.addDocumentListener(new RowEditorChangeListener(1));
                    add(createLabeledPanel("Name:", myNameEditor), BorderLayout.CENTER);
                    new TextFieldCompletionProvider() {

                        @Override
                        protected void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix, @NotNull CompletionResultSet result) {
                            final PsiCodeFragment fragment = item.typeCodeFragment;
                            if (fragment instanceof PsiTypeCodeFragment) {
                                final PsiType type;
                                try {
                                    type = ((PsiTypeCodeFragment) fragment).getType();
                                } catch (Exception e) {
                                    return;
                                }
                                final SuggestedNameInfo info = JavaCodeStyleManager.getInstance(myProject).suggestVariableName(VariableKind.PARAMETER, null, null, type);
                                for (String completionVariant : info.names) {
                                    final LookupElementBuilder element = LookupElementBuilder.create(completionVariant);
                                    result.addElement(element.withLookupString(completionVariant.toLowerCase(Locale.ENGLISH)));
                                }
                            }
                        }
                    }.apply(myNameEditor, item.parameter.getName());
                    if (!item.isEllipsisType() && item.parameter.getOldIndex() == -1) {
                        final JPanel additionalPanel = new JPanel(new BorderLayout());
                        final Document doc = PsiDocumentManager.getInstance(getProject()).getDocument(item.defaultValueCodeFragment);
                        myDefaultValueEditor = new EditorTextField(doc, getProject(), getFileType());
                        ((PsiExpressionCodeFragment) item.defaultValueCodeFragment).setExpectedType(getRowType(item));
                        myDefaultValueEditor.setPreferredWidth(getTable().getWidth() / 2);
                        myDefaultValueEditor.addDocumentListener(new RowEditorChangeListener(2));
                        additionalPanel.add(createLabeledPanel("Default value:", myDefaultValueEditor), BorderLayout.WEST);
                        if (!isGenerateDelegate()) {
                            myAnyVar = new JCheckBox("&Use Any Var");
                            UIUtil.applyStyle(UIUtil.ComponentStyle.SMALL, myAnyVar);
                            DialogUtil.registerMnemonic(myAnyVar, '&');
                            myAnyVar.addActionListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    item.parameter.setUseAnySingleVariable(myAnyVar.isSelected());
                                }
                            });
                            final JPanel anyVarPanel = new JPanel(new BorderLayout());
                            anyVarPanel.add(myAnyVar, BorderLayout.SOUTH);
                            UIUtil.addInsets(anyVarPanel, JBUI.insetsBottom(8));
                            additionalPanel.add(anyVarPanel, BorderLayout.CENTER);
                        //additionalPanel.setPreferredSize(new Dimension(t.getWidth() / 3, -1));
                        }
                        add(additionalPanel, BorderLayout.SOUTH);
                    }
                }

                @Override
                public JBTableRow getValue() {
                    return new JBTableRow() {

                        @Override
                        public Object getValueAt(int column) {
                            switch(column) {
                                case 0:
                                    return item.typeCodeFragment;
                                case 1:
                                    return myNameEditor.getText().trim();
                                case 2:
                                    return item.defaultValueCodeFragment;
                                case 3:
                                    return myAnyVar != null && myAnyVar.isSelected();
                            }
                            return null;
                        }
                    };
                }

                @Override
                public JComponent getPreferredFocusedComponent() {
                    final MouseEvent me = getMouseEvent();
                    if (me == null) {
                        return myTypeEditor.getFocusTarget();
                    }
                    final double x = me.getPoint().getX();
                    return x <= getTypesColumnWidth() ? myTypeEditor.getFocusTarget() : myDefaultValueEditor == null || x <= getNamesColumnWidth() ? myNameEditor.getFocusTarget() : myDefaultValueEditor.getFocusTarget();
                }

                @Override
                public JComponent[] getFocusableComponents() {
                    final List<JComponent> focusable = new ArrayList<>();
                    focusable.add(myTypeEditor.getFocusTarget());
                    focusable.add(myNameEditor.getFocusTarget());
                    if (myDefaultValueEditor != null) {
                        focusable.add(myDefaultValueEditor.getFocusTarget());
                    }
                    if (myAnyVar != null) {
                        focusable.add(myAnyVar);
                    }
                    return focusable.toArray(new JComponent[focusable.size()]);
                }
            };
        }

        @Override
        protected boolean isRowEmpty(int row) {
            ParameterInfoImpl parameter = getRowItem(row).parameter;
            return StringUtil.isEmpty(parameter.getName()) && StringUtil.isEmpty(parameter.getTypeText());
        }
    };
}
Also used : AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) Document(com.intellij.openapi.editor.Document) EditorTextFieldJBTableRowRenderer(com.intellij.util.ui.table.EditorTextFieldJBTableRowRenderer) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) JBTableRow(com.intellij.util.ui.table.JBTableRow) MouseEvent(java.awt.event.MouseEvent) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) JBTableRowEditor(com.intellij.util.ui.table.JBTableRowEditor) ActionListener(java.awt.event.ActionListener) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder)

Example 2 with JBTableRow

use of com.intellij.util.ui.table.JBTableRow in project intellij-community by JetBrains.

the class PyChangeSignatureDialog method getTableEditor.

@Override
protected JBTableRowEditor getTableEditor(final JTable t, final ParameterTableModelItemBase<PyParameterInfo> item) {
    return new JBTableRowEditor() {

        private EditorTextField myNameEditor;

        private EditorTextField myDefaultValueEditor;

        private JCheckBox myDefaultInSignature;

        @Override
        public void prepareEditor(JTable table, int row) {
            setLayout(new GridLayout(1, 3));
            final JPanel parameterPanel = createParameterPanel();
            add(parameterPanel);
            final JPanel defaultValuePanel = createDefaultValuePanel();
            add(defaultValuePanel);
            final JPanel defaultValueCheckBox = createDefaultValueCheckBox();
            add(defaultValueCheckBox);
            final String nameText = myNameEditor.getText();
            myDefaultValueEditor.setEnabled(!nameText.startsWith("*") && !PyNames.CANONICAL_SELF.equals(nameText));
            myDefaultInSignature.setEnabled(!nameText.startsWith("*") && !PyNames.CANONICAL_SELF.equals(nameText));
        }

        private JPanel createDefaultValueCheckBox() {
            final JPanel defaultValuePanel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP, 4, 2, true, false));
            final JBLabel inSignatureLabel = new JBLabel(PyBundle.message("refactoring.change.signature.dialog.default.value.checkbox"), UIUtil.ComponentStyle.SMALL);
            IJSwingUtilities.adjustComponentsOnMac(inSignatureLabel, myDefaultInSignature);
            defaultValuePanel.add(inSignatureLabel, BorderLayout.WEST);
            myDefaultInSignature = new JCheckBox();
            myDefaultInSignature.setSelected(((PyParameterTableModelItem) item).isDefaultInSignature());
            myDefaultInSignature.addItemListener(new ItemListener() {

                @Override
                public void itemStateChanged(ItemEvent event) {
                    ((PyParameterTableModelItem) item).setDefaultInSignature(myDefaultInSignature.isSelected());
                }
            });
            myDefaultInSignature.addChangeListener(mySignatureUpdater);
            myDefaultInSignature.setEnabled(item.parameter.getOldIndex() == -1);
            defaultValuePanel.add(myDefaultInSignature, BorderLayout.EAST);
            return defaultValuePanel;
        }

        private JPanel createDefaultValuePanel() {
            final JPanel defaultValuePanel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP, 4, 2, true, false));
            final Document doc = PsiDocumentManager.getInstance(getProject()).getDocument(item.defaultValueCodeFragment);
            myDefaultValueEditor = new EditorTextField(doc, getProject(), getFileType());
            final JBLabel defaultValueLabel = new JBLabel(PyBundle.message("refactoring.change.signature.dialog.default.value.label"), UIUtil.ComponentStyle.SMALL);
            IJSwingUtilities.adjustComponentsOnMac(defaultValueLabel, myDefaultValueEditor);
            defaultValuePanel.add(defaultValueLabel);
            defaultValuePanel.add(myDefaultValueEditor);
            myDefaultValueEditor.setPreferredWidth(t.getWidth() / 2);
            myDefaultValueEditor.addDocumentListener(mySignatureUpdater);
            return defaultValuePanel;
        }

        private JPanel createParameterPanel() {
            final JPanel namePanel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP, 4, 2, true, false));
            myNameEditor = new EditorTextField(item.parameter.getName(), getProject(), getFileType());
            final JBLabel nameLabel = new JBLabel(PyBundle.message("refactoring.change.signature.dialog.name.label"), UIUtil.ComponentStyle.SMALL);
            IJSwingUtilities.adjustComponentsOnMac(nameLabel, myNameEditor);
            namePanel.add(nameLabel);
            namePanel.add(myNameEditor);
            myNameEditor.setPreferredWidth(t.getWidth() / 2);
            myNameEditor.addDocumentListener(new DocumentAdapter() {

                @Override
                public void documentChanged(DocumentEvent event) {
                    fireDocumentChanged(event, 0);
                    myDefaultValueEditor.setEnabled(!myNameEditor.getText().startsWith("*"));
                    myDefaultInSignature.setEnabled(!myNameEditor.getText().startsWith("*"));
                }
            });
            myNameEditor.addDocumentListener(mySignatureUpdater);
            return namePanel;
        }

        @Override
        public JBTableRow getValue() {
            return new JBTableRow() {

                @Override
                public Object getValueAt(int column) {
                    switch(column) {
                        case 0:
                            return myNameEditor.getText().trim();
                        case 1:
                            return new Pair<>(item.defaultValueCodeFragment, ((PyParameterTableModelItem) item).isDefaultInSignature());
                    }
                    return null;
                }
            };
        }

        @Override
        public JComponent getPreferredFocusedComponent() {
            return myNameEditor.getFocusTarget();
        }

        @Override
        public JComponent[] getFocusableComponents() {
            final List<JComponent> focusable = new ArrayList<>();
            focusable.add(myNameEditor.getFocusTarget());
            if (myDefaultValueEditor != null) {
                focusable.add(myDefaultValueEditor.getFocusTarget());
            }
            return focusable.toArray(new JComponent[focusable.size()]);
        }
    };
}
Also used : ItemEvent(java.awt.event.ItemEvent) ArrayList(java.util.ArrayList) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) Document(com.intellij.openapi.editor.Document) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) JBTableRowEditor(com.intellij.util.ui.table.JBTableRowEditor) EditorTextField(com.intellij.ui.EditorTextField) JBLabel(com.intellij.ui.components.JBLabel) ItemListener(java.awt.event.ItemListener) JBTableRow(com.intellij.util.ui.table.JBTableRow) VerticalFlowLayout(com.intellij.openapi.ui.VerticalFlowLayout) Pair(com.intellij.openapi.util.Pair)

Aggregations

Document (com.intellij.openapi.editor.Document)2 JBTableRow (com.intellij.util.ui.table.JBTableRow)2 JBTableRowEditor (com.intellij.util.ui.table.JBTableRowEditor)2 ArrayList (java.util.ArrayList)2 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)1 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)1 VerticalFlowLayout (com.intellij.openapi.ui.VerticalFlowLayout)1 Pair (com.intellij.openapi.util.Pair)1 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)1 EditorTextField (com.intellij.ui.EditorTextField)1 JBLabel (com.intellij.ui.components.JBLabel)1 EditorTextFieldJBTableRowRenderer (com.intellij.util.ui.table.EditorTextFieldJBTableRowRenderer)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 MouseEvent (java.awt.event.MouseEvent)1