KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsGraphicsUtils


1 /*
2  * @(#)WindowsGraphicsUtils.java 1.16 06/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import com.sun.java.swing.SwingUtilities2;
11
12 import java.awt.*;
13
14 import javax.swing.*;
15
16 import com.sun.java.swing.plaf.windows.TMSchema.*;
17
18 /**
19  * A collection of static utility methods used for rendering the Windows look
20  * and feel.
21  *
22  * @version 1.16 12/19/06
23  * @author Mark Davidson
24  * @since 1.4
25  */

26 public class WindowsGraphicsUtils {
27     
28     /**
29      * Renders a text String in Windows without the mnemonic.
30      * This is here because the WindowsUI hiearchy doesn't match the Component heirarchy. All
31      * the overriden paintText methods of the ButtonUI delegates will call this static method.
32      * <p>
33      * @param g Graphics context
34      * @param b Current button to render
35      * @param textRect Bounding rectangle to render the text.
36      * @param text String to render
37      */

38     public static void paintText(Graphics g, AbstractButton b,
39                     Rectangle textRect, String JavaDoc text,
40                     int textShiftOffset) {
41         ButtonModel model = b.getModel();
42         FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
43
44     int mnemIndex = b.getDisplayedMnemonicIndex();
45     // W2K Feature: Check to see if the Underscore should be rendered.
46
if (WindowsLookAndFeel.isMnemonicHidden() == true) {
47             mnemIndex = -1;
48     }
49
50     /* Draw the Text */
51     Color color = b.getForeground();
52     if(model.isEnabled()) {
53         /*** paint the text normally */
54             if(!(b instanceof JMenuItem && model.isArmed())
55                 && !(b instanceof JMenu && (model.isSelected() || model.isRollover()))) {
56                 /* We shall not set foreground color for selected menu or
57                  * armed menuitem. Foreground must be set in appropriate
58                  * Windows* class because these colors passes from
59                  * BasicMenuItemUI as protected fields and we can't
60                  * reach them from this class */

61             g.setColor(b.getForeground());
62             }
63         SwingUtilities2.drawStringUnderlineCharAt(b, g,text, mnemIndex,
64                       textRect.x + textShiftOffset,
65                       textRect.y + fm.getAscent() + textShiftOffset);
66     } else { /*** paint the text disabled ***/
67         color = UIManager.getColor("Button.disabledForeground");
68         Color shadow = UIManager.getColor("Button.disabledShadow");
69
70         XPStyle xp = XPStyle.getXP();
71         if (xp != null) {
72                 Part part = WindowsButtonUI.getXPButtonType(b);
73                 color = xp.getColor(b, part, State.DISABLED, Prop.TEXTCOLOR,
74                     color);
75                 // to work around an apparent bug in Windows, use the pushbutton
76
// color for disabled toolbar buttons if the disabled color is the
77
// same as the enabled color
78
if (part == Part.TP_BUTTON) {
79                     Color enabledColor = xp.getColor(b, part, State.NORMAL,
80                         Prop.TEXTCOLOR, color);
81                     if (color != null && color.equals(enabledColor)) {
82                         color = xp.getColor(b, Part.TP_BUTTON, State.DISABLED,
83                             Prop.TEXTCOLOR, color);
84                     }
85                 }
86         } else {
87         // Paint shadow only if not XP
88
if (shadow == null) {
89             shadow = b.getBackground().darker();
90         }
91         g.setColor(shadow);
92         SwingUtilities2.drawStringUnderlineCharAt(b, g,text,
93                                  mnemIndex,
94                                  textRect.x,
95                                  textRect.y + fm.getAscent());
96         }
97         if (color == null) {
98         color = b.getBackground().brighter();
99         }
100         g.setColor(color);
101         SwingUtilities2.drawStringUnderlineCharAt(b, g,text,
102                              mnemIndex,
103                              textRect.x - 1,
104                              textRect.y + fm.getAscent() - 1);
105     }
106     }
107
108     static boolean isLeftToRight(Component c) {
109         return c.getComponentOrientation().isLeftToRight();
110     }
111 }
112
113
Popular Tags