FSF GCC merge 02/23/03
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicButtonUI.java
blob88ac6f25ae22a76e01700633d57f9cd9f4f5c15c
1 package javax.swing.plaf.basic;
3 import javax.swing.*;
4 import javax.swing.plaf.*;
5 import java.awt.*;
8 public class BasicButtonUI extends ButtonUI
10 int gap = 3;
11 // int y_text_space = 2, x_text_space + 5;
13 Color textColor, disabledTextColor;
14 Color pressedBackgroundColor;
15 Color normalBackgroundColor;
18 public static ComponentUI createUI(final JComponent c)
20 return new BasicButtonUI();
24 public void installUI(final JComponent c)
26 super.installUI(c);
28 textColor = new Color(0,0,0);
29 disabledTextColor = new Color(130, 130, 130);
30 pressedBackgroundColor = new Color(150,150,150);
31 pressedBackgroundColor = new Color(150,150,150);
32 normalBackgroundColor = new Color(192,192,192);
36 public Dimension getPreferredSize(JComponent c)
38 AbstractButton b = (AbstractButton)c;
39 Dimension d = BasicGraphicsUtils.getPreferredSize(b,
40 gap,
41 b.getText(),
42 b.getIcon(),
43 b.getVerticalAlignment(),
44 b.getHorizontalAlignment(),
45 b.getHorizontalTextPosition(),
46 b.getVerticalTextPosition());
47 // System.out.println("^^^^^^^^^^^^^^^^^^^^^^ BASIC-PREF="+d + ",T="+b.text);
48 return d;
52 public void paint(Graphics g, JComponent c)
54 AbstractButton b = (AbstractButton) c;
56 Rectangle tr = new Rectangle();
57 Rectangle ir = new Rectangle();
58 Rectangle vr = new Rectangle();
60 Font f = c.getFont();
62 g.setFont(f);
64 FontMetrics fm = SwingUtilities.getFontMetrics(f);
66 Insets i = c.getInsets();
68 vr.x = i.left;
69 vr.y = i.top;
70 vr.width = b.getWidth() - (i.right + vr.x);
71 vr.height = b.getHeight() - (i.bottom + vr.y);
73 //System.out.println(" VIEW-RECT-BUTTON="+vr+", insets="+i+", FONTM="+fm);
75 String text = SwingUtilities.layoutCompoundLabel(c,
76 fm,
77 b.getText(),
78 b.getIcon(),
79 b.getVerticalAlignment(),
80 b.getHorizontalAlignment(),
81 b.getVerticalTextPosition(),
82 b.getHorizontalTextPosition(),
83 vr,
84 ir,
85 tr,
86 gap);
88 if (b.getModel().isPressed() ||
89 b.getModel().isSelected())
91 //System.out.println("paint pressed");
92 paintButtonPressed(g, c);
94 else
96 //System.out.println("paint normal");
97 paintButtonNormal(g, c);
100 paintIcon(g, c, ir);
101 paintText(g, c, tr, b.getText());
102 paintFocus(g, c, vr, tr, ir);
106 protected void paintFocus(Graphics g,
107 JComponent c,
108 Rectangle vr,
109 Rectangle tr,
110 Rectangle ir)
114 protected void paintIcon(Graphics g,
115 JComponent c,
116 Rectangle iconRect)
118 AbstractButton b = (AbstractButton) c;
119 if (b.getIcon() != null)
121 int x = iconRect.x;
122 int y = iconRect.y;
124 System.out.println("WE HAVE AN ICON: " + b.getIcon());
126 b.getIcon().paintIcon(c, g, x, y);
128 else
130 //System.out.println("NO ICON FOR BUTTON:" + b.text);
134 protected void paintButtonPressed(Graphics g,
135 JComponent b)
137 Dimension size = b.getSize();
139 g.setColor(pressedBackgroundColor);
140 g.fillRect(1,1,size.width-2, size.height-2);
144 protected void paintButtonNormal(Graphics g,
145 JComponent b)
147 Dimension size = b.getSize();
149 g.setColor(normalBackgroundColor);
150 g.fillRect(1,1,size.width-2, size.height-2);
154 protected void paintText(Graphics g,
155 JComponent c,
156 Rectangle textRect,
157 String text)
159 Font f = c.getFont();
161 g.setFont(f);
163 FontMetrics fm = SwingUtilities.getFontMetrics(f);
165 g.setColor(c.isEnabled() ? textColor : disabledTextColor);
167 BasicGraphicsUtils.drawString(g,
168 text,
170 textRect.x,
171 textRect.y + fm.getAscent()/2);