TrippleCheckBox.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  11. * A checkbox with tree states. Specially for the indication that none, all or partial amount of
  12. * things are selected.
  13. */
  14. public class TrippleCheckBox extends JCheckBox implements Icon, ActionListener {
  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. State state;
  20. public TrippleCheckBox() {
  21. this("", State.unselected);
  22. }
  23. public TrippleCheckBox(String text) {
  24. this(text, State.unselected);
  25. }
  26. public TrippleCheckBox(String text, State selection) {
  27. super(text, selection == State.selected);
  28. setSelectionState(selection);
  29. addActionListener(this);
  30. setIcon(this);
  31. }
  32. @Override
  33. public boolean isSelected() {
  34. return (getSelectionState() == State.selected);
  35. }
  36. public State getSelectionState() {
  37. return state;
  38. }
  39. public void setSelectionState(State selection) {
  40. state = selection;
  41. setSelected(state == State.selected);
  42. repaint();
  43. }
  44. @Override
  45. public void paintIcon(Component c, Graphics g, int x, int y) {
  46. icon.paintIcon(c, g, x, y);
  47. if (getSelectionState() != State.mid_state_selection) {
  48. return;
  49. }
  50. int w = getIconWidth();
  51. int h = getIconHeight();
  52. g.setColor(Color.white);
  53. g.fillRect(x + 1, y + 1, w - 2, h - 2);
  54. g.setColor(c.isEnabled() ? enabledBackgroundColor : disabledBackgroundColor);
  55. g.fillRect(x + 4, y + 4, w - 8, h - 8);
  56. if (!c.isEnabled()) {
  57. return;
  58. }
  59. g.setColor(borderColor);
  60. g.drawRect(x + 4, y + 4, w - 9, h - 9);
  61. }
  62. @Override
  63. public int getIconWidth() {
  64. return icon.getIconWidth();
  65. }
  66. @Override
  67. public int getIconHeight() {
  68. return icon.getIconHeight();
  69. }
  70. public void actionPerformed(ActionEvent e) {
  71. switch (getSelectionState()) {
  72. case unselected:
  73. setSelectionState(State.selected);
  74. break;
  75. case mid_state_selection:
  76. setSelectionState(State.unselected);
  77. break;
  78. case selected:
  79. setSelectionState(State.unselected);
  80. break;
  81. }
  82. }
  83. public enum State {
  84. unselected, mid_state_selection, selected
  85. }
  86. }