1 19 20 package org.netbeans.swing.tabcontrol.plaf; 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.awt.FontMetrics ; 25 import java.awt.Graphics ; 26 import java.awt.Graphics2D ; 27 import java.awt.Insets ; 28 import java.awt.geom.AffineTransform ; 29 import javax.swing.AbstractButton ; 30 import javax.swing.BorderFactory ; 31 import javax.swing.Icon ; 32 import javax.swing.JComponent ; 33 import javax.swing.plaf.ComponentUI ; 34 import javax.swing.plaf.basic.BasicToggleButtonUI ; 35 import org.netbeans.swing.tabcontrol.TabDisplayer; 36 import org.openide.awt.HtmlRenderer; 37 38 53 public class SlidingTabDisplayerButtonUI extends BasicToggleButtonUI { 54 private static final SlidingTabDisplayerButtonUI INSTANCE = new SlidingTabDisplayerButtonUI(); 55 56 private SlidingTabDisplayerButtonUI() { 57 } 58 59 public static ComponentUI createUI(JComponent c) { 60 return INSTANCE; 61 } 62 63 65 public void installUI(JComponent c) { 66 installDefaults((AbstractButton ) c); 67 installListeners((AbstractButton ) c); 68 installBorder((AbstractButton ) c); 69 } 70 71 72 protected void installBorder (AbstractButton b) { 73 b.setBorder (BorderFactory.createEtchedBorder()); 74 } 75 76 78 public void uninstallUI(JComponent c) { 79 uninstallListeners((AbstractButton ) c); 80 uninstallDefaults((AbstractButton ) c); 81 } 82 83 85 public void installDefaults (AbstractButton b) { 86 b.setFocusable (false); 87 } 88 89 public Dimension getMinimumSize(JComponent c) { 90 return getPreferredSize(c); 91 } 92 93 public Dimension getPreferredSize(JComponent c) { 94 return null; } 96 97 public Dimension getMaximumSize(JComponent c) { 98 return getPreferredSize(c); 99 } 100 101 103 public final void paint(Graphics g, JComponent c) { 104 105 BasicSlidingTabDisplayerUI.IndexButton b = 106 (BasicSlidingTabDisplayerUI.IndexButton) c; 107 108 Graphics2D g2d = (Graphics2D ) g; 109 110 paintBackground (g2d, b); 111 112 Object orientation = b.getOrientation(); 113 114 AffineTransform tr = g2d.getTransform(); 115 if (orientation == TabDisplayer.ORIENTATION_EAST) { 116 g2d.rotate( Math.PI / 2 ); 117 g2d.translate( 0, - c.getWidth() ); 118 } else if (orientation == TabDisplayer.ORIENTATION_WEST) { 119 g2d.rotate(-Math.PI / 2); 120 g2d.translate(-c.getHeight(), 0); 121 } 122 123 paintIconAndText (g2d, b, orientation); 124 g2d.setTransform (tr); 125 } 126 127 128 protected void paintBackground (Graphics2D g, BasicSlidingTabDisplayerUI.IndexButton b) { 129 Color c = b.isSelected() ? Color.ORANGE : b.getBackground(); 130 g.setColor (c); 131 g.fillRect (0, 0, b.getWidth(), b.getHeight()); 132 } 133 134 135 protected final void paintIconAndText (Graphics2D g, BasicSlidingTabDisplayerUI.IndexButton b, Object orientation) { 136 FontMetrics fm = g.getFontMetrics(b.getFont()); 137 Insets ins = b.getInsets(); 138 139 boolean flip = orientation == TabDisplayer.ORIENTATION_EAST || 140 orientation == TabDisplayer.ORIENTATION_WEST; 141 142 int txtX = flip ? ins.top : ins.left; 143 144 int txtY = orientation == TabDisplayer.ORIENTATION_EAST ? ins.right : 145 orientation == TabDisplayer.ORIENTATION_WEST ? ins.left : ins.top; 146 147 int txtW = flip ? b.getHeight() - (ins.top + ins.bottom): 148 b.getWidth() - (ins.left + ins.right); 149 150 int iconX = txtX; 151 int iconY = txtY; 152 153 int txtH = fm.getHeight(); 154 txtY += fm.getMaxAscent(); 155 156 Icon icon = b.getIcon(); 157 158 int iconH = icon.getIconHeight(); 159 int iconW = icon.getIconWidth(); 160 161 int workingHeight; 162 if (flip) { 163 workingHeight = b.getWidth() - (ins.left + ins.right); 164 } else { 165 workingHeight = b.getHeight() - (ins.top + ins.bottom); 166 } 167 txtY += (workingHeight / 2) - (txtH / 2); 168 iconY += (workingHeight / 2) - (iconH / 2); 169 170 if (icon != null && iconW > 0 && iconH > 0) { 171 txtX += iconW + b.getIconTextGap(); 172 icon.paintIcon (b, g, iconX, iconY); 173 txtW -= iconH + b.getIconTextGap(); 174 } 175 176 HtmlRenderer.renderString(b.getText(), g, txtX, txtY, txtW, txtH, b.getFont(), 177 b.getForeground(), HtmlRenderer.STYLE_TRUNCATE, true); 178 } 179 180 private static SlidingTabDisplayerButtonUI AQUA_INSTANCE = null; 181 182 184 public static final class Aqua extends SlidingTabDisplayerButtonUI { 185 public static ComponentUI createUI(JComponent c) { 186 if (AQUA_INSTANCE == null) { 187 AQUA_INSTANCE = new Aqua(); 188 } 189 return AQUA_INSTANCE; 190 } 191 192 protected void installBorder (AbstractButton b) { 193 b.setBorder (BorderFactory.createEmptyBorder (5,2,2,2)); 194 } 195 196 protected void paintBackground (Graphics2D g, BasicSlidingTabDisplayerUI.IndexButton b) { 197 GenericGlowingChiclet chic = GenericGlowingChiclet.INSTANCE; 198 int state = 0; 199 state |= b.isSelected() ? chic.STATE_SELECTED : 0; 200 state |= b.getModel().isPressed() ? chic.STATE_PRESSED : 0; 201 state |= b.isActive() ? chic.STATE_ACTIVE : 0; 202 203 chic.setState(state); 204 chic.setArcs(0.2f, 0.2f, 0.2f, 0.2f); 205 chic.setBounds (0, 1, b.getWidth(), b.getHeight()); 206 chic.setAllowVertical(true); 207 chic.draw(g); 208 chic.setAllowVertical(false); 209 } 210 } 211 } 212 | Popular Tags |