TrippleCheckBox.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package holeg.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. final static Icon icon = UIManager.getIcon("CheckBox.icon");
  12. private static final Color borderColor = new Color(81, 81, 81);
  13. private static final Color enabledBackgroundColor = new Color(51, 51, 51);
  14. private static final Color disabledBackgroundColor = new Color(122, 138, 153);
  15. State state;
  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. }
  46. int w = getIconWidth();
  47. int h = getIconHeight();
  48. g.setColor(Color.white);
  49. g.fillRect(x + 1, y + 1, w - 2, h - 2);
  50. g.setColor(c.isEnabled() ? enabledBackgroundColor : disabledBackgroundColor);
  51. g.fillRect(x + 4, y + 4, w - 8, h - 8);
  52. if (!c.isEnabled()) {
  53. return;
  54. }
  55. g.setColor(borderColor);
  56. g.drawRect(x + 4, y + 4, w - 9, h - 9);
  57. }
  58. @Override
  59. public int getIconWidth() {
  60. return icon.getIconWidth();
  61. }
  62. @Override
  63. public int getIconHeight() {
  64. return icon.getIconHeight();
  65. }
  66. public void actionPerformed(ActionEvent e) {
  67. switch (getSelectionState()) {
  68. case unselected:
  69. setSelectionState(State.selected);
  70. break;
  71. case mid_state_selection:
  72. setSelectionState(State.unselected);
  73. break;
  74. case selected:
  75. setSelectionState(State.unselected);
  76. break;
  77. }
  78. }
  79. public enum State {
  80. unselected, mid_state_selection, selected
  81. }
  82. }