1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package ui.view.component;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.Icon;
- import javax.swing.JCheckBox;
- import javax.swing.UIManager;
- public class TrippleCheckBox extends JCheckBox implements Icon, ActionListener {
- public enum State {
- unselected, mid_state_selection, selected
- };
- State state;
- final static Icon icon = UIManager.getIcon("CheckBox.icon");
- public TrippleCheckBox() {
- this("", State.unselected);
- }
- public TrippleCheckBox(String text) {
- this(text, State.unselected);
- }
- public TrippleCheckBox(String text, State selection) {
- super(text, selection == State.selected);
- setSelectionState(selection);
- addActionListener(this);
- setIcon(this);
- }
- @Override
- public boolean isSelected() {
- return (getSelectionState() == State.selected);
- }
- public State getSelectionState() {
- return state;
- }
- public void setSelectionState(State selection) {
- state = selection;
- setSelected(state == State.selected);
- repaint();
- }
- @Override
- public void paintIcon(Component c, Graphics g, int x, int y) {
- icon.paintIcon(c, g, x, y);
- if (getSelectionState() != State.mid_state_selection)
- return;
- int w = getIconWidth();
- int h = getIconHeight();
- g.setColor(Color.white);
- g.fillRect(x + 1, y + 1, w - 2, h - 2);
- g.setColor(c.isEnabled() ? new Color(51, 51, 51) : new Color(122, 138, 153));
- g.fillRect(x + 4, y + 4, w - 8, h - 8);
- if (!c.isEnabled())
- return;
- g.setColor(new Color(81, 81, 81));
- g.drawRect(x + 4, y + 4, w - 9, h - 9);
- }
- @Override
- public int getIconWidth() {
- return icon.getIconWidth();
- }
- @Override
- public int getIconHeight() {
- return icon.getIconHeight();
- }
- public void actionPerformed(ActionEvent e) {
- switch (getSelectionState()) {
- case unselected:
- setSelectionState(State.selected);
- break;
- case mid_state_selection:
- setSelectionState(State.unselected);
- break;
- case selected:
- setSelectionState(State.unselected);
- break;
- }
- }
- }
|