TrippleCheckBox.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. private static final Color borderColor = new Color(81, 81, 81);
  17. private static final Color enabledBackgroundColor = new Color(51, 51, 51);
  18. private static final Color disabledBackgroundColor = new Color(122, 138, 153);
  19. public TrippleCheckBox() {
  20. this("", State.unselected);
  21. }
  22. public TrippleCheckBox(String text) {
  23. this(text, State.unselected);
  24. }
  25. public TrippleCheckBox(String text, State selection) {
  26. super(text, selection == State.selected);
  27. setSelectionState(selection);
  28. addActionListener(this);
  29. setIcon(this);
  30. }
  31. @Override
  32. public boolean isSelected() {
  33. return (getSelectionState() == State.selected);
  34. }
  35. public State getSelectionState() {
  36. return state;
  37. }
  38. public void setSelectionState(State selection) {
  39. state = selection;
  40. setSelected(state == State.selected);
  41. repaint();
  42. }
  43. @Override
  44. public void paintIcon(Component c, Graphics g, int x, int y) {
  45. icon.paintIcon(c, g, x, y);
  46. if (getSelectionState() != State.mid_state_selection)
  47. return;
  48. int w = getIconWidth();
  49. int h = getIconHeight();
  50. g.setColor(Color.white);
  51. g.fillRect(x + 1, y + 1, w - 2, h - 2);
  52. g.setColor(c.isEnabled() ? enabledBackgroundColor : disabledBackgroundColor);
  53. g.fillRect(x + 4, y + 4, w - 8, h - 8);
  54. if (!c.isEnabled())
  55. return;
  56. g.setColor(borderColor);
  57. g.drawRect(x + 4, y + 4, w - 9, h - 9);
  58. }
  59. @Override
  60. public int getIconWidth() {
  61. return icon.getIconWidth();
  62. }
  63. @Override
  64. public int getIconHeight() {
  65. return icon.getIconHeight();
  66. }
  67. public void actionPerformed(ActionEvent e) {
  68. switch (getSelectionState()) {
  69. case unselected:
  70. setSelectionState(State.selected);
  71. break;
  72. case mid_state_selection:
  73. setSelectionState(State.unselected);
  74. break;
  75. case selected:
  76. setSelectionState(State.unselected);
  77. break;
  78. }
  79. }
  80. }