I18nViewUtil.java

1
package com.jsql.view.swing.util;
2
3
import com.jsql.util.I18nUtil;
4
import com.jsql.util.StringUtil;
5
import com.jsql.view.swing.dialog.DialogAbout;
6
import com.jsql.view.swing.text.JPlaceholder;
7
import com.jsql.view.swing.text.JToolTipI18n;
8
import com.jsql.view.swing.tree.model.NodeModelEmpty;
9
import org.apache.commons.lang3.StringUtils;
10
11
import javax.swing.*;
12
import javax.swing.text.JTextComponent;
13
import java.util.*;
14
import java.util.function.BiConsumer;
15
16
public class I18nViewUtil {
17
18
    /**
19
     * A list of graphical components for each i18n keys in the main properties
20
     */
21
    private static final Map<String, Set<Object>> componentsLocalized = new HashMap<>();
22
    
23
    // Initialize the list of graphical components
24
    static {
25
        for (String keyI18n: I18nUtil.BUNDLE_ROOT.keySet()) {
26
            I18nViewUtil.componentsLocalized.put(keyI18n, new HashSet<>());
27
        }
28
    }
29
30
    private I18nViewUtil() {
31
        // Utility class
32
    }
33
    
34
    /**
35
     * Return the i18n keys of components whose text is replaced
36
     * when the translation changes.
37
     * @return a set of key names of an i18n key in the properties
38
     */
39
    public static Set<String> keys() {
40 1 1. keys : replaced return value with Collections.emptySet for com/jsql/view/swing/util/I18nViewUtil::keys → NO_COVERAGE
        return I18nViewUtil.componentsLocalized.keySet();
41
    }
42
    
43
    /**
44
     * Get a list of graphical components whose text corresponds
45
     * to the i18n key in the properties.
46
     * @param key name of an i18n key in the properties
47
     * @return set of graphical components
48
     */
49
    public static Set<Object> componentsByKey(String key) {
50 1 1. componentsByKey : replaced return value with Collections.emptySet for com/jsql/view/swing/util/I18nViewUtil::componentsByKey → NO_COVERAGE
        return I18nViewUtil.componentsLocalized.get(key);
51
    }
52
53
    public static void switchI18nComponents() {
54
        Map<Class<?>, BiConsumer<Object, String>> classHandlers = new LinkedHashMap<>();  // key order required
55 1 1. lambda$switchI18nComponents$0 : removed call to com/jsql/view/swing/text/JPlaceholder::setPlaceholderText → NO_COVERAGE
        classHandlers.put(JPlaceholder.class, (c, s) -> ((JPlaceholder) c).setPlaceholderText(I18nUtil.valueByKey(s)));
56 1 1. lambda$switchI18nComponents$1 : removed call to com/jsql/view/swing/dialog/DialogAbout::setTitle → NO_COVERAGE
        classHandlers.put(DialogAbout.class, (c, s) -> ((DialogAbout) c).setTitle(I18nUtil.valueByKey(s) + " " + StringUtil.APP_NAME));
57 1 1. lambda$switchI18nComponents$2 : removed call to com/jsql/view/swing/text/JToolTipI18n::setText → NO_COVERAGE
        classHandlers.put(JToolTipI18n.class, (c, s) -> ((JToolTipI18n) c).setText(I18nViewUtil.valueByKey(s)));
58 1 1. lambda$switchI18nComponents$3 : removed call to com/jsql/view/swing/tree/model/NodeModelEmpty::setText → NO_COVERAGE
        classHandlers.put(NodeModelEmpty.class, (c, s) -> ((NodeModelEmpty) c).setText(I18nViewUtil.valueByKey(s)));
59 1 1. lambda$switchI18nComponents$4 : removed call to javax/swing/JLabel::setText → NO_COVERAGE
        classHandlers.put(JLabel.class, (c, s) -> ((JLabel) c).setText(I18nViewUtil.valueByKey(s)));
60 1 1. lambda$switchI18nComponents$5 : removed call to javax/swing/JMenuItem::setText → NO_COVERAGE
        classHandlers.put(JMenuItem.class, (c, s) -> ((JMenuItem) c).setText(I18nViewUtil.valueByKey(s)));
61 1 1. lambda$switchI18nComponents$6 : removed call to javax/swing/JButton::setText → NO_COVERAGE
        classHandlers.put(JButton.class, (c, s) -> ((JButton) c).setText(I18nViewUtil.valueByKey(s)));
62 1 1. lambda$switchI18nComponents$7 : removed call to javax/swing/JComboBox::setToolTipText → NO_COVERAGE
        classHandlers.put(JComboBox.class, (c, s) -> ((JComboBox<?>) c).setToolTipText(I18nViewUtil.valueByKey(s)));
63 1 1. lambda$switchI18nComponents$8 : removed call to javax/swing/text/JTextComponent::setText → NO_COVERAGE
        classHandlers.put(JTextComponent.class, (c, s) -> ((JTextComponent) c).setText(I18nViewUtil.valueByKey(s))); // fallback
64
        for (String key : I18nViewUtil.keys()) {
65
            for (Object component : I18nViewUtil.componentsByKey(key)) {
66
                classHandlers.entrySet().stream()
67 2 1. lambda$switchI18nComponents$9 : replaced boolean return with false for com/jsql/view/swing/util/I18nViewUtil::lambda$switchI18nComponents$9 → NO_COVERAGE
2. lambda$switchI18nComponents$9 : replaced boolean return with true for com/jsql/view/swing/util/I18nViewUtil::lambda$switchI18nComponents$9 → NO_COVERAGE
                .filter(entry -> entry.getKey().isInstance(component))
68
                .findFirst()
69 2 1. switchI18nComponents : removed call to java/util/Optional::ifPresent → NO_COVERAGE
2. lambda$switchI18nComponents$10 : removed call to java/util/function/BiConsumer::accept → NO_COVERAGE
                .ifPresent(entry -> entry.getValue().accept(component, key));
70
            }
71
        }
72
    }
73
    
74
    /**
75
     * Add a graphical component to those whose text must be changed when
76
     * the language changes.
77
     * @param key name of an i18n key in the properties
78
     * @param component graphical component which will receive the translated text
79
     */
80
    public static void addComponentForKey(String key, Object component) {
81
        I18nViewUtil.componentsLocalized.get(key.replace(" ", "_")).add(component);  // e.g BIND BINARY
82
    }
83
84
    /**
85
     * Return the text corresponding to an i18n key in the properties.
86
     * @param key an i18n key in the properties
87
     * @return text corresponding to the key
88
     */
89
    public static String valueByKey(String key) {
90 2 1. valueByKey : replaced return value with "" for com/jsql/view/swing/util/I18nViewUtil::valueByKey → NO_COVERAGE
2. valueByKey : negated conditional → NO_COVERAGE
        return I18nViewUtil.isNonUbuntu(I18nUtil.getCurrentLocale())
91
        ? I18nViewUtil.formatNonLatin(I18nUtil.valueByKey(key))
92
        : I18nUtil.valueByKey(key);
93
    }
94
95
    public static boolean isNonUbuntu(Locale locale) {
96 2 1. isNonUbuntu : negated conditional → NO_COVERAGE
2. isNonUbuntu : replaced boolean return with true for com/jsql/view/swing/util/I18nViewUtil::isNonUbuntu → NO_COVERAGE
        return Locale.forLanguageTag("zh").getLanguage().equals(locale.getLanguage())
97 1 1. isNonUbuntu : negated conditional → NO_COVERAGE
        || Locale.forLanguageTag("ko").getLanguage().equals(locale.getLanguage())
98 1 1. isNonUbuntu : negated conditional → NO_COVERAGE
        || Locale.forLanguageTag("ja").getLanguage().equals(locale.getLanguage());
99
    }
100
101
    public static String formatNonLatin(String label) {
102 1 1. formatNonLatin : replaced return value with "" for com/jsql/view/swing/util/I18nViewUtil::formatNonLatin → NO_COVERAGE
        return I18nViewUtil.formatNonLatin(label, StringUtils.EMPTY);
103
    }
104
105
    public static String formatNonLatin(String label, String custom) {
106 1 1. formatNonLatin : replaced return value with "" for com/jsql/view/swing/util/I18nViewUtil::formatNonLatin → NO_COVERAGE
        return String.format(
107
            "<html><span style=\"font-family:'%s';%s\">%s</span></html>",
108
            UiUtil.FONT_NAME_MONO_ASIAN,
109
            custom,
110
            label
111
        );
112
    }
113
}

