~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to flamingo/src/main/java/org/pushingpixels/flamingo/internal/utils/KeyTipRenderingUtilities.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Flamingo Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without 
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 *  o Redistributions of source code must retain the above copyright notice, 
 
8
 *    this list of conditions and the following disclaimer. 
 
9
 *     
 
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. 
 
13
 *     
 
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. 
 
17
 *     
 
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. 
 
29
 */
 
30
package org.pushingpixels.flamingo.internal.utils;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.font.LineMetrics;
 
34
import java.awt.geom.RoundRectangle2D;
 
35
import java.util.Collection;
 
36
 
 
37
import javax.swing.*;
 
38
 
 
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;
 
43
 
 
44
public class KeyTipRenderingUtilities {
 
45
        private static int INSETS = 3;
 
46
 
 
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);
 
51
        }
 
52
 
 
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);
 
58
 
 
59
                buttonRendererPane.setBounds(rect.x, rect.y, rect.width, rect.height);
 
60
                Graphics2D g2d = (Graphics2D) g.create();
 
61
 
 
62
                g2d.setComposite(AlphaComposite.SrcOver.derive(toPaintEnabled ? 1.0f
 
63
                                : 0.5f));
 
64
 
 
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);
 
68
                g2d.clip(roundRect);
 
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);
 
72
                g2d.setClip(clip);
 
73
 
 
74
                g2d.setColor(FlamingoUtilities.getBorderColor());
 
75
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 
76
                                RenderingHints.VALUE_ANTIALIAS_ON);
 
77
                g2d.draw(roundRect);
 
78
 
 
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);
 
83
                g2d.setFont(font);
 
84
                int strWidth = g2d.getFontMetrics().stringWidth(keyTip);
 
85
                //int strHeight = g2d.getFontMetrics().getHeight();
 
86
 
 
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);
 
93
 
 
94
                g2d.dispose();
 
95
        }
 
96
 
 
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)
 
103
                        return;
 
104
 
 
105
                boolean found = false;
 
106
                for (KeyTipManager.KeyTipLink link : currLinks) {
 
107
                        found = (link.comp == menuButton);
 
108
                        if (found)
 
109
                                break;
 
110
                }
 
111
 
 
112
                if (!found)
 
113
                        return;
 
114
 
 
115
                // System.out.println("Painting key tip for " + menuButton.getText());
 
116
 
 
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());
 
131
                }
 
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
 
138
                                        // the right edge
 
139
                                        KeyTipRenderingUtilities.renderKeyTip(g, menuButton,
 
140
                                                        new Rectangle(layoutInfo.popupClickArea.x
 
141
                                                                        + layoutInfo.popupClickArea.width
 
142
                                                                        - pref.width - 4, Math.min(prefCenter.y
 
143
                                                                        - pref.height / 2,
 
144
                                                                        layoutInfo.actionClickArea.y
 
145
                                                                                        + layoutInfo.actionClickArea.height
 
146
                                                                                        - pref.height), pref.width,
 
147
                                                                        pref.height), popupKeyTip, menuButton
 
148
                                                                        .getPopupModel().isEnabled());
 
149
                                } else {
 
150
                                        KeyTipRenderingUtilities
 
151
                                                        .renderKeyTip(
 
152
                                                                        g,
 
153
                                                                        menuButton,
 
154
                                                                        new Rectangle(
 
155
                                                                                        prefCenter.x - pref.width / 2,
 
156
                                                                                        Math
 
157
                                                                                                        .min(
 
158
                                                                                                                        prefCenter.y
 
159
                                                                                                                                        - pref.height
 
160
                                                                                                                                        / 2,
 
161
                                                                                                                        layoutInfo.popupClickArea.y
 
162
                                                                                                                                        + layoutInfo.popupClickArea.height
 
163
                                                                                                                                        - pref.height),
 
164
                                                                                        pref.width, pref.height),
 
165
                                                                        popupKeyTip, menuButton.getPopupModel()
 
166
                                                                                        .isEnabled());
 
167
                                }
 
168
                        } else {
 
169
                                // horizontally centered along the bottom edge
 
170
                                KeyTipRenderingUtilities
 
171
                                                .renderKeyTip(
 
172
                                                                g,
 
173
                                                                menuButton,
 
174
                                                                new Rectangle(
 
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());
 
182
                        }
 
183
                }
 
184
        }
 
185
}