2
* Copyright (c) 2005-2010 Flamingo Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Flamingo Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package org.pushingpixels.flamingo.internal.utils;
33
import java.awt.geom.GeneralPath;
35
import javax.swing.SwingConstants;
36
import javax.swing.SwingUtilities;
38
import org.pushingpixels.flamingo.api.common.JCommandButton;
39
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
42
* Helper implementation of {@link ResizableIcon} that draws an arrow.
44
* @author Kirill Grouchnikov
46
public class ArrowResizableIcon implements ResizableIcon {
50
private Dimension initialDim;
53
* The current icon width.
58
* The current icon height.
63
* Arrow direction. One of {@link SwingConstants#SOUTH},
64
* {@link SwingConstants#NORTH}, {@link SwingConstants#EAST} or
65
* {@link SwingConstants#WEST}.
67
protected int direction;
70
* Creates a new arrow resizable icon.
73
* Initial icon dimension.
75
* Arrow direction. Must be one of {@link SwingConstants#SOUTH},
76
* {@link SwingConstants#NORTH}, {@link SwingConstants#EAST} or
77
* {@link SwingConstants#WEST}.
79
public ArrowResizableIcon(Dimension initialDim, int direction) {
80
this.initialDim = initialDim;
81
this.width = initialDim.width;
82
this.height = initialDim.height;
83
this.direction = direction;
87
* Creates a new arrow resizable icon.
90
* Initial icon dimension.
92
* Arrow direction. Must be one of {@link SwingConstants#SOUTH},
93
* {@link SwingConstants#NORTH}, {@link SwingConstants#EAST} or
94
* {@link SwingConstants#WEST}.
96
public ArrowResizableIcon(int initialDim, int direction) {
97
this(new Dimension(initialDim, initialDim), direction);
100
public void revertToOriginalDimension() {
101
this.width = initialDim.width;
102
this.height = initialDim.height;
106
public void setDimension(Dimension newDimension) {
107
this.width = newDimension.width;
108
this.height = newDimension.height;
112
public int getIconHeight() {
117
public int getIconWidth() {
121
protected boolean toPaintEnabled(Component c) {
122
return c.isEnabled();
126
public void paintIcon(Component c, Graphics g, int x, int y) {
127
Graphics2D graphics = (Graphics2D) g.create();
129
float strokeWidth = this.width / 7.0f;
130
if (strokeWidth < 1.0f)
132
Stroke stroke = new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT,
133
BasicStroke.JOIN_MITER);
135
graphics.setStroke(stroke);
137
GeneralPath gp = new GeneralPath();
139
case SwingUtilities.SOUTH:
141
gp.lineTo((float) 0.5 * (width - 1), height - 2);
142
gp.lineTo(width - 1, 2);
144
case SwingUtilities.NORTH:
145
gp.moveTo(0, height - 2);
146
gp.lineTo((float) 0.5 * (width - 1), 2);
147
gp.lineTo(width - 1, height - 2);
149
case SwingUtilities.EAST:
151
gp.lineTo(width - 2, (float) 0.5 * (height - 1));
152
gp.lineTo(2, height - 1);
154
case SwingUtilities.WEST:
155
gp.moveTo(width - 2, 0);
156
gp.lineTo(2, (float) 0.5 * (height - 1));
157
gp.lineTo(width - 2, height - 1);
161
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
162
RenderingHints.VALUE_ANTIALIAS_ON);
163
graphics.translate(x, y + 1);
164
Color dropColor = this.toPaintEnabled(c) ? new Color(255, 255, 255, 196)
165
: new Color(255, 255, 255, 32);
166
graphics.setColor(dropColor);
169
graphics.translate(0, -1);
170
Color arrowColor = this.toPaintEnabled(c) ? Color.black : Color.gray;
171
graphics.setColor(arrowColor);
172
if (this.width < 9) {
173
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
174
RenderingHints.VALUE_ANTIALIAS_OFF);
177
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
178
RenderingHints.VALUE_ANTIALIAS_ON);
183
public static class CommandButtonPopupIcon extends ArrowResizableIcon {
185
public CommandButtonPopupIcon(int initialDim, int direction) {
186
super(initialDim, direction);
190
protected boolean toPaintEnabled(Component c) {
191
JCommandButton jcb = (JCommandButton) c;
192
return jcb.isEnabled() && jcb.getPopupModel().isEnabled();