Debian Codegraph @

src/com/jidesoft/icons/RolloverIcon.java

raw
1 /*
2 * @(#)NavitaionTreeIcon.java 11/3/2011
3 *
4 * Copyright 2002 - 2011 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.icons;
8
9 import com.jidesoft.swing.JideSwingUtilities;
10
11 import javax.swing.*;
12 import java.awt.*;
13 import java.awt.geom.GeneralPath;
14
15 /**
16 * <code>RolloverIcon</code> provides the expanded and collapsed tree icons that has rollover and fade effect. However
17 * it can be used to implement icon for any other purpose, not just the tree icons.
18 *
19 * @since 3.3.0
20 */
21 public class RolloverIcon implements Icon {
22 /**
23 * All colors used by the default icons. You can change them to fit your L&F.
24 */
25 public static Color COLOR_COLLAPSED_FILL_ROLLOVER = new Color(199, 235, 250);
26 public static Color COLOR_COLLAPSED_FILL = Color.WHITE;
27 public static Color COLOR_COLLAPSED_ROLLOVER = new Color(28, 196, 247);
28 public static Color COLOR_COLLAPSED = new Color(165, 165, 165);
29 public static Color COLOR_EXPANDED_FILL_ROLLOVER = new Color(130, 223, 251);
30 public static Color COLOR_EXPANDED_FILL = new Color(89, 89, 89);
31 public static Color COLOR_EXPANDED_ROLLOVER = new Color(28, 196, 247);
32 public static Color COLOR_EXPANDED = new Color(35, 35, 35);
33
34 public final static int ICON_EXPANDED = 0;
35 public final static int ICON_COLLAPSED = 1;
36
37 private Icon _normalIcon;
38 private Icon _rolloverIcon;
39 private IconRolloverSupport _rolloverSupport;
40
41 /**
42 * An interface that should be implemented on a component if you want to use an icon that supports rollover and fade
43 * (fade in or fade out) effect.
44 */
45 public static interface IconRolloverSupport {
46 /**
47 * Checks if the mouse is over an icon. In the implementation, you can use a MouseMotionListener to detect the
48 * mouse position and see if it is over the icon.
49 *
50 * @param x x of the icon
51 * @param y y of the icon
52 * @param width icon width
53 * @param height icon height
54 * @return true if the mouse is over the icon. False if not.
55 */
56 boolean isIconRollover(int x, int y, int width, int height);
57
58 /**
59 * Checks if the icon should be faded. Because the fade is an animation, we will call {@link #getIconAlpha()}
60 * immediately to find out the alpha value of the fade. In your implementation, you can use an Animator or any
61 * other animation frameworks to change the iconAlpha value and ask the icon to repaint itself again.
62 *
63 * @return true of the icon should be faded. If it returns true, {@link #getIconAlpha()} will be called to find
64 * out the alpha value.
65 */
66 boolean isIconFade();
67
68 /**
69 * Gets the icon alpha value. We will paint the icon using the provided alpha value to create the fade effect.
70 *
71 * @return the icon alpha value.
72 */
73 float getIconAlpha();
74 }
75
76 /**
77 * The default collapsed tree icon.
78 */
79 public static class DefaultCollapsedIcon implements Icon {
80 private IconRolloverSupport _rolloverSupport;
81
82 public DefaultCollapsedIcon(IconRolloverSupport rolloverSupport) {
83 _rolloverSupport = rolloverSupport;
84 }
85
86 @Override
87 public void paintIcon(Component c, Graphics g, int x, int y) {
88 Graphics2D g2 = (Graphics2D) g;
89 Color orgColor = g2.getColor();
90 Object o = JideSwingUtilities.setupShapeAntialiasing(g);
91 GeneralPath path = new GeneralPath();
92 if (c.getComponentOrientation() == ComponentOrientation.RIGHT_TO_LEFT) {
93 path.moveTo(x + 4, y);
94 path.lineTo(x, y + 4);
95 path.lineTo(x + 4, y + 8);
96 }
97 else {
98 path.moveTo(x, y);
99 path.lineTo(x + 4, y + 4);
100 path.lineTo(x, y + 8);
101 }
102 path.closePath();
103 boolean rollover = _rolloverSupport.isIconRollover(x, y, getIconWidth(), getIconHeight());
104 g2.setColor(rollover ? COLOR_COLLAPSED_FILL_ROLLOVER : COLOR_COLLAPSED_FILL);
105 g2.fill(path);
106 g2.setColor(rollover ? COLOR_COLLAPSED_ROLLOVER : COLOR_COLLAPSED);
107 g2.draw(path);
108 g2.setColor(orgColor);
109 JideSwingUtilities.restoreShapeAntialiasing(g, o);
110 }
111
112 @Override
113 public int getIconWidth() {
114 return 6;
115 }
116
117 @Override
118 public int getIconHeight() {
119 return 9;
120 }
121 }
122
123 /**
124 * The default expanded tree icon.
125 */
126 public static class DefaultExpandedIcon implements Icon {
127 private IconRolloverSupport _rolloverSupport;
128
129 public DefaultExpandedIcon(IconRolloverSupport rolloverSupport) {
130 _rolloverSupport = rolloverSupport;
131 }
132
133 @Override
134 public void paintIcon(Component c, Graphics g, int x, int y) {
135 Graphics2D g2 = (Graphics2D) g;
136 Color orgColor = g2.getColor();
137 Object o = JideSwingUtilities.setupShapeAntialiasing(g);
138 GeneralPath path = new GeneralPath();
139 if (c.getComponentOrientation() == ComponentOrientation.RIGHT_TO_LEFT) {
140 path.moveTo(x, y);
141 path.lineTo(x, y + 5);
142 path.lineTo(x + 5, y + 5);
143 }
144 else {
145 path.moveTo(x + 5, y);
146 path.lineTo(x + 5, y + 5);
147 path.lineTo(x, y + 5);
148 }
149 path.closePath();
150 boolean rollover = _rolloverSupport.isIconRollover(x, y, getIconWidth(), getIconHeight());
151 g2.setColor(rollover ? COLOR_EXPANDED_FILL_ROLLOVER : COLOR_EXPANDED_FILL);
152 g2.fill(path);
153 g2.setColor(rollover ? COLOR_EXPANDED_ROLLOVER : COLOR_EXPANDED);
154 g2.draw(path);
155 g2.setColor(orgColor);
156 JideSwingUtilities.restoreShapeAntialiasing(g, o);
157 }
158
159 @Override
160 public int getIconWidth() {
161 return 6;
162 }
163
164 @Override
165 public int getIconHeight() {
166 return 6;
167 }
168 }
169
170 /**
171 * Creates an RolloverIcon.
172 *
173 * @param rolloverSupport the IconRolloverSupport interface that should be implemented on a component.
174 * @param iconType the icon type. It could be either {@link #ICON_EXPANDED} or {@link #ICON_COLLAPSED}.
175 */
176 public RolloverIcon(IconRolloverSupport rolloverSupport, int iconType) {
177 _rolloverSupport = rolloverSupport;
178 switch (iconType) {
179 case ICON_EXPANDED:
180 _normalIcon = new DefaultExpandedIcon(rolloverSupport);
181 break;
182 case ICON_COLLAPSED:
183 _normalIcon = new DefaultCollapsedIcon(rolloverSupport);
184 break;
185 }
186 _rolloverIcon = null;
187 }
188
189 /**
190 * Creates an RolloverIcon.
191 *
192 * @param rolloverSupport the IconRolloverSupport interface that should be implemented on a component.
193 * @param normalIcon the normal icon
194 */
195 public RolloverIcon(IconRolloverSupport rolloverSupport, Icon normalIcon) {
196 _rolloverSupport = rolloverSupport;
197 _normalIcon = normalIcon;
198 _rolloverIcon = null;
199 }
200
201 /**
202 * Creates an RolloverIcon.
203 *
204 * @param rolloverSupport the IconRolloverSupport interface that should be implemented on a component.
205 * @param normalIcon the normal icon. This icon will be used to determine the icon size. The size of the
206 * rollover icon is ignored so you should make sure the two icons have the same size.
207 * @param rolloverIcon the rollover icon
208 */
209 public RolloverIcon(IconRolloverSupport rolloverSupport, Icon normalIcon, Icon rolloverIcon) {
210 _rolloverSupport = rolloverSupport;
211 _normalIcon = normalIcon;
212 _rolloverIcon = rolloverIcon;
213 }
214
215 @Override
216 public void paintIcon(Component c, Graphics g, int x, int y) {
217 Graphics2D g2 = (Graphics2D) g;
218 Composite orgComposite = g2.getComposite();
219 if (_rolloverSupport.isIconFade()) {
220 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _rolloverSupport.getIconAlpha()));
221 }
222 boolean rollover = _rolloverIcon != null && _rolloverIcon != _normalIcon && _rolloverSupport.isIconRollover(x, y, getIconWidth(), getIconHeight());
223 if (rollover) {
224 _rolloverIcon.paintIcon(c, g2, x, y);
225 }
226 else {
227 _normalIcon.paintIcon(c, g2, x, y);
228 }
229 g2.setComposite(orgComposite);
230 }
231
232 @Override
233 public int getIconWidth() {
234 return _normalIcon.getIconWidth();
235 }
236
237 @Override
238 public int getIconHeight() {
239 return _normalIcon.getIconHeight();
240 }
241 }