Search in sources :

Example 51 with DocumentEvent

use of com.intellij.openapi.editor.event.DocumentEvent in project kotlin by JetBrains.

the class MoveKotlinNestedClassesDialog method initClassChooser.

private void initClassChooser(@NotNull KtClassOrObject initialTargetClass) {
    //noinspection ConstantConditions
    originalClassField.setText(originalClass.getFqName().asString());
    //noinspection ConstantConditions
    targetClassChooser = new ReferenceEditorComboWithBrowseButton(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            TreeClassChooser chooser = new TreeJavaClassChooserDialog(RefactoringBundle.message("choose.destination.class"), myProject, GlobalSearchScope.projectScope(myProject), new ClassFilter() {

                @Override
                public boolean isAccepted(PsiClass aClass) {
                    if (!(aClass instanceof KtLightClassForSourceDeclaration))
                        return false;
                    KtClassOrObject classOrObject = ((KtLightClassForSourceDeclaration) aClass).getKotlinOrigin();
                    if (classOrObject instanceof KtObjectDeclaration) {
                        return !((KtObjectDeclaration) classOrObject).isObjectLiteral();
                    }
                    if (classOrObject instanceof KtClass) {
                        KtClass ktClass = (KtClass) classOrObject;
                        return !(ktClass.isInner() || ktClass.isAnnotation());
                    }
                    return false;
                }
            }, null, null, true) {

                @Nullable
                @Override
                protected PsiClass getSelectedFromTreeUserObject(DefaultMutableTreeNode node) {
                    PsiClass psiClass = super.getSelectedFromTreeUserObject(node);
                    if (psiClass != null)
                        return psiClass;
                    Object userObject = node.getUserObject();
                    if (!(userObject instanceof KtClassOrObjectTreeNode))
                        return null;
                    return LightClassUtilsKt.toLightClass(((KtClassOrObjectTreeNode) userObject).getValue());
                }
            };
            chooser.selectDirectory((targetClass != null ? targetClass : originalClass).getContainingFile().getContainingDirectory());
            chooser.showDialog();
            PsiClass aClass = chooser.getSelected();
            if (aClass instanceof KtLightClassForSourceDeclaration) {
                targetClass = ((KtLightClassForSourceDeclaration) aClass).getKotlinOrigin();
                targetClassChooser.setText(aClass.getQualifiedName());
            }
        }
    }, initialTargetClass.getFqName().asString(), myProject, true, JavaCodeFragment.VisibilityChecker.PROJECT_SCOPE_VISIBLE, RECENTS_KEY);
    targetClassChooser.getChildComponent().getDocument().addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            PsiClass aClass = JavaPsiFacade.getInstance(myProject).findClass(targetClassChooser.getText(), GlobalSearchScope.projectScope(myProject));
            targetClass = aClass instanceof KtLightClassForSourceDeclaration ? ((KtLightClassForSourceDeclaration) aClass).getKotlinOrigin() : null;
            validateButtons();
        }
    });
    targetClassChooserPanel.add(targetClassChooser);
}
Also used : KtClassOrObjectTreeNode(org.jetbrains.kotlin.idea.projectView.KtClassOrObjectTreeNode) TreeClassChooser(com.intellij.ide.util.TreeClassChooser) KtLightClassForSourceDeclaration(org.jetbrains.kotlin.asJava.classes.KtLightClassForSourceDeclaration) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ActionEvent(java.awt.event.ActionEvent) PsiClass(com.intellij.psi.PsiClass) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) ReferenceEditorComboWithBrowseButton(com.intellij.ui.ReferenceEditorComboWithBrowseButton) TreeJavaClassChooserDialog(com.intellij.ide.util.TreeJavaClassChooserDialog) ActionListener(java.awt.event.ActionListener) ClassFilter(com.intellij.ide.util.ClassFilter)

Example 52 with DocumentEvent

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

Example 53 with DocumentEvent

use of com.intellij.openapi.editor.event.DocumentEvent in project android by JetBrains.

the class EditLogFilterDialog method createEditorFields.

