ludc
2024-09-14 36c2449aec5b51e5ed4e5c6841154b746060e09a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.vci.client.ui.swing;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ActionMapUIResource;
 
public class TristateCheckBox extends JCheckBox{
 
    /**
     * 
     */
    private static final long serialVersionUID = -6160864065526319995L;
 
    /** This is a type-safe enumerated type */
    public static class State { private State() { } }
    public static final State NOT_SELECTED = new State();
    public static final State SELECTED = new State();
    public static final State DONT_CARE = new State();
    
    private final TristateDecorator model;
    
    public TristateCheckBox(String text, Icon icon, State initial){
        super(text, icon);
        //Add a listener for when the mouse is pressed
        super.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                grabFocus();
                model.nextState();
            }
        });
        
        //Reset the keyboard action map
        ActionMap map = new ActionMapUIResource();
        map.put("pressed", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                grabFocus();
                model.nextState();
            }
        });
        map.put("released", null);
        SwingUtilities.replaceUIActionMap(this, map);
        model = new TristateDecorator(getModel());
        setModel(model);
        setState(initial);
    }
    
    public TristateCheckBox(String text, State initial) {
        this(text, null, initial);
    }
    public TristateCheckBox(String text) {
        this(text, DONT_CARE);
    }
    public TristateCheckBox() {
        this(null);
    }
    
    /**
     * Set the new state to either SELECTED, NOT_SELECTED or
     * DONT_CARE.  If state == null, it is treated as DONT_CARE.
     * @param state
     */
    public void setState(State state) {
        model.setState(state);
    }
    
    /**
     * Return the current state, which is determined by the
     * selection status of the model.
     * @return
     */
    public State getState() {
        return model.getState();
    }
    
    public void setSelected(boolean b) {
        if (b) {
            setState(SELECTED);
        } else {
            setState(NOT_SELECTED);
        }
    }
    
    private class TristateDecorator implements ButtonModel {
        private final ButtonModel other;
        private TristateDecorator(ButtonModel other) {
            this.other = other;
        }
        
        private void setState(State state) {
            if (state == NOT_SELECTED) { 
                other.setArmed(false);
                setPressed(false);
                setSelected(false);
            } else if (state == SELECTED) {
                other.setArmed(false);
                setPressed(false);
                setSelected(true);
            } else { // either "null" or DONT_CARE
                other.setArmed(true);
                setPressed(true);
                setSelected(false);
            }
        }
        
        /**
         * The current state is embedded in the selection / armed
         * state of the model.
         * 
         * We return the SELECTED state when the checkbox is selected
         * but not armed, DONT_CARE state when the checkbox is
         * selected and armed (grey) and NOT_SELECTED when the
         * checkbox is deselected.
         * 
         * @return
         */
        private State getState() {
            if (isSelected() && !isArmed()) {
                // normal black tick
                return SELECTED;
            } else if (isSelected() && isArmed()) {
                // don't care grey tick
                return DONT_CARE;
            } else {
                // normal deselected
                return NOT_SELECTED;
            }
        }
        
        /**
         * We rotate between NOT_SELECTED, SELECTED and DONT_CARE
         *
         */
        private void nextState() {
            State current = getState();
            if (current == NOT_SELECTED) {
                setState(SELECTED);
            } else if (current == SELECTED) {
                setState(DONT_CARE);
            } else if (current == DONT_CARE) {
                setState(NOT_SELECTED);
            }
        }
        
        /**
         * Filter: No one may change the armed status except us. 
         */
        public void setArmed(boolean b) {
        }
        
        /**
         * We disable focusing on the component when it is not
         * enabled.
         */
        public void setEnabled(boolean b) {
            setFocusable(b);
            other.setEnabled(b);
        }
        
        /** All these methods simply delegate to the "other" model
         * that is being decorated. */
        public boolean isArmed() { return other.isArmed(); }
        public boolean isSelected() { return other.isSelected(); }
        public boolean isEnabled() { return other.isEnabled(); }
        public boolean isPressed() { return other.isPressed(); }
        public boolean isRollover() { return other.isRollover(); }
        public void setSelected(boolean b) { other.setSelected(b); }
        public void setPressed(boolean b) { other.setPressed(b); }
        public void setRollover(boolean b) { other.setRollover(b); }
        public void setMnemonic(int key) { other.setMnemonic(key); }
        public int getMnemonic() { return other.getMnemonic(); }
        public void setActionCommand(String s) {
            other.setActionCommand(s);
        }
        public String getActionCommand() {
            return other.getActionCommand();
        }
        public void setGroup(ButtonGroup group) {
            other.setGroup(group);
        }
        public void addActionListener(ActionListener l) {
            other.addActionListener(l);
        }
        public void removeActionListener(ActionListener l) {
            other.removeActionListener(l);
        }
        public void addItemListener(ItemListener l) {
            other.addItemListener(l);
        }
        public void removeItemListener(ItemListener l) {
            other.removeItemListener(l);
        }
        public void addChangeListener(ChangeListener l) {
            other.addChangeListener(l);
        }
        public void removeChangeListener(ChangeListener l) {
            other.removeChangeListener(l);
        }
        public Object[] getSelectedObjects() {
            return other.getSelectedObjects();
        }
    }
}