|
| 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 | +} |
0 commit comments