• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jiangxincode / ApkToolBoxGUI / #1162

06 Sep 2025 02:20AM UTC coverage: 2.955% (-0.001%) from 2.956%
#1162

push

jiangxincode
[Feature]Simplify the persist of some Swing Components 2

0 of 4 new or added lines in 3 files covered. (0.0%)

1 existing line in 1 file now uncovered.

248 of 8392 relevant lines covered (2.96%)

0.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/edu/jiangxin/apktoolbox/swing/keeper/UiStateKeeper.java
1
package edu.jiangxin.apktoolbox.swing.keeper;
2

3
import edu.jiangxin.apktoolbox.utils.Constants;
4
import edu.jiangxin.apktoolbox.utils.Utils;
5
import org.apache.commons.configuration2.Configuration;
6
import org.apache.commons.lang3.StringUtils;
7
import org.apache.commons.lang3.Strings;
8
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.Logger;
10

11
import javax.swing.*;
12
import javax.swing.text.JTextComponent;
13
import java.awt.*;
14
import java.io.File;
15
import java.util.Objects;
16
import java.util.function.BiConsumer;
17

18
public final class UiStateKeeper {
×
19
    private static final Logger logger = LogManager.getLogger(UiStateKeeper.class.getSimpleName());
×
20
    private static final Configuration conf = Utils.getConfiguration();
×
21

22
    public static void save(Container root) {
23
        if (root == null) {
×
24
            return;
×
25
        }
26
        String prefix = root.getName() == null ? root.getClass().getSimpleName() : root.getName();
×
27
        scan(root, prefix, (c, key) -> {
×
28
            String value = valueOf(c);
×
29
            if (!StringUtils.isEmpty(value)) {
×
30
                conf.setProperty(key, value);
×
31
            }
32
        });
×
33
    }
×
34

35
    public static void restore(Container root) {
36
        if (root == null) {
×
37
            return;
×
38
        }
39
        String prefix = root.getName() == null ? root.getClass().getSimpleName() : root.getName();
×
40
        scan(root, prefix, (c, key) -> {
×
41
            String value = conf.getString(key);
×
42
            if (value != null) {
×
43
                setValue(c, value);
×
44
            }
45
        });
×
46
    }
×
47

48
    private static void scan(Container root, String prefix, BiConsumer<Component, String> action) {
49
        for (Component c : SwingUtils.getAllComponents(root)) {
×
50
            String name = c.getName();
×
51
            if (StringUtils.isEmpty(name) || !name.startsWith(Constants.KEY_PREFIX)) {
×
52
                continue;
×
53
            }
54
            action.accept(c, prefix + "." + Strings.CS.remove(name, Constants.KEY_PREFIX));
×
55
        }
×
56
    }
×
57

58
    private static String valueOf(Component c) {
59
        if (c instanceof JTextComponent) {
×
UNCOV
60
            String text = ((JTextComponent) c).getText();
×
61
            logger.info("JTextComponent name={}, text={}", c.getName(), text);
×
62
            return ((JTextComponent) c).getText();
×
63
        }
64
        if (c instanceof JCheckBox) {
×
65
            return String.valueOf(((JCheckBox) c).isSelected());
×
66
        }
67
        if (c instanceof JRadioButton) {
×
68
            return String.valueOf(((JRadioButton) c).isSelected());
×
69
        }
70
        if (c instanceof JComboBox) {
×
71
            return Objects.toString(((JComboBox<?>) c).getSelectedItem(), "");
×
72
        }
73
        if (c instanceof JFileChooser) {
×
74
            return ((JFileChooser) c).getSelectedFile() == null ? "" :
×
75
                    ((JFileChooser) c).getSelectedFile().getAbsolutePath();
×
76
        }
77
        return null;
×
78
    }
79

80
    private static void setValue(Component c, String v) {
81
        try {
82
            if (c instanceof JTextComponent) {
×
83
                ((JTextComponent) c).setText(v);
×
84
            } else if (c instanceof JCheckBox) {
×
85
                ((JCheckBox) c).setSelected(Boolean.parseBoolean(v));
×
86
            } else if (c instanceof JRadioButton) {
×
87
                ((JRadioButton) c).setSelected(Boolean.parseBoolean(v));
×
88
            } else if (c instanceof JComboBox) {
×
89
                ((JComboBox<?>) c).setSelectedItem(v);
×
90
            } else if (c instanceof JFileChooser) {
×
91
                File f = new File(v);
×
92
                if (f.exists()) ((JFileChooser) c).setSelectedFile(f);
×
93
            }
94
        } catch (Exception ignored) {
×
95
            logger.error("setValue error, component name={}, value={}", c.getName(), v);
×
96
        }
×
97
    }
×
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc