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

jiangxincode / ApkToolBoxGUI / #631

02 Mar 2024 04:18PM UTC coverage: 3.216% (-0.02%) from 3.232%
#631

push

jiangxincode
fix codefactor issues

0 of 180 new or added lines in 11 files covered. (0.0%)

3 existing lines in 2 files now uncovered.

240 of 7463 relevant lines covered (3.22%)

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/extend/autocomplete/AutoCompleter.java
1
package edu.jiangxin.apktoolbox.swing.extend.autocomplete;
2

3
import org.apache.commons.lang3.StringUtils;
4

5
import javax.swing.*;
6
import java.awt.*;
7
import java.awt.event.KeyEvent;
8
import java.awt.event.KeyListener;
9
import java.util.ArrayList;
10
import java.util.List;
11

12
class AutoCompleter<E> implements KeyListener {
13
    private JComboBox<E> comboBox;
14
    private JTextField textField;
15
    private ComboBoxModel<E> comboBoxModel;
16

NEW
17
    public AutoCompleter(JComboBox<E> comboBox) {
×
NEW
18
        if (comboBox == null) {
×
NEW
19
            throw new AutoCompleterInitException("comboBox is null");
×
20
        }
NEW
21
        this.comboBox = comboBox;
×
NEW
22
        ComboBoxEditor comboBoxEditor = comboBox.getEditor();
×
NEW
23
        if (comboBoxEditor == null) {
×
NEW
24
            throw new AutoCompleterInitException("comboBoxEditor is null");
×
25
        }
NEW
26
        Component component = comboBoxEditor.getEditorComponent();
×
NEW
27
        if (!(component instanceof JTextField)) {
×
NEW
28
            throw new AutoCompleterInitException("component is invalid");
×
29
        }
NEW
30
        this.textField = (JTextField) component;
×
NEW
31
        this.textField.addKeyListener(this);
×
NEW
32
        this.comboBoxModel = comboBox.getModel();
×
NEW
33
    }
×
34

35
    @Override
36
    public void keyTyped(KeyEvent e) {
37
        // do nothing
NEW
38
    }
×
39

40
    @Override
41
    public void keyPressed(KeyEvent e) {
42
        // do nothing
NEW
43
    }
×
44

45
    @Override
46
    public void keyReleased(KeyEvent e) {
NEW
47
        char keyChar = e.getKeyChar();
×
NEW
48
        if (Character.isAlphabetic(keyChar) || Character.isDigit(keyChar) || Character.isWhitespace(keyChar)
×
49
                || keyChar == KeyEvent.VK_BACK_SPACE) {
NEW
50
            String text = textField.getText();
×
NEW
51
            autoComplete(text);
×
NEW
52
            textField.setText(text);
×
53
        }
NEW
54
    }
×
55

56
    protected void autoComplete(String text) {
NEW
57
        E[] opts = getMatchingOptions(text);
×
NEW
58
        comboBox.setModel(new DefaultComboBoxModel<>(opts));
×
NEW
59
        if (comboBox.getItemCount() > 0) {
×
NEW
60
            comboBox.showPopup();
×
61
        }
NEW
62
    }
×
63

64
    @SuppressWarnings("unchecked")
65
    private E[] getMatchingOptions(String text) {
NEW
66
        List<E> list = new ArrayList<>();
×
NEW
67
        ComboBoxModel<E> tmp = this.comboBoxModel;
×
NEW
68
        for (int i = 0; i < tmp.getSize(); i++) {
×
NEW
69
            E itemObj = tmp.getElementAt(i);
×
NEW
70
            if (StringUtils.isEmpty(text) || StringUtils.containsIgnoreCase(itemObj.toString(), text)) {
×
NEW
71
                list.add(itemObj);
×
72
            }
73
        }
NEW
74
        return (E[]) list.toArray();
×
75
    }
76

77
}
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