KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicToggleButtonUI


1 /*
2  * @(#)BasicToggleButtonUI.java 1.58 03/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 javax.swing.plaf.basic;
9
10 import java.awt.*;
11 import java.awt.event.*;
12
13 import javax.swing.*;
14 import javax.swing.border.*;
15 import javax.swing.plaf.*;
16 import javax.swing.text.View JavaDoc;
17
18
19
20 /**
21  * BasicToggleButton implementation
22  * <p>
23  *
24  * @version 1.58 12/19/03
25  * @author Jeff Dinkins
26  */

27 public class BasicToggleButtonUI extends BasicButtonUI JavaDoc {
28
29     private final static BasicToggleButtonUI JavaDoc toggleButtonUI = new BasicToggleButtonUI JavaDoc();
30
31     private final static String JavaDoc propertyPrefix = "ToggleButton" + ".";
32
33     // ********************************
34
// Create PLAF
35
// ********************************
36
public static ComponentUI createUI(JComponent b) {
37         return toggleButtonUI;
38     }
39
40     protected String JavaDoc getPropertyPrefix() {
41         return propertyPrefix;
42     }
43     
44
45     // ********************************
46
// Paint Methods
47
// ********************************
48
public void paint(Graphics g, JComponent c) {
49         AbstractButton b = (AbstractButton) c;
50         ButtonModel model = b.getModel();
51     
52         Dimension size = b.getSize();
53         FontMetrics fm = g.getFontMetrics();
54
55         Insets i = c.getInsets();
56
57         Rectangle viewRect = new Rectangle(size);
58
59         viewRect.x += i.left;
60         viewRect.y += i.top;
61         viewRect.width -= (i.right + viewRect.x);
62         viewRect.height -= (i.bottom + viewRect.y);
63
64         Rectangle iconRect = new Rectangle();
65         Rectangle textRect = new Rectangle();
66
67         Font f = c.getFont();
68         g.setFont(f);
69
70         // layout the text and icon
71
String JavaDoc text = SwingUtilities.layoutCompoundLabel(
72             c, fm, b.getText(), b.getIcon(),
73             b.getVerticalAlignment(), b.getHorizontalAlignment(),
74             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
75             viewRect, iconRect, textRect,
76         b.getText() == null ? 0 : b.getIconTextGap());
77
78         g.setColor(b.getBackground());
79
80         if (model.isArmed() && model.isPressed() || model.isSelected()) {
81             paintButtonPressed(g,b);
82         }
83     
84         // Paint the Icon
85
if(b.getIcon() != null) {
86             paintIcon(g, b, iconRect);
87         }
88     
89         // Draw the Text
90
if(text != null && !text.equals("")) {
91             View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
92             if (v != null) {
93                v.paint(g, textRect);
94             } else {
95                paintText(g, b, textRect, text);
96             }
97         }
98     
99         // draw the dashed focus line.
100
if (b.isFocusPainted() && b.hasFocus()) {
101         paintFocus(g, b, viewRect, textRect, iconRect);
102         }
103     }
104
105     protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
106         ButtonModel model = b.getModel();
107         Icon icon = null;
108         
109         if(!model.isEnabled()) {
110         if(model.isSelected()) {
111                icon = (Icon) b.getDisabledSelectedIcon();
112         } else {
113                icon = (Icon) b.getDisabledIcon();
114         }
115         } else if(model.isPressed() && model.isArmed()) {
116             icon = (Icon) b.getPressedIcon();
117             if(icon == null) {
118                 // Use selected icon
119
icon = (Icon) b.getSelectedIcon();
120             }
121         } else if(model.isSelected()) {
122             if(b.isRolloverEnabled() && model.isRollover()) {
123         icon = (Icon) b.getRolloverSelectedIcon();
124         if (icon == null) {
125             icon = (Icon) b.getSelectedIcon();
126         }
127             } else {
128         icon = (Icon) b.getSelectedIcon();
129             }
130         } else if(b.isRolloverEnabled() && model.isRollover()) {
131             icon = (Icon) b.getRolloverIcon();
132         }
133         
134         if(icon == null) {
135             icon = (Icon) b.getIcon();
136         }
137         
138         icon.paintIcon(b, g, iconRect.x, iconRect.y);
139     }
140
141     /**
142      * Overriden so that the text will not be rendered as shifted for
143      * Toggle buttons and subclasses.
144      */

145     protected int getTextShiftOffset() {
146     return 0;
147     }
148
149 }
150
Popular Tags