1 package snow.sortabletable; 2 3 4 import java.util.Vector ; 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.awt.font.*; 8 import java.awt.geom.*; 9 10 import javax.swing.*; 11 import javax.swing.table.*; 12 import javax.swing.plaf.*; 13 import javax.swing.plaf.metal.*; 14 15 16 17 18 19 29 public class SortDirectionSelfDrawingIcon implements Icon 30 { 31 32 34 35 public static final int Ascending = 1; 37 public static final int Descending = 2; 38 public static final int Left = 3; 39 public static final int Right = 4; 40 41 42 50 51 private Color shapeBorderColor = null; 53 54 private int height = 0; 55 private int width = 0; 56 57 58 private GradientShapeEntity[] shapeEntities = null; 59 61 private Font basisFont; 63 private int strokeThickness = 1; 64 65 66 67 71 public SortDirectionSelfDrawingIcon( final int iconStyle ) 72 { 73 this.basisFont = UIManager.getFont("Tree.font"); 75 this.strokeThickness = 1 + this.basisFont.getSize()/16; 76 this.defineIcon( iconStyle ); 77 } 79 80 81 82 83 84 85 private void defineIcon( final int iconStyle ) 86 { 87 90 final Color bg = UIManager.getColor("TextField.background"); 91 final Color fg = UIManager.getColor("TextField.foreground"); 92 93 int r = ( bg.getRed() + 4*fg.getRed() ) / 5; 95 int g = ( bg.getGreen() + 4*fg.getGreen() ) / 5; 96 int b = ( bg.getBlue() + 4*fg.getBlue() ) / 5; 97 this.shapeBorderColor = new Color(r,g,b); 98 99 this.height = 0; 101 this.width = 0; 102 103 String version = System.getProperty("java.version"); 105 if( version.compareTo("1.4") < 0 ) 106 { 107 this.height = ( 4*this.basisFont.getSize() )/3; 110 this.width = this.height/2; 111 } 112 else 113 { 114 this.height = (7*this.basisFont.getSize()/6); 117 this.width = this.height/2; 118 } 119 120 Vector <GradientShapeEntity> shapeVector = new Vector <GradientShapeEntity>(); 122 GradientShapeEntity shape = null; Color shapeInsideColor = null; 124 Color shapeInsideSelectedColor = null; 125 int verticalOffset = this.height/4; 126 int h2 = this.height*3/4; 127 128 int halfHeight = 0; 129 switch( iconStyle ) 131 { 132 case Ascending : 133 shapeInsideColor = new Color(130,242,110); 135 shapeInsideSelectedColor = new Color(190,250,170); 136 shape = new GradientShapeEntity( this.shapeBorderColor, 137 this.shapeBorderColor, 138 shapeInsideColor, 139 shapeInsideSelectedColor, 140 1f * this.width, 141 0f, 142 this.strokeThickness ); 143 shape.moveTo(0,verticalOffset); 144 shape.lineTo(this.width/2,h2); 145 shape.lineTo(this.width,verticalOffset); 146 shape.closePath(); 147 shapeVector.addElement( shape ); 148 break; 149 150 case Descending : 151 shapeInsideColor = new Color(130,242,110); 153 shapeInsideSelectedColor = new Color(190,250,170); 154 shape = new GradientShapeEntity( this.shapeBorderColor, 155 this.shapeBorderColor, 156 shapeInsideColor, 157 shapeInsideSelectedColor, 158 1f * this.width, 159 0f, 160 this.strokeThickness ); 161 shape.moveTo(0,h2); 162 shape.lineTo(this.width/2,verticalOffset); 163 shape.lineTo(this.width,h2); 164 shape.closePath(); 165 shapeVector.addElement( shape ); 166 break; 167 168 case Left : 169 shapeInsideColor = new Color(130,242,110); 171 shapeInsideSelectedColor = new Color(190,250,170); 172 shape = new GradientShapeEntity( this.shapeBorderColor, 173 this.shapeBorderColor, 174 shapeInsideColor, 175 shapeInsideSelectedColor, 176 1f * this.width, 177 0f, 178 this.strokeThickness ); 179 shape.moveTo(width,height); 180 shape.lineTo(width/4, height/2); 181 shape.lineTo(width,0); 182 shape.closePath(); 183 shapeVector.addElement( shape ); 184 break; 185 186 case Right : 187 188 shapeInsideColor = new Color(130,242,110); 190 shapeInsideSelectedColor = new Color(190,250,170); 191 shape = new GradientShapeEntity(this.shapeBorderColor, 192 this.shapeBorderColor, 193 shapeInsideColor, 194 shapeInsideSelectedColor, 195 1f * this.width, 196 0f, 197 this.strokeThickness ); 198 shape.moveTo(0,height); 199 shape.lineTo(width*3/4, height/2); 200 shape.lineTo(0,0); 201 shape.closePath(); 202 shapeVector.addElement( shape ); 203 break; 204 205 206 207 default: 208 } 210 211 this.shapeEntities = new GradientShapeEntity[ shapeVector.size() ]; 213 shapeVector.copyInto( this.shapeEntities ); 214 } 216 217 218 219 220 221 222 227 public void paintIcon( Component c, Graphics g, int x, int y ) 228 { 229 final Graphics2D graphics2D = (Graphics2D)g; 230 final Color saveColor = graphics2D.getColor(); 232 final AffineTransform backupTransform = graphics2D.getTransform(); 233 g.translate(x,y); 234 final Stroke backupStroke = graphics2D.getStroke(); 235 final Paint savePaint = graphics2D.getPaint(); 236 237 final Object backupAntiAliasRendering = graphics2D.getRenderingHint( RenderingHints.KEY_ANTIALIASING ); 238 final Object backupQualityRendering = graphics2D.getRenderingHint( RenderingHints.KEY_RENDERING ); 239 240 242 graphics2D.setRenderingHint( RenderingHints.KEY_ANTIALIASING, 243 RenderingHints.VALUE_ANTIALIAS_ON ); 244 graphics2D.setRenderingHint( RenderingHints.KEY_RENDERING, 245 RenderingHints.VALUE_RENDER_QUALITY ); 246 247 248 for( int i=0; i < this.shapeEntities.length; i++ ) 249 { 250 this.shapeEntities[i].paint( graphics2D ); 251 } 252 253 graphics2D.setTransform( backupTransform ); 255 graphics2D.setColor(saveColor); 256 graphics2D.setStroke(backupStroke); 257 258 graphics2D.setRenderingHint( RenderingHints.KEY_ANTIALIASING,backupAntiAliasRendering ); 259 graphics2D.setRenderingHint( RenderingHints.KEY_RENDERING,backupQualityRendering ); 260 261 graphics2D.setPaint(savePaint); 262 } 264 265 266 267 268 273 public int getIconWidth() 274 { 275 return this.width + 1 + this.basisFont.getSize()/10; } 277 278 279 280 285 public int getIconHeight() 286 { 287 return this.height + 1 + this.basisFont.getSize()/10; } 289 290 291 292 293 294 295 338 339 340 341 401 402 403 404 405 406 } | Popular Tags |