Search in sources :

Example 1 with InputValidatorEx

use of com.intellij.openapi.ui.InputValidatorEx in project intellij-elixir by KronicDeth.

the class CreateElixirModuleAction method buildDialog.

/*
     * Protected Instance Methods
     */
/**
     * todo: the Application-template, Supervisor-template, GenServer-template, GenEvent-template should be improved
     */
@Override
protected void buildDialog(@NotNull Project project, @NotNull final PsiDirectory directory, @NotNull CreateFileFromTemplateDialog.Builder builder) {
    builder.setTitle(NEW_ELIXIR_MODULE).addKind("Empty module", ElixirIcons.FILE, "Elixir Module").addKind("Elixir Application", ElixirIcons.ELIXIR_APPLICATION, "Elixir Application").addKind("Elixir Supervisor", ElixirIcons.ELIXIR_SUPERVISOR, "Elixir Supervisor").addKind("Elixir GenServer", ElixirIcons.ELIXIR_GEN_SERVER, "Elixir GenServer").addKind("Elixir GenEvent", ElixirIcons.ELIXIR_GEN_EVENT, "Elixir GenEvent").setValidator(new InputValidatorEx() {

        /*
                     * Public Instance Methods
                     */
        @Override
        public boolean canClose(String inputString) {
            return !StringUtil.isEmptyOrSpaces(inputString) && getErrorText(inputString) == null;
        }

        @Override
        public boolean checkInput(String inputString) {
            return checkFormat(inputString) && checkDoesNotExist(inputString);
        }

        @Nullable
        @Override
        public String getErrorText(String inputString) {
            String errorText = null;
            if (!StringUtil.isEmpty(inputString)) {
                if (!checkFormat(inputString)) {
                    errorText = String.format(INVALID_MODULE_MESSAGE_FMT, inputString);
                } else if (!checkDoesNotExist(inputString)) {
                    String fullPath = fullPath(directory, ancestorDirectoryNamesBaseNamePair(inputString));
                    errorText = String.format(EXISTING_MODULE_MESSAGE_FMT, fullPath);
                }
            }
            return errorText;
        }

        /*
                     * Private Instance Methods
                     */
        private boolean checkDoesNotExist(@NotNull String moduleName) {
            Pair<List<String>, String> ancestorDirectoryNamesBaseNamePair = ancestorDirectoryNamesBaseNamePair(moduleName);
            List<String> ancestorDirectoryNames = ancestorDirectoryNamesBaseNamePair.first;
            PsiDirectory currentDirectory = directory;
            boolean doesNotExists = false;
            for (String ancestorDirectoryName : ancestorDirectoryNames) {
                PsiDirectory subdirectory = currentDirectory.findSubdirectory(ancestorDirectoryName);
                if (subdirectory == null) {
                    doesNotExists = true;
                    break;
                }
                currentDirectory = subdirectory;
            }
            // if all the directories exist
            if (!doesNotExists) {
                String baseName = ancestorDirectoryNamesBaseNamePair.second;
                doesNotExists = currentDirectory.findFile(baseName) == null;
            }
            return doesNotExists;
        }

        private boolean checkFormat(@NotNull String inputString) {
            Matcher matcher = MODULE_NAME_PATTERN.matcher(inputString);
            return matcher.matches();
        }
    });
}
Also used : Matcher(java.util.regex.Matcher) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList) List(java.util.List) InputValidatorEx(com.intellij.openapi.ui.InputValidatorEx) Nullable(org.jetbrains.annotations.Nullable) Pair(com.intellij.openapi.util.Pair)

Example 2 with InputValidatorEx

use of com.intellij.openapi.ui.InputValidatorEx in project intellij-community by JetBrains.

the class CreateResourceBundleDialogComponent method createUIComponents.

