1 /*
2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.swing.plaf.basic;
27 
28 import sun.awt.AppContext;
29 
30 import java.awt.*;
31 import java.awt.event.*;
32 
33 import javax.swing.*;
34 import javax.swing.border.*;
35 import javax.swing.plaf.*;
36 import javax.swing.text.View;
37 
38 
39 
40 /**
41  * BasicToggleButton implementation
42  *
43  * @author Jeff Dinkins
44  */
45 public class BasicToggleButtonUI extends BasicButtonUI {
46 
47     private static final Object BASIC_TOGGLE_BUTTON_UI_KEY = new Object();
48 
49     private static final String propertyPrefix = "ToggleButton" + ".";
50 
51     // ********************************
52     //          Create PLAF
53     // ********************************
54 
55     /**
56      * Returns an instance of {@code BasicToggleButtonUI}.
57      *
58      * @param b a component
59      * @return an instance of {@code BasicToggleButtonUI}
60      */
createUI(JComponent b)61     public static ComponentUI createUI(JComponent b) {
62         AppContext appContext = AppContext.getAppContext();
63         BasicToggleButtonUI toggleButtonUI =
64                 (BasicToggleButtonUI) appContext.get(BASIC_TOGGLE_BUTTON_UI_KEY);
65         if (toggleButtonUI == null) {
66             toggleButtonUI = new BasicToggleButtonUI();
67             appContext.put(BASIC_TOGGLE_BUTTON_UI_KEY, toggleButtonUI);
68         }
69         return toggleButtonUI;
70     }
71 
getPropertyPrefix()72     protected String getPropertyPrefix() {
73         return propertyPrefix;
74     }
75 
76 
77     // ********************************
78     //          Paint Methods
79     // ********************************
paint(Graphics g, JComponent c)80     public void paint(Graphics g, JComponent c) {
81         AbstractButton b = (AbstractButton) c;
82         ButtonModel model = b.getModel();
83 
84         Dimension size = b.getSize();
85         FontMetrics fm = g.getFontMetrics();
86 
87         Insets i = c.getInsets();
88 
89         Rectangle viewRect = new Rectangle(size);
90 
91         viewRect.x += i.left;
92         viewRect.y += i.top;
93         viewRect.width -= (i.right + viewRect.x);
94         viewRect.height -= (i.bottom + viewRect.y);
95 
96         Rectangle iconRect = new Rectangle();
97         Rectangle textRect = new Rectangle();
98 
99         Font f = c.getFont();
100         g.setFont(f);
101 
102         // layout the text and icon
103         String text = SwingUtilities.layoutCompoundLabel(
104             c, fm, b.getText(), b.getIcon(),
105             b.getVerticalAlignment(), b.getHorizontalAlignment(),
106             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
107             viewRect, iconRect, textRect,
108             b.getText() == null ? 0 : b.getIconTextGap());
109 
110         g.setColor(b.getBackground());
111 
112         if (model.isArmed() && model.isPressed() || model.isSelected()) {
113             paintButtonPressed(g,b);
114         }
115 
116         // Paint the Icon
117         if(b.getIcon() != null) {
118             paintIcon(g, b, iconRect);
119         }
120 
121         // Draw the Text
122         if(text != null && !text.equals("")) {
123             View v = (View) c.getClientProperty(BasicHTML.propertyKey);
124             if (v != null) {
125                v.paint(g, textRect);
126             } else {
127                paintText(g, b, textRect, text);
128             }
129         }
130 
131         // draw the dashed focus line.
132         if (b.isFocusPainted() && b.hasFocus()) {
133             paintFocus(g, b, viewRect, textRect, iconRect);
134         }
135     }
136 
137     /**
138      * Paints an icon in the specified location.
139      *
140      * @param g an instance of {@code Graphics}
141      * @param b an instance of {@code Button}
142      * @param iconRect bounds of an icon
143      */
paintIcon(Graphics g, AbstractButton b, Rectangle iconRect)144     protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
145         ButtonModel model = b.getModel();
146         Icon icon = null;
147 
148         if(!model.isEnabled()) {
149             if(model.isSelected()) {
150                icon = b.getDisabledSelectedIcon();
151             } else {
152                icon = b.getDisabledIcon();
153             }
154         } else if(model.isPressed() && model.isArmed()) {
155             icon = b.getPressedIcon();
156             if(icon == null) {
157                 // Use selected icon
158                 icon = b.getSelectedIcon();
159             }
160         } else if(model.isSelected()) {
161             if(b.isRolloverEnabled() && model.isRollover()) {
162                 icon = b.getRolloverSelectedIcon();
163                 if (icon == null) {
164                     icon = b.getSelectedIcon();
165                 }
166             } else {
167                 icon = b.getSelectedIcon();
168             }
169         } else if(b.isRolloverEnabled() && model.isRollover()) {
170             icon = b.getRolloverIcon();
171         }
172 
173         if(icon == null) {
174             icon = b.getIcon();
175         }
176 
177         icon.paintIcon(b, g, iconRect.x, iconRect.y);
178     }
179 
180     /**
181      * Overriden so that the text will not be rendered as shifted for
182      * Toggle buttons and subclasses.
183      */
getTextShiftOffset()184     protected int getTextShiftOffset() {
185         return 0;
186     }
187 
188 }
189