-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathVerticalLabelUI.java
More file actions
191 lines (172 loc) · 5.76 KB
/
VerticalLabelUI.java
File metadata and controls
191 lines (172 loc) · 5.76 KB
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
/**
* @(#)VerticalLabelUI.java 1.0 02/18/09
*/
package darrylbu.plaf.vertical;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicLabelUI;
/**
* A UI delegate for JLabel that rotates the label 90º
* <P>
* Extends {@link BasicLabelUI}.
* <P>
* The only difference between the appearance of labels in the Basic and Metal
* L&Fs is the manner in which diabled text is painted. As VerticalLabelUI
* does not override the method paintDisabledText, this class can be adapted
* for Metal L&F by extending MetalLabelUI instead of BasicLabelUI.
* <P>
* No other changes are required.
*
* @author Darryl
*/
public class VerticalLabelUI extends BasicLabelUI {
private boolean clockwise = false;
// see comment in BasicLabelUI
Rectangle verticalViewR = new Rectangle();
Rectangle verticalIconR = new Rectangle();
Rectangle verticalTextR = new Rectangle();
protected static VerticalLabelUI verticalLabelUI =
new VerticalLabelUI();
private final static VerticalLabelUI SAFE_VERTICAL_LABEL_UI =
new VerticalLabelUI();
/**
* Constructs a <code>VerticalLabelUI</code> with the default anticlockwise
* rotation
*/
public VerticalLabelUI() {
}
/**
* Constructs a <code>VerticalLabelUI</code> with the desired rotation.
* <P>
* @param clockwise true to rotate clockwise, false for anticlockwise
*/
public VerticalLabelUI(boolean clockwise) {
this.clockwise = clockwise;
}
/**
* @see ComponentUI#createUI(javax.swing.JComponent)
*/
public static ComponentUI createUI(JComponent c) {
if (System.getSecurityManager() != null) {
return SAFE_VERTICAL_LABEL_UI;
} else {
return verticalLabelUI;
}
}
/**
* Overridden to always return -1, since a vertical label does not have a
* meaningful baseline.
*
* @see ComponentUI#getBaseline(JComponent, int, int)
*/
@Override
public int getBaseline(JComponent c, int width, int height) {
super.getBaseline(c, width, height);
return -1;
}
/**
* Overridden to always return Component.BaselineResizeBehavior.OTHER,
* since a vertical label does not have a meaningful baseline
*
* @see ComponentUI#getBaselineResizeBehavior(javax.swing.JComponent)
*/
@Override
public Component.BaselineResizeBehavior getBaselineResizeBehavior(
JComponent c) {
super.getBaselineResizeBehavior(c);
return Component.BaselineResizeBehavior.OTHER;
}
/**
* Transposes the view rectangles as appropriate for a vertical view
* before invoking the super method and copies them after they have been
* altered by {@link SwingUtilities#layoutCompoundLabel(FontMetrics, String,
* Icon, int, int, int, int, Rectangle, Rectangle, Rectangle, int)}
*/
@Override
protected String layoutCL(JLabel label, FontMetrics fontMetrics,
String text, Icon icon, Rectangle viewR, Rectangle iconR,
Rectangle textR) {
verticalViewR = transposeRectangle(viewR, verticalViewR);
verticalIconR = transposeRectangle(iconR, verticalIconR);
verticalTextR = transposeRectangle(textR, verticalTextR);
text = super.layoutCL(label, fontMetrics, text, icon,
verticalViewR, verticalIconR, verticalTextR);
viewR = copyRectangle(verticalViewR, viewR);
iconR = copyRectangle(verticalIconR, iconR);
textR = copyRectangle(verticalTextR, textR);
return text;
}
/**
* Transforms the Graphics for vertical rendering and invokes the
* super method.
*/
@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2 = (Graphics2D) g.create();
if (clockwise) {
g2.rotate(Math.PI / 2, c.getSize().width / 2, c.getSize().width / 2);
} else {
g2.rotate(-Math.PI / 2, c.getSize().height / 2, c.getSize().height / 2);
}
super.paint(g2, c);
}
/**
* Returns a Dimension appropriate for vertical rendering
*
* @see ComponentUI#getPreferredSize(javax.swing.JComponent)
*/
@Override
public Dimension getPreferredSize(JComponent c) {
return transposeDimension(super.getPreferredSize(c));
}
/**
* Returns a Dimension appropriate for vertical rendering
*
* @see ComponentUI#getMaximumSize(javax.swing.JComponent)
*/
@Override
public Dimension getMaximumSize(JComponent c) {
return transposeDimension(super.getMaximumSize(c));
}
/**
* Returns a Dimension appropriate for vertical rendering
*
* @see ComponentUI#getMinimumSize(javax.swing.JComponent)
*/
@Override
public Dimension getMinimumSize(JComponent c) {
return transposeDimension(super.getMinimumSize(c));
}
private Dimension transposeDimension(Dimension from) {
return new Dimension(from.height, from.width + 2);
}
private Rectangle transposeRectangle(Rectangle from, Rectangle to) {
if (to == null) {
to = new Rectangle();
}
to.x = from.y;
to.y = from.x;
to.width = from.height;
to.height = from.width;
return to;
}
private Rectangle copyRectangle(Rectangle from, Rectangle to) {
if (to == null) {
to = new Rectangle();
}
to.x = from.x;
to.y = from.y;
to.width = from.width;
to.height = from.height;
return to;
}
}