Showing the changes to java/pkg/javax/swing/plaf/basic/BasicToggleButtonUI.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,175 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997, 2008, 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 | * <p> | ||
| 43 | * | ||
| 44 | * @author Jeff Dinkins | ||
| 45 | */ | ||
| 46 | public class BasicToggleButtonUI extends BasicButtonUI { | ||
| 47 | |||
| 48 | private static final Object BASIC_TOGGLE_BUTTON_UI_KEY = new Object(); | ||
| 49 | |||
| 50 | private final static String propertyPrefix = "ToggleButton" + "."; | ||
| 51 | |||
| 52 | // ******************************** | ||
| 53 | // Create PLAF | ||
| 54 | // ******************************** | ||
| 55 | public static ComponentUI createUI(JComponent b) { | ||
| 56 | AppContext appContext = AppContext.getAppContext(); | ||
| 57 | BasicToggleButtonUI toggleButtonUI = | ||
| 58 | (BasicToggleButtonUI) appContext.get(BASIC_TOGGLE_BUTTON_UI_KEY); | ||
| 59 | if (toggleButtonUI == null) { | ||
| 60 | toggleButtonUI = new BasicToggleButtonUI(); | ||
| 61 | appContext.put(BASIC_TOGGLE_BUTTON_UI_KEY, toggleButtonUI); | ||
| 62 | } | ||
| 63 | return toggleButtonUI; | ||
| 64 | } | ||
| 65 | |||
| 66 | protected String getPropertyPrefix() { | ||
| 67 | return propertyPrefix; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | // ******************************** | ||
| 72 | // Paint Methods | ||
| 73 | // ******************************** | ||
| 74 | public void paint(Graphics g, JComponent c) { | ||
| 75 | AbstractButton b = (AbstractButton) c; | ||
| 76 | ButtonModel model = b.getModel(); | ||
| 77 | |||
| 78 | Dimension size = b.getSize(); | ||
| 79 | FontMetrics fm = g.getFontMetrics(); | ||
| 80 | |||
| 81 | Insets i = c.getInsets(); | ||
| 82 | |||
| 83 | Rectangle viewRect = new Rectangle(size); | ||
| 84 | |||
| 85 | viewRect.x += i.left; | ||
| 86 | viewRect.y += i.top; | ||
| 87 | viewRect.width -= (i.right + viewRect.x); | ||
| 88 | viewRect.height -= (i.bottom + viewRect.y); | ||
| 89 | |||
| 90 | Rectangle iconRect = new Rectangle(); | ||
| 91 | Rectangle textRect = new Rectangle(); | ||
| 92 | |||
| 93 | Font f = c.getFont(); | ||
| 94 | g.setFont(f); | ||
| 95 | |||
| 96 | // layout the text and icon | ||
| 97 | String text = SwingUtilities.layoutCompoundLabel( | ||
| 98 | c, fm, b.getText(), b.getIcon(), | ||
| 99 | b.getVerticalAlignment(), b.getHorizontalAlignment(), | ||
| 100 | b.getVerticalTextPosition(), b.getHorizontalTextPosition(), | ||
| 101 | viewRect, iconRect, textRect, | ||
| 102 | b.getText() == null ? 0 : b.getIconTextGap()); | ||
| 103 | |||
| 104 | g.setColor(b.getBackground()); | ||
| 105 | |||
| 106 | if (model.isArmed() && model.isPressed() || model.isSelected()) { | ||
| 107 | paintButtonPressed(g,b); | ||
| 108 | } | ||
| 109 | |||
| 110 | // Paint the Icon | ||
| 111 | if(b.getIcon() != null) { | ||
| 112 | paintIcon(g, b, iconRect); | ||
| 113 | } | ||
| 114 | |||
| 115 | // Draw the Text | ||
| 116 | if(text != null && !text.equals("")) { | ||
| 117 | View v = (View) c.getClientProperty(BasicHTML.propertyKey); | ||
| 118 | if (v != null) { | ||
| 119 | v.paint(g, textRect); | ||
| 120 | } else { | ||
| 121 | paintText(g, b, textRect, text); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | // draw the dashed focus line. | ||
| 126 | if (b.isFocusPainted() && b.hasFocus()) { | ||
| 127 | paintFocus(g, b, viewRect, textRect, iconRect); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) { | ||
| 132 | ButtonModel model = b.getModel(); | ||
| 133 | Icon icon = null; | ||
| 134 | |||
| 135 | if(!model.isEnabled()) { | ||
| 136 | if(model.isSelected()) { | ||
| 137 | icon = b.getDisabledSelectedIcon(); | ||
| 138 | } else { | ||
| 139 | icon = b.getDisabledIcon(); | ||
| 140 | } | ||
| 141 | } else if(model.isPressed() && model.isArmed()) { | ||
| 142 | icon = b.getPressedIcon(); | ||
| 143 | if(icon == null) { | ||
| 144 | // Use selected icon | ||
| 145 | icon = b.getSelectedIcon(); | ||
| 146 | } | ||
| 147 | } else if(model.isSelected()) { | ||
| 148 | if(b.isRolloverEnabled() && model.isRollover()) { | ||
| 149 | icon = b.getRolloverSelectedIcon(); | ||
| 150 | if (icon == null) { | ||
| 151 | icon = b.getSelectedIcon(); | ||
| 152 | } | ||
| 153 | } else { | ||
| 154 | icon = b.getSelectedIcon(); | ||
| 155 | } | ||
| 156 | } else if(b.isRolloverEnabled() && model.isRollover()) { | ||
| 157 | icon = b.getRolloverIcon(); | ||
| 158 | } | ||
| 159 | |||
| 160 | if(icon == null) { | ||
| 161 | icon = b.getIcon(); | ||
| 162 | } | ||
| 163 | |||
| 164 | icon.paintIcon(b, g, iconRect.x, iconRect.y); | ||
| 165 | } | ||
| 166 | |||
| 167 | /** | ||
| 168 | * Overriden so that the text will not be rendered as shifted for | ||
| 169 | * Toggle buttons and subclasses. | ||
| 170 | */ | ||
| 171 | protected int getTextShiftOffset() { | ||
| 172 | return 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | } | ||