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

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

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

0.0
/exist-core/src/main/java/org/exist/client/tristatecheckbox/TristateCheckBox.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.client.tristatecheckbox;
25

26
import javax.swing.*;
27
import javax.swing.plaf.metal.MetalIconFactory;
28
import java.awt.*;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31

32
/**
33
 * See <a href="https://stackoverflow.com/questions/1263323/tristate-checkboxes-in-java">Tristate Checkboxes in Java</a>
34
 *
35
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
36
 */
37
public class TristateCheckBox extends JCheckBox implements Icon, ActionListener {
38

39
    static final boolean INDETERMINATE_AS_SELECTED = true;  //consider INDETERMINATE as selected ?
40
    static final Icon icon = MetalIconFactory.getCheckBoxIcon();
×
41

42
    public TristateCheckBox() { this(""); }
×
43

44
    public TristateCheckBox(final String text) {
45
        this(text, TristateState.DESELECTED);
×
46
    }
×
47

48
    public TristateCheckBox(final String text, final TristateState state) {
49
        /* tri-state checkbox has 3 selection states:
50
         * 0 unselected
51
         * 1 mid-state selection
52
         * 2 fully selected
53
         */
54
        super(text, state == TristateState.SELECTED);
×
55

56
        switch (state) {
×
57
            case SELECTED: setSelected(true);
×
58
            case INDETERMINATE:
59
            case DESELECTED:
60
                putClientProperty("SelectionState", state);
×
61
                break;
×
62
            default:
63
                throw new IllegalArgumentException();
×
64
        }
65
        addActionListener(this);
×
66
        setIcon(this);
×
67
    }
×
68

69
    @Override
70
    public boolean isSelected() {
71
        if (INDETERMINATE_AS_SELECTED && (getSelectionState() != TristateState.DESELECTED)) {
×
72
            return true;
×
73
        } else {
74
            return super.isSelected();
×
75
        }
76
    }
77

78
    public TristateState getSelectionState() {
79
        return (getClientProperty("SelectionState") != null ? (TristateState) getClientProperty("SelectionState") :
×
80
                super.isSelected() ? TristateState.SELECTED : TristateState.DESELECTED);
×
81
    }
82

83
    public void setSelectionState(final TristateState state) {
84
        switch (state) {
×
85
            case SELECTED: setSelected(true);
×
86
                break;
×
87
            case INDETERMINATE:
88
            case DESELECTED: setSelected(false);
×
89
                break;
×
90
            default:
91
                throw new IllegalArgumentException();
×
92
        }
93
        putClientProperty("SelectionState", state);
×
94
    }
×
95

96
    @Override
97
    public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
98
        icon.paintIcon(c, g, x, y);
×
99
        if (getSelectionState() != TristateState.INDETERMINATE) {
×
100
            return;
×
101
        }
102

103
        final int w = getIconWidth();
×
104
        final int h = getIconHeight();
×
105
        g.setColor(c.isEnabled() ? new Color(51, 51, 51) : new Color(122, 138, 153));
×
106
        g.fillRect(x+4, y+4, w-8, h-8);
×
107

108
        if (!c.isEnabled()) {
×
109
            return;
×
110
        }
111
        g.setColor(new Color(81, 81, 81));
×
112
        g.drawRect(x+4, y+4, w-9, h-9);
×
113
    }
×
114

115
    @Override
116
    public int getIconWidth() {
117
        return icon.getIconWidth();
×
118
    }
119

120
    @Override
121
    public int getIconHeight() {
122
        return icon.getIconHeight();
×
123
    }
124

125
    public void actionPerformed(final ActionEvent e) {
126
        final TristateCheckBox tcb = (TristateCheckBox) e.getSource();
×
127
        if (tcb.getSelectionState() == TristateState.DESELECTED) {
×
128
            tcb.setSelected(false);
×
129
        }
130

131
        tcb.putClientProperty("SelectionState", tcb.getSelectionState() == TristateState.SELECTED ? TristateState.DESELECTED :
×
132
                tcb.getSelectionState().next());
×
133

134
//        // test
135
//        System.out.println(">>>>IS SELECTED: "+tcb.isSelected());
136
//        System.out.println(">>>>IN MID STATE: "+(tcb.getSelectionState() == TristateState.INDETERMINATE));
137
    }
×
138
}
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

© 2025 Coveralls, Inc