Skip to content

Commit c5871b0

Browse files
committed
Extracting the validation messages into a separate bundle and making them more flexible
1 parent 0591186 commit c5871b0

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed

src/com/magento/idea/magento2plugin/generation/validator/NewModuleFormValidator.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.magento.idea.magento2plugin.generation.validator;
66

77
import com.magento.idea.magento2plugin.generation.NewModuleForm;
8+
import com.magento.idea.magento2plugin.resources.ValidatorBundle;
89
import com.magento.idea.magento2plugin.util.Regex;
910
import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil;
1011

@@ -24,40 +25,40 @@ public String validate()
2425
{
2526
String packageName = form.getPackageName();
2627
if (packageName.length() == 0) {
27-
return "Package Name must not be empty.";
28+
return ValidatorBundle.message("validator.notEmpty", "Package Name");
2829
}
2930

3031
if (!packageName.matches(Regex.ALPHANUMERIC)) {
31-
return "Package Name must contain letters and numbers only.";
32+
return ValidatorBundle.message("validator.alphaNumericCharacters", "Package Name");
3233
}
3334

3435
if (!Character.isUpperCase(packageName.charAt(0)) && !Character.isDigit(packageName.charAt(0))) {
35-
return "Package Name must start from a number or a capital letter";
36+
return ValidatorBundle.message("validator.startWithNumberOrCapitalLetter", "Package Name");
3637
}
3738

3839
String moduleName = form.getModuleName();
3940
if (moduleName.length() == 0) {
40-
return "Module Name must not be empty.";
41+
return ValidatorBundle.message("validator.notEmpty", "Module Name");
4142
}
4243

4344
if (!moduleName.matches(Regex.ALPHANUMERIC)) {
44-
return "Module Name must contain letters and numbers only.";
45+
return ValidatorBundle.message("validator.alphaNumericCharacters", "Module Name");
4546
}
4647

4748
if (!Character.isUpperCase(moduleName.charAt(0)) && !Character.isDigit(moduleName.charAt(0))) {
48-
return "Module Name must start from a number or a capital letter";
49+
return ValidatorBundle.message("validator.startWithNumberOrCapitalLetter", "Module Name");
4950
}
5051

5152
if (form.getModuleVersion().length() == 0) {
52-
return "Module Version must not be empty.";
53+
return ValidatorBundle.message("validator.notEmpty", "Module Version");
5354
}
5455

5556
if (form.getModuleDescription().length() == 0) {
56-
return "Module Version must not be empty.";
57+
return ValidatorBundle.message("validator.notEmpty", "Module Description");
5758
}
5859

5960
if (!MagentoBasePathUtil.isMagentoFolderValid(form.getMagentoPath())) {
60-
return "Please specify valid magento installation path!";
61+
return ValidatorBundle.message("validator.package.validPath");
6162
}
6263

6364
return null;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.resources;
7+
8+
import com.intellij.CommonBundle;
9+
import com.intellij.openapi.util.text.StringUtil;
10+
import org.jetbrains.annotations.NonNls;
11+
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
13+
import org.jetbrains.annotations.PropertyKey;
14+
import java.util.ResourceBundle;
15+
16+
public class ValidatorBundle {
17+
/** The {@link ResourceBundle} path. */
18+
@NonNls
19+
public static final String BUNDLE_NAME = "messages.ValidatorBundle";
20+
21+
/** The {@link ResourceBundle} instance. */
22+
@NotNull
23+
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
24+
25+
/**
26+
* Available syntax list.
27+
*/
28+
public enum Syntax {
29+
GLOB, REGEXP;
30+
31+
@NonNls
32+
private static final String KEY = "syntax:";
33+
34+
@Nullable
35+
public static Syntax find(@Nullable String name) {
36+
if (name == null) {
37+
return null;
38+
}
39+
40+
try {
41+
return Syntax.valueOf(name.toUpperCase());
42+
} catch (IllegalArgumentException iae) {
43+
return null;
44+
}
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return super.toString().toLowerCase();
50+
}
51+
52+
/**
53+
* @return element presentation
54+
*/
55+
public String getPresentation() {
56+
return StringUtil.join(KEY, " ", toString());
57+
}
58+
}
59+
60+
/**
61+
* Loads a {@link String} from the {@link #BUNDLE} {@link ResourceBundle}.
62+
*
63+
* @param key the key of the resource
64+
* @param params the optional parameters for the specific resource
65+
*
66+
* @return the {@link String} value or {@code null} if no resource found for the key
67+
*/
68+
public static String message(@PropertyKey(resourceBundle = BUNDLE_NAME) String key, Object... params) {
69+
return CommonBundle.message(BUNDLE, key, params);
70+
}
71+
72+
/**
73+
* Loads a {@link String} from the {@link #BUNDLE} {@link ResourceBundle}.
74+
*
75+
* @param key the key of the resource
76+
* @param defaultValue the default value that will be returned if there is nothing set
77+
* @param params the optional parameters for the specific resource
78+
*
79+
* @return the {@link String} value or {@code null} if no resource found for the key
80+
*/
81+
public static String messageOrDefault(
82+
@PropertyKey(resourceBundle = BUNDLE_NAME) String key,
83+
String defaultValue,
84+
Object... params
85+
) {
86+
return CommonBundle.messageOrDefault(BUNDLE, key, defaultValue, params);
87+
}
88+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
validator.notEmpty={0} must not be empty
2+
validator.package.validPath=Please specify a valid Magento 2 installation path!
3+
validator.alphaNumericCharacters={0} must contain letters and numbers only
4+
validator.startWithNumberOrCapitalLetter={0} must start from a number or a capital letter

0 commit comments

Comments
 (0)