use of com.intellij.openapi.ui.InputValidator in project intellij-community by JetBrains.
the class ImportToggleAliasIntention method doInvoke.
@Override
public void doInvoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
// sanity check: isAvailable must have set it.
final IntentionState state = IntentionState.fromContext(editor, file);
//
// we set in in the source
final String target_name;
// we replace it in the source
final String remove_name;
PyReferenceExpression reference = sure(state.myImportElement.getImportReferenceExpression());
// search for references to us with the right name
try {
String imported_name = PyPsiUtils.toPath(reference);
if (state.myAlias != null) {
// have to remove alias, rename everything to original
target_name = imported_name;
remove_name = state.myAlias;
} else {
// ask for and add alias
Application application = ApplicationManager.getApplication();
if (application != null && !application.isUnitTestMode()) {
String alias = Messages.showInputDialog(project, PyBundle.message("INTN.alias.for.$0.dialog.title", imported_name), "Add Alias", Messages.getQuestionIcon(), "", new InputValidator() {
@Override
public boolean checkInput(String inputString) {
return PyNames.isIdentifier(inputString);
}
@Override
public boolean canClose(String inputString) {
return PyNames.isIdentifier(inputString);
}
});
if (alias == null) {
return;
}
target_name = alias;
} else {
// test mode
target_name = "alias";
}
remove_name = imported_name;
}
final PsiElement referee = reference.getReference().resolve();
if (referee != null && imported_name != null) {
final Collection<PsiReference> references = new ArrayList<>();
final ScopeOwner scope = PsiTreeUtil.getParentOfType(state.myImportElement, ScopeOwner.class);
PsiTreeUtil.processElements(scope, new PsiElementProcessor() {
public boolean execute(@NotNull PsiElement element) {
getReferences(element);
if (element instanceof PyStringLiteralExpression) {
final PsiLanguageInjectionHost host = (PsiLanguageInjectionHost) element;
final List<Pair<PsiElement, TextRange>> files = InjectedLanguageManager.getInstance(project).getInjectedPsiFiles(host);
if (files != null) {
for (Pair<PsiElement, TextRange> pair : files) {
final PsiElement first = pair.getFirst();
if (first instanceof ScopeOwner) {
final ScopeOwner scopeOwner = (ScopeOwner) first;
PsiTreeUtil.processElements(scopeOwner, new PsiElementProcessor() {
public boolean execute(@NotNull PsiElement element) {
getReferences(element);
return true;
}
});
}
}
}
}
return true;
}
private void getReferences(PsiElement element) {
if (element instanceof PyReferenceExpression && PsiTreeUtil.getParentOfType(element, PyImportElement.class) == null) {
PyReferenceExpression ref = (PyReferenceExpression) element;
if (remove_name.equals(PyPsiUtils.toPath(ref))) {
// filter out other names that might resolve to our target
PsiElement resolved = ref.getReference().resolve();
if (resolved == referee)
references.add(ref.getReference());
}
}
}
});
// no references here is OK by us.
if (showConflicts(project, findDefinitions(target_name, references, Collections.<PsiElement>emptySet()), target_name, null)) {
// got conflicts
return;
}
// alter the import element
PyElementGenerator generator = PyElementGenerator.getInstance(project);
final LanguageLevel languageLevel = LanguageLevel.forElement(state.myImportElement);
if (state.myAlias != null) {
// remove alias
ASTNode node = sure(state.myImportElement.getNode());
ASTNode parent = sure(node.getTreeParent());
// this is the reference
node = sure(node.getFirstChildNode());
// things past the reference: space, 'as', and alias
node = sure(node.getTreeNext());
parent.removeRange(node, null);
} else {
// add alias
ASTNode my_ielt_node = sure(state.myImportElement.getNode());
PyImportElement fountain = generator.createFromText(languageLevel, PyImportElement.class, "import foo as " + target_name, new int[] { 0, 2 });
// at import elt
ASTNode graft_node = sure(fountain.getNode());
// at ref
graft_node = sure(graft_node.getFirstChildNode());
// space
graft_node = sure(graft_node.getTreeNext());
my_ielt_node.addChild((ASTNode) graft_node.clone());
// 'as'
graft_node = sure(graft_node.getTreeNext());
my_ielt_node.addChild((ASTNode) graft_node.clone());
// space
graft_node = sure(graft_node.getTreeNext());
my_ielt_node.addChild((ASTNode) graft_node.clone());
// alias
graft_node = sure(graft_node.getTreeNext());
my_ielt_node.addChild((ASTNode) graft_node.clone());
}
// alter references
for (PsiReference ref : references) {
ASTNode ref_name_node = sure(sure(ref.getElement()).getNode());
ASTNode parent = sure(ref_name_node.getTreeParent());
ASTNode new_name_node = generator.createExpressionFromText(languageLevel, target_name).getNode();
assert new_name_node != null;
parent.replaceChild(ref_name_node, new_name_node);
}
}
} catch (IncorrectOperationException ignored) {
PyUtil.showBalloon(project, PyBundle.message("QFIX.action.failed"), MessageType.WARNING);
}
}
use of com.intellij.openapi.ui.InputValidator in project intellij-community by JetBrains.
the class StringEditorDialog method promptNewKeyName.
private static String promptNewKeyName(final Project project, final PropertiesFile propFile, final String key) {
String newName;
int index = 0;
do {
index++;
newName = key + index;
} while (propFile.findPropertyByKey(newName) != null);
InputValidator validator = new InputValidator() {
public boolean checkInput(String inputString) {
return inputString.length() > 0 && propFile.findPropertyByKey(inputString) == null;
}
public boolean canClose(String inputString) {
return checkInput(inputString);
}
};
return Messages.showInputDialog(project, UIDesignerBundle.message("edit.text.unique.key.prompt"), UIDesignerBundle.message("edit.text.multiple.usages.title"), Messages.getQuestionIcon(), newName, validator);
}
use of com.intellij.openapi.ui.InputValidator in project intellij-community by JetBrains.
the class CompilerConfigurationImpl method convertPatterns.
public void convertPatterns() {
if (!needPatternConversion()) {
return;
}
try {
boolean ok;
try {
ok = doConvertPatterns();
} catch (MalformedPatternException ignored) {
ok = false;
}
if (!ok) {
final String initialPatternString = patternsToString(getRegexpPatterns());
final String message = CompilerBundle.message("message.resource.patterns.format.changed", ApplicationNamesInfo.getInstance().getProductName(), initialPatternString, CommonBundle.getOkButtonText(), CommonBundle.getCancelButtonText());
final String wildcardPatterns = Messages.showInputDialog(myProject, message, CompilerBundle.message("pattern.conversion.dialog.title"), Messages.getWarningIcon(), initialPatternString, new InputValidator() {
@Override
public boolean checkInput(String inputString) {
return true;
}
@Override
public boolean canClose(String inputString) {
final StringTokenizer tokenizer = new StringTokenizer(inputString, ";", false);
StringBuilder malformedPatterns = new StringBuilder();
while (tokenizer.hasMoreTokens()) {
String pattern = tokenizer.nextToken();
try {
addWildcardResourcePattern(pattern);
} catch (MalformedPatternException e) {
malformedPatterns.append("\n\n");
malformedPatterns.append(pattern);
malformedPatterns.append(": ");
malformedPatterns.append(e.getMessage());
}
}
if (malformedPatterns.length() > 0) {
Messages.showErrorDialog(CompilerBundle.message("error.bad.resource.patterns", malformedPatterns.toString()), CompilerBundle.message("bad.resource.patterns.dialog.title"));
removeWildcardPatterns();
return false;
}
return true;
}
});
if (wildcardPatterns == null) {
// cancel pressed
loadDefaultWildcardPatterns();
}
}
} finally {
myWildcardPatternsInitialized = true;
}
}
use of com.intellij.openapi.ui.InputValidator in project intellij-community by JetBrains.
the class PackageChooserDialog method createNewPackage.
private void createNewPackage() {
final PsiPackage selectedPackage = getTreeSelection();
if (selectedPackage == null)
return;
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, () -> {
final Runnable action = () -> {
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 PsiPackage 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);
}
use of com.intellij.openapi.ui.InputValidator in project intellij-community by JetBrains.
the class CreateNewProjectGroupAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
final InputValidator validator = new InputValidator() {
@Override
public boolean checkInput(String inputString) {
inputString = inputString.trim();
return getGroup(inputString) == null;
}
@Override
public boolean canClose(String inputString) {
return true;
}
};
final String newGroup = Messages.showInputDialog((Project) null, "Project group name", "Create New Project Group", null, null, validator);
if (newGroup != null) {
final ProjectGroup group = new ProjectGroup(newGroup);
RecentProjectsManager.getInstance().addGroup(group);
rebuildRecentProjectsList(e);
}
}
Aggregations