1 package org.catacomb.druid.swing;
2
3 import java.awt.Color;
4 import java.awt.event.MouseAdapter;
5 import java.awt.event.MouseEvent;
6
7 import javax.swing.*;
8 import javax.swing.border.Border;
9
10
11
12 public class RolloverEffect extends MouseAdapter {
13
14 JComponent button;
15
16 int inormal;
17 int iactive;
18
19 Border normalBorder;
20 Border activeBorder;
21
22 public final static int NONE = 0;
23 public final static int ETCHED_DOWN = 1;
24 public final static int ETCHED_UP = 2;
25 public final static int RAISED = 2;
26
27 Color bgColor;
28
29 int pL;
30 int pR;
31 int pT;
32 int pB;
33
34 private boolean hasPadding;
35
36 public RolloverEffect(JComponent buttonIn) {
37 this(buttonIn, ETCHED_DOWN, ETCHED_UP);
38 }
39
40
41
42 public RolloverEffect(JComponent buttonIn, int norm, int active) {
43 hasPadding = false;
44 bgColor = buttonIn.getBackground();
45
46 inormal = norm;
47 iactive = active;
48
49 button = buttonIn;
50
51
52 makeBorders();
53
54
55 if (button instanceof AbstractButton) {
56 ((AbstractButton)button).setBorderPainted(true);
57
58 } else if (button instanceof JMenu) {
59 ((JMenu)button).setBorderPainted(true);
60
61 } else if (button instanceof JCheckBox) {
62 ((JCheckBox)button).setBorderPainted(true);
63
64 } else if (button instanceof JPanel) {
65
66 }
67
68 mouseExited(null);
69
70 }
71
72
73
74
75 public void setPadding(int p) {
76 setPadding(p, p, p, p);
77 }
78
79
80 public void setPadding(int pl, int pr, int pt, int pb) {
81 pL = pl;
82 pR = pr;
83 pT = pt;
84 pB = pb;
85 hasPadding = true;
86 makeBorders();
87 mouseExited(null);
88 }
89
90
91 public void setBg(Color c) {
92 bgColor = c;
93 makeBorders();
94 mouseExited(null);
95 }
96
97
98
99
100
101 public void makeBorders() {
102 normalBorder = makeBorder(inormal);
103 activeBorder = makeBorder(iactive);
104 }
105
106
107
108 public void mouseEntered(MouseEvent me) {
109 button.setBorder(activeBorder);
110 }
111
112
113 public void mouseExited(MouseEvent me) {
114 button.setBorder(normalBorder);
115 }
116
117
118 private Border makeBorder(int type) {
119 Color c = bgColor;
120 Color cbr = myBrighter(c);
121 Color cdk = myDarker(c);
122
123
124 Border ret = null;
125 if (type == ETCHED_DOWN) {
126
127 ret = BorderFactory.createEtchedBorder(cbr, cdk);
128
129 } else if (type == ETCHED_UP) {
130
131 ret = BorderFactory.createEtchedBorder(cdk, cbr);
132 } else {
133 ret = BorderFactory.createEmptyBorder(2, 2, 2, 2);
134 }
135
136
137 if (hasPadding) {
138 Border bdr = BorderFactory.createEmptyBorder(pT, pL, pB, pR);
139 ret = BorderFactory.createCompoundBorder(ret, bdr);
140 }
141 return ret;
142 }
143
144
145 public static Color myBrighter(Color c) {
146 return linMod(c, 35);
147 }
148
149
150 public static Color myDarker(Color c) {
151 return linMod(c, -35);
152 }
153
154
155 public static Color linMod(Color c, int d) {
156 int r = c.getRed();
157 int g = c.getGreen();
158 int b = c.getBlue();
159
160 r += d;
161 g += d;
162 b += d;
163 r = (r > 0 ? (r < 255 ? r : 255) : 0);
164 g = (g > 0 ? (g < 255 ? g : 255) : 0);
165 b = (b > 0 ? (b < 255 ? b : 255) : 0);
166 return new Color(r, g, b);
167 }
168
169
170
171
172
173 }