1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.*; 34 import java.beans.PropertyChangeListener ; 35 36 import javax.swing.AbstractButton ; 37 import javax.swing.ButtonModel ; 38 import javax.swing.JComponent ; 39 import javax.swing.JToolBar ; 40 import javax.swing.SwingUtilities ; 41 import javax.swing.UIManager ; 42 import javax.swing.border.EmptyBorder ; 43 import javax.swing.plaf.ComponentUI ; 44 import javax.swing.plaf.basic.BasicHTML ; 45 import javax.swing.plaf.metal.MetalToggleButtonUI ; 46 import javax.swing.text.View ; 47 48 import com.jgoodies.looks.LookUtils; 49 import com.jgoodies.looks.Options; 50 import com.jgoodies.looks.common.ButtonMarginListener; 51 52 63 public class PlasticToggleButtonUI extends MetalToggleButtonUI { 64 65 private static final PlasticToggleButtonUI INSTANCE = 66 new PlasticToggleButtonUI(); 67 68 74 protected static final String HTML_KEY = BasicHTML.propertyKey; 75 76 private boolean borderPaintsFocus; 77 private PropertyChangeListener buttonMarginListener; 78 79 public static ComponentUI createUI(JComponent b) { 80 return INSTANCE; 81 } 82 83 86 public void installDefaults(AbstractButton b) { 87 super.installDefaults(b); 88 LookUtils.installNarrowMargin(b, getPropertyPrefix()); 89 borderPaintsFocus = 90 Boolean.TRUE.equals( 91 UIManager.get("ToggleButton.borderPaintsFocus")); 92 } 93 94 97 public void installListeners(AbstractButton b) { 98 super.installListeners(b); 99 if (buttonMarginListener == null) { 100 buttonMarginListener = new ButtonMarginListener(getPropertyPrefix()); 101 } 102 b.addPropertyChangeListener(Options.IS_NARROW_KEY, buttonMarginListener); 103 } 104 105 108 public void uninstallListeners(AbstractButton b) { 109 super.uninstallListeners(b); 110 b.removePropertyChangeListener(buttonMarginListener); 111 } 112 113 115 public void update(Graphics g, JComponent c) { 116 AbstractButton b = (AbstractButton ) c; 117 if (c.isOpaque()) { 118 if (isToolBarButton(b)) { 119 c.setOpaque(false); 120 } else if (b.isContentAreaFilled()) { 121 g.setColor(c.getBackground()); 122 g.fillRect(0, 0, c.getWidth(), c.getHeight()); 123 124 if (is3D(b)) { 125 Rectangle r = 126 new Rectangle( 127 1, 128 1, 129 c.getWidth() - 2, 130 c.getHeight() - 1); 131 PlasticUtils.add3DEffekt(g, r); 132 } 133 } 134 } 135 paint(g, c); 136 } 137 138 141 protected void paintFocus( 142 Graphics g, 143 AbstractButton b, 144 Rectangle viewRect, 145 Rectangle textRect, 146 Rectangle iconRect) { 147 148 if (borderPaintsFocus) 149 return; 150 151 boolean isDefault = false; 152 int topLeftInset = isDefault ? 3 : 2; 153 int width = b.getWidth() - 1 - topLeftInset * 2; 154 int height = b.getHeight() - 1 - topLeftInset * 2; 155 156 g.setColor(getFocusColor()); 157 g.drawRect(topLeftInset, topLeftInset, width - 1, height - 1); 158 } 159 160 164 public void paint(Graphics g, JComponent c) { 165 AbstractButton b = (AbstractButton ) c; 166 ButtonModel model = b.getModel(); 167 168 Dimension size = b.getSize(); 169 FontMetrics fm = g.getFontMetrics(); 170 171 Insets i = c.getInsets(); 172 173 Rectangle viewRect = new Rectangle(size); 174 175 viewRect.x += i.left; 176 viewRect.y += i.top; 177 viewRect.width -= (i.right + viewRect.x); 178 viewRect.height -= (i.bottom + viewRect.y); 179 180 Rectangle iconRect = new Rectangle(); 181 Rectangle textRect = new Rectangle(); 182 183 Font f = c.getFont(); 184 g.setFont(f); 185 186 String text = 188 SwingUtilities.layoutCompoundLabel( 189 c, 190 fm, 191 b.getText(), 192 b.getIcon(), 193 b.getVerticalAlignment(), 194 b.getHorizontalAlignment(), 195 b.getVerticalTextPosition(), 196 b.getHorizontalTextPosition(), 197 viewRect, 198 iconRect, 199 textRect, 200 b.getText() == null ? 0 : b.getIconTextGap()); 201 202 g.setColor(b.getBackground()); 203 204 if (model.isArmed() && model.isPressed() || model.isSelected()) { 205 paintButtonPressed(g, b); 206 } 217 218 if (b.getIcon() != null) { 220 paintIcon(g, b, iconRect); 221 } 222 223 if (text != null && !text.equals("")) { 225 View v = (View ) c.getClientProperty(HTML_KEY); 226 if (v != null) { 227 v.paint(g, textRect); 228 } else { 229 paintText(g, c, textRect, text); 230 } 231 } 232 233 if (b.isFocusPainted() && b.hasFocus()) { 235 paintFocus(g, b, viewRect, textRect, iconRect); 236 } 237 } 238 239 241 247 protected boolean isToolBarButton(AbstractButton b) { 248 Container parent = b.getParent(); 249 return parent != null 250 && (parent instanceof JToolBar 251 || parent.getParent() instanceof JToolBar ); 252 } 253 254 260 protected boolean is3D(AbstractButton b) { 261 if (PlasticUtils.force3D(b)) 262 return true; 263 if (PlasticUtils.forceFlat(b)) 264 return false; 265 ButtonModel model = b.getModel(); 266 return PlasticUtils.is3D("ToggleButton.") 267 && b.isBorderPainted() 268 && model.isEnabled() 269 && !model.isPressed() 270 && !(b.getBorder() instanceof EmptyBorder ); 271 272 278 } 279 280 } | Popular Tags |