Mutations

40

1.1
Location : keys
Killed by : none
replaced return value with Collections.emptySet for com/jsql/view/swing/util/I18nViewUtil::keys → NO_COVERAGE

50

1.1
Location : componentsByKey
Killed by : none
replaced return value with Collections.emptySet for com/jsql/view/swing/util/I18nViewUtil::componentsByKey → NO_COVERAGE

55

1.1
Location : lambda$switchI18nComponents$0
Killed by : none
removed call to com/jsql/view/swing/text/JPlaceholder::setPlaceholderText → NO_COVERAGE

56

1.1
Location : lambda$switchI18nComponents$1
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogAbout::setTitle → NO_COVERAGE

57

1.1
Location : lambda$switchI18nComponents$2
Killed by : none
removed call to com/jsql/view/swing/text/JToolTipI18n::setText → NO_COVERAGE

58

1.1
Location : lambda$switchI18nComponents$3
Killed by : none
removed call to com/jsql/view/swing/tree/model/NodeModelEmpty::setText → NO_COVERAGE

59

1.1
Location : lambda$switchI18nComponents$4
Killed by : none
removed call to javax/swing/JLabel::setText → NO_COVERAGE

60

1.1
Location : lambda$switchI18nComponents$5
Killed by : none
removed call to javax/swing/JMenuItem::setText → NO_COVERAGE

