IDEA-216075 Convert New File action to the same popup design as New Class

GitOrigin-RevId: 601c3933d54d40eaaf9d1e226cff9b667825a082
This commit is contained in:
Mikhail Sokolov
2019-07-02 06:52:16 +03:00
committed by intellij-monorepo-bot
parent 61d6e6937d
commit 9f8a7ac6fd
4 changed files with 90 additions and 18 deletions
@@ -4,11 +4,15 @@ package com.intellij.ide.actions;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.IdeView;
import com.intellij.ide.ui.newItemPopup.NewItemPopupUtil;
import com.intellij.ide.ui.newItemPopup.NewItemSimplePopupPanel;
import com.intellij.ide.util.DirectoryChooserUtil;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.Experiments;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
@@ -16,6 +20,9 @@ import com.intellij.psi.impl.file.PsiDirectoryFactory;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.function.Consumer;
public class CreateDirectoryOrPackageAction extends AnAction implements DumbAware {
public CreateDirectoryOrPackageAction() {
super(IdeBundle.message("action.create.new.directory.or.package"), IdeBundle.message("action.create.new.directory.or.package"), null);
@@ -45,11 +52,18 @@ public class CreateDirectoryOrPackageAction extends AnAction implements DumbAwar
}
String initialText = validator.getInitialText();
Messages.showInputDialog(project, message, title, null, initialText, validator, TextRange.from(initialText.length(), 0));
Consumer<PsiElement> consumer = element -> {
if (element != null) {
view.selectElement(element);
}
};
final PsiElement result = validator.getCreatedElement();
if (result != null) {
view.selectElement(result);
if (Experiments.isFeatureEnabled("show.create.new.element.in.popup")) {
createLightWeightPopup(title, initialText, validator, consumer).showCenteredInCurrentWindow(project);
}
else {
Messages.showInputDialog(project, message, title, null, initialText, validator, TextRange.from(initialText.length(), 0));
consumer.accept(validator.getCreatedElement());
}
}
@@ -95,4 +109,24 @@ public class CreateDirectoryOrPackageAction extends AnAction implements DumbAwar
presentation.setIcon(PlatformIcons.FOLDER_ICON);
}
}
private static JBPopup createLightWeightPopup(String title, String initialText, CreateGroupHandler validator, Consumer<PsiElement> consumer) {
NewItemSimplePopupPanel contentPanel = new NewItemSimplePopupPanel();
JTextField nameField = contentPanel.getTextField();
nameField.setText(initialText);
JBPopup popup = NewItemPopupUtil.createNewItemPopup(title, contentPanel, nameField);
contentPanel.setApplyAction(event -> {
String name = nameField.getText();
if (validator.checkInput(name) && validator.canClose(name)) {
popup.closeOk(event);
consumer.accept(validator.getCreatedElement());
}
else {
String errorMessage = validator.getErrorText(name);
contentPanel.setError(errorMessage);
}
});
return popup;
}
}
@@ -84,7 +84,7 @@ public class CreateFileAction extends CreateElementActionBase implements DumbAwa
}
private JBPopup createLightWeightPopup(MyInputValidator validator, Consumer<PsiElement[]> consumer) {
SimpleCreateFileDialogPanel contentPanel = new SimpleCreateFileDialogPanel();
NewItemSimplePopupPanel contentPanel = new NewItemSimplePopupPanel();
JTextField nameField = contentPanel.getTextField();
JBPopup popup = NewItemPopupUtil.createNewItemPopup(IdeBundle.message("title.new.file"), contentPanel, nameField);
contentPanel.setApplyAction(event -> {
@@ -263,11 +263,4 @@ public class CreateFileAction extends CreateElementActionBase implements DumbAwa
return super.canClose(getFileName(inputString));
}
}
private static class SimpleCreateFileDialogPanel extends NewItemSimplePopupPanel {
public JTextField getTextField() {
return myTextField;
}
}
}
@@ -73,6 +73,10 @@ public class NewItemSimplePopupPanel extends JBPanel implements Disposable {
if (myErrorPopup != null && !myErrorPopup.isDisposed()) Disposer.dispose(myErrorPopup);
}
public JTextField getTextField() {
return myTextField;
}
@NotNull
protected ExtendableTextField createTextField() {
ExtendableTextField res = new ExtendableTextField();
@@ -7,18 +7,25 @@ import com.intellij.ide.actions.CreateDirectoryOrPackageHandler;
import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.fileTemplates.FileTemplateUtil;
import com.intellij.ide.ui.newItemPopup.NewItemPopupUtil;
import com.intellij.ide.ui.newItemPopup.NewItemSimplePopupPanel;
import com.intellij.ide.util.DirectoryChooserUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.application.Experiments;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.psi.*;
import com.jetbrains.python.PyNames;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.function.Consumer;
/**
* @author yole
*/
@@ -48,13 +55,27 @@ public class CreatePackageAction extends DumbAwareAction {
}
}
};
Messages.showInputDialog(project, IdeBundle.message("prompt.enter.new.package.name"),
IdeBundle.message("title.new.package"),
Messages.getQuestionIcon(), "", validator);
final PsiFileSystemItem result = validator.getCreatedElement();
if (result != null) {
view.selectElement(result);
Consumer<PsiFileSystemItem> consumer = item -> {
if (item != null) {
view.selectElement(item);
}
};
if (Experiments.isFeatureEnabled("show.create.new.element.in.popup")) {
JBPopup popup = createLightWeightPopup(validator, consumer);
if (project != null) {
popup.showCenteredInCurrentWindow(project);
}
else {
popup.showInFocusCenter();
}
}
else {
Messages.showInputDialog(project, IdeBundle.message("prompt.enter.new.package.name"), IdeBundle.message("title.new.package"), Messages.getQuestionIcon(), "", validator);
consumer.accept(validator.getCreatedElement());
}
}
public static void createInitPyInHierarchy(PsiDirectory created, PsiDirectory ancestor) {
@@ -64,6 +85,26 @@ public class CreatePackageAction extends DumbAwareAction {
} while(created != null && !created.equals(ancestor));
}
private static JBPopup createLightWeightPopup(CreateDirectoryOrPackageHandler validator,
Consumer<PsiFileSystemItem> consumer) {
NewItemSimplePopupPanel contentPanel = new NewItemSimplePopupPanel();
JTextField nameField = contentPanel.getTextField();
JBPopup popup = NewItemPopupUtil.createNewItemPopup(IdeBundle.message("title.new.package"), contentPanel, nameField);
contentPanel.setApplyAction(event -> {
String name = nameField.getText();
if (validator.checkInput(name) && validator.canClose(name)) {
popup.closeOk(event);
consumer.accept(validator.getCreatedElement());
}
else {
String errorMessage = validator.getErrorText(name);
contentPanel.setError(errorMessage);
}
});
return popup;
}
private static void createInitPy(PsiDirectory directory) {
final FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance(directory.getProject());
final FileTemplate template = fileTemplateManager.getInternalTemplate("Python Script");