1 7 8 package javax.swing.plaf.metal; 9 10 import java.awt.Dimension ; 11 import java.awt.Graphics ; 12 import java.awt.Color ; 13 import java.awt.Polygon ; 14 15 import javax.swing.*; 16 17 import javax.swing.plaf.basic.BasicArrowButton ; 18 19 20 36 public class MetalScrollButton extends BasicArrowButton 37 { 38 private static Color shadowColor; 39 private static Color highlightColor; 40 private boolean isFreeStanding = false; 41 42 private int buttonWidth; 43 44 public MetalScrollButton( int direction, int width, boolean freeStanding ) 45 { 46 super( direction ); 47 48 shadowColor = UIManager.getColor("ScrollBar.darkShadow"); 49 highlightColor = UIManager.getColor("ScrollBar.highlight"); 50 51 buttonWidth = width; 52 isFreeStanding = freeStanding; 53 } 54 55 public void setFreeStanding( boolean freeStanding ) 56 { 57 isFreeStanding = freeStanding; 58 } 59 60 public void paint( Graphics g ) 61 { 62 boolean leftToRight = MetalUtils.isLeftToRight(this); 63 boolean isEnabled = getParent().isEnabled(); 64 65 Color arrowColor = isEnabled ? MetalLookAndFeel.getControlInfo() : MetalLookAndFeel.getControlDisabled(); 66 boolean isPressed = getModel().isPressed(); 67 int width = getWidth(); 68 int height = getHeight(); 69 int w = width; 70 int h = height; 71 int arrowHeight = (height+1) / 4; 72 int arrowWidth = (height+1) / 2; 73 74 if ( isPressed ) 75 { 76 g.setColor( MetalLookAndFeel.getControlShadow() ); 77 } 78 else 79 { 80 g.setColor( getBackground() ); 81 } 82 83 g.fillRect( 0, 0, width, height ); 84 85 if ( getDirection() == NORTH ) 86 { 87 if ( !isFreeStanding ) { 88 height +=1; 89 g.translate( 0, -1 ); 90 width += 2; 91 if ( !leftToRight ) { 92 g.translate( -1, 0 ); 93 } 94 } 95 96 g.setColor( arrowColor ); 98 int startY = ((h+1) - arrowHeight) / 2; 99 int startX = (w / 2); 100 for (int line = 0; line < arrowHeight; line++) { 102 g.drawLine( startX-line, startY+line, startX +line+1, startY+line); 103 } 104 108 109 if (isEnabled) { 110 g.setColor( highlightColor ); 111 112 if ( !isPressed ) 113 { 114 g.drawLine( 1, 1, width - 3, 1 ); 115 g.drawLine( 1, 1, 1, height - 1 ); 116 } 117 118 g.drawLine( width - 1, 1, width - 1, height - 1 ); 119 120 g.setColor( shadowColor ); 121 g.drawLine( 0, 0, width - 2, 0 ); 122 g.drawLine( 0, 0, 0, height - 1 ); 123 g.drawLine( width - 2, 2, width - 2, height - 1 ); 124 } else { 125 MetalUtils.drawDisabledBorder(g, 0, 0, width, height+1); 126 } 127 if ( !isFreeStanding ) { 128 height -= 1; 129 g.translate( 0, 1 ); 130 width -= 2; 131 if ( !leftToRight ) { 132 g.translate( 1, 0 ); 133 } 134 } 135 } 136 else if ( getDirection() == SOUTH ) 137 { 138 if ( !isFreeStanding ) { 139 height += 1; 140 width += 2; 141 if ( !leftToRight ) { 142 g.translate( -1, 0 ); 143 } 144 } 145 146 g.setColor( arrowColor ); 148 149 int startY = (((h+1) - arrowHeight) / 2)+ arrowHeight-1; 150 int startX = (w / 2); 151 152 154 for (int line = 0; line < arrowHeight; line++) { 155 g.drawLine( startX-line, startY-line, startX +line+1, startY-line); 156 } 157 158 162 163 if (isEnabled) { 164 g.setColor( highlightColor ); 165 166 if ( !isPressed ) 167 { 168 g.drawLine( 1, 0, width - 3, 0 ); 169 g.drawLine( 1, 0, 1, height - 3 ); 170 } 171 172 g.drawLine( 1, height - 1, width - 1, height - 1 ); 173 g.drawLine( width - 1, 0, width - 1, height - 1 ); 174 175 g.setColor( shadowColor ); 176 g.drawLine( 0, 0, 0, height - 2 ); 177 g.drawLine( width - 2, 0, width - 2, height - 2 ); 178 g.drawLine( 2, height - 2, width - 2, height - 2 ); 179 } else { 180 MetalUtils.drawDisabledBorder(g, 0,-1, width, height+1); 181 } 182 183 if ( !isFreeStanding ) { 184 height -= 1; 185 width -= 2; 186 if ( !leftToRight ) { 187 g.translate( 1, 0 ); 188 } 189 } 190 } 191 else if ( getDirection() == EAST ) 192 { 193 if ( !isFreeStanding ) { 194 height += 2; 195 width += 1; 196 } 197 198 g.setColor( arrowColor ); 200 201 int startX = (((w+1) - arrowHeight) / 2) + arrowHeight-1; 202 int startY = (h / 2); 203 204 206 for (int line = 0; line < arrowHeight; line++) { 207 g.drawLine( startX-line, startY-line, startX -line, startY+line+1); 208 } 209 210 211 215 216 if (isEnabled) { 217 g.setColor( highlightColor ); 218 219 if ( !isPressed ) 220 { 221 g.drawLine( 0, 1, width - 3, 1 ); 222 g.drawLine( 0, 1, 0, height - 3 ); 223 } 224 225 g.drawLine( width - 1, 1, width - 1, height - 1 ); 226 g.drawLine( 0, height - 1, width - 1, height - 1 ); 227 228 g.setColor( shadowColor ); 229 g.drawLine( 0, 0,width - 2, 0 ); 230 g.drawLine( width - 2, 2, width - 2, height - 2 ); 231 g.drawLine( 0, height - 2, width - 2, height - 2 ); 232 } else { 233 MetalUtils.drawDisabledBorder(g,-1,0, width+1, height); 234 } 235 if ( !isFreeStanding ) { 236 height -= 2; 237 width -= 1; 238 } 239 } 240 else if ( getDirection() == WEST ) 241 { 242 if ( !isFreeStanding ) { 243 height += 2; 244 width += 1; 245 g.translate( -1, 0 ); 246 } 247 248 g.setColor( arrowColor ); 250 251 int startX = (((w+1) - arrowHeight) / 2); 252 int startY = (h / 2); 253 254 255 for (int line = 0; line < arrowHeight; line++) { 256 g.drawLine( startX+line, startY-line, startX +line, startY+line+1); 257 } 258 259 263 264 if (isEnabled) { 265 g.setColor( highlightColor ); 266 267 268 if ( !isPressed ) 269 { 270 g.drawLine( 1, 1, width - 1, 1 ); 271 g.drawLine( 1, 1, 1, height - 3 ); 272 } 273 274 g.drawLine( 1, height - 1, width - 1, height - 1 ); 275 276 g.setColor( shadowColor ); 277 g.drawLine( 0, 0, width - 1, 0 ); 278 g.drawLine( 0, 0, 0, height - 2 ); 279 g.drawLine( 2, height - 2, width - 1, height - 2 ); 280 } else { 281 MetalUtils.drawDisabledBorder(g,0,0, width+1, height); 282 } 283 284 if ( !isFreeStanding ) { 285 height -= 2; 286 width -= 1; 287 g.translate( 1, 0 ); 288 } 289 } 290 } 291 292 public Dimension getPreferredSize() 293 { 294 if ( getDirection() == NORTH ) 295 { 296 return new Dimension ( buttonWidth, buttonWidth - 2 ); 297 } 298 else if ( getDirection() == SOUTH ) 299 { 300 return new Dimension ( buttonWidth, buttonWidth - (isFreeStanding ? 1 : 2) ); 301 } 302 else if ( getDirection() == EAST ) 303 { 304 return new Dimension ( buttonWidth - (isFreeStanding ? 1 : 2), buttonWidth ); 305 } 306 else if ( getDirection() == WEST ) 307 { 308 return new Dimension ( buttonWidth - 2, buttonWidth ); 309 } 310 else 311 { 312 return new Dimension ( 0, 0 ); 313 } 314 } 315 316 public Dimension getMinimumSize() 317 { 318 return getPreferredSize(); 319 } 320 321 public Dimension getMaximumSize() 322 { 323 return new Dimension ( Integer.MAX_VALUE, Integer.MAX_VALUE ); 324 } 325 326 public int getButtonWidth() { 327 return buttonWidth; 328 } 329 } 330 331 | Popular Tags |