61

1.1
Location : lambda$switchI18nComponents$6
Killed by : none
removed call to javax/swing/JButton::setText → NO_COVERAGE

62

1.1
Location : lambda$switchI18nComponents$7
Killed by : none
removed call to javax/swing/JComboBox::setToolTipText → NO_COVERAGE

63

1.1
Location : lambda$switchI18nComponents$8
Killed by : none
removed call to javax/swing/text/JTextComponent::setText → NO_COVERAGE

67

1.1
Location : lambda$switchI18nComponents$9
Killed by : none
replaced boolean return with false for com/jsql/view/swing/util/I18nViewUtil::lambda$switchI18nComponents$9 → NO_COVERAGE

2.2
Location : lambda$switchI18nComponents$9
Killed by : none
replaced boolean return with true for com/jsql/view/swing/util/I18nViewUtil::lambda$switchI18nComponents$9 → NO_COVERAGE

69

1.1
Location : switchI18nComponents
Killed by : none
removed call to java/util/Optional::ifPresent → NO_COVERAGE

2.2
Location : lambda$switchI18nComponents$10
Killed by : none
removed call to java/util/function/BiConsumer::accept → NO_COVERAGE

90

1.1
Location : valueByKey
Killed by : none
replaced return value with "" for com/jsql/view/swing/util/I18nViewUtil::valueByKey → NO_COVERAGE

2.2
Location : valueByKey
Killed by : none
negated conditional → NO_COVERAGE

96

1.1
Location : isNonUbuntu
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : isNonUbuntu
Killed by : none
replaced boolean return with true for com/jsql/view/swing/util/I18nViewUtil::isNonUbuntu → NO_COVERAGE

97

1.1
Location : isNonUbuntu
Killed by : none
negated conditional → NO_COVERAGE

98

1.1
Location : isNonUbuntu
Killed by : none
negated conditional → NO_COVERAGE

102

1.1
Location : formatNonLatin
Killed by : none
replaced return value with "" for com/jsql/view/swing/util/I18nViewUtil::formatNonLatin → NO_COVERAGE

106

1.1
Location : formatNonLatin
Killed by : none
replaced return value with "" for com/jsql/view/swing/util/I18nViewUtil::formatNonLatin → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.19.1