|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | - |
17 | 16 | package com.intellij.java.impl.ide.actions; |
18 | 17 |
|
19 | 18 | import com.intellij.java.impl.ide.fileTemplates.JavaCreateFromTemplateHandler; |
|
24 | 23 | import com.intellij.java.language.psi.PsiClass; |
25 | 24 | import com.intellij.java.language.psi.PsiNameHelper; |
26 | 25 | import com.intellij.java.language.psi.util.PsiUtil; |
27 | | -import consulo.application.AllIcons; |
| 26 | +import consulo.annotation.access.RequiredReadAction; |
| 27 | +import consulo.annotation.component.ActionImpl; |
| 28 | +import consulo.annotation.component.ActionParentRef; |
| 29 | +import consulo.annotation.component.ActionRef; |
| 30 | +import consulo.annotation.component.ActionRefAnchor; |
28 | 31 | import consulo.application.dumb.DumbAware; |
29 | 32 | import consulo.fileTemplate.FileTemplate; |
30 | 33 | import consulo.fileTemplate.FileTemplateManager; |
|
51 | 54 | * |
52 | 55 | * @since 5.1 |
53 | 56 | */ |
| 57 | +@ActionImpl(id = "JavaNewClass", parents = @ActionParentRef(value = @ActionRef(id = "NewGroup1"), anchor = ActionRefAnchor.FIRST)) |
54 | 58 | public class CreateClassAction extends JavaCreateTemplateInPackageAction<PsiClass> implements DumbAware { |
55 | | - public CreateClassAction() { |
56 | | - super(LocalizeValue.empty(), LocalizeValue.empty(), AllIcons.Nodes.Class, true); |
57 | | - } |
58 | | - |
59 | | - @Override |
60 | | - protected void buildDialog(final Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) { |
61 | | - builder.setTitle(JavaCoreLocalize.actionCreateNewClass()); |
62 | | - builder.addKind(JavaLocalize.titleClass(), AllIcons.Nodes.Class, JavaTemplateUtil.INTERNAL_CLASS_TEMPLATE_NAME); |
63 | | - builder.addKind(JavaLocalize.titleInterface(), AllIcons.Nodes.Interface, JavaTemplateUtil.INTERNAL_INTERFACE_TEMPLATE_NAME); |
64 | | - |
65 | | - LanguageLevel level = PsiUtil.getLanguageLevel(directory); |
66 | | - if (level.isAtLeast(LanguageLevel.JDK_16)) { |
67 | | - builder.addKind(JavaLocalize.titleRecord(), PlatformIconGroup.nodesRecord(), JavaTemplateUtil.INTERNAL_RECORD_TEMPLATE_NAME); |
| 59 | + public CreateClassAction() { |
| 60 | + super(LocalizeValue.empty(), LocalizeValue.empty(), PlatformIconGroup.nodesClass(), true); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + @RequiredReadAction |
| 65 | + protected void buildDialog(final Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) { |
| 66 | + builder.setTitle(JavaCoreLocalize.actionCreateNewClass()); |
| 67 | + builder.addKind(JavaLocalize.titleClass(), PlatformIconGroup.nodesClass(), JavaTemplateUtil.INTERNAL_CLASS_TEMPLATE_NAME); |
| 68 | + builder.addKind( |
| 69 | + JavaLocalize.titleInterface(), |
| 70 | + PlatformIconGroup.nodesInterface(), |
| 71 | + JavaTemplateUtil.INTERNAL_INTERFACE_TEMPLATE_NAME |
| 72 | + ); |
| 73 | + |
| 74 | + LanguageLevel level = PsiUtil.getLanguageLevel(directory); |
| 75 | + if (level.isAtLeast(LanguageLevel.JDK_16)) { |
| 76 | + builder.addKind(JavaLocalize.titleRecord(), PlatformIconGroup.nodesRecord(), JavaTemplateUtil.INTERNAL_RECORD_TEMPLATE_NAME); |
| 77 | + } |
| 78 | + if (level.isAtLeast(LanguageLevel.JDK_1_5)) { |
| 79 | + builder.addKind(JavaLocalize.titleEnum(), PlatformIconGroup.nodesEnum(), JavaTemplateUtil.INTERNAL_ENUM_TEMPLATE_NAME); |
| 80 | + builder.addKind( |
| 81 | + JavaLocalize.titleAnnotation(), |
| 82 | + PlatformIconGroup.nodesAnnotationtype(), |
| 83 | + JavaTemplateUtil.INTERNAL_ANNOTATION_TYPE_TEMPLATE_NAME |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + for (FileTemplate template : FileTemplateManager.getInstance(project).getAllTemplates()) { |
| 88 | + JavaCreateFromTemplateHandler handler = new JavaCreateFromTemplateHandler(); |
| 89 | + if (handler.handlesTemplate(template) && JavaCreateFromTemplateHandler.canCreate(directory)) { |
| 90 | + builder.addKind(LocalizeValue.of(template.getName()), JavaFileType.INSTANCE.getIcon(), template.getName()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + builder.setValidator(new InputValidatorEx() { |
| 95 | + @Override |
| 96 | + public String getErrorText(String inputString) { |
| 97 | + if (inputString.length() > 0 && !PsiNameHelper.getInstance(project).isQualifiedName(inputString)) { |
| 98 | + return "This is not a valid Java qualified name"; |
| 99 | + } |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + @RequiredUIAccess |
| 104 | + @Override |
| 105 | + public boolean checkInput(String inputString) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + @RequiredUIAccess |
| 110 | + @Override |
| 111 | + public boolean canClose(String inputString) { |
| 112 | + return !StringUtil.isEmptyOrSpaces(inputString) && getErrorText(inputString) == null; |
| 113 | + } |
| 114 | + }); |
68 | 115 | } |
69 | | - if (level.isAtLeast(LanguageLevel.JDK_1_5)) { |
70 | | - builder.addKind(JavaLocalize.titleEnum(), AllIcons.Nodes.Enum, JavaTemplateUtil.INTERNAL_ENUM_TEMPLATE_NAME); |
71 | | - builder.addKind(JavaLocalize.titleAnnotation(), AllIcons.Nodes.Annotationtype, JavaTemplateUtil.INTERNAL_ANNOTATION_TYPE_TEMPLATE_NAME); |
| 116 | + |
| 117 | + @Override |
| 118 | + protected Class<? extends ModuleExtension> getModuleExtensionClass() { |
| 119 | + return JavaModuleExtension.class; |
72 | 120 | } |
73 | 121 |
|
74 | | - for (FileTemplate template : FileTemplateManager.getInstance(project).getAllTemplates()) { |
75 | | - final JavaCreateFromTemplateHandler handler = new JavaCreateFromTemplateHandler(); |
76 | | - if (handler.handlesTemplate(template) && JavaCreateFromTemplateHandler.canCreate(directory)) { |
77 | | - builder.addKind(LocalizeValue.of(template.getName()), JavaFileType.INSTANCE.getIcon(), template.getName()); |
78 | | - } |
| 122 | + @Nonnull |
| 123 | + @Override |
| 124 | + protected LocalizeValue getErrorTitle() { |
| 125 | + return JavaCoreLocalize.titleCannotCreateClass(); |
79 | 126 | } |
80 | 127 |
|
81 | | - builder.setValidator(new InputValidatorEx() { |
82 | | - @Override |
83 | | - public String getErrorText(String inputString) { |
84 | | - if (inputString.length() > 0 && !PsiNameHelper.getInstance(project).isQualifiedName(inputString)) { |
85 | | - return "This is not a valid Java qualified name"; |
86 | | - } |
87 | | - return null; |
88 | | - } |
89 | | - |
90 | | - @RequiredUIAccess |
91 | | - @Override |
92 | | - public boolean checkInput(String inputString) { |
93 | | - return true; |
94 | | - } |
95 | | - |
96 | | - @RequiredUIAccess |
97 | | - @Override |
98 | | - public boolean canClose(String inputString) { |
99 | | - return !StringUtil.isEmptyOrSpaces(inputString) && getErrorText(inputString) == null; |
100 | | - } |
101 | | - }); |
102 | | - } |
103 | | - |
104 | | - @Override |
105 | | - protected Class<? extends ModuleExtension> getModuleExtensionClass() { |
106 | | - return JavaModuleExtension.class; |
107 | | - } |
108 | | - |
109 | | - @Nonnull |
110 | | - @Override |
111 | | - protected LocalizeValue getErrorTitle() { |
112 | | - return JavaCoreLocalize.titleCannotCreateClass(); |
113 | | - } |
114 | | - |
115 | | - @Nonnull |
116 | | - @Override |
117 | | - protected LocalizeValue getActionName(PsiDirectory directory, String newName, String templateName) { |
118 | | - return JavaCoreLocalize.progressCreatingClass(StringUtil.getQualifiedName(JavaDirectoryService.getInstance().getPackage(directory).getQualifiedName(), newName)); |
119 | | - } |
120 | | - |
121 | | - @Override |
122 | | - protected final PsiClass doCreate(PsiDirectory dir, String className, String templateName) throws IncorrectOperationException { |
123 | | - return JavaDirectoryService.getInstance().createClass(dir, className, templateName, true); |
124 | | - } |
125 | | - |
126 | | - @Override |
127 | | - protected String removeExtension(String templateName, String className) { |
128 | | - return StringUtil.trimEnd(className, ".java"); |
129 | | - } |
130 | | - |
131 | | - @Override |
132 | | - protected PsiElement getNavigationElement(@Nonnull PsiClass createdElement) { |
133 | | - return createdElement.getLBrace(); |
134 | | - } |
135 | | - |
136 | | - @Override |
137 | | - protected void postProcess(PsiClass createdElement, String templateName, Map<String, String> customProperties) { |
138 | | - super.postProcess(createdElement, templateName, customProperties); |
139 | | - |
140 | | - moveCaretAfterNameIdentifier(createdElement); |
141 | | - } |
| 128 | + @Nonnull |
| 129 | + @Override |
| 130 | + protected LocalizeValue getActionName(PsiDirectory directory, String newName, String templateName) { |
| 131 | + return JavaCoreLocalize.progressCreatingClass(StringUtil.getQualifiedName( |
| 132 | + JavaDirectoryService.getInstance().getPackage(directory).getQualifiedName(), |
| 133 | + newName |
| 134 | + )); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected final PsiClass doCreate(PsiDirectory dir, String className, String templateName) throws IncorrectOperationException { |
| 139 | + return JavaDirectoryService.getInstance().createClass(dir, className, templateName, true); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + protected String removeExtension(String templateName, String className) { |
| 144 | + return StringUtil.trimEnd(className, ".java"); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected PsiElement getNavigationElement(@Nonnull PsiClass createdElement) { |
| 149 | + return createdElement.getLBrace(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + @RequiredUIAccess |
| 154 | + protected void postProcess(PsiClass createdElement, String templateName, Map<String, String> customProperties) { |
| 155 | + super.postProcess(createdElement, templateName, customProperties); |
| 156 | + |
| 157 | + moveCaretAfterNameIdentifier(createdElement); |
| 158 | + } |
142 | 159 | } |
0 commit comments