-
-
Notifications
You must be signed in to change notification settings - Fork 256
/
FlatArrowButton.java
246 lines (207 loc) · 6.24 KB
/
FlatArrowButton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicArrowButton;
/**
* Button that draws a scaled arrow in one direction.
*
* @author Karl Tauber
*/
public class FlatArrowButton
extends BasicArrowButton
implements UIResource
{
public static final int DEFAULT_ARROW_WIDTH = 9;
protected boolean chevron;
protected Color foreground;
protected Color disabledForeground;
protected Color hoverForeground;
protected Color hoverBackground;
protected Color pressedForeground;
protected Color pressedBackground;
private int arrowWidth = DEFAULT_ARROW_WIDTH;
private float arrowThickness = 1;
private float xOffset = 0;
private float yOffset = 0;
private boolean roundBorderAutoXOffset = true;
private boolean hover;
private boolean pressed;
public FlatArrowButton( int direction, String type, Color foreground, Color disabledForeground,
Color hoverForeground, Color hoverBackground, Color pressedForeground, Color pressedBackground )
{
super( direction, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE );
updateStyle( type, foreground, disabledForeground, hoverForeground, hoverBackground,
pressedForeground, pressedBackground );
setOpaque( false );
setBorder( null );
if( hoverForeground != null || hoverBackground != null ||
pressedForeground != null || pressedBackground != null )
{
addMouseListener( new MouseAdapter() {
@Override
public void mouseEntered( MouseEvent e ) {
hover = true;
repaint();
}
@Override
public void mouseExited( MouseEvent e ) {
hover = false;
repaint();
}
@Override
public void mousePressed( MouseEvent e ) {
if( SwingUtilities.isLeftMouseButton( e ) ) {
pressed = true;
repaint();
}
}
@Override
public void mouseReleased( MouseEvent e ) {
if( SwingUtilities.isLeftMouseButton( e ) ) {
pressed = false;
repaint();
}
}
} );
}
}
/** @since 2 */
public void updateStyle( String type, Color foreground, Color disabledForeground,
Color hoverForeground, Color hoverBackground, Color pressedForeground, Color pressedBackground )
{
this.chevron = FlatUIUtils.isChevron( type );
this.foreground = foreground;
this.disabledForeground = disabledForeground;
this.hoverForeground = hoverForeground;
this.hoverBackground = hoverBackground;
this.pressedForeground = pressedForeground;
this.pressedBackground = pressedBackground;
}
public int getArrowWidth() {
return arrowWidth;
}
public void setArrowWidth( int arrowWidth ) {
this.arrowWidth = arrowWidth;
}
/** @since 3 */
public float getArrowThickness() {
return arrowThickness;
}
/** @since 3 */
public void setArrowThickness( float arrowThickness ) {
this.arrowThickness = arrowThickness;
}
protected boolean isHover() {
return hover;
}
protected boolean isPressed() {
return pressed;
}
public float getXOffset() {
return xOffset;
}
public void setXOffset( float xOffset ) {
this.xOffset = xOffset;
}
public float getYOffset() {
return yOffset;
}
public void setYOffset( float yOffset ) {
this.yOffset = yOffset;
}
/** @since 3 */
public boolean isRoundBorderAutoXOffset() {
return roundBorderAutoXOffset;
}
/** @since 3 */
public void setRoundBorderAutoXOffset( boolean roundBorderAutoXOffset ) {
this.roundBorderAutoXOffset = roundBorderAutoXOffset;
}
protected Color deriveBackground( Color background ) {
return background;
}
protected Color deriveForeground( Color foreground ) {
return FlatUIUtils.deriveColor( foreground, this.foreground );
}
/**
* Returns the color used to paint the arrow.
*
* @since 1.2
*/
protected Color getArrowColor() {
return isEnabled()
? (pressedForeground != null && isPressed()
? pressedForeground
: (hoverForeground != null && isHover()
? hoverForeground
: foreground))
: disabledForeground;
}
@Override
public Dimension getPreferredSize() {
return scale( super.getPreferredSize() );
}
@Override
public Dimension getMinimumSize() {
return scale( super.getMinimumSize() );
}
@Override
public void paint( Graphics g ) {
Object[] oldRenderingHints = FlatUIUtils.setRenderingHints( g );
// paint hover or pressed background
if( isEnabled() ) {
Color background = (pressedBackground != null && isPressed())
? pressedBackground
: (hoverBackground != null && isHover()
? hoverBackground
: null);
if( background != null ) {
g.setColor( deriveBackground( background ) );
paintBackground( (Graphics2D) g );
}
}
// paint arrow
g.setColor( deriveForeground( getArrowColor() ) );
paintArrow( (Graphics2D) g );
FlatUIUtils.resetRenderingHints( g, oldRenderingHints );
}
protected void paintBackground( Graphics2D g ) {
g.fillRect( 0, 0, getWidth(), getHeight() );
}
protected void paintArrow( Graphics2D g ) {
int x = 0;
// move arrow for round borders
if( isRoundBorderAutoXOffset() ) {
Container parent = getParent();
boolean vert = (direction == NORTH || direction == SOUTH);
if( vert && parent instanceof JComponent && FlatUIUtils.hasRoundBorder( (JComponent) parent ) )
x -= scale( parent.getComponentOrientation().isLeftToRight() ? 1 : -1 );
}
FlatUIUtils.paintArrow( g, x, 0, getWidth(), getHeight(), getDirection(), chevron,
getArrowWidth(), getArrowThickness(), getXOffset(), getYOffset() );
}
}