1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.Container ; 34 import java.awt.Graphics ; 35 import java.awt.Rectangle ; 36 import java.beans.PropertyChangeListener ; 37 38 import javax.swing.AbstractButton ; 39 import javax.swing.ButtonModel ; 40 import javax.swing.JButton ; 41 import javax.swing.JComponent ; 42 import javax.swing.JToolBar ; 43 import javax.swing.UIManager ; 44 import javax.swing.border.EmptyBorder ; 45 import javax.swing.plaf.ComponentUI ; 46 import javax.swing.plaf.metal.MetalButtonUI ; 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 PlasticButtonUI extends MetalButtonUI { 64 65 private static final PlasticButtonUI INSTANCE = new PlasticButtonUI(); 66 67 private boolean borderPaintsFocus; 68 private PropertyChangeListener buttonMarginListener; 69 70 public static ComponentUI createUI(JComponent b) { 71 return INSTANCE; 72 } 73 74 77 public void installDefaults(AbstractButton b) { 78 super.installDefaults(b); 79 LookUtils.installNarrowMargin(b, getPropertyPrefix()); 80 borderPaintsFocus = 81 Boolean.TRUE.equals(UIManager.get("Button.borderPaintsFocus")); 82 } 83 84 87 public void installListeners(AbstractButton b) { 88 super.installListeners(b); 89 if (buttonMarginListener == null) { 90 buttonMarginListener = new ButtonMarginListener(getPropertyPrefix()); 91 } 92 b.addPropertyChangeListener(Options.IS_NARROW_KEY, buttonMarginListener); 93 } 94 95 98 public void uninstallListeners(AbstractButton b) { 99 super.uninstallListeners(b); 100 b.removePropertyChangeListener(buttonMarginListener); 101 } 102 103 105 public void update(Graphics g, JComponent c) { 106 if (c.isOpaque()) { 107 AbstractButton b = (AbstractButton ) c; 108 if (isToolBarButton(b)) { 109 c.setOpaque(false); 110 } else if (b.isContentAreaFilled()) { 111 g.setColor(c.getBackground()); 112 g.fillRect(0, 0, c.getWidth(), c.getHeight()); 113 114 if (is3D(b)) { 115 Rectangle r = 116 new Rectangle ( 117 1, 118 1, 119 c.getWidth() - 2, 120 c.getHeight() - 1); 121 PlasticUtils.add3DEffekt(g, r); 122 } 123 } 124 } 125 paint(g, c); 126 } 127 128 131 protected void paintFocus( 132 Graphics g, 133 AbstractButton b, 134 Rectangle viewRect, 135 Rectangle textRect, 136 Rectangle iconRect) { 137 138 if (borderPaintsFocus) { 139 return; 140 } 141 142 boolean isDefault = 143 b instanceof JButton && ((JButton ) b).isDefaultButton(); 144 int topLeftInset = isDefault ? 3 : 2; 145 int width = b.getWidth() - 1 - topLeftInset * 2; 146 int height = b.getHeight() - 1 - topLeftInset * 2; 147 148 g.setColor(getFocusColor()); 149 g.drawRect(topLeftInset, topLeftInset, width - 1, height - 1); 150 } 151 152 154 160 protected boolean isToolBarButton(AbstractButton b) { 161 Container parent = b.getParent(); 162 return parent != null 163 && (parent instanceof JToolBar 164 || parent.getParent() instanceof JToolBar ); 165 } 166 167 173 protected boolean is3D(AbstractButton b) { 174 if (PlasticUtils.force3D(b)) 175 return true; 176 if (PlasticUtils.forceFlat(b)) 177 return false; 178 ButtonModel model = b.getModel(); 179 return PlasticUtils.is3D("Button.") 180 && b.isBorderPainted() 181 && model.isEnabled() 182 && !(model.isPressed() && model.isArmed()) 183 && !(b.getBorder() instanceof EmptyBorder ); 184 185 191 } 192 193 } | Popular Tags |