@SuppressWarnings("unchecked")
private void createUIComponents() {
    final JBList projectExistLocalesList = new JBList();
    final MyExistLocalesListModel existLocalesListModel = new MyExistLocalesListModel();
    projectExistLocalesList.setModel(existLocalesListModel);
    projectExistLocalesList.setCellRenderer(getLocaleRenderer());
    myProjectExistLocalesPanel = ToolbarDecorator.createDecorator(projectExistLocalesList).disableRemoveAction().disableUpDownActions().createPanel();
    myProjectExistLocalesPanel.setBorder(IdeBorderFactory.createTitledBorder("Project locales", false));
    final JBList localesToAddList = new JBList();
    final List<Locale> locales;
    final List<Locale> restrictedLocales;
    if (myResourceBundle == null) {
        locales = Collections.singletonList(PropertiesUtil.DEFAULT_LOCALE);
        restrictedLocales = Collections.emptyList();
    } else {
        locales = Collections.emptyList();
        restrictedLocales = ContainerUtil.map(myResourceBundle.getPropertiesFiles(), PropertiesFile::getLocale);
    }
    myLocalesModel = new CollectionListModel<Locale>(locales) {

        @Override
        public void add(@NotNull List<? extends Locale> elements) {
            final List<Locale> currentItems = getItems();
            elements = ContainerUtil.filter(elements, locale -> !restrictedLocales.contains(locale) && !currentItems.contains(locale));
            super.add(elements);
        }
    };
    localesToAddList.setModel(myLocalesModel);
    localesToAddList.setCellRenderer(getLocaleRenderer());
    localesToAddList.addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            projectExistLocalesList.clearSelection();
        }
    });
    projectExistLocalesList.addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            localesToAddList.clearSelection();
        }
    });
    myNewBundleLocalesPanel = ToolbarDecorator.createDecorator(localesToAddList).setAddAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            final String rawAddedLocales = Messages.showInputDialog(myProject, PropertiesBundle.message("create.resource.bundle.dialog.add.locales.validator.message"), PropertiesBundle.message("create.resource.bundle.dialog.add.locales.validator.title"), null, null, new InputValidatorEx() {

                @Nullable
                @Override
                public String getErrorText(String inputString) {
                    return checkInput(inputString) ? null : "Invalid locales";
                }

                @Override
                public boolean checkInput(String inputString) {
                    return extractLocalesFromString(inputString) != null;
                }

                @Override
                public boolean canClose(String inputString) {
                    return checkInput(inputString);
                }
            });
            if (rawAddedLocales != null) {
                final List<Locale> locales = extractLocalesFromString(rawAddedLocales);
                LOG.assertTrue(locales != null);
                myLocalesModel.add(locales);
            }
        }
    }).setAddActionName("Add locales by suffix").disableUpDownActions().createPanel();
    myNewBundleLocalesPanel.setBorder(IdeBorderFactory.createTitledBorder("Locales to add", false));
    myAddLocaleFromExistButton = new JButton(AllIcons.Actions.Forward);
    new ClickListener() {

        @Override
        public boolean onClick(@NotNull MouseEvent event, int clickCount) {
            if (clickCount == 1) {
                myLocalesModel.add(ContainerUtil.map(projectExistLocalesList.getSelectedValues(), o -> (Locale) o));
                return true;
            }
            return false;
        }
    }.installOn(myAddLocaleFromExistButton);
    projectExistLocalesList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            final List<Locale> currentItems = myLocalesModel.getItems();
            for (Object o : projectExistLocalesList.getSelectedValues()) {
                Locale l = (Locale) o;
                if (!restrictedLocales.contains(l) && !currentItems.contains(l)) {
                    myAddLocaleFromExistButton.setEnabled(true);
                    return;
                }
            }
            myAddLocaleFromExistButton.setEnabled(false);
        }
    });
    myAddLocaleFromExistButton.setEnabled(false);
    myAddAllButton = new JButton("Add All");
    new ClickListener() {

        @Override
        public boolean onClick(@NotNull MouseEvent event, int clickCount) {
            if (clickCount == 1) {
                myLocalesModel.add(existLocalesListModel.getLocales());
            }
            return false;
        }
    }.installOn(myAddAllButton);
}
Also used : ListSelectionEvent(javax.swing.event.ListSelectionEvent) InputValidatorEx(com.intellij.openapi.ui.InputValidatorEx) ListSelectionListener(javax.swing.event.ListSelectionListener) JBList(com.intellij.ui.components.JBList) JBList(com.intellij.ui.components.JBList)

Example 3 with InputValidatorEx

use of com.intellij.openapi.ui.InputValidatorEx in project intellij-community by JetBrains.

the class NewFolderAction method createNewFolder.

