List of usage examples for com.intellij.openapi.ui InputValidator InputValidator
InputValidator
From source file:auto.fix.ConstantsExtractorFix.java
License:Apache License
public void applyDefaultFix(Project project, String command, boolean useSuggestedName) { IntroduceAndPropagateConstantHandler introduceConstantHandler = new IntroduceAndPropagateConstantHandler( project, constantExpression); if (useSuggestedName) { introduceConstantHandler.setPropagateSettings(command, IntroduceAndPropagateConstantHandler.extractDefaultFieldName(constantExpression), false); } else {// ww w . j av a 2 s. c o m //TODO: there's some utility that let's you highlight the actual text in the file and rename right in the editor - figure out how to use that String suggestedName = IntroduceAndPropagateConstantHandler.extractDefaultFieldName(constantExpression); String constantName = Messages.showEditableChooseDialog("Set constant name", "Rename Suggested Constant Name", null, new String[] {}, suggestedName, new InputValidator() { public boolean checkInput(String inputString) { return true; } public boolean canClose(String inputString) { return true; } }); introduceConstantHandler.setPropagateSettings(command, constantName != null ? constantName : suggestedName, false); } }
From source file:com.intellij.application.options.editor.JavaAutoImportConfigurable.java
License:Apache License
public JavaAutoImportConfigurable() { mySmartPasteCombo.addItem(INSERT_IMPORTS_ALWAYS); mySmartPasteCombo.addItem(INSERT_IMPORTS_ASK); mySmartPasteCombo.addItem(INSERT_IMPORTS_NONE); myExcludePackagesList = new JBList(); myExcludeFromImportAndCompletionPanel.add( ToolbarDecorator.createDecorator(myExcludePackagesList).setAddAction(new AnActionButtonRunnable() { @Override//ww w .j a v a 2 s . co m public void run(AnActionButton button) { InputValidator validator = new InputValidator() { @Override public boolean checkInput(String inputString) { return ourPackagePattern.matcher(inputString).matches(); } @Override public boolean canClose(String inputString) { return checkInput(inputString); } }; String packageName = Messages.showInputDialog(myWholePanel, ApplicationBundle.message("exclude.from.completion.prompt"), ApplicationBundle.message("exclude.from.completion.title"), Messages.getWarningIcon(), "", validator); addExcludePackage(packageName); } }).disableUpDownActions().createPanel(), BorderLayout.CENTER); myExcludePackagesList.getEmptyText() .setText(ApplicationBundle.message("exclude.from.imports.no.exclusions")); }
From source file:com.intellij.codeInsight.daemon.impl.analysis.AddSchemaPrefixIntention.java
License:Apache License
@Override public void invoke(@Nonnull Project project, Editor editor, @Nonnull PsiElement element) throws IncorrectOperationException { final XmlAttribute xmlns = getXmlnsDeclaration(element); if (xmlns == null) { return;/* ww w .j a va2 s .c om*/ } final String namespace = xmlns.getValue(); final XmlTag tag = xmlns.getParent(); if (tag != null) { final Set<String> ns = tag.getLocalNamespaceDeclarations().keySet(); final String nsPrefix = Messages.showInputDialog(project, "Namespace Prefix:", NAME, Messages.getInformationIcon(), "", new InputValidator() { @RequiredDispatchThread @Override public boolean checkInput(String inputString) { return !ns.contains(inputString); } @RequiredDispatchThread @Override public boolean canClose(String inputString) { return checkInput(inputString); } }); if (nsPrefix == null) { return; } final List<XmlTag> tags = new ArrayList<XmlTag>(); final List<XmlAttributeValue> values = new ArrayList<XmlAttributeValue>(); new WriteCommandAction(project, NAME, tag.getContainingFile()) { @Override protected void run(Result result) throws Throwable { tag.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(XmlTag tag) { if (tag.getNamespace().equals(namespace) && tag.getNamespacePrefix().length() == 0) { tags.add(tag); } super.visitXmlTag(tag); } @Override public void visitXmlAttributeValue(XmlAttributeValue value) { PsiReference ref = null; boolean skip = false; for (PsiReference reference : value.getReferences()) { if (reference instanceof TypeOrElementOrAttributeReference) { ref = reference; } else if (reference instanceof SchemaPrefixReference) { skip = true; break; } } if (!skip && ref != null) { final PsiElement xmlElement = ref.resolve(); if (xmlElement instanceof XmlElement) { final XmlTag tag = PsiTreeUtil.getParentOfType(xmlElement, XmlTag.class, false); if (tag != null) { if (tag.getNamespace().equals(namespace)) { if (ref.getRangeInElement().getLength() == value.getValue().length()) { //no ns prefix values.add(value); } } } } } } }); for (XmlAttributeValue value : values) { ((XmlAttribute) value.getParent()).setValue(nsPrefix + ":" + value.getValue()); } for (XmlTag xmlTag : tags) { xmlTag.setName(nsPrefix + ":" + xmlTag.getLocalName()); } xmlns.setName("xmlns:" + nsPrefix); } }.execute(); } }
From source file:com.intellij.codeInspection.ex.SeverityEditorDialog.java
License:Apache License
public SeverityEditorDialog(final JComponent parent, final HighlightSeverity severity, final SeverityRegistrar severityRegistrar) { super(parent, true); mySeverityRegistrar = severityRegistrar; myOptionsList.setCellRenderer(new DefaultListCellRenderer() { @Override/*from ww w .ja va 2 s.co m*/ public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { final Component rendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value instanceof SeverityBasedTextAttributes) { setText(((SeverityBasedTextAttributes) value).getSeverity().toString()); } return rendererComponent; } }); myOptionsList.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { if (myCurrentSelection != null) { apply(myCurrentSelection); } myCurrentSelection = (SeverityBasedTextAttributes) myOptionsList.getSelectedValue(); if (myCurrentSelection != null) { reset(myCurrentSelection); myCard.show(myRightPanel, mySeverityRegistrar.isDefaultSeverity(myCurrentSelection.getSeverity()) ? DEFAULT : EDITABLE); } } }); myOptionsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JPanel leftPanel = ToolbarDecorator.createDecorator(myOptionsList) .setAddAction(new AnActionButtonRunnable() { @Override public void run(AnActionButton button) { final String name = Messages.showInputDialog(myPanel, InspectionsBundle.message("highlight.severity.create.dialog.name.label"), InspectionsBundle.message("highlight.severity.create.dialog.title"), Messages.getQuestionIcon(), "", new InputValidator() { @Override public boolean checkInput(final String inputString) { final ListModel listModel = myOptionsList.getModel(); for (int i = 0; i < listModel.getSize(); i++) { final String severityName = ((SeverityBasedTextAttributes) listModel .getElementAt(i)).getSeverity().myName; if (Comparing.strEqual(severityName, inputString)) return false; } return true; } @Override public boolean canClose(final String inputString) { return checkInput(inputString); } }); if (name == null) return; final TextAttributes textAttributes = CodeInsightColors.WARNINGS_ATTRIBUTES .getDefaultAttributes(); HighlightInfoType.HighlightInfoTypeImpl info = new HighlightInfoType.HighlightInfoTypeImpl( new HighlightSeverity(name, 50), TextAttributesKey.createTextAttributesKey(name)); SeverityBasedTextAttributes newSeverityBasedTextAttributes = new SeverityBasedTextAttributes( textAttributes.clone(), info); ((DefaultListModel) myOptionsList.getModel()).addElement(newSeverityBasedTextAttributes); myOptionsList.clearSelection(); ListScrollingUtil.selectItem(myOptionsList, newSeverityBasedTextAttributes); } }).setMoveUpAction(new AnActionButtonRunnable() { @Override public void run(AnActionButton button) { apply(myCurrentSelection); ListUtil.moveSelectedItemsUp(myOptionsList); } }).setMoveDownAction(new AnActionButtonRunnable() { @Override public void run(AnActionButton button) { apply(myCurrentSelection); ListUtil.moveSelectedItemsDown(myOptionsList); } }).createPanel(); ToolbarDecorator.findRemoveButton(leftPanel).addCustomUpdater(new AnActionButtonUpdater() { @Override public boolean isEnabled(AnActionEvent e) { return !mySeverityRegistrar.isDefaultSeverity( ((SeverityBasedTextAttributes) myOptionsList.getSelectedValue()).getSeverity()); } }); ToolbarDecorator.findUpButton(leftPanel).addCustomUpdater(new AnActionButtonUpdater() { @Override public boolean isEnabled(AnActionEvent e) { boolean canMove = ListUtil.canMoveSelectedItemsUp(myOptionsList); if (canMove) { SeverityBasedTextAttributes pair = (SeverityBasedTextAttributes) myOptionsList .getSelectedValue(); if (pair != null && mySeverityRegistrar.isDefaultSeverity(pair.getSeverity())) { final int newPosition = myOptionsList.getSelectedIndex() - 1; pair = (SeverityBasedTextAttributes) myOptionsList.getModel().getElementAt(newPosition); if (mySeverityRegistrar.isDefaultSeverity(pair.getSeverity())) { canMove = false; } } } return canMove; } }); ToolbarDecorator.findDownButton(leftPanel).addCustomUpdater(new AnActionButtonUpdater() { @Override public boolean isEnabled(AnActionEvent e) { boolean canMove = ListUtil.canMoveSelectedItemsDown(myOptionsList); if (canMove) { SeverityBasedTextAttributes pair = (SeverityBasedTextAttributes) myOptionsList .getSelectedValue(); if (pair != null && mySeverityRegistrar.isDefaultSeverity(pair.getSeverity())) { final int newPosition = myOptionsList.getSelectedIndex() + 1; pair = (SeverityBasedTextAttributes) myOptionsList.getModel().getElementAt(newPosition); if (mySeverityRegistrar.isDefaultSeverity(pair.getSeverity())) { canMove = false; } } } return canMove; } }); myPanel = new JPanel(new BorderLayout()); myPanel.add(leftPanel, BorderLayout.CENTER); myCard = new CardLayout(); myRightPanel = new JPanel(myCard); final JPanel disabled = new JPanel(new GridBagLayout()); final JButton button = new JButton(InspectionsBundle.message("severities.default.settings.message")); button.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { editColorsAndFonts(); } }); disabled.add(button, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); myRightPanel.add(DEFAULT, disabled); myRightPanel.add(EDITABLE, myOptionsPanel); myCard.show(myRightPanel, EDITABLE); myPanel.add(myRightPanel, BorderLayout.EAST); fillList(severity); init(); setTitle(InspectionsBundle.message("severities.editor.dialog.title")); reset((SeverityBasedTextAttributes) myOptionsList.getSelectedValue()); }
From source file:com.intellij.ide.bookmarks.BookmarkManager.java
License:Apache License
public void editDescription(@NotNull Bookmark bookmark) { String description = Messages.showInputDialog(myProject, IdeBundle.message("action.bookmark.edit.description.dialog.message"), IdeBundle.message("action.bookmark.edit.description.dialog.title"), Messages.getQuestionIcon(), bookmark.getDescription(), new InputValidator() { @Override/*from w w w . ja v a 2 s . co m*/ public boolean checkInput(String inputString) { return true; } @Override public boolean canClose(String inputString) { return true; } }); if (description != null) { setDescription(bookmark, description); } }
From source file:com.intellij.ide.favoritesTreeView.actions.AddNewFavoritesListAction.java
License:Apache License
public static String doAddNewFavoritesList(final Project project) { final FavoritesManager favoritesManager = FavoritesManager.getInstance(project); final String name = Messages.showInputDialog(project, IdeBundle.message("prompt.input.new.favorites.list.name"), IdeBundle.message("title.add.new.favorites.list"), Messages.getInformationIcon(), getUniqueName(project), new InputValidator() { @Override/* w w w.ja va 2 s . c o m*/ public boolean checkInput(String inputString) { return inputString != null && inputString.trim().length() > 0; } @Override public boolean canClose(String inputString) { inputString = inputString.trim(); if (favoritesManager.getAvailableFavoritesListNames().contains(inputString)) { Messages.showErrorDialog(project, IdeBundle.message("error.favorites.list.already.exists", inputString.trim()), IdeBundle.message("title.unable.to.add.favorites.list")); return false; } return inputString.length() > 0; } }); if (name == null || name.length() == 0) return null; favoritesManager.createNewList(name); return name; }
From source file:com.intellij.ide.favoritesTreeView.FavoritesManager.java
License:Apache License
public void renameList(final Project project, @NotNull String listName) { final String newName = Messages.showInputDialog(project, IdeBundle.message("prompt.input.favorites.list.new.name", listName), IdeBundle.message("title.rename.favorites.list"), Messages.getInformationIcon(), listName, new InputValidator() { @Override// w w w .j av a 2s. c om public boolean checkInput(String inputString) { return inputString != null && inputString.trim().length() > 0; } @Override public boolean canClose(String inputString) { inputString = inputString.trim(); if (myName2FavoritesRoots.keySet().contains(inputString) || myProviders.keySet().contains(inputString)) { Messages.showErrorDialog(project, IdeBundle.message("error.favorites.list.already.exists", inputString.trim()), IdeBundle.message("title.unable.to.add.favorites.list")); return false; } return !inputString.isEmpty(); } }); if (newName != null && renameFavoritesList(listName, newName)) { rootsChanged(); } }
From source file:com.intellij.ide.util.PackageChooserDialog.java
License:Apache License
private void createNewPackage() { final PsiJavaPackage selectedPackage = getTreeSelection(); if (selectedPackage == null) return;/*from ww w . ja v a 2s . com*/ final String newPackageName = Messages.showInputDialog(myProject, IdeBundle.message("prompt.enter.a.new.package.name"), IdeBundle.message("title.new.package"), Messages.getQuestionIcon(), "", new InputValidator() { public boolean checkInput(final String inputString) { return inputString != null && inputString.length() > 0; } public boolean canClose(final String inputString) { return checkInput(inputString); } }); if (newPackageName == null) return; CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { public void run() { final Runnable action = new Runnable() { public void run() { try { String newQualifiedName = selectedPackage.getQualifiedName(); if (!Comparing.strEqual(newQualifiedName, "")) newQualifiedName += "."; newQualifiedName += newPackageName; final PsiDirectory dir = PackageUtil.findOrCreateDirectoryForPackage(myProject, newQualifiedName, null, false); if (dir == null) return; final PsiJavaPackage newPackage = JavaDirectoryService.getInstance().getPackage(dir); DefaultMutableTreeNode node = (DefaultMutableTreeNode) myTree.getSelectionPath() .getLastPathComponent(); final DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(); newChild.setUserObject(newPackage); node.add(newChild); final DefaultTreeModel model = (DefaultTreeModel) myTree.getModel(); model.nodeStructureChanged(node); final TreePath selectionPath = myTree.getSelectionPath(); TreePath path; if (selectionPath == null) { path = new TreePath(newChild.getPath()); } else { path = selectionPath.pathByAddingChild(newChild); } myTree.setSelectionPath(path); myTree.scrollPathToVisible(path); myTree.expandPath(path); } catch (IncorrectOperationException e) { Messages.showMessageDialog(getContentPane(), StringUtil.getMessage(e), CommonBundle.getErrorTitle(), Messages.getErrorIcon()); if (LOG.isDebugEnabled()) { LOG.debug(e); } } } }; ApplicationManager.getApplication().runReadAction(action); } }, IdeBundle.message("command.create.new.package"), null); }
From source file:com.intellij.ide.util.scopeChooser.EditScopesDialog.java
License:Apache License
@Override protected void doOKAction() { final Object selectedObject = ((ScopeChooserConfigurable) getConfigurable()).getSelectedObject(); if (selectedObject instanceof NamedScope) { mySelectedScope = (NamedScope) selectedObject; }//from w w w. j ava 2 s . c o m super.doOKAction(); if (myCheckShared && mySelectedScope != null) { final Project project = getProject(); final DependencyValidationManager manager = DependencyValidationManager.getInstance(project); NamedScope scope = manager.getScope(mySelectedScope.getName()); if (scope == null) { if (Messages.showYesNoDialog(IdeBundle.message("scope.unable.to.save.scope.message"), IdeBundle.message("scope.unable.to.save.scope.title"), Messages.getErrorIcon()) == DialogWrapper.OK_EXIT_CODE) { final String newName = Messages.showInputDialog(project, IdeBundle.message("add.scope.name.label"), IdeBundle.message("scopes.save.dialog.title.shared"), Messages.getQuestionIcon(), mySelectedScope.getName(), new InputValidator() { @Override public boolean checkInput(String inputString) { return inputString != null && inputString.length() > 0 && manager.getScope(inputString) == null; } @Override public boolean canClose(String inputString) { return checkInput(inputString); } }); if (newName != null) { final PackageSet packageSet = mySelectedScope.getValue(); scope = new NamedScope(newName, packageSet != null ? packageSet.createCopy() : null); mySelectedScope = scope; manager.addScope(mySelectedScope); } } } } }
From source file:com.intellij.ide.util.scopeChooser.ScopeChooserConfigurable.java
License:Apache License
private void createScope(final boolean isLocal, String title, final PackageSet set) { final String newName = Messages.showInputDialog(myTree, IdeBundle.message("add.scope.name.label"), title, Messages.getInformationIcon(), createUniqueName(), new InputValidator() { @Override/*from w w w.j av a2 s . c o m*/ public boolean checkInput(String inputString) { final NamedScopesHolder holder = isLocal ? myLocalScopesManager : mySharedScopesManager; for (NamedScope scope : holder.getPredefinedScopes()) { if (Comparing.strEqual(scope.getName(), inputString.trim())) { return false; } } return inputString.trim().length() > 0; } @Override public boolean canClose(String inputString) { return checkInput(inputString); } }); if (newName != null) { final NamedScope scope = new NamedScope(newName, set); addNewScope(scope, isLocal); } }