KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > sortabletable > SortDirectionSelfDrawingIcon


1 package snow.sortabletable;
2
3
4 import java.util.Vector JavaDoc;
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  /**
20   * An Icon, which draws its content using awt/swing
21   * without connection to a file on disk.
22   *
23   * This one is specialized for the views of filetree's.
24   * It draws file and directory icons.
25   *
26   * On themechanges, best is to just recreate and set this renderer.
27   *
28   */

29 public class SortDirectionSelfDrawingIcon implements Icon
30 {
31
32   // The icon file types, to be passed to the constructor :
33

34
35   // types :
36
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   // Private color keys :
43
/* private static final int ColorBlack = 0; // Black -> black or white, depends on background
44   private static final int ColorRed = 1;
45   private static final int ColorGreen = 2;
46   private static final int ColorBlue = 3; // All colors are set dependent on the background
47   private static final int ColorYellow = 4; // so that a certain contrast is guaranteed.
48   private static final int ColorGray = 5;
49   private static final int ColorOrange = 6; */

50
51   // Member attributes :
52
private Color shapeBorderColor = null;
53
54   private int height = 0;
55   private int width = 0;
56
57
58   private GradientShapeEntity[] shapeEntities = null;
59   // The shape entities to be drawn. They are children
60

61   private Font basisFont; // Used for geometric size calculations.
62

63   private int strokeThickness = 1;
64
65
66
67  /**
68   * Creates a filetreeicon with the specified type.
69   * The available types are defined static in this class.
70   */

71   public SortDirectionSelfDrawingIcon( final int iconStyle )
72   {
73      //JTableHeader g;
74
this.basisFont = UIManager.getFont("Tree.font");
75     this.strokeThickness = 1 + this.basisFont.getSize()/16;
76     this.defineIcon( iconStyle );
77   } // Constructor
78

79
80
81
82
83
84
85   private void defineIcon( final int iconStyle )
86   {
87     // Give directories constant UI independent signal colors :
88
// this.textColor = new Color(60,90,140);
89

90     final Color bg = UIManager.getColor("TextField.background");
91     final Color fg = UIManager.getColor("TextField.foreground");
92
93     // border color [UI dependant] :
94
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     // Icon size :
100
this.height = 0;
101     this.width = 0;
102
103     // The font metrics has changed a bit since jdk 1.4, so adjust the sizes :
104
String JavaDoc version = System.getProperty("java.version");
105     if( version.compareTo("1.4") < 0 )
106      {
107        // jdk 1.3 or less :
108
// Icon size :
109
this.height = ( 4*this.basisFont.getSize() )/3;
110        this.width = this.height/2;
111      }
112     else
113      {
114        // jdk 1.4 or later :
115
// Icon size :
116
this.height = (7*this.basisFont.getSize()/6);
117        this.width = this.height/2;
118      }
119
120     // The shape entities :
121
Vector JavaDoc<GradientShapeEntity> shapeVector = new Vector JavaDoc<GradientShapeEntity>();
122     GradientShapeEntity shape = null; // a work attribute
123
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     // 1) main body : 3 cases :
130
switch( iconStyle )
131      {
132        case Ascending :
133             // inside colors [ UI independant : default green ]:
134
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             // inside colors [ UI independant : default green ]:
152
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             // inside colors [ UI independant : default green ]:
170
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             // inside colors [ UI independant : default green ]:
189
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      } // switch
209

210
211     // Copy the entities into the shapeEntities member array :
212
this.shapeEntities = new GradientShapeEntity[ shapeVector.size() ];
213     shapeVector.copyInto( this.shapeEntities );
214   } // defineIconForDirectory
215

216
217
218
219
220
221
222  /**
223   * Draws the icon at the specified location. Icon implementations
224   * may use the Component argument to get properties useful for
225   * painting, e.g. the foreground or background color.
226   */

227   public void paintIcon( Component c, Graphics g, int x, int y )
228   {
229     final Graphics2D graphics2D = (Graphics2D)g;
230     // Backup:
231
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 JavaDoc backupAntiAliasRendering = graphics2D.getRenderingHint( RenderingHints.KEY_ANTIALIASING );
238     final Object JavaDoc backupQualityRendering = graphics2D.getRenderingHint( RenderingHints.KEY_RENDERING );
239
240     // Reconfigure / draw:
241

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     // Restore :
254
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   } // paintIcon
263

264
265
266
267
268   /**
269    * Returns the icon's width.
270    *
271    * @return an int specifying the fixed width of the icon.
272    */

273   public int getIconWidth()
274   {
275     return this.width + 1 + this.basisFont.getSize()/10; // always add a small offset
276
}
277
278
279
280   /**
281    * Returns the icon's height.
282    *
283    * @return an int specifying the fixed height of the icon.
284    */

285   public int getIconHeight()
286   {
287     return this.height + 1 + this.basisFont.getSize()/10; // always add a small offset
288
}
289
290
291
292
293
294
295   /*
296   private Color createColorWithContrastToBackground( final int textColorKey )
297   {
298     final Color background = UIManager.getColor("TextField.background");
299     final int average = (background.getRed()+background.getGreen()+background.getBlue())/3;
300     int contrast = 255-average;
301     if( contrast < 50 ) contrast = 50; // dont saturate all
302     if( contrast > 200 ) contrast = 200; // dont saturate all
303     Color c = null;
304     switch( textColorKey )
305      {
306        case ColorBlack :
307             if( average > 122 )
308              {
309                c = new Color(0,0,0);
310              } else
311              {
312                c = new Color(255,255,255);
313              }
314             break;
315        case ColorRed :
316                c = this.createColorFromFractions(0.850,0.075,0.075,contrast);
317             break;
318        case ColorOrange :
319                c = this.createColorFromFractions(0.500,0.350,0.150,contrast);
320             break;
321        case ColorYellow :
322                c = this.createColorFromFractions(0.450,0.450,0.100,contrast);
323             break;
324        case ColorGreen :
325                c = this.createColorFromFractions(0.075,0.850,0.075,contrast);
326             break;
327        case ColorBlue :
328                c = this.createColorFromFractions(0.075,0.075,0.850,contrast);
329             break;
330        case ColorGray :
331                c = new Color( contrast,contrast,contrast );
332             break;
333        default :
334             c = new Color(30,30,30); // should not happen
335      } // case
336     return c;
337   } */

338
339
340
341  /**
342   * Creates a color with the passed color fractions, which
343   * has the passed average value.
344   * The sum of the fractions should equal one.
345   *
346   private Color createColorFromFractions( final double rFraction,
347                                            final double gFraction,
348                                            final double bFraction,
349                                            final int grayScaleAverage )
350   {
351     final double avInitial = ( rFraction + gFraction + bFraction ) / 3.0;
352     double scalingFactor = 1.0;
353     if( avInitial > 1e-10 )
354      {
355        scalingFactor = 1.0 * grayScaleAverage / avInitial;
356      }
357     double r = rFraction * scalingFactor;
358     double g = gFraction * scalingFactor;
359     double b = bFraction * scalingFactor;
360     // If we can't reach the fractions, try to hold the average
361     // by transferring from the biggest value. Its only
362     // an approximative correction somehow, not more :
363     if( (rFraction > gFraction) && (rFraction > bFraction) )
364      {
365        if( r > 255.0 )
366         {
367           final double halfOver = (r-255.0)/2.0;
368           r = 255.0;
369           g += halfOver;
370           b += halfOver;
371         }
372      }
373     else
374     if( (gFraction > rFraction) && (gFraction > bFraction) )
375      {
376        if( g > 255.0 )
377         {
378           final double halfOver = (g-255.0)/2.0;
379           g = 255.0;
380           r += halfOver;
381           b += halfOver;
382         }
383      }
384     else
385     if( (bFraction > rFraction) && (bFraction > gFraction) )
386      {
387        if( b > 255.0 )
388         {
389           final double halfOver = (b-255.0)/2.0;
390           b = 255.0;
391           r += halfOver;
392           g += halfOver;
393         }
394      }
395     // chop:
396     if( r > 255.0 ) r = 255;
397     if( g > 255.0 ) g = 255;
398     if( b > 255.0 ) b = 255;
399     return new Color( (int)r, (int)g, (int)b );
400   }*/

401
402
403
404
405
406 } // SelfDrawingFileTreeIcon
Popular Tags