private static void createNewFolder(FileSystemTree fileSystemTree) {
    final VirtualFile file = fileSystemTree.getNewFileParent();
    if (file == null || !file.isDirectory())
        return;
    final InputValidatorEx validator = new NewFolderValidator(file);
    final String newFolderName = Messages.showInputDialog(UIBundle.message("create.new.folder.enter.new.folder.name.prompt.text"), UIBundle.message("new.folder.dialog.title"), Messages.getQuestionIcon(), "", validator);
    if (newFolderName == null) {
        return;
    }
    Exception failReason = ((FileSystemTreeImpl) fileSystemTree).createNewFolder(file, newFolderName);
    if (failReason != null) {
        Messages.showMessageDialog(UIBundle.message("create.new.folder.could.not.create.folder.error.message", newFolderName), UIBundle.message("error.dialog.title"), Messages.getErrorIcon());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileSystemTreeImpl(com.intellij.openapi.fileChooser.ex.FileSystemTreeImpl) InputValidatorEx(com.intellij.openapi.ui.InputValidatorEx)

Example 4 with InputValidatorEx

use of com.intellij.openapi.ui.InputValidatorEx in project intellij-community by JetBrains.

the class InstanceFilterEditor method addClassFilter.

protected void addClassFilter() {
    String idString = Messages.showInputDialog(myProject, DebuggerBundle.message("add.instance.filter.dialog.prompt"), DebuggerBundle.message("add.instance.filter.dialog.title"), Messages.getQuestionIcon(), null, new InputValidatorEx() {

        @Nullable
        @Override
        public String getErrorText(String inputString) {
            try {
                //noinspection ResultOfMethodCallIgnored
                Long.parseLong(inputString);
                return null;
            } catch (NumberFormatException e) {
                return DebuggerBundle.message("add.instance.filter.dialog.error.numeric.value.expected");
            }
        }

        @Override
        public boolean checkInput(String inputString) {
            return getErrorText(inputString) == null;
        }

        @Override
        public boolean canClose(String inputString) {
            return getErrorText(inputString) == null;
        }
    });
    if (idString != null) {
        ClassFilter filter = createFilter(idString);
        myTableModel.addRow(filter);
        int row = myTableModel.getRowCount() - 1;
        myTable.getSelectionModel().setSelectionInterval(row, row);
        myTable.scrollRectToVisible(myTable.getCellRect(row, 0, true));
        IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
            IdeFocusManager.getGlobalInstance().requestFocus(myTable, true);
        });
    }
}
Also used : ClassFilter(com.intellij.ui.classFilter.ClassFilter) InputValidatorEx(com.intellij.openapi.ui.InputValidatorEx) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with InputValidatorEx

use of com.intellij.openapi.ui.InputValidatorEx in project intellij-community by JetBrains.

the class CreateClassAction method buildDialog.

@Override
protected void buildDialog(final Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) {
    builder.setTitle(IdeBundle.message("action.create.new.class")).addKind("Class", PlatformIcons.CLASS_ICON, JavaTemplateUtil.INTERNAL_CLASS_TEMPLATE_NAME).addKind("Interface", PlatformIcons.INTERFACE_ICON, JavaTemplateUtil.INTERNAL_INTERFACE_TEMPLATE_NAME);
    if (PsiUtil.getLanguageLevel(directory).isAtLeast(LanguageLevel.JDK_1_5)) {
        builder.addKind("Enum", PlatformIcons.ENUM_ICON, JavaTemplateUtil.INTERNAL_ENUM_TEMPLATE_NAME);
        builder.addKind("Annotation", PlatformIcons.ANNOTATION_TYPE_ICON, JavaTemplateUtil.INTERNAL_ANNOTATION_TYPE_TEMPLATE_NAME);
    }
    for (FileTemplate template : FileTemplateManager.getInstance(project).getAllTemplates()) {
        final JavaCreateFromTemplateHandler handler = new JavaCreateFromTemplateHandler();
        if (handler.handlesTemplate(template) && JavaCreateFromTemplateHandler.canCreate(directory)) {
            builder.addKind(template.getName(), JavaFileType.INSTANCE.getIcon(), template.getName());
        }
    }
    builder.setValidator(new InputValidatorEx() {

        @Override
        public String getErrorText(String inputString) {
            if (inputString.length() > 0 && !PsiNameHelper.getInstance(project).isQualifiedName(inputString)) {
                return "This is not a valid Java qualified name";
            }
            return null;
        }

        @Override
        public boolean checkInput(String inputString) {
            return true;
        }

        @Override
        public boolean canClose(String inputString) {
            return !StringUtil.isEmptyOrSpaces(inputString) && getErrorText(inputString) == null;
        }
    });
}
Also used : JavaCreateFromTemplateHandler(com.intellij.ide.fileTemplates.JavaCreateFromTemplateHandler) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) InputValidatorEx(com.intellij.openapi.ui.InputValidatorEx)

Aggregations

InputValidatorEx (com.intellij.openapi.ui.InputValidatorEx)6 Nullable (org.jetbrains.annotations.Nullable)3 ProjectGroup (com.intellij.ide.ProjectGroup)1 ProjectGroupActionGroup (com.intellij.ide.ProjectGroupActionGroup)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 JavaCreateFromTemplateHandler (com.intellij.ide.fileTemplates.JavaCreateFromTemplateHandler)1 FileSystemTreeImpl (com.intellij.openapi.fileChooser.ex.FileSystemTreeImpl)1 Pair (com.intellij.openapi.util.Pair)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 ClassFilter (com.intellij.ui.classFilter.ClassFilter)1 JBList (com.intellij.ui.components.JBList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1 ListSelectionListener (javax.swing.event.ListSelectionListener)1