private void createEditorFields() {
    myNameFieldLabel.setLabelFor(myFilterNameField);
    myLogMessageField = new RegexFilterComponent(LOG_FILTER_MESSAGE_HISTORY, FILTER_HISTORY_SIZE);
    myLogMessageFieldWrapper.add(myLogMessageField);
    myLogMessageLabel.setLabelFor(myLogMessageField);
    myTagField = new RegexFilterComponent(LOG_FILTER_TAG_HISTORY, FILTER_HISTORY_SIZE);
    myTagFieldWrapper.add(myTagField);
    myLogTagLabel.setLabelFor(myTagField);
    myPidField = new TextFieldWithAutoCompletion<>(myProject, new TextFieldWithAutoCompletion.StringsCompletionProvider(null, null) {

        @NotNull
        @Override
        public Collection<String> getItems(String prefix, boolean cached, CompletionParameters parameters) {
            parseExistingMessagesIfNecessary();
            setItems(myUsedPids);
            return super.getItems(prefix, cached, parameters);
        }

        @Override
        public int compare(String item1, String item2) {
            final int pid1 = Integer.parseInt(item1);
            final int pid2 = Integer.parseInt(item2);
            return Comparing.compare(pid1, pid2);
        }
    }, true, null);
    myPidFieldWrapper.add(myPidField);
    myPidLabel.setLabelFor(myPidField);
    myPackageNameField = new RegexFilterComponent(LOG_FILTER_PACKAGE_NAME_HISTORY, FILTER_HISTORY_SIZE);
    myPackageNameFieldWrapper.add(myPackageNameField);
    myPackageNameLabel.setLabelFor(myPackageNameField);
    myLogLevelCombo.setModel(new EnumComboBoxModel<>(Log.LogLevel.class));
    myLogLevelCombo.setRenderer(new ListCellRendererWrapper() {

        @Override
        public void customize(JList list, Object value, int index, boolean selected, boolean hasFocus) {
            if (value != null) {
                setText(StringUtil.capitalize(((Log.LogLevel) value).getStringValue().toLowerCase()));
            }
        }
    });
    myLogLevelCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (myActiveFilter == null) {
                return;
            }
            Log.LogLevel selectedItem = (Log.LogLevel) myLogLevelCombo.getSelectedItem();
            myActiveFilter.setLogLevel(selectedItem.getStringValue());
        }
    });
    final Key<JComponent> componentKey = new Key<>("myComponent");
    myFilterNameField.getDocument().putUserData(componentKey, myFilterNameField);
    myPidField.getDocument().putUserData(componentKey, myPidField);
    DocumentListener l = new DocumentListener() {

        @Override
        public void documentChanged(DocumentEvent e) {
            if (myActiveFilter == null) {
                return;
            }
            String text = e.getDocument().getText().trim();
            JComponent src = e.getDocument().getUserData(componentKey);
            if (src == myPidField) {
                myActiveFilter.setPid(text);
            } else if (src == myFilterNameField) {
                int index = myFiltersList.getSelectedIndex();
                if (index != -1) {
                    myFiltersListModel.setElementAt(text, index);
                }
                myActiveFilter.setName(text);
            }
        }
    };
    myFilterNameField.getDocument().addDocumentListener(l);
    myPidField.getDocument().addDocumentListener(l);
    RegexFilterComponent.Listener rl = new RegexFilterComponent.Listener() {

        @Override
        public void filterChanged(RegexFilterComponent filter) {
            if (myActiveFilter == null) {
                return;
            }
            if (filter == myTagField) {
                myActiveFilter.setLogTagPattern(filter.getFilter());
                myActiveFilter.setLogTagIsRegex(filter.isRegex());
            } else if (filter == myLogMessageField) {
                myActiveFilter.setLogMessagePattern(filter.getFilter());
                myActiveFilter.setLogMessageIsRegex(filter.isRegex());
            } else if (filter == myPackageNameField) {
                myActiveFilter.setPackageNamePattern(filter.getFilter());
                myActiveFilter.setPackageNameIsRegex(filter.isRegex());
            }
        }
    };
    myTagField.addRegexListener(rl);
    myLogMessageField.addRegexListener(rl);
    myPackageNameField.addRegexListener(rl);
}
Also used : DocumentListener(com.intellij.openapi.editor.event.DocumentListener) DocumentListener(com.intellij.openapi.editor.event.DocumentListener) ActionListener(java.awt.event.ActionListener) ListSelectionListener(javax.swing.event.ListSelectionListener) Log(com.android.ddmlib.Log) ActionEvent(java.awt.event.ActionEvent) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) ActionListener(java.awt.event.ActionListener) Key(com.intellij.openapi.util.Key)

Example 54 with DocumentEvent

use of com.intellij.openapi.editor.event.DocumentEvent in project android by JetBrains.

the class TemplateParameterStep2 method addJBDocumentListener.

private void addJBDocumentListener(com.intellij.openapi.editor.Document doc, final JComponent textField) {
    DocumentListener listener = new DocumentListener() {

        @Override
        public void documentChanged(DocumentEvent event) {
            saveState(textField);
        }
    };
    Disposable disposable = getDisposable();
    if (disposable != null) {
        doc.addDocumentListener(listener, disposable);
    } else {
        doc.addDocumentListener(listener);
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) DocumentListener(com.intellij.openapi.editor.event.DocumentListener) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent)

Aggregations

DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)54 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)40 Document (com.intellij.openapi.editor.Document)14 DocumentListener (com.intellij.openapi.editor.event.DocumentListener)12 EditorTextField (com.intellij.ui.EditorTextField)8 ActionEvent (java.awt.event.ActionEvent)8 ActionListener (java.awt.event.ActionListener)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Nullable (org.jetbrains.annotations.Nullable)5 Disposable (com.intellij.openapi.Disposable)4 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)4 Editor (com.intellij.openapi.editor.Editor)4 ReferenceEditorComboWithBrowseButton (com.intellij.ui.ReferenceEditorComboWithBrowseButton)4 NotNull (org.jetbrains.annotations.NotNull)4 Language (com.intellij.lang.Language)3 AnAction (com.intellij.openapi.actionSystem.AnAction)3 Module (com.intellij.openapi.module.Module)3 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)2 FrozenDocument (com.intellij.openapi.editor.impl.FrozenDocument)2 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)2