TrippleCheckBox.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. repaint();
  39. }
  40. @Override
  41. public void paintIcon(Component c, Graphics g, int x, int y) {
  42. icon.paintIcon(c, g, x, y);
  43. if (getSelectionState() != State.mid_state_selection)
  44. return;
  45. int w = getIconWidth();
  46. int h = getIconHeight();
  47. g.setColor(Color.white);
  48. g.fillRect(x + 1, y + 1, w - 2, h - 2);
  49. g.setColor(c.isEnabled() ? new Color(51, 51, 51) : new Color(122, 138, 153));
  50. g.fillRect(x + 4, y + 4, w - 8, h - 8);
  51. if (!c.isEnabled())
  52. return;
  53. g.setColor(new Color(81, 81, 81));
  54. g.drawRect(x + 4, y + 4, w - 9, h - 9);
  55. }
  56. @Override
  57. public int getIconWidth() {
  58. return icon.getIconWidth();
  59. }
  60. @Override
  61. public int getIconHeight() {
  62. return icon.getIconHeight();
  63. }
  64. public void actionPerformed(ActionEvent e) {
  65. switch (getSelectionState()) {
  66. case unselected:
  67. setSelectionState(State.selected);
  68. break;
  69. case mid_state_selection:
  70. setSelectionState(State.unselected);
  71. break;
  72. case selected:
  73. setSelectionState(State.unselected);
  74. break;
  75. }
  76. }
  77. }