1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.Adjustable ; 34 import java.awt.Color ; 35 import java.awt.Graphics ; 36 import java.awt.Rectangle ; 37 38 import javax.swing.JButton ; 39 import javax.swing.JComponent ; 40 import javax.swing.UIManager ; 41 import javax.swing.plaf.ComponentUI ; 42 import javax.swing.plaf.metal.MetalScrollBarUI ; 43 44 45 53 54 public final class PlasticScrollBarUI extends MetalScrollBarUI { 55 56 private static final String PROPERTY_PREFIX = "ScrollBar."; 57 public static final String MAX_BUMPS_WIDTH_KEY = PROPERTY_PREFIX + "maxBumpsWidth"; 58 59 private static Color shadowColor; 60 private static Color highlightColor; 61 private static Color darkShadowColor; 62 private static Color thumbColor; 63 private static Color thumbShadow; 64 private static Color thumbHighlightColor; 65 66 private PlasticBumps bumps; 67 68 69 public static ComponentUI createUI(JComponent b) { 70 return new PlasticScrollBarUI(); 71 } 72 73 74 protected void installDefaults() { 75 super.installDefaults(); 76 bumps = new PlasticBumps(10, 10, thumbHighlightColor, thumbShadow, thumbColor); 77 } 78 79 80 protected JButton createDecreaseButton(int orientation) { 81 decreaseButton = new PlasticArrowButton(orientation, scrollBarWidth, isFreeStanding); 82 return decreaseButton; 83 } 84 85 86 protected JButton createIncreaseButton(int orientation) { 87 increaseButton = new PlasticArrowButton(orientation, scrollBarWidth, isFreeStanding); 88 return increaseButton; 89 } 90 91 92 protected void configureScrollBarColors() { 93 super.configureScrollBarColors(); 94 shadowColor = UIManager.getColor(PROPERTY_PREFIX + "shadow"); 95 highlightColor = UIManager.getColor(PROPERTY_PREFIX + "highlight"); 96 darkShadowColor = UIManager.getColor(PROPERTY_PREFIX + "darkShadow"); 97 thumbColor = UIManager.getColor(PROPERTY_PREFIX + "thumb"); 98 thumbShadow = UIManager.getColor(PROPERTY_PREFIX + "thumbShadow"); 99 thumbHighlightColor = UIManager.getColor(PROPERTY_PREFIX + "thumbHighlight"); 100 } 101 102 103 protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) { 104 g.translate(trackBounds.x, trackBounds.y); 105 106 boolean leftToRight = PlasticUtils.isLeftToRight(c); 107 108 if (scrollbar.getOrientation() == Adjustable.VERTICAL) { 109 if (!isFreeStanding) { 110 if (!leftToRight) { 111 trackBounds.width += 1; 112 g.translate(-1, 0); 113 } else { 114 trackBounds.width += 2; 115 } 116 } 117 118 if (c.isEnabled()) { 119 g.setColor(darkShadowColor); 120 g.drawLine(0, 0, 0, trackBounds.height - 1); 121 g.drawLine(trackBounds.width - 2, 0, trackBounds.width - 2, trackBounds.height - 1); 122 g.drawLine(1, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1); 123 g.drawLine(1, 0, trackBounds.width - 2, 0); 124 125 g.setColor(shadowColor); 126 g.drawLine(1, 1, 1, trackBounds.height - 2); 128 g.drawLine(1, 1, trackBounds.width - 3, 1); 129 if (scrollbar.getValue() != scrollbar.getMaximum()) { int y = thumbRect.y + thumbRect.height - trackBounds.y; 131 g.drawLine(1, y, trackBounds.width - 1, y); 132 } 133 g.setColor(highlightColor); 134 g.drawLine(trackBounds.width - 1, 0, trackBounds.width - 1, trackBounds.height - 1); 135 } else { 136 PlasticUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height); 137 } 138 139 if (!isFreeStanding) { 140 if (!leftToRight) { 141 trackBounds.width -= 1; 142 g.translate(1, 0); 143 } else { 144 trackBounds.width -= 2; 145 } 146 } 147 } else { if (!isFreeStanding) { 149 trackBounds.height += 2; 150 } 151 152 if (c.isEnabled()) { 153 g.setColor(darkShadowColor); 154 g.drawLine(0, 0, trackBounds.width - 1, 0); g.drawLine(0, 1, 0, trackBounds.height - 2); g.drawLine(0, trackBounds.height - 2, trackBounds.width - 1, trackBounds.height - 2); 157 g.drawLine(trackBounds.width - 1, 1, trackBounds.width - 1, trackBounds.height - 1); 159 160 g.setColor(shadowColor); 162 g.drawLine(1, 1, trackBounds.width - 2, 1); g.drawLine(1, 1, 1, trackBounds.height - 3); g.drawLine(0, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1); 166 if (scrollbar.getValue() != scrollbar.getMaximum()) { int x = thumbRect.x + thumbRect.width - trackBounds.x; 169 g.drawLine(x, 1, x, trackBounds.height - 1); 170 } 171 } else { 172 PlasticUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height); 173 } 174 175 if (!isFreeStanding) { 176 trackBounds.height -= 2; 177 } 178 } 179 g.translate(-trackBounds.x, -trackBounds.y); 180 } 181 182 183 protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) { 184 if (!c.isEnabled()) { 185 return; 186 } 187 188 boolean leftToRight = PlasticUtils.isLeftToRight(c); 189 190 g.translate(thumbBounds.x, thumbBounds.y); 191 192 if (scrollbar.getOrientation() == Adjustable.VERTICAL) { 193 if (!isFreeStanding) { 194 if (!leftToRight) { 195 thumbBounds.width += 1; 196 g.translate(-1, 0); 197 } else { 198 thumbBounds.width += 2; 199 } 200 201 } 202 203 g.setColor(thumbColor); 204 g.fillRect(0, 0, thumbBounds.width - 2, thumbBounds.height - 1); 205 206 g.setColor(thumbShadow); 207 g.drawRect(0, 0, thumbBounds.width - 2, thumbBounds.height - 1); 208 209 g.setColor(thumbHighlightColor); 210 g.drawLine(1, 1, thumbBounds.width - 3, 1); 211 g.drawLine(1, 1, 1, thumbBounds.height - 2); 212 213 paintBumps(g, c, 3, 4, thumbBounds.width - 6, thumbBounds.height - 7); 214 215 if (!isFreeStanding) { 216 if (!leftToRight) { 217 thumbBounds.width -= 1; 218 g.translate(1, 0); 219 } else { 220 thumbBounds.width -= 2; 221 } 222 } 223 } else { if (!isFreeStanding) { 225 thumbBounds.height += 2; 226 } 227 228 g.setColor(thumbColor); 229 g.fillRect(0, 0, thumbBounds.width - 1, thumbBounds.height - 2); 230 231 g.setColor(thumbShadow); 232 g.drawRect(0, 0, thumbBounds.width - 1, thumbBounds.height - 2); 233 234 g.setColor(thumbHighlightColor); 235 g.drawLine(1, 1, thumbBounds.width - 2, 1); 236 g.drawLine(1, 1, 1, thumbBounds.height - 3); 237 238 paintBumps(g, c, 4, 3, thumbBounds.width - 7, thumbBounds.height - 6); 239 240 if (!isFreeStanding) { 241 thumbBounds.height -= 2; 242 } 243 } 244 g.translate(-thumbBounds.x, -thumbBounds.y); 245 246 if (PlasticUtils.is3D(PROPERTY_PREFIX)) 247 paintThumb3D(g, thumbBounds); 248 249 } 250 251 252 private void paintBumps(Graphics g, JComponent c, int x, int y, int width, int height) { 253 if (!useNarrowBumps()) { 254 bumps.setBumpArea(width, height); 255 bumps.paintIcon(c, g, x, y); 256 } else { 257 int MAX_WIDTH= UIManager.getInt(MAX_BUMPS_WIDTH_KEY); 258 int myWidth = Math.min(MAX_WIDTH, width); 259 int myHeight = Math.min(MAX_WIDTH, height); 260 int myX = x + (width - myWidth) / 2; 261 int myY = y + (height - myHeight) / 2; 262 bumps.setBumpArea(myWidth, myHeight); 263 bumps.paintIcon(c, g, myX, myY); 264 } 265 } 266 267 268 private void paintThumb3D(Graphics g, Rectangle thumbBounds) { 269 boolean isHorizontal = scrollbar.getOrientation() == Adjustable.HORIZONTAL; 270 int width = thumbBounds.width - (isHorizontal ? 3 : 1); 271 int height = thumbBounds.height - (isHorizontal ? 1 : 3); 272 Rectangle r = new Rectangle (thumbBounds.x + 2, thumbBounds.y + 2, width, height); 273 PlasticUtils.addLight3DEffekt(g, r, isHorizontal); 274 } 275 276 277 279 private boolean useNarrowBumps() { 280 Object value = UIManager.get(MAX_BUMPS_WIDTH_KEY); 281 return value != null && value instanceof Integer ; 282 } 283 } | Popular Tags |