Search in sources :

Example 41 with DocumentAdapter

use of com.intellij.openapi.editor.event.DocumentAdapter 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

DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)41 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)40 Document (com.intellij.openapi.editor.Document)10 EditorTextField (com.intellij.ui.EditorTextField)6 ActionEvent (java.awt.event.ActionEvent)6 ActionListener (java.awt.event.ActionListener)6 Disposable (com.intellij.openapi.Disposable)4 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ReferenceEditorComboWithBrowseButton (com.intellij.ui.ReferenceEditorComboWithBrowseButton)4 NotNull (org.jetbrains.annotations.NotNull)4 AnAction (com.intellij.openapi.actionSystem.AnAction)3 Editor (com.intellij.openapi.editor.Editor)3 Module (com.intellij.openapi.module.Module)3 Pair (com.intellij.openapi.util.Pair)3 Ref (com.intellij.openapi.util.Ref)3 Nullable (org.jetbrains.annotations.Nullable)3 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)2 Language (com.intellij.lang.Language)2 CustomShortcutSet (com.intellij.openapi.actionSystem.CustomShortcutSet)2