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.font.LineMetrics;
34
import java.awt.geom.RoundRectangle2D;
35
import java.util.Collection;
39
import org.pushingpixels.flamingo.api.common.CommandButtonLayoutManager;
40
import org.pushingpixels.flamingo.api.common.JCommandMenuButton;
41
import org.pushingpixels.flamingo.api.common.JCommandButton.CommandButtonKind;
42
import org.pushingpixels.flamingo.api.common.JCommandButton.CommandButtonPopupOrientationKind;
44
public class KeyTipRenderingUtilities {
45
private static int INSETS = 3;
47
public static Dimension getPrefSize(FontMetrics fm, String keyTip) {
48
int prefWidth = fm.stringWidth(keyTip) + 2 * INSETS + 1;
49
int prefHeight = fm.getHeight() + INSETS - 1;
50
return new Dimension(prefWidth, prefHeight);
53
public static void renderKeyTip(Graphics g, Container c, Rectangle rect,
54
String keyTip, boolean toPaintEnabled) {
55
CellRendererPane buttonRendererPane = new CellRendererPane();
56
JButton rendererButton = new JButton("");
57
rendererButton.setEnabled(toPaintEnabled);
59
buttonRendererPane.setBounds(rect.x, rect.y, rect.width, rect.height);
60
Graphics2D g2d = (Graphics2D) g.create();
62
g2d.setComposite(AlphaComposite.SrcOver.derive(toPaintEnabled ? 1.0f
65
Shape clip = g2d.getClip();
66
RoundRectangle2D.Double roundRect = new RoundRectangle2D.Double(rect.x,
67
rect.y, rect.width - 1, rect.height - 1, 6, 6);
69
buttonRendererPane.paintComponent(g2d, rendererButton, c, rect.x
70
- rect.width / 2, rect.y - rect.height / 2, 2 * rect.width,
71
2 * rect.height, true);
74
g2d.setColor(FlamingoUtilities.getBorderColor());
75
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
76
RenderingHints.VALUE_ANTIALIAS_ON);
79
g2d.setColor(FlamingoUtilities.getColor(Color.black,
80
"Button.foreground"));
81
Font font = UIManager.getFont("Button.font");
82
font = font.deriveFont(font.getSize() + 1.0f);
84
int strWidth = g2d.getFontMetrics().stringWidth(keyTip);
85
//int strHeight = g2d.getFontMetrics().getHeight();
87
g2d.translate(rect.x, rect.y);
88
LineMetrics lineMetrics = g2d.getFontMetrics().getLineMetrics(keyTip, g2d);
89
int strHeight = (int)lineMetrics.getHeight();
90
g2d.drawString(keyTip, (rect.width - strWidth + 1) / 2,
91
(rect.height + strHeight) / 2
92
- g2d.getFontMetrics().getDescent() + 1);
97
public static void renderMenuButtonKeyTips(Graphics g,
98
JCommandMenuButton menuButton,
99
CommandButtonLayoutManager layoutManager) {
100
Collection<KeyTipManager.KeyTipLink> currLinks = KeyTipManager
101
.defaultManager().getCurrentlyShownKeyTips();
102
if (currLinks == null)
105
boolean found = false;
106
for (KeyTipManager.KeyTipLink link : currLinks) {
107
found = (link.comp == menuButton);
115
// System.out.println("Painting key tip for " + menuButton.getText());
117
String actionKeyTip = menuButton.getActionKeyTip();
118
String popupKeyTip = menuButton.getPopupKeyTip();
119
CommandButtonLayoutManager.CommandButtonLayoutInfo layoutInfo = layoutManager
120
.getLayoutInfo(menuButton, g);
121
Point prefCenter = menuButton.getUI().getKeyTipAnchorCenterPoint();
122
if ((layoutInfo.iconRect.width > 0) && (actionKeyTip != null)) {
123
Dimension pref = KeyTipRenderingUtilities.getPrefSize(g
124
.getFontMetrics(), actionKeyTip);
125
KeyTipRenderingUtilities.renderKeyTip(g, menuButton, new Rectangle(
126
prefCenter.x - pref.width / 2, Math.min(prefCenter.y
127
- pref.height / 2, layoutInfo.actionClickArea.y
128
+ layoutInfo.actionClickArea.height - pref.height),
129
pref.width, pref.height), actionKeyTip, menuButton
130
.getActionModel().isEnabled());
132
if ((layoutInfo.popupClickArea.width > 0) && (popupKeyTip != null)) {
133
Dimension pref = KeyTipRenderingUtilities.getPrefSize(g
134
.getFontMetrics(), popupKeyTip);
135
if (menuButton.getPopupOrientationKind() == CommandButtonPopupOrientationKind.SIDEWARD) {
136
if (menuButton.getCommandButtonKind() != CommandButtonKind.POPUP_ONLY) {
137
// vertically aligned with the action keytip along
139
KeyTipRenderingUtilities.renderKeyTip(g, menuButton,
140
new Rectangle(layoutInfo.popupClickArea.x
141
+ layoutInfo.popupClickArea.width
142
- pref.width - 4, Math.min(prefCenter.y
144
layoutInfo.actionClickArea.y
145
+ layoutInfo.actionClickArea.height
146
- pref.height), pref.width,
147
pref.height), popupKeyTip, menuButton
148
.getPopupModel().isEnabled());
150
KeyTipRenderingUtilities
155
prefCenter.x - pref.width / 2,
161
layoutInfo.popupClickArea.y
162
+ layoutInfo.popupClickArea.height
164
pref.width, pref.height),
165
popupKeyTip, menuButton.getPopupModel()
169
// horizontally centered along the bottom edge
170
KeyTipRenderingUtilities
175
(layoutInfo.popupClickArea.x
176
+ layoutInfo.popupClickArea.width - pref.width) / 2,
177
layoutInfo.popupClickArea.y
178
+ layoutInfo.popupClickArea.height
179
- pref.height, pref.width,
180
pref.height), popupKeyTip, menuButton
181
.getPopupModel().isEnabled());