TrippleCheckBox.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package ui.view.component;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.Icon;
  8. import javax.swing.JCheckBox;
  9. import javax.swing.UIManager;
  10. public class TrippleCheckBox extends JCheckBox implements Icon, ActionListener {
  11. public enum State {
  12. unselected, mid_state_selection, selected
  13. };
  14. State state;
  15. final static Icon icon = UIManager.getIcon("CheckBox.icon");
  16. public TrippleCheckBox() {
  17. this("", State.unselected);
  18. }
  19. public TrippleCheckBox(String text) {
  20. this(text, State.unselected);
  21. }
  22. public TrippleCheckBox(String text, State selection) {
  23. super(text, selection == State.selected);
  24. setSelectionState(selection);
  25. addActionListener(this);
  26. setIcon(this);
  27. }
  28. @Override
  29. public boolean isSelected() {
  30. return (getSelectionState() == State.selected);
  31. }
  32. public State getSelectionState() {
  33. return state;
  34. }
  35. public void setSelectionState(State selection) {
  36. state = selection;
  37. setSelected(state == State.selected);
  38. }
  39. @Override
  40. public void paintIcon(Component c, Graphics g, int x, int y) {
  41. icon.paintIcon(c, g, x, y);
  42. if (getSelectionState() != State.mid_state_selection)
  43. return;
  44. int w = getIconWidth();
  45. int h = getIconHeight();
  46. g.setColor(Color.white);
  47. g.fillRect(x + 1, y + 1, w - 2, h - 2);
  48. g.setColor(c.isEnabled() ? new Color(51, 51, 51) : new Color(122, 138, 153));
  49. g.fillRect(x + 4, y + 4, w - 8, h - 8);
  50. if (!c.isEnabled())
  51. return;
  52. g.setColor(new Color(81, 81, 81));
  53. g.drawRect(x + 4, y + 4, w - 9, h - 9);
  54. }
  55. @Override
  56. public int getIconWidth() {
  57. return icon.getIconWidth();
  58. }
  59. @Override
  60. public int getIconHeight() {
  61. return icon.getIconHeight();
  62. }
  63. public void actionPerformed(ActionEvent e) {
  64. switch (getSelectionState()) {
  65. case unselected:
  66. setSelectionState(State.selected);
  67. break;
  68. case mid_state_selection:
  69. setSelectionState(State.unselected);
  70. break;
  71. case selected:
  72. setSelectionState(State.unselected);
  73. break;
  74. }
  75. }
  76. }