1 package com.jsql.view.swing.tree.custom;
2
3 import javax.swing.*;
4 import javax.swing.plaf.UIResource;
5 import javax.swing.plaf.metal.MetalLookAndFeel;
6 import java.awt.*;
7 import java.io.Serializable;
8
9 public class CheckBoxMenuItemIconCustom implements Icon, UIResource, Serializable {
10
11 @Override
12 public void paintIcon(Component component, Graphics graphics, int x, int y) {
13 AbstractButton abstractButton = (AbstractButton) component;
14 ButtonModel model = abstractButton.getModel();
15 boolean isSelected = model.isSelected();
16 boolean isEnabled = model.isEnabled();
17 boolean isPressed = model.isPressed();
18 boolean isArmed = model.isArmed();
19
20 graphics.translate(x, y);
21
22 if (isEnabled) {
23 if (isPressed || isArmed) {
24 graphics.setColor(MetalLookAndFeel.getControlInfo());
25 graphics.drawLine(0, 0, 8, 0);
26 graphics.drawLine(0, 0, 0, 8);
27 graphics.drawLine(8, 2, 8, 8);
28 graphics.drawLine(2, 8, 8, 8);
29
30 graphics.setColor(MetalLookAndFeel.getPrimaryControl());
31 } else {
32 graphics.setColor(MetalLookAndFeel.getControlDarkShadow());
33 graphics.drawLine(0, 0, 8, 0);
34 graphics.drawLine(0, 0, 0, 8);
35 graphics.drawLine(8, 2, 8, 8);
36 graphics.drawLine(2, 8, 8, 8);
37
38 graphics.setColor(MetalLookAndFeel.getControlHighlight());
39 }
40
41 graphics.drawLine(1, 1, 7, 1);
42 graphics.drawLine(1, 1, 1, 7);
43 graphics.drawLine(9, 1, 9, 9);
44 graphics.drawLine(1, 9, 9, 9);
45 } else {
46 graphics.setColor(MetalLookAndFeel.getMenuDisabledForeground());
47 graphics.drawRect(0, 0, 8, 8);
48 }
49
50 if (isSelected) {
51 this.drawSelected(component, graphics, abstractButton, model, isEnabled);
52 }
53 graphics.translate(-x, -y);
54 }
55
56 private void drawSelected(Component component, Graphics graphics, AbstractButton abstractButton, ButtonModel model, boolean isEnabled) {
57 if (isEnabled) {
58 if (model.isArmed() || (component instanceof JMenu && model.isSelected())) {
59 graphics.setColor(MetalLookAndFeel.getMenuSelectedForeground());
60 } else {
61 graphics.setColor(abstractButton.getForeground());
62 }
63 } else {
64 graphics.setColor(MetalLookAndFeel.getMenuDisabledForeground());
65 }
66
67 graphics.drawLine(2, 2, 2, 6);
68 graphics.drawLine(3, 2, 3, 6);
69 graphics.drawLine(4, 4, 8, 0);
70 graphics.drawLine(4, 5, 9, 0);
71 }
72
73 @Override
74 public int getIconWidth() {
75 return 10;
76 }
77
78 @Override
79 public int getIconHeight() {
80 return 10;
81 